3
0
Fork 0

Dropped 1.12.x support

This commit is contained in:
SBDeveloper 2022-09-02 20:39:09 +02:00
parent 8443e77c59
commit ad1abd8492
3 changed files with 47 additions and 227 deletions

View file

@ -87,10 +87,10 @@ public final class ThemeParkPlus extends JavaPlugin {
if (Bukkit.getPluginManager().getPlugin("ThemePark") == null && Bukkit.getPluginManager().getPlugin("ParkPlusMC") == null) { 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!"); getLogger().severe("ThemePark or ParkPlusMC not found! Some functions will not work without it!");
} else if (Bukkit.getPluginManager().getPlugin("ThemePark") != null) { } else if (Bukkit.getPluginManager().getPlugin("ThemePark") != null) {
getLogger().severe("Enabling ThemePark hook..."); getLogger().info("Enabling ThemePark hook...");
hasParkHook = true; hasParkHook = true;
} else if (Bukkit.getPluginManager().getPlugin("ParkPlusMC") != null) { } else if (Bukkit.getPluginManager().getPlugin("ParkPlusMC") != null) {
getLogger().severe("Enabling ParkPlusMC hook..."); getLogger().info("Enabling ParkPlusMC hook...");
hasParkHook = true; hasParkHook = true;
} }

View file

@ -169,19 +169,14 @@ public class TPPCMD implements TabExecutor {
long onDelayTicks = Long.parseLong(args[5]) * 20L; long onDelayTicks = Long.parseLong(args[5]) * 20L;
long offDelayTicks = args.length == 7 ? Long.parseLong(args[6]) * 20L : 20L; long offDelayTicks = args.length == 7 ? Long.parseLong(args[6]) * 20L : 20L;
Block block = loc.getBlock(); final Block block = loc.getBlock();
Material oldMaterial = block.getType(); final 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.");
Bukkit.getScheduler().scheduleSyncDelayedTask(ThemeParkPlus.getInstance(), () -> { Bukkit.getScheduler().scheduleSyncDelayedTask(ThemeParkPlus.getInstance(), () -> {
block.setType(redstoneMaterial); block.setType(XMaterial.REDSTONE_BLOCK.parseMaterial());
Bukkit.getScheduler().scheduleSyncDelayedTask(ThemeParkPlus.getInstance(), () -> { Bukkit.getScheduler().scheduleSyncDelayedTask(ThemeParkPlus.getInstance(), () -> {
BlockState state = block.getState(); BlockState state = block.getState();
state.setType(oldMaterial); state.setType(oldMaterial);
if (!XMaterial.isNewVersion()) state.setRawData(oldData);
state.update(true); state.update(true);
}, offDelayTicks); }, offDelayTicks);
}, onDelayTicks); }, onDelayTicks);

View file

@ -2,81 +2,30 @@ package nl.sbdeveloper.themeparkplus.util;
import com.cryptomorin.xseries.XMaterial; import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState; import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import java.lang.reflect.InvocationTargetException; import org.bukkit.block.data.Openable;
import java.lang.reflect.Method; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class LGUtil { 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 LGUtil() {}
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) {}
}
public static boolean zetLampAan(Block lampBlock) { public static boolean zetLampAan(Block lampBlock) {
if (is113Plus) {
if ((isLamp(lampBlock)) && (!isAan(lampBlock))) { if ((isLamp(lampBlock)) && (!isAan(lampBlock))) {
setBlockData(lampBlock, lampAanData); lampBlock.setBlockData(lampAanData);
return true; return true;
} }
return false; 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) { public static boolean zetLampUit(Block lampBlock) {
if (is113Plus) {
if ((isLamp(lampBlock)) && (isAan(lampBlock))) { if ((isLamp(lampBlock)) && (isAan(lampBlock))) {
setBlockData(lampBlock, lampUitData); lampBlock.setBlockData(lampUitData);
return true;
}
return false;
}
if (isAan(lampBlock)) {
lampBlock.setType(Objects.requireNonNull(Material.matchMaterial(XMaterial.REDSTONE_LAMP.getLegacy()[1])));
return true; return true;
} }
return false; return false;
@ -87,43 +36,25 @@ public class LGUtil {
} }
public static boolean openGate(Block b, BlockFace openFace) { public static boolean openGate(Block b, BlockFace openFace) {
if (is113Plus) { BlockData blockData = b.getBlockData();
org.bukkit.block.data.BlockData blockData = b.getBlockData();
if (isOpenable(b)) { if (isOpenable(b)) {
org.bukkit.block.data.Openable op = (org.bukkit.block.data.Openable) blockData; Openable op = (Openable) blockData;
if (op.isOpen()) return false; if (op.isOpen()) return false;
op.setOpen(true); op.setOpen(true);
b.setBlockData(blockData); b.setBlockData(blockData);
org.bukkit.block.data.Directional dir = (org.bukkit.block.data.Directional) blockData; Directional dir = (Directional) blockData;
dir.setFacing(openFace); dir.setFacing(openFace);
b.setBlockData(blockData); b.setBlockData(blockData);
return true; 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; return false;
} }
public static boolean closeGate(Block b) { public static boolean closeGate(Block b) {
if (is113Plus) { BlockData blockData = b.getBlockData();
org.bukkit.block.data.BlockData blockData = b.getBlockData();
if (isOpenable(b)) { if (isOpenable(b)) {
org.bukkit.block.data.Openable op = (org.bukkit.block.data.Openable) blockData; Openable op = (Openable) blockData;
if (!op.isOpen()) { if (!op.isOpen()) {
return false; return false;
} }
@ -131,142 +62,36 @@ public class LGUtil {
b.setBlockData(blockData); b.setBlockData(blockData);
return true; 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; return false;
} }
public static BlockFace getDirection(Block b) { public static BlockFace getDirection(Block b) {
if (is113Plus) { BlockData blockData = b.getBlockData();
org.bukkit.block.data.BlockData blockData = b.getBlockData();
if (isOpenable(b)) { if (isOpenable(b)) {
org.bukkit.block.data.Directional dir = (org.bukkit.block.data.Directional) blockData; Directional dir = (Directional) blockData;
return dir.getFacing(); 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; return null;
} }
public static boolean isOpen(Block b) { public static boolean isOpen(Block b) {
if (is113Plus) { BlockData blockData = b.getBlockData();
org.bukkit.block.data.BlockData blockData = b.getBlockData();
if (isOpenable(b)) { if (isOpenable(b)) {
org.bukkit.block.data.Openable op = (org.bukkit.block.data.Openable) blockData; Openable op = (Openable) blockData;
return op.isOpen(); 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; return false;
} }
/* Gate codes sponsored by MrWouter <3 */ public static boolean isOpenable(@NotNull Block b) {
public static boolean isOpenable(Block b) {
if (b == null) {
return false;
}
if (is113Plus) {
return b.getBlockData() instanceof org.bukkit.block.data.Openable; 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) { private static boolean isAan(Block lamp) {
if (is113Plus) { return lamp.getBlockData().getAsString().contains("lit=true");
return getAsString(getBlockData(lamp)).contains("lit=true");
} else {
return lamp.getType() == Material.matchMaterial(XMaterial.REDSTONE_LAMP.getLegacy()[0]);
}
} }
private static boolean isLamp(Block lamp) { private static boolean isLamp(Block lamp) {
if (is113Plus) {
return lamp.getType() == XMaterial.REDSTONE_LAMP.parseMaterial(); 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;
}
}