diff --git a/build.gradle b/build.gradle index d54777c..b281d45 100644 --- a/build.gradle +++ b/build.gradle @@ -1,59 +1,59 @@ -plugins { - id 'java' -} - -group = 'ru.redguy' -version = '1.0.0' - -repositories { - mavenCentral() - maven { - name = 'papermc-repo' - url = 'https://papermc.io/repo/repository/maven-public/' - } - maven { - name = 'sonatype' - url = 'https://oss.sonatype.org/content/groups/public/' - } - maven { - url = 'https://repo.redguy.ru/repository/maven-public/' - } -} - -dependencies { - compileOnly 'com.destroystokyo.paper:paper-api:1.12.2-R0.1-SNAPSHOT' - compileOnly 'org.bukkit:craftbukkit:1.12.2-R0.1-20190527.155144-32' - compileOnly "com.github.MilkBowl:VaultAPI:1.7" - compileOnly 'com.sk89q.worldguard:worldguard-legacy:6.2' -} - -def targetJavaVersion = 8 -java { - def javaVersion = JavaVersion.toVersion(targetJavaVersion) - sourceCompatibility = javaVersion - targetCompatibility = javaVersion - if (JavaVersion.current() < javaVersion) { - toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion) - } -} - -tasks.withType(JavaCompile).configureEach { - if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) { - options.release = targetJavaVersion - } -} - -processResources { - def props = [version: version] - inputs.properties props - filteringCharset 'UTF-8' - filesMatching('plugin.yml') { - expand props - } -} - -task copytorun(type: Copy) { - delete file("$projectDir/Server/plugins/Extended*") - from file("$projectDir/build/libs/ExtendedPistons-${version}.jar") - into file("$projectDir/Server/plugins/") +plugins { + id 'java' +} + +group = 'ru.redguy' +version = '1.0.1' + +repositories { + mavenCentral() + maven { + name = 'papermc-repo' + url = 'https://papermc.io/repo/repository/maven-public/' + } + maven { + name = 'sonatype' + url = 'https://oss.sonatype.org/content/groups/public/' + } + maven { + url = 'https://repo.redguy.ru/repository/maven-public/' + } +} + +dependencies { + compileOnly 'com.destroystokyo.paper:paper-api:1.12.2-R0.1-SNAPSHOT' + compileOnly 'org.bukkit:craftbukkit:1.12.2-R0.1-20190527.155144-32' + compileOnly "com.github.MilkBowl:VaultAPI:1.7" + compileOnly 'com.sk89q.worldguard:worldguard-legacy:6.2' +} + +def targetJavaVersion = 8 +java { + def javaVersion = JavaVersion.toVersion(targetJavaVersion) + sourceCompatibility = javaVersion + targetCompatibility = javaVersion + if (JavaVersion.current() < javaVersion) { + toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion) + } +} + +tasks.withType(JavaCompile).configureEach { + if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) { + options.release = targetJavaVersion + } +} + +processResources { + def props = [version: version] + inputs.properties props + filteringCharset 'UTF-8' + filesMatching('plugin.yml') { + expand props + } +} + +task copytorun(type: Copy) { + delete file("$projectDir/Server/plugins/Extended*") + from file("$projectDir/build/libs/ExtendedPistons-${version}.jar") + into file("$projectDir/Server/plugins/") } \ No newline at end of file diff --git a/gradlew.bat b/gradlew.bat index 107acd3..ac1b06f 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,89 +1,89 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto execute - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/ru/redguy/extendedpistons/BlockPistonExtendedExtendEvent.java b/src/main/java/ru/redguy/extendedpistons/BlockPistonExtendedExtendEvent.java index 96aafaa..7f1db24 100644 --- a/src/main/java/ru/redguy/extendedpistons/BlockPistonExtendedExtendEvent.java +++ b/src/main/java/ru/redguy/extendedpistons/BlockPistonExtendedExtendEvent.java @@ -1,17 +1,17 @@ -package ru.redguy.extendedpistons; - -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.event.block.BlockPistonExtendEvent; - -import java.util.List; - -public class BlockPistonExtendedExtendEvent extends BlockPistonExtendEvent { - public BlockPistonExtendedExtendEvent(Block block, int length, BlockFace direction) { - super(block, length, direction); - } - - public BlockPistonExtendedExtendEvent(Block block, List blocks, BlockFace direction) { - super(block, blocks, direction); - } -} +package ru.redguy.extendedpistons; + +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.event.block.BlockPistonExtendEvent; + +import java.util.List; + +public class BlockPistonExtendedExtendEvent extends BlockPistonExtendEvent { + public BlockPistonExtendedExtendEvent(Block block, int length, BlockFace direction) { + super(block, length, direction); + } + + public BlockPistonExtendedExtendEvent(Block block, List blocks, BlockFace direction) { + super(block, blocks, direction); + } +} diff --git a/src/main/java/ru/redguy/extendedpistons/Period.java b/src/main/java/ru/redguy/extendedpistons/Period.java index 0a76449..d1e4824 100644 --- a/src/main/java/ru/redguy/extendedpistons/Period.java +++ b/src/main/java/ru/redguy/extendedpistons/Period.java @@ -1,30 +1,30 @@ -package ru.redguy.extendedpistons; - -import java.time.Instant; -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; - -public class Period { - private final Instant from; - private final Instant to; - - public Period(Date from, Date to) { - this.from = nowtifyDate(from); - this.to = nowtifyDate(to); - } - - public boolean isBehind(Instant date) { - return from.isBefore(date)&&to.isAfter(date); - } - - public Instant nowtifyDate(Date ins) { - Calendar now = GregorianCalendar.getInstance(); - Calendar cal = GregorianCalendar.getInstance(); - cal.setTime(ins); - cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); - cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); - cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)); - return cal.getTime().toInstant(); - } -} +package ru.redguy.extendedpistons; + +import java.time.Instant; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +public class Period { + private final Instant from; + private final Instant to; + + public Period(Date from, Date to) { + this.from = nowtifyDate(from); + this.to = nowtifyDate(to); + } + + public boolean isBehind(Instant date) { + return from.isBefore(date)&&to.isAfter(date); + } + + public Instant nowtifyDate(Date ins) { + Calendar now = GregorianCalendar.getInstance(); + Calendar cal = GregorianCalendar.getInstance(); + cal.setTime(ins); + cal.set(Calendar.YEAR, now.get(Calendar.YEAR)); + cal.set(Calendar.MONTH, now.get(Calendar.MONTH)); + cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)); + return cal.getTime().toInstant(); + } +} diff --git a/src/main/java/ru/redguy/extendedpistons/PistonExtendsChecker.java b/src/main/java/ru/redguy/extendedpistons/PistonExtendsChecker.java index e732c40..1485561 100644 --- a/src/main/java/ru/redguy/extendedpistons/PistonExtendsChecker.java +++ b/src/main/java/ru/redguy/extendedpistons/PistonExtendsChecker.java @@ -1,265 +1,265 @@ -package ru.redguy.extendedpistons; - -import com.google.common.collect.Lists; -import net.minecraft.server.v1_12_R1.*; -import org.bukkit.Location; -import org.bukkit.block.BlockFace; -import org.bukkit.craftbukkit.v1_12_R1.CraftWorld; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -public class PistonExtendsChecker { - private final net.minecraft.server.v1_12_R1.World a; - private final BlockPosition b; - private final BlockPosition c; - private final EnumDirection d; - private final List e = Lists.newArrayList(); - private final List f = Lists.newArrayList(); - private org.bukkit.World bw; - private int maxLength; - - private PistonExtendsChecker(World var1, BlockPosition var2, EnumDirection var3, boolean var4) { - this.a = var1; - this.b = var2; - if (var4) { - this.d = var3; - this.c = var2.shift(var3); - } else { - this.d = var3.opposite(); - this.c = var2.shift(var3, 2); - } - - } - - public PistonExtendsChecker(org.bukkit.World var1, Location var2, BlockFace var3, boolean var4, int maxLength) { - this(((CraftWorld)var1).getHandle(),new BlockPosition(var2.getBlockX(),var2.getBlockY(),var2.getBlockZ()),cast(var3),var4); - this.bw = var1; - this.maxLength = maxLength; - } - - public boolean a() { - this.e.clear(); - this.f.clear(); - IBlockData var1 = this.a.getType(this.c); - if (!BlockPiston.a(var1, this.a, this.c, this.d, false, this.d)) { - if (var1.o() == EnumPistonReaction.DESTROY) { - this.f.add(this.c); - return true; - } else { - return false; - } - } else if (!this.a(this.c, this.d)) { - return false; - } else { - for(int var2 = 0; var2 < this.e.size(); ++var2) { - BlockPosition var3 = (BlockPosition)this.e.get(var2); - if (this.a.getType(var3).getBlock() == Blocks.SLIME && !this.a(var3)) { - return false; - } - } - return true; - } - } - - private boolean a(BlockPosition var1, EnumDirection var2) { - IBlockData var3 = this.a.getType(var1); - Block var4 = var3.getBlock(); - if (var3.getMaterial() == Material.AIR) { - return true; - } else if (!BlockPiston.a(var3, this.a, var1, this.d, false, var2)) { - return true; - } else if (var1.equals(this.b)) { - return true; - } else if (this.e.contains(var1)) { - return true; - } else { - int var5 = 1; - if (var5 + this.e.size() > maxLength) { - return false; - } else { - while(var4 == Blocks.SLIME) { - BlockPosition var6 = var1.shift(this.d.opposite(), var5); - var3 = this.a.getType(var6); - var4 = var3.getBlock(); - if (var3.getMaterial() == Material.AIR || !BlockPiston.a(var3, this.a, var6, this.d, false, this.d.opposite()) || var6.equals(this.b)) { - break; - } - - ++var5; - if (var5 + this.e.size() > maxLength) { - return false; - } - } - - int var12 = 0; - - int var7; - for(var7 = var5 - 1; var7 >= 0; --var7) { - this.e.add(var1.shift(this.d.opposite(), var7)); - ++var12; - } - - var7 = 1; - - while(true) { - BlockPosition var8 = var1.shift(this.d, var7); - int var9 = this.e.indexOf(var8); - if (var9 > -1) { - this.a(var12, var9); - - for(int var10 = 0; var10 <= var9 + var12; ++var10) { - BlockPosition var11 = (BlockPosition)this.e.get(var10); - if (this.a.getType(var11).getBlock() == Blocks.SLIME && !this.a(var11)) { - return false; - } - } - - return true; - } - - var3 = this.a.getType(var8); - if (var3.getMaterial() == Material.AIR) { - return true; - } - - if (!BlockPiston.a(var3, this.a, var8, this.d, true, this.d) || var8.equals(this.b)) { - return false; - } - - if (var3.o() == EnumPistonReaction.DESTROY) { - this.f.add(var8); - return true; - } - - if (this.e.size() >= maxLength) { - return false; - } - - this.e.add(var8); - ++var12; - ++var7; - } - } - } - } - - private void a(int var1, int var2) { - ArrayList var3 = Lists.newArrayList(); - ArrayList var4 = Lists.newArrayList(); - ArrayList var5 = Lists.newArrayList(); - var3.addAll(this.e.subList(0, var2)); - var4.addAll(this.e.subList(this.e.size() - var1, this.e.size())); - var5.addAll(this.e.subList(var2, this.e.size() - var1)); - this.e.clear(); - this.e.addAll(var3); - this.e.addAll(var4); - this.e.addAll(var5); - } - - private boolean a(BlockPosition var1) { - EnumDirection[] var2 = EnumDirection.values(); - int var3 = var2.length; - - for(int var4 = 0; var4 < var3; ++var4) { - EnumDirection var5 = var2[var4]; - if (var5.k() != this.d.k() && !this.a(var1.shift(var5), var5)) { - return false; - } - } - - return true; - } - - public List getMovedBlocks() { - return this.e; - } - - public List getBrokenBlocks() { - return this.f; - } - - public List getMovedBlocksLocations() { - return this.e.stream().map(bp -> new Location(bw,bp.getX(),bp.getY(),bp.getZ())).collect(Collectors.toList()); - } - - public List getBrokenBlocksLocations() { - return this.f.stream().map(bp -> new Location(bw,bp.getX(),bp.getY(),bp.getZ())).collect(Collectors.toList()); - } - - public List getMovedBlocksObjects() { - return this.e.stream().map(bp -> bw.getBlockAt(bp.getX(),bp.getY(),bp.getZ())).collect(Collectors.toList()); - } - - public List getBrokenBlocksObjects() { - return this.f.stream().map(bp -> bw.getBlockAt(bp.getX(),bp.getY(),bp.getZ())).collect(Collectors.toList()); - } - - public static EnumDirection cast(BlockFace face) { - switch (face) { - case UP: - return EnumDirection.UP; - case DOWN: - return EnumDirection.DOWN; - case WEST: - return EnumDirection.WEST; - case SOUTH: - return EnumDirection.SOUTH; - case NORTH: - return EnumDirection.NORTH; - case EAST: - return EnumDirection.EAST; - } - return null; - } - - public static class BlockPiston { - public static boolean a(IBlockData iblockdata, World world, BlockPosition blockposition, EnumDirection enumdirection, boolean flag, EnumDirection enumdirection1) { - Block block = iblockdata.getBlock(); - if (block == Blocks.OBSIDIAN) { - return false; - } else if (!world.getWorldBorder().a(blockposition)) { - return false; - } else if (blockposition.getY() < 0 || enumdirection == EnumDirection.DOWN && blockposition.getY() == 0) { - return false; - } else if (blockposition.getY() > world.getHeight() - 1 || enumdirection == EnumDirection.UP && blockposition.getY() == world.getHeight() - 1) { - return false; - } else { - if (block != Blocks.PISTON && block != Blocks.STICKY_PISTON) { - if (iblockdata.b(world, blockposition) == -1.0F) { - return false; - } - - switch(iblockdata.o().ordinal()) { - case 2: - return flag; - case 3: - return false; - case 4: - default: - break; - case 5: - if (enumdirection == enumdirection1) { - return true; - } - - return false; - } - } else if ((Boolean)iblockdata.get(net.minecraft.server.v1_12_R1.BlockPiston.EXTENDED)) { - return false; - } - - if(block.isTileEntity()) { - if(block == Blocks.CHEST || block == Blocks.FURNACE || block == Blocks.LIT_FURNACE) { - return ExtendedPistons.INSTANCE.isAllowedTime(); - } else { - return false; - } - } else { - return true; - } - } - } - } -} +package ru.redguy.extendedpistons; + +import com.google.common.collect.Lists; +import net.minecraft.server.v1_12_R1.*; +import org.bukkit.Location; +import org.bukkit.block.BlockFace; +import org.bukkit.craftbukkit.v1_12_R1.CraftWorld; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class PistonExtendsChecker { + private final net.minecraft.server.v1_12_R1.World a; + private final BlockPosition b; + private final BlockPosition c; + private final EnumDirection d; + private final List e = Lists.newArrayList(); + private final List f = Lists.newArrayList(); + private org.bukkit.World bw; + private int maxLength; + + private PistonExtendsChecker(World var1, BlockPosition var2, EnumDirection var3, boolean var4) { + this.a = var1; + this.b = var2; + if (var4) { + this.d = var3; + this.c = var2.shift(var3); + } else { + this.d = var3.opposite(); + this.c = var2.shift(var3, 2); + } + + } + + public PistonExtendsChecker(org.bukkit.World var1, Location var2, BlockFace var3, boolean var4, int maxLength) { + this(((CraftWorld)var1).getHandle(),new BlockPosition(var2.getBlockX(),var2.getBlockY(),var2.getBlockZ()),cast(var3),var4); + this.bw = var1; + this.maxLength = maxLength; + } + + public boolean a() { + this.e.clear(); + this.f.clear(); + IBlockData var1 = this.a.getType(this.c); + if (!BlockPiston.a(var1, this.a, this.c, this.d, false, this.d)) { + if (var1.o() == EnumPistonReaction.DESTROY) { + this.f.add(this.c); + return true; + } else { + return false; + } + } else if (!this.a(this.c, this.d)) { + return false; + } else { + for(int var2 = 0; var2 < this.e.size(); ++var2) { + BlockPosition var3 = (BlockPosition)this.e.get(var2); + if (this.a.getType(var3).getBlock() == Blocks.SLIME && !this.a(var3)) { + return false; + } + } + return true; + } + } + + private boolean a(BlockPosition var1, EnumDirection var2) { + IBlockData var3 = this.a.getType(var1); + Block var4 = var3.getBlock(); + if (var3.getMaterial() == Material.AIR) { + return true; + } else if (!BlockPiston.a(var3, this.a, var1, this.d, false, var2)) { + return true; + } else if (var1.equals(this.b)) { + return true; + } else if (this.e.contains(var1)) { + return true; + } else { + int var5 = 1; + if (var5 + this.e.size() > maxLength) { + return false; + } else { + while(var4 == Blocks.SLIME) { + BlockPosition var6 = var1.shift(this.d.opposite(), var5); + var3 = this.a.getType(var6); + var4 = var3.getBlock(); + if (var3.getMaterial() == Material.AIR || !BlockPiston.a(var3, this.a, var6, this.d, false, this.d.opposite()) || var6.equals(this.b)) { + break; + } + + ++var5; + if (var5 + this.e.size() > maxLength) { + return false; + } + } + + int var12 = 0; + + int var7; + for(var7 = var5 - 1; var7 >= 0; --var7) { + this.e.add(var1.shift(this.d.opposite(), var7)); + ++var12; + } + + var7 = 1; + + while(true) { + BlockPosition var8 = var1.shift(this.d, var7); + int var9 = this.e.indexOf(var8); + if (var9 > -1) { + this.a(var12, var9); + + for(int var10 = 0; var10 <= var9 + var12; ++var10) { + BlockPosition var11 = (BlockPosition)this.e.get(var10); + if (this.a.getType(var11).getBlock() == Blocks.SLIME && !this.a(var11)) { + return false; + } + } + + return true; + } + + var3 = this.a.getType(var8); + if (var3.getMaterial() == Material.AIR) { + return true; + } + + if (!BlockPiston.a(var3, this.a, var8, this.d, true, this.d) || var8.equals(this.b)) { + return false; + } + + if (var3.o() == EnumPistonReaction.DESTROY) { + this.f.add(var8); + return true; + } + + if (this.e.size() >= maxLength) { + return false; + } + + this.e.add(var8); + ++var12; + ++var7; + } + } + } + } + + private void a(int var1, int var2) { + ArrayList var3 = Lists.newArrayList(); + ArrayList var4 = Lists.newArrayList(); + ArrayList var5 = Lists.newArrayList(); + var3.addAll(this.e.subList(0, var2)); + var4.addAll(this.e.subList(this.e.size() - var1, this.e.size())); + var5.addAll(this.e.subList(var2, this.e.size() - var1)); + this.e.clear(); + this.e.addAll(var3); + this.e.addAll(var4); + this.e.addAll(var5); + } + + private boolean a(BlockPosition var1) { + EnumDirection[] var2 = EnumDirection.values(); + int var3 = var2.length; + + for(int var4 = 0; var4 < var3; ++var4) { + EnumDirection var5 = var2[var4]; + if (var5.k() != this.d.k() && !this.a(var1.shift(var5), var5)) { + return false; + } + } + + return true; + } + + public List getMovedBlocks() { + return this.e; + } + + public List getBrokenBlocks() { + return this.f; + } + + public List getMovedBlocksLocations() { + return this.e.stream().map(bp -> new Location(bw,bp.getX(),bp.getY(),bp.getZ())).collect(Collectors.toList()); + } + + public List getBrokenBlocksLocations() { + return this.f.stream().map(bp -> new Location(bw,bp.getX(),bp.getY(),bp.getZ())).collect(Collectors.toList()); + } + + public List getMovedBlocksObjects() { + return this.e.stream().map(bp -> bw.getBlockAt(bp.getX(),bp.getY(),bp.getZ())).collect(Collectors.toList()); + } + + public List getBrokenBlocksObjects() { + return this.f.stream().map(bp -> bw.getBlockAt(bp.getX(),bp.getY(),bp.getZ())).collect(Collectors.toList()); + } + + public static EnumDirection cast(BlockFace face) { + switch (face) { + case UP: + return EnumDirection.UP; + case DOWN: + return EnumDirection.DOWN; + case WEST: + return EnumDirection.WEST; + case SOUTH: + return EnumDirection.SOUTH; + case NORTH: + return EnumDirection.NORTH; + case EAST: + return EnumDirection.EAST; + } + return null; + } + + public static class BlockPiston { + public static boolean a(IBlockData iblockdata, World world, BlockPosition blockposition, EnumDirection enumdirection, boolean flag, EnumDirection enumdirection1) { + Block block = iblockdata.getBlock(); + if (block == Blocks.OBSIDIAN) { + return false; + } else if (!world.getWorldBorder().a(blockposition)) { + return false; + } else if (blockposition.getY() < 0 || enumdirection == EnumDirection.DOWN && blockposition.getY() == 0) { + return false; + } else if (blockposition.getY() > world.getHeight() - 1 || enumdirection == EnumDirection.UP && blockposition.getY() == world.getHeight() - 1) { + return false; + } else { + if (block != Blocks.PISTON && block != Blocks.STICKY_PISTON) { + if (iblockdata.b(world, blockposition) == -1.0F) { + return false; + } + + switch(iblockdata.o().ordinal()) { + case 2: + return flag; + case 3: + return false; + case 4: + default: + break; + case 5: + if (enumdirection == enumdirection1) { + return true; + } + + return false; + } + } else if ((Boolean)iblockdata.get(net.minecraft.server.v1_12_R1.BlockPiston.EXTENDED)) { + return false; + } + + if(block.isTileEntity()) { + if(block == Blocks.CHEST || block == Blocks.FURNACE || block == Blocks.LIT_FURNACE) { + return ExtendedPistons.INSTANCE.isAllowedTime(); + } else { + return false; + } + } else { + return true; + } + } + } + } +} diff --git a/src/main/java/ru/redguy/extendedpistons/WorldListener.java b/src/main/java/ru/redguy/extendedpistons/WorldListener.java index 2a184da..32ba66c 100644 --- a/src/main/java/ru/redguy/extendedpistons/WorldListener.java +++ b/src/main/java/ru/redguy/extendedpistons/WorldListener.java @@ -1,458 +1,458 @@ -package ru.redguy.extendedpistons; - -import com.sk89q.worldguard.bukkit.RegionContainer; -import com.sk89q.worldguard.bukkit.RegionQuery; -import com.sk89q.worldguard.bukkit.WorldGuardPlugin; -import com.sk89q.worldguard.protection.flags.DefaultFlag; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.block.*; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.*; -import org.bukkit.inventory.DoubleChestInventory; -import org.bukkit.material.PistonBaseMaterial; - -import java.util.Comparator; -import java.util.*; - -public class WorldListener implements Listener { - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onPistonChanged(BlockPistonExtendEvent e) { - if(e.getBlocks().size()>0) e.setCancelled(true); - /*if (e instanceof BlockPistonExtendedExtendEvent) return; - Block block = e.getBlock(); - BlockState state = block.getState(); - PistonBaseMaterial piston = (PistonBaseMaterial) state.getData(); - int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); - - PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), e.getBlock().getLocation(), piston.getFacing(), true, maxBlocks); - - if (!checker.a()) { - e.setCancelled(true); - }*/ - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onPistonChanged(BlockPistonRetractEvent e) { - if(e.getBlocks().size()>0) e.setCancelled(true); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockBreak(BlockBreakEvent e) { - Material blockType = e.getBlock().getType(); - if (!(blockType.equals(Material.REDSTONE_BLOCK) || blockType.equals(Material.REDSTONE_TORCH_ON))) return; - HashMap states = new HashMap<>(); - HashMap powers = new HashMap<>(); - for (BlockFace face : new BlockFace[]{BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { - states.put(face, e.getBlock().getRelative(face).getState()); - powers.put(face, e.getBlock().getRelative(face).getBlockPower()); - } - Bukkit.getScheduler().runTask(ExtendedPistons.INSTANCE, () -> { - if (ExtendedPistons.INSTANCE.isStickyPistonPulls()) { - ArrayList checkedBlockFaces = new ArrayList() {{ - add(BlockFace.SELF); - add(BlockFace.UP); - add(BlockFace.DOWN); - }}; - if (e.getBlock().getType().equals(Material.REDSTONE_WIRE)) { - for (BlockFace blockFace : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { - Block block = e.getBlock().getRelative(blockFace); - if (block.getBlockPower() == 0 && powers.get(blockFace) != 0) checkedBlockFaces.add(blockFace); - } - } else { - checkedBlockFaces.add(BlockFace.NORTH); - checkedBlockFaces.add(BlockFace.SOUTH); - checkedBlockFaces.add(BlockFace.EAST); - checkedBlockFaces.add(BlockFace.WEST); - } - for (BlockFace face : checkedBlockFaces) { - Block block = e.getBlock().getRelative(face); - BlockState state = states.get(face); - if (state.getType().equals(Material.PISTON_STICKY_BASE)) { - PistonBaseMaterial piston; - try { - piston = (PistonBaseMaterial) state.getData(); - } catch (ClassCastException ex) { - continue; - } - int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); - - PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), false, maxBlocks); - boolean isMovable = checker.a(); - List blocks = checker.getMovedBlocksObjects(); - List toBreak = checker.getBrokenBlocksObjects(); - - if (isMovable) { - if (!isBlocksAllowedToInteract(copyWithAdd(blocks, toBreak), piston.getFacing())) continue; - Collections.reverse(blocks); - for (Block block1 : toBreak) { - block1.breakNaturally(); - } - for (Block b : blocks) { - Block b2 = b.getRelative(piston.getFacing().getOppositeFace()); - b2.setType(b.getType()); - b2.setData(b.getData()); //Deprecated but powerful - if (b.getType().equals(Material.CHEST)) { - Chest oldChest = (Chest) b.getState(); - Chest newChest = (Chest) b2.getState(); - newChest.getBlockInventory().setContents(oldChest.getBlockInventory().getContents()); - oldChest.getBlockInventory().clear(); - } else if (b.getType().equals(Material.FURNACE) || b.getType().equals(Material.BURNING_FURNACE)) { - Furnace oldFurnace = (Furnace) b.getState(); - Furnace newFurnace = (Furnace) b2.getState(); - newFurnace.getInventory().setContents(oldFurnace.getInventory().getContents()); - oldFurnace.getInventory().clear(); - } - b.setType(Material.AIR); - } - } - } - } - } - }); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockPlace(BlockPlaceEvent e) { - Material blockType = e.getBlock().getType(); - if (!(blockType.equals(Material.REDSTONE_BLOCK) || blockType.equals(Material.REDSTONE_TORCH_ON))) return; - HashMap states = new HashMap<>(); - HashMap powers = new HashMap<>(); - for (BlockFace face : new BlockFace[]{BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { - states.put(face, e.getBlock().getRelative(face).getState()); - powers.put(face, e.getBlock().getRelative(face).getBlockPower()); - } - Bukkit.getScheduler().runTask(ExtendedPistons.INSTANCE, () -> { - ArrayList checkedBlockFaces = new ArrayList() {{ - add(BlockFace.SELF); - add(BlockFace.UP); - add(BlockFace.DOWN); - }}; - if (e.getBlock().getType().equals(Material.REDSTONE_WIRE)) { - for (BlockFace blockFace : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { - Block block = e.getBlock().getRelative(blockFace); - if (block.getBlockPower() != 0 && powers.get(blockFace) == 0) checkedBlockFaces.add(blockFace); - } - } else { - checkedBlockFaces.add(BlockFace.NORTH); - checkedBlockFaces.add(BlockFace.SOUTH); - checkedBlockFaces.add(BlockFace.EAST); - checkedBlockFaces.add(BlockFace.WEST); - } - for (BlockFace face : checkedBlockFaces) { - Block block = e.getBlock().getRelative(face); - if (block.getType().equals(Material.PISTON_BASE) || block.getType().equals(Material.PISTON_STICKY_BASE)) { - BlockState state = states.get(face); - PistonBaseMaterial piston; - try { - piston = (PistonBaseMaterial) state.getData(); - } catch (ClassCastException ex) { - continue; - } - int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); - - PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), true, maxBlocks); - boolean isMovable = checker.a(); - List blocks = checker.getMovedBlocksObjects(); - List toBreak = checker.getBrokenBlocksObjects(); - - upper: - for (Block b : blocks) { - if (b.getType().equals(Material.CHEST)) { - Block b2 = b.getRelative(piston.getFacing()); - ArrayList faces = new ArrayList() {{ - add(BlockFace.NORTH); - add(BlockFace.SOUTH); - add(BlockFace.WEST); - add(BlockFace.EAST); - }}; - faces.remove(piston.getFacing().getOppositeFace()); - int chests = 0; - for (BlockFace blockFace : faces) { - Block b3 = b2.getRelative(blockFace); - if (b3.getType() == Material.CHEST) { - chests++; - Chest chest = (Chest) b3.getState(); - if (chest.getInventory() instanceof DoubleChestInventory) { - isMovable = false; - break upper; - } - } - } - if (chests > 1) { - isMovable = false; - break; - } else if (chests == 1) { - if (b.getType() == Material.CHEST) { - Chest chest = (Chest) b.getState(); - if (chest.getInventory() instanceof DoubleChestInventory) { - isMovable = false; - break; - } - } - } - } - } - - if (isMovable) { - if (!isBlocksAllowedToInteract(copyWithAdd(blocks, toBreak), piston.getFacing())) return; - Collections.reverse(blocks); - for (Block block1 : toBreak) { - block1.breakNaturally(); - } - for (Block b : blocks) { - Block b2 = b.getRelative(piston.getFacing()); - b2.setType(b.getType()); - b2.setData(b.getData()); //Deprecated but powerful - if (b.getType().equals(Material.CHEST)) { - Chest oldChest = (Chest) b.getState(); - Chest newChest = (Chest) b2.getState(); - newChest.getBlockInventory().setContents(oldChest.getBlockInventory().getContents()); - oldChest.getBlockInventory().clear(); - } else if (b.getType().equals(Material.FURNACE) || b.getType().equals(Material.BURNING_FURNACE)) { - Furnace oldFurnace = (Furnace) b.getState(); - Furnace newFurnace = (Furnace) b2.getState(); - newFurnace.getInventory().setContents(oldFurnace.getInventory().getContents()); - oldFurnace.getInventory().clear(); - } - b.setType(Material.AIR); - } - } - } - } - }); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onRedstone(BlockRedstoneEvent e) { - HashMap> states = new HashMap<>(); - for (BlockFace face : new BlockFace[]{BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { - states.put(face, new HashMap<>()); - for (BlockFace blockFace : new BlockFace[]{BlockFace.UP, BlockFace.SELF, BlockFace.DOWN}) { - states.get(face).put(blockFace,e.getBlock().getRelative(face).getRelative(blockFace).getState()); - } - } - Bukkit.getScheduler().runTask(ExtendedPistons.INSTANCE, () -> { - if (e.getOldCurrent() != 0 && e.getNewCurrent() == 0) { - if (ExtendedPistons.INSTANCE.isStickyPistonPulls()) { - ArrayList checkedBlockFaces = new ArrayList() {{ - add(BlockFace.SELF); - add(BlockFace.UP); - add(BlockFace.DOWN); - }}; - if (e.getBlock().getType().equals(Material.REDSTONE_WIRE)) { - for (BlockFace blockFace : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { - Block block = e.getBlock().getRelative(blockFace); - if (block.getBlockPower() == 0) checkedBlockFaces.add(blockFace); - } - } else { - checkedBlockFaces.add(BlockFace.NORTH); - checkedBlockFaces.add(BlockFace.SOUTH); - checkedBlockFaces.add(BlockFace.EAST); - checkedBlockFaces.add(BlockFace.WEST); - } - for (BlockFace face : checkedBlockFaces) { - ArrayList subs = new ArrayList() {{ - add(BlockFace.SELF); - }}; - if (e.getBlock().getType().equals(Material.LEVER)) { - subs.add(BlockFace.UP); - subs.add(BlockFace.DOWN); - } - for (BlockFace sub : subs) { - Block block = e.getBlock().getRelative(face); - block = block.getRelative(sub); - BlockState state = states.get(face).get(sub); - if (state.getType().equals(Material.PISTON_STICKY_BASE)) { - PistonBaseMaterial piston; - try { - piston = (PistonBaseMaterial) state.getData(); - } catch (ClassCastException ex) { - continue; - } - int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); - - PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), false, maxBlocks); - boolean isMovable = checker.a(); - List blocks = checker.getMovedBlocksObjects(); - List toBreak = checker.getBrokenBlocksObjects(); - - if (isMovable) { - Collections.reverse(blocks); - if (!isBlocksAllowedToInteract(copyWithAdd(blocks, toBreak), piston.getFacing())) - continue; - for (Block block1 : toBreak) { - block1.breakNaturally(); - } - for (Block b : blocks) { - Block b2 = b.getRelative(piston.getFacing().getOppositeFace()); - b2.setType(b.getType()); - b2.setData(b.getData()); //Deprecated but powerful - if (b.getType().equals(Material.CHEST)) { - Chest oldChest = (Chest) b.getState(); - Chest newChest = (Chest) b2.getState(); - newChest.getBlockInventory().setContents(oldChest.getBlockInventory().getContents()); - oldChest.getBlockInventory().clear(); - } else if (b.getType().equals(Material.FURNACE) || b.getType().equals(Material.BURNING_FURNACE)) { - Furnace oldFurnace = (Furnace) b.getState(); - Furnace newFurnace = (Furnace) b2.getState(); - newFurnace.getInventory().setContents(oldFurnace.getInventory().getContents()); - oldFurnace.getInventory().clear(); - } - b.setType(Material.AIR); - } - } - } - } - } - } - } else { - ArrayList checkedBlockFaces = new ArrayList() {{ - add(BlockFace.SELF); - add(BlockFace.UP); - add(BlockFace.DOWN); - }}; - if (e.getBlock().getType().equals(Material.REDSTONE_WIRE)) { - for (BlockFace blockFace : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { - Block block = e.getBlock().getRelative(blockFace); - if (block.getBlockPower() != 0) checkedBlockFaces.add(blockFace); - } - } else { - checkedBlockFaces.add(BlockFace.NORTH); - checkedBlockFaces.add(BlockFace.SOUTH); - checkedBlockFaces.add(BlockFace.EAST); - checkedBlockFaces.add(BlockFace.WEST); - } - for (BlockFace face : checkedBlockFaces) { - ArrayList subs = new ArrayList() {{ - add(BlockFace.SELF); - }}; - if (e.getBlock().getType().equals(Material.LEVER)) { - subs.add(BlockFace.UP); - subs.add(BlockFace.DOWN); - } - for (BlockFace sub : subs) { - Block block = e.getBlock().getRelative(face); - block = block.getRelative(sub); - if (block.getType().equals(Material.PISTON_BASE) || block.getType().equals(Material.PISTON_STICKY_BASE)) { - BlockState state = states.get(face).get(sub); - PistonBaseMaterial piston; - try { - piston = (PistonBaseMaterial) state.getData(); - } catch (ClassCastException ex) { - continue; - } - int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); - - PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), true, maxBlocks); - boolean isMovable = checker.a(); - List blocks = checker.getMovedBlocksObjects(); - List toBreak = checker.getBrokenBlocksObjects(); - - upper: - for (Block b : blocks) { - if (b.getType().equals(Material.CHEST)) { - Block b2 = b.getRelative(piston.getFacing()); - ArrayList faces = new ArrayList() {{ - add(BlockFace.NORTH); - add(BlockFace.SOUTH); - add(BlockFace.WEST); - add(BlockFace.EAST); - }}; - faces.remove(piston.getFacing().getOppositeFace()); - int chests = 0; - for (BlockFace blockFace : faces) { - Block b3 = b2.getRelative(blockFace); - if (b3.getType() == Material.CHEST) { - chests++; - Chest chest = (Chest) b3.getState(); - if (chest.getInventory() instanceof DoubleChestInventory) { - isMovable = false; - break upper; - } - } - } - if (chests > 1) { - isMovable = false; - break; - } else if (chests == 1) { - if (b.getType() == Material.CHEST) { - Chest chest = (Chest) b.getState(); - if (chest.getInventory() instanceof DoubleChestInventory) { - isMovable = false; - break; - } - } - } - } - } - - if (isMovable) { - if (!isBlocksAllowedToInteract(copyWithAdd(blocks, toBreak), piston.getFacing())) - return; - Collections.reverse(blocks); - for (Block block1 : toBreak) { - block1.breakNaturally(); - } - for (Block b : blocks) { - Block b2 = b.getRelative(piston.getFacing()); - b2.setType(b.getType()); - b2.setData(b.getData()); //Deprecated but powerful - if (b.getType().equals(Material.CHEST)) { - Chest oldChest = (Chest) b.getState(); - Chest newChest = (Chest) b2.getState(); - newChest.getBlockInventory().setContents(oldChest.getBlockInventory().getContents()); - oldChest.getBlockInventory().clear(); - } else if (b.getType().equals(Material.FURNACE) || b.getType().equals(Material.BURNING_FURNACE)) { - Furnace oldFurnace = (Furnace) b.getState(); - Furnace newFurnace = (Furnace) b2.getState(); - newFurnace.getInventory().setContents(oldFurnace.getInventory().getContents()); - oldFurnace.getInventory().clear(); - } - b.setType(Material.AIR); - } - } - } - } - } - } - }); - } - - public int findNearbyPlayerMaxBlocks(Location location) { - Optional op = Bukkit.getOnlinePlayers().stream().filter(p -> p.getLocation().getWorld().equals(location.getWorld())).min(Comparator.comparingDouble(p -> p.getLocation().distance(location))); - if (op.isPresent()) { - String[] groups = ExtendedPistons.INSTANCE.permsService.getPlayerGroups(op.get()); - Optional intOp = Arrays.stream(groups).map(group -> (ExtendedPistons.INSTANCE.conf.isSet("piston-move-limitations." + group) && ExtendedPistons.INSTANCE.conf.isInt("piston-move-limitations." + group)) ? ExtendedPistons.INSTANCE.conf.getInt("piston-move-limitations." + group) : 0).max(Comparator.comparingInt(i -> i)); - if (intOp.isPresent()) { - return intOp.get() == 0 ? 12 : intOp.get(); - } - } - return 12; - } - - public boolean isBlocksAllowedToInteract(List blocks, BlockFace bf) { - WorldGuardPlugin wg = ExtendedPistons.INSTANCE.wg; - RegionContainer container = wg.getRegionContainer(); - RegionQuery query = container.createQuery(); - for (Block block : blocks) { - boolean state = query.testState(block.getLocation(), (Player) null, DefaultFlag.PISTONS); - if (!state) - return false; - state = query.testState(block.getRelative(bf).getLocation(), (Player) null, DefaultFlag.PISTONS); - if (!state) - return false; - } - return true; - } - - public List copyWithAdd(List original, List add) { - List copy = new ArrayList<>(original); - copy.addAll(add); - return copy; - } -} +package ru.redguy.extendedpistons; + +import com.sk89q.worldguard.bukkit.RegionContainer; +import com.sk89q.worldguard.bukkit.RegionQuery; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.flags.DefaultFlag; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.*; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.*; +import org.bukkit.inventory.DoubleChestInventory; +import org.bukkit.material.PistonBaseMaterial; + +import java.util.Comparator; +import java.util.*; + +public class WorldListener implements Listener { + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onPistonChanged(BlockPistonExtendEvent e) { + if(e.getBlocks().size()>0) e.setCancelled(true); + /*if (e instanceof BlockPistonExtendedExtendEvent) return; + Block block = e.getBlock(); + BlockState state = block.getState(); + PistonBaseMaterial piston = (PistonBaseMaterial) state.getData(); + int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); + + PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), e.getBlock().getLocation(), piston.getFacing(), true, maxBlocks); + + if (!checker.a()) { + e.setCancelled(true); + }*/ + } + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onPistonChanged(BlockPistonRetractEvent e) { + if(e.getBlocks().size()>0) e.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockBreak(BlockBreakEvent e) { + Material blockType = e.getBlock().getType(); + if (!(blockType.equals(Material.REDSTONE_BLOCK) || blockType.equals(Material.REDSTONE_TORCH_ON))) return; + HashMap states = new HashMap<>(); + HashMap powers = new HashMap<>(); + for (BlockFace face : new BlockFace[]{BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { + states.put(face, e.getBlock().getRelative(face).getState()); + powers.put(face, e.getBlock().getRelative(face).getBlockPower()); + } + Bukkit.getScheduler().runTask(ExtendedPistons.INSTANCE, () -> { + if (ExtendedPistons.INSTANCE.isStickyPistonPulls()) { + ArrayList checkedBlockFaces = new ArrayList() {{ + add(BlockFace.SELF); + add(BlockFace.UP); + add(BlockFace.DOWN); + }}; + if (e.getBlock().getType().equals(Material.REDSTONE_WIRE)) { + for (BlockFace blockFace : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { + Block block = e.getBlock().getRelative(blockFace); + if (block.getBlockPower() == 0 && powers.get(blockFace) != 0) checkedBlockFaces.add(blockFace); + } + } else { + checkedBlockFaces.add(BlockFace.NORTH); + checkedBlockFaces.add(BlockFace.SOUTH); + checkedBlockFaces.add(BlockFace.EAST); + checkedBlockFaces.add(BlockFace.WEST); + } + for (BlockFace face : checkedBlockFaces) { + Block block = e.getBlock().getRelative(face); + BlockState state = states.get(face); + if (state.getType().equals(Material.PISTON_STICKY_BASE)) { + PistonBaseMaterial piston; + try { + piston = (PistonBaseMaterial) state.getData(); + } catch (ClassCastException ex) { + continue; + } + int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); + + PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), false, maxBlocks); + boolean isMovable = checker.a(); + List blocks = checker.getMovedBlocksObjects(); + List toBreak = checker.getBrokenBlocksObjects(); + + if (isMovable) { + if (!isBlocksAllowedToInteract(copyWithAdd(blocks, toBreak), piston.getFacing())) continue; + Collections.reverse(blocks); + for (Block block1 : toBreak) { + block1.breakNaturally(); + } + for (Block b : blocks) { + Block b2 = b.getRelative(piston.getFacing().getOppositeFace()); + b2.setType(b.getType()); + b2.setData(b.getData()); //Deprecated but powerful + if (b.getType().equals(Material.CHEST)) { + Chest oldChest = (Chest) b.getState(); + Chest newChest = (Chest) b2.getState(); + newChest.getBlockInventory().setContents(oldChest.getBlockInventory().getContents()); + oldChest.getBlockInventory().clear(); + } else if (b.getType().equals(Material.FURNACE) || b.getType().equals(Material.BURNING_FURNACE)) { + Furnace oldFurnace = (Furnace) b.getState(); + Furnace newFurnace = (Furnace) b2.getState(); + newFurnace.getInventory().setContents(oldFurnace.getInventory().getContents()); + oldFurnace.getInventory().clear(); + } + b.setType(Material.AIR); + } + } + } + } + } + }); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockPlace(BlockPlaceEvent e) { + Material blockType = e.getBlock().getType(); + if (!(blockType.equals(Material.REDSTONE_BLOCK) || blockType.equals(Material.REDSTONE_TORCH_ON))) return; + HashMap states = new HashMap<>(); + HashMap powers = new HashMap<>(); + for (BlockFace face : new BlockFace[]{BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { + states.put(face, e.getBlock().getRelative(face).getState()); + powers.put(face, e.getBlock().getRelative(face).getBlockPower()); + } + Bukkit.getScheduler().runTask(ExtendedPistons.INSTANCE, () -> { + ArrayList checkedBlockFaces = new ArrayList() {{ + add(BlockFace.SELF); + add(BlockFace.UP); + add(BlockFace.DOWN); + }}; + if (e.getBlock().getType().equals(Material.REDSTONE_WIRE)) { + for (BlockFace blockFace : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { + Block block = e.getBlock().getRelative(blockFace); + if (block.getBlockPower() != 0 && powers.get(blockFace) == 0) checkedBlockFaces.add(blockFace); + } + } else { + checkedBlockFaces.add(BlockFace.NORTH); + checkedBlockFaces.add(BlockFace.SOUTH); + checkedBlockFaces.add(BlockFace.EAST); + checkedBlockFaces.add(BlockFace.WEST); + } + for (BlockFace face : checkedBlockFaces) { + Block block = e.getBlock().getRelative(face); + if (block.getType().equals(Material.PISTON_BASE) || block.getType().equals(Material.PISTON_STICKY_BASE)) { + BlockState state = states.get(face); + PistonBaseMaterial piston; + try { + piston = (PistonBaseMaterial) state.getData(); + } catch (ClassCastException ex) { + continue; + } + int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); + + PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), true, maxBlocks); + boolean isMovable = checker.a(); + List blocks = checker.getMovedBlocksObjects(); + List toBreak = checker.getBrokenBlocksObjects(); + + upper: + for (Block b : blocks) { + if (b.getType().equals(Material.CHEST)) { + Block b2 = b.getRelative(piston.getFacing()); + ArrayList faces = new ArrayList() {{ + add(BlockFace.NORTH); + add(BlockFace.SOUTH); + add(BlockFace.WEST); + add(BlockFace.EAST); + }}; + faces.remove(piston.getFacing().getOppositeFace()); + int chests = 0; + for (BlockFace blockFace : faces) { + Block b3 = b2.getRelative(blockFace); + if (b3.getType() == Material.CHEST) { + chests++; + Chest chest = (Chest) b3.getState(); + if (chest.getInventory() instanceof DoubleChestInventory) { + isMovable = false; + break upper; + } + } + } + if (chests > 1) { + isMovable = false; + break; + } else if (chests == 1) { + if (b.getType() == Material.CHEST) { + Chest chest = (Chest) b.getState(); + if (chest.getInventory() instanceof DoubleChestInventory) { + isMovable = false; + break; + } + } + } + } + } + + if (isMovable) { + if (!isBlocksAllowedToInteract(copyWithAdd(blocks, toBreak), piston.getFacing())) return; + Collections.reverse(blocks); + for (Block block1 : toBreak) { + block1.breakNaturally(); + } + for (Block b : blocks) { + Block b2 = b.getRelative(piston.getFacing()); + b2.setType(b.getType()); + b2.setData(b.getData()); //Deprecated but powerful + if (b.getType().equals(Material.CHEST)) { + Chest oldChest = (Chest) b.getState(); + Chest newChest = (Chest) b2.getState(); + newChest.getBlockInventory().setContents(oldChest.getBlockInventory().getContents()); + oldChest.getBlockInventory().clear(); + } else if (b.getType().equals(Material.FURNACE) || b.getType().equals(Material.BURNING_FURNACE)) { + Furnace oldFurnace = (Furnace) b.getState(); + Furnace newFurnace = (Furnace) b2.getState(); + newFurnace.getInventory().setContents(oldFurnace.getInventory().getContents()); + oldFurnace.getInventory().clear(); + } + b.setType(Material.AIR); + } + } + } + } + }); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onRedstone(BlockRedstoneEvent e) { + HashMap> states = new HashMap<>(); + for (BlockFace face : new BlockFace[]{BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { + states.put(face, new HashMap<>()); + for (BlockFace blockFace : new BlockFace[]{BlockFace.UP, BlockFace.SELF, BlockFace.DOWN}) { + states.get(face).put(blockFace,e.getBlock().getRelative(face).getRelative(blockFace).getState()); + } + } + Bukkit.getScheduler().runTask(ExtendedPistons.INSTANCE, () -> { + if (e.getOldCurrent() != 0 && e.getNewCurrent() == 0) { + if (ExtendedPistons.INSTANCE.isStickyPistonPulls()) { + ArrayList checkedBlockFaces = new ArrayList() {{ + add(BlockFace.SELF); + add(BlockFace.UP); + add(BlockFace.DOWN); + }}; + if (e.getBlock().getType().equals(Material.REDSTONE_WIRE)) { + for (BlockFace blockFace : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { + Block block = e.getBlock().getRelative(blockFace); + if (block.getBlockPower() == 0) checkedBlockFaces.add(blockFace); + } + } else { + checkedBlockFaces.add(BlockFace.NORTH); + checkedBlockFaces.add(BlockFace.SOUTH); + checkedBlockFaces.add(BlockFace.EAST); + checkedBlockFaces.add(BlockFace.WEST); + } + for (BlockFace face : checkedBlockFaces) { + ArrayList subs = new ArrayList() {{ + add(BlockFace.SELF); + }}; + if (e.getBlock().getType().equals(Material.LEVER)) { + subs.add(BlockFace.UP); + subs.add(BlockFace.DOWN); + } + for (BlockFace sub : subs) { + Block block = e.getBlock().getRelative(face); + block = block.getRelative(sub); + BlockState state = states.get(face).get(sub); + if (state.getType().equals(Material.PISTON_STICKY_BASE)) { + PistonBaseMaterial piston; + try { + piston = (PistonBaseMaterial) state.getData(); + } catch (ClassCastException ex) { + continue; + } + int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); + + PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), false, maxBlocks); + boolean isMovable = checker.a(); + List blocks = checker.getMovedBlocksObjects(); + List toBreak = checker.getBrokenBlocksObjects(); + + if (isMovable) { + Collections.reverse(blocks); + if (!isBlocksAllowedToInteract(copyWithAdd(blocks, toBreak), piston.getFacing())) + continue; + for (Block block1 : toBreak) { + block1.breakNaturally(); + } + for (Block b : blocks) { + Block b2 = b.getRelative(piston.getFacing().getOppositeFace()); + b2.setType(b.getType()); + b2.setData(b.getData()); //Deprecated but powerful + if (b.getType().equals(Material.CHEST)) { + Chest oldChest = (Chest) b.getState(); + Chest newChest = (Chest) b2.getState(); + newChest.getBlockInventory().setContents(oldChest.getBlockInventory().getContents()); + oldChest.getBlockInventory().clear(); + } else if (b.getType().equals(Material.FURNACE) || b.getType().equals(Material.BURNING_FURNACE)) { + Furnace oldFurnace = (Furnace) b.getState(); + Furnace newFurnace = (Furnace) b2.getState(); + newFurnace.getInventory().setContents(oldFurnace.getInventory().getContents()); + oldFurnace.getInventory().clear(); + } + b.setType(Material.AIR); + } + } + } + } + } + } + } else { + ArrayList checkedBlockFaces = new ArrayList() {{ + add(BlockFace.SELF); + add(BlockFace.UP); + add(BlockFace.DOWN); + }}; + if (e.getBlock().getType().equals(Material.REDSTONE_WIRE)) { + for (BlockFace blockFace : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { + Block block = e.getBlock().getRelative(blockFace); + if (block.getBlockPower() != 0) checkedBlockFaces.add(blockFace); + } + } else { + checkedBlockFaces.add(BlockFace.NORTH); + checkedBlockFaces.add(BlockFace.SOUTH); + checkedBlockFaces.add(BlockFace.EAST); + checkedBlockFaces.add(BlockFace.WEST); + } + for (BlockFace face : checkedBlockFaces) { + ArrayList subs = new ArrayList() {{ + add(BlockFace.SELF); + }}; + if (e.getBlock().getType().equals(Material.LEVER)) { + subs.add(BlockFace.UP); + subs.add(BlockFace.DOWN); + } + for (BlockFace sub : subs) { + Block block = e.getBlock().getRelative(face); + block = block.getRelative(sub); + if (block.getType().equals(Material.PISTON_BASE) || block.getType().equals(Material.PISTON_STICKY_BASE)) { + BlockState state = states.get(face).get(sub); + PistonBaseMaterial piston; + try { + piston = (PistonBaseMaterial) state.getData(); + } catch (ClassCastException ex) { + continue; + } + int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); + + PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), true, maxBlocks); + boolean isMovable = checker.a(); + List blocks = checker.getMovedBlocksObjects(); + List toBreak = checker.getBrokenBlocksObjects(); + + upper: + for (Block b : blocks) { + if (b.getType().equals(Material.CHEST)) { + Block b2 = b.getRelative(piston.getFacing()); + ArrayList faces = new ArrayList() {{ + add(BlockFace.NORTH); + add(BlockFace.SOUTH); + add(BlockFace.WEST); + add(BlockFace.EAST); + }}; + faces.remove(piston.getFacing().getOppositeFace()); + int chests = 0; + for (BlockFace blockFace : faces) { + Block b3 = b2.getRelative(blockFace); + if (b3.getType() == Material.CHEST) { + chests++; + Chest chest = (Chest) b3.getState(); + if (chest.getInventory() instanceof DoubleChestInventory) { + isMovable = false; + break upper; + } + } + } + if (chests > 1) { + isMovable = false; + break; + } else if (chests == 1) { + if (b.getType() == Material.CHEST) { + Chest chest = (Chest) b.getState(); + if (chest.getInventory() instanceof DoubleChestInventory) { + isMovable = false; + break; + } + } + } + } + } + + if (isMovable) { + if (!isBlocksAllowedToInteract(copyWithAdd(blocks, toBreak), piston.getFacing())) + return; + Collections.reverse(blocks); + for (Block block1 : toBreak) { + block1.breakNaturally(); + } + for (Block b : blocks) { + Block b2 = b.getRelative(piston.getFacing()); + b2.setType(b.getType()); + b2.setData(b.getData()); //Deprecated but powerful + if (b.getType().equals(Material.CHEST)) { + Chest oldChest = (Chest) b.getState(); + Chest newChest = (Chest) b2.getState(); + newChest.getBlockInventory().setContents(oldChest.getBlockInventory().getContents()); + oldChest.getBlockInventory().clear(); + } else if (b.getType().equals(Material.FURNACE) || b.getType().equals(Material.BURNING_FURNACE)) { + Furnace oldFurnace = (Furnace) b.getState(); + Furnace newFurnace = (Furnace) b2.getState(); + newFurnace.getInventory().setContents(oldFurnace.getInventory().getContents()); + oldFurnace.getInventory().clear(); + } + b.setType(Material.AIR); + } + } + } + } + } + } + }); + } + + public int findNearbyPlayerMaxBlocks(Location location) { + Optional op = Bukkit.getOnlinePlayers().stream().filter(p -> p.getLocation().getWorld().equals(location.getWorld())).min(Comparator.comparingDouble(p -> p.getLocation().distance(location))); + if (op.isPresent()) { + String[] groups = ExtendedPistons.INSTANCE.permsService.getPlayerGroups(op.get()); + Optional intOp = Arrays.stream(groups).map(group -> (ExtendedPistons.INSTANCE.conf.isSet("piston-move-limitations." + group) && ExtendedPistons.INSTANCE.conf.isInt("piston-move-limitations." + group)) ? ExtendedPistons.INSTANCE.conf.getInt("piston-move-limitations." + group) : 0).max(Comparator.comparingInt(i -> i)); + if (intOp.isPresent()) { + return intOp.get() == 0 ? 12 : intOp.get(); + } + } + return 12; + } + + public boolean isBlocksAllowedToInteract(List blocks, BlockFace bf) { + WorldGuardPlugin wg = ExtendedPistons.INSTANCE.wg; + RegionContainer container = wg.getRegionContainer(); + RegionQuery query = container.createQuery(); + for (Block block : blocks) { + boolean state = query.testState(block.getLocation(), (Player) null, DefaultFlag.PISTONS); + if (!state) + return false; + state = query.testState(block.getRelative(bf).getLocation(), (Player) null, DefaultFlag.PISTONS); + if (!state) + return false; + } + return true; + } + + public List copyWithAdd(List original, List add) { + List copy = new ArrayList<>(original); + copy.addAll(add); + return copy; + } +}