Dropped 1.12.x support
This commit is contained in:
parent
8443e77c59
commit
ad1abd8492
3 changed files with 47 additions and 227 deletions
|
@ -87,10 +87,10 @@ public final class ThemeParkPlus extends JavaPlugin {
|
|||
if (Bukkit.getPluginManager().getPlugin("ThemePark") == null && Bukkit.getPluginManager().getPlugin("ParkPlusMC") == null) {
|
||||
getLogger().severe("ThemePark or ParkPlusMC not found! Some functions will not work without it!");
|
||||
} else if (Bukkit.getPluginManager().getPlugin("ThemePark") != null) {
|
||||
getLogger().severe("Enabling ThemePark hook...");
|
||||
getLogger().info("Enabling ThemePark hook...");
|
||||
hasParkHook = true;
|
||||
} else if (Bukkit.getPluginManager().getPlugin("ParkPlusMC") != null) {
|
||||
getLogger().severe("Enabling ParkPlusMC hook...");
|
||||
getLogger().info("Enabling ParkPlusMC hook...");
|
||||
hasParkHook = true;
|
||||
}
|
||||
|
||||
|
|
|
@ -169,19 +169,14 @@ public class TPPCMD implements TabExecutor {
|
|||
long onDelayTicks = Long.parseLong(args[5]) * 20L;
|
||||
long offDelayTicks = args.length == 7 ? Long.parseLong(args[6]) * 20L : 20L;
|
||||
|
||||
Block block = loc.getBlock();
|
||||
Material oldMaterial = block.getType();
|
||||
byte oldData = block.getState().getRawData();
|
||||
Material redstoneMaterial = XMaterial.REDSTONE_BLOCK.parseMaterial();
|
||||
|
||||
if (redstoneMaterial == null) throw new IllegalArgumentException("Redstone Material not found.");
|
||||
final Block block = loc.getBlock();
|
||||
final Material oldMaterial = block.getType();
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(ThemeParkPlus.getInstance(), () -> {
|
||||
block.setType(redstoneMaterial);
|
||||
block.setType(XMaterial.REDSTONE_BLOCK.parseMaterial());
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(ThemeParkPlus.getInstance(), () -> {
|
||||
BlockState state = block.getState();
|
||||
state.setType(oldMaterial);
|
||||
if (!XMaterial.isNewVersion()) state.setRawData(oldData);
|
||||
state.update(true);
|
||||
}, offDelayTicks);
|
||||
}, onDelayTicks);
|
||||
|
|
|
@ -2,81 +2,30 @@ package nl.sbdeveloper.themeparkplus.util;
|
|||
|
||||
import com.cryptomorin.xseries.XMaterial;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.BlockState;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.block.data.Directional;
|
||||
import org.bukkit.block.data.Openable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class LGUtil {
|
||||
private static boolean is113Plus = false;
|
||||
private static final BlockData lampAanData = Bukkit.createBlockData(XMaterial.REDSTONE_LAMP.parseMaterial(), "lit=true");
|
||||
private static final BlockData lampUitData = Bukkit.createBlockData(XMaterial.REDSTONE_LAMP.parseMaterial(), "lit=false");
|
||||
|
||||
private static Method getBlockDataMethod;
|
||||
private static Method setBlockDataMethod;
|
||||
private static Method getAsStringMethod;
|
||||
private static Object lampAanData;
|
||||
private static Object lampUitData;
|
||||
|
||||
static {
|
||||
try {
|
||||
Class<?> blockDataClass = Class.forName("org.bukkit.block.data.BlockData");
|
||||
Method methodCreateBlockData = Bukkit.class.getMethod("createBlockData", String.class);
|
||||
getBlockDataMethod = Block.class.getMethod("getBlockData");
|
||||
setBlockDataMethod = Block.class.getMethod("setBlockData", blockDataClass,
|
||||
Boolean.TYPE);
|
||||
getAsStringMethod = blockDataClass.getMethod("getAsString");
|
||||
lampAanData = methodCreateBlockData.invoke(null, "minecraft:redstone_lamp[lit=true]");
|
||||
lampUitData = methodCreateBlockData.invoke(null, "minecraft:redstone_lamp[lit=false]");
|
||||
is113Plus = true;
|
||||
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {}
|
||||
}
|
||||
private LGUtil() {}
|
||||
|
||||
public static boolean zetLampAan(Block lampBlock) {
|
||||
if (is113Plus) {
|
||||
if ((isLamp(lampBlock)) && (!isAan(lampBlock))) {
|
||||
setBlockData(lampBlock, lampAanData);
|
||||
|
||||
lampBlock.setBlockData(lampAanData);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isAan(lampBlock)) {
|
||||
final Block neighbor = getNeighbor(lampBlock);
|
||||
if (neighbor != null) {
|
||||
BlockState neighborState = neighbor.getState();
|
||||
try {
|
||||
neighbor.setType(Material.REDSTONE_BLOCK);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
neighborState.update(true, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean zetLampUit(Block lampBlock) {
|
||||
if (is113Plus) {
|
||||
if ((isLamp(lampBlock)) && (isAan(lampBlock))) {
|
||||
setBlockData(lampBlock, lampUitData);
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isAan(lampBlock)) {
|
||||
lampBlock.setType(Objects.requireNonNull(Material.matchMaterial(XMaterial.REDSTONE_LAMP.getLegacy()[1])));
|
||||
lampBlock.setBlockData(lampUitData);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -87,43 +36,25 @@ public class LGUtil {
|
|||
}
|
||||
|
||||
public static boolean openGate(Block b, BlockFace openFace) {
|
||||
if (is113Plus) {
|
||||
org.bukkit.block.data.BlockData blockData = b.getBlockData();
|
||||
BlockData blockData = b.getBlockData();
|
||||
if (isOpenable(b)) {
|
||||
org.bukkit.block.data.Openable op = (org.bukkit.block.data.Openable) blockData;
|
||||
Openable op = (Openable) blockData;
|
||||
if (op.isOpen()) return false;
|
||||
op.setOpen(true);
|
||||
b.setBlockData(blockData);
|
||||
|
||||
org.bukkit.block.data.Directional dir = (org.bukkit.block.data.Directional) blockData;
|
||||
Directional dir = (Directional) blockData;
|
||||
dir.setFacing(openFace);
|
||||
b.setBlockData(blockData);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
BlockState state = b.getState();
|
||||
if (isOpenable(b)) {
|
||||
org.bukkit.material.Openable openable = (org.bukkit.material.Openable) state.getData();
|
||||
if (openable.isOpen()) return false;
|
||||
openable.setOpen(true);
|
||||
state.setData((org.bukkit.material.MaterialData) openable);
|
||||
state.update();
|
||||
|
||||
org.bukkit.material.Directional dir = (org.bukkit.material.Directional) state.getData();
|
||||
dir.setFacingDirection(openFace);
|
||||
state.setData((org.bukkit.material.MaterialData) dir);
|
||||
state.update();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean closeGate(Block b) {
|
||||
if (is113Plus) {
|
||||
org.bukkit.block.data.BlockData blockData = b.getBlockData();
|
||||
BlockData blockData = b.getBlockData();
|
||||
if (isOpenable(b)) {
|
||||
org.bukkit.block.data.Openable op = (org.bukkit.block.data.Openable) blockData;
|
||||
Openable op = (Openable) blockData;
|
||||
if (!op.isOpen()) {
|
||||
return false;
|
||||
}
|
||||
|
@ -131,142 +62,36 @@ public class LGUtil {
|
|||
b.setBlockData(blockData);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
BlockState state = b.getState();
|
||||
if (isOpenable(b)) {
|
||||
org.bukkit.material.Openable openable = (org.bukkit.material.Openable) state.getData();
|
||||
if (!openable.isOpen()) {
|
||||
return false;
|
||||
}
|
||||
openable.setOpen(false);
|
||||
state.setData((org.bukkit.material.MaterialData) openable);
|
||||
state.update();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static BlockFace getDirection(Block b) {
|
||||
if (is113Plus) {
|
||||
org.bukkit.block.data.BlockData blockData = b.getBlockData();
|
||||
BlockData blockData = b.getBlockData();
|
||||
if (isOpenable(b)) {
|
||||
org.bukkit.block.data.Directional dir = (org.bukkit.block.data.Directional) blockData;
|
||||
Directional dir = (Directional) blockData;
|
||||
return dir.getFacing();
|
||||
}
|
||||
} else {
|
||||
BlockState state = b.getState();
|
||||
if (isOpenable(b)) {
|
||||
org.bukkit.material.Directional dir = (org.bukkit.material.Directional) state.getData();
|
||||
return dir.getFacing();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isOpen(Block b) {
|
||||
if (is113Plus) {
|
||||
org.bukkit.block.data.BlockData blockData = b.getBlockData();
|
||||
BlockData blockData = b.getBlockData();
|
||||
if (isOpenable(b)) {
|
||||
org.bukkit.block.data.Openable op = (org.bukkit.block.data.Openable) blockData;
|
||||
Openable op = (Openable) blockData;
|
||||
return op.isOpen();
|
||||
}
|
||||
} else {
|
||||
BlockState state = b.getState();
|
||||
if (isOpenable(b)) {
|
||||
org.bukkit.material.Openable openable = (org.bukkit.material.Openable) state.getData();
|
||||
return openable.isOpen();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Gate codes sponsored by MrWouter <3 */
|
||||
public static boolean isOpenable(Block b) {
|
||||
if (b == null) {
|
||||
return false;
|
||||
}
|
||||
if (is113Plus) {
|
||||
public static boolean isOpenable(@NotNull Block b) {
|
||||
return b.getBlockData() instanceof org.bukkit.block.data.Openable;
|
||||
} else {
|
||||
return b.getState().getData() instanceof org.bukkit.material.Openable;
|
||||
}
|
||||
}
|
||||
|
||||
private static Block getNeighbor(Block lampBlock) {
|
||||
List<Block> possibleNeighbors = new ArrayList<>();
|
||||
if (lampBlock.getLocation().getY() > 0.0D) {
|
||||
possibleNeighbors.add(lampBlock.getLocation().add(0.0D, -1.0D, 0.0D).getBlock());
|
||||
}
|
||||
if (lampBlock.getLocation().getY() < 255.0D) {
|
||||
possibleNeighbors.add(lampBlock.getLocation().add(0.0D, 1.0D, 0.0D).getBlock());
|
||||
}
|
||||
possibleNeighbors.add(lampBlock.getLocation().add(0.0D, 0.0D, 1.0D).getBlock());
|
||||
possibleNeighbors.add(lampBlock.getLocation().add(0.0D, 0.0D, -1.0D).getBlock());
|
||||
possibleNeighbors.add(lampBlock.getLocation().add(1.0D, 0.0D, 0.0D).getBlock());
|
||||
possibleNeighbors.add(lampBlock.getLocation().add(-1.0D, 0.0D, 0.0D).getBlock());
|
||||
|
||||
for (Block neighbor: possibleNeighbors) {
|
||||
if (neighbor.getType() == Material.AIR) {
|
||||
return neighbor;
|
||||
}
|
||||
}
|
||||
|
||||
for (Block neighbor: possibleNeighbors) {
|
||||
if ((neighbor.getState().getClass().getSimpleName().equals("CraftBlockState")) && (neighbor.getType() != Material.valueOf(XMaterial.PISTON_HEAD.getLegacy()[0]) && neighbor.getType() != Material.valueOf(XMaterial.MOVING_PISTON.getLegacy()[1])) && (neighbor.getType() != Material.valueOf("REDSTONE_LAMP_ON"))) {
|
||||
return neighbor;
|
||||
}
|
||||
}
|
||||
|
||||
for (Block neighbor: possibleNeighbors) {
|
||||
if ((neighbor.getState().getClass().getSimpleName().equals("CraftBlockState")) && (neighbor.getType() != Material.valueOf(XMaterial.PISTON_HEAD.getLegacy()[0]) && neighbor.getType() != Material.valueOf(XMaterial.MOVING_PISTON.getLegacy()[1]))) {
|
||||
return neighbor;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean isAan(Block lamp) {
|
||||
if (is113Plus) {
|
||||
return getAsString(getBlockData(lamp)).contains("lit=true");
|
||||
} else {
|
||||
return lamp.getType() == Material.matchMaterial(XMaterial.REDSTONE_LAMP.getLegacy()[0]);
|
||||
}
|
||||
return lamp.getBlockData().getAsString().contains("lit=true");
|
||||
}
|
||||
|
||||
private static boolean isLamp(Block lamp) {
|
||||
if (is113Plus) {
|
||||
return lamp.getType() == XMaterial.REDSTONE_LAMP.parseMaterial();
|
||||
} else {
|
||||
return Arrays.asList(XMaterial.REDSTONE_LAMP.getLegacy()).contains(lamp.getType().name());
|
||||
}
|
||||
}
|
||||
|
||||
private static void setBlockData(Block lamp, Object blockData) {
|
||||
try {
|
||||
setBlockDataMethod.invoke(lamp, blockData,
|
||||
Boolean.FALSE);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static Object getBlockData(Block lamp) {
|
||||
try {
|
||||
return getBlockDataMethod.invoke(lamp);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String getAsString(Object blockData) {
|
||||
try {
|
||||
return (String) getAsStringMethod.invoke(blockData, new Object[0]);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue