diff --git a/pom.xml b/pom.xml index 44b63a3..aefefc6 100644 --- a/pom.xml +++ b/pom.xml @@ -449,7 +449,7 @@ default - !spigot + !dontrundefault @@ -462,7 +462,7 @@ com.destroystokyo.paper paper-api - 1.13.2-R0.1-SNAPSHOT + 1.16.5-R0.1-SNAPSHOT provided @@ -485,7 +485,7 @@ - true + true ${project.name}-Spigot @@ -503,7 +503,7 @@ - latest + backwards paper-repo @@ -514,10 +514,26 @@ com.destroystokyo.paper paper-api - 1.16.4-R0.1-SNAPSHOT + 1.13.2-R0.1-SNAPSHOT provided + + true + + + ${project.name}-1.13.2 + + + maven-compiler-plugin + + + **/PaperLatest*.java + + + + + diff --git a/src/main/java/com/Acrobot/Breeze/Utils/ImplementationAdapter.java b/src/main/java/com/Acrobot/Breeze/Utils/ImplementationAdapter.java new file mode 100644 index 0000000..2403551 --- /dev/null +++ b/src/main/java/com/Acrobot/Breeze/Utils/ImplementationAdapter.java @@ -0,0 +1,51 @@ +package com.Acrobot.Breeze.Utils; + +import com.Acrobot.Breeze.Utils.ImplementationFeatures.PaperLatestHolder; +import com.Acrobot.Breeze.Utils.ImplementationFeatures.PaperLatestState; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; + +import java.util.function.BiFunction; + +public class ImplementationAdapter { + + private static BiFunction HOLDER_PROVIDER; + private static BiFunction STATE_PROVIDER; + + static { + try { + InventoryHolder.class.getMethod("getHolder", boolean.class); + HOLDER_PROVIDER = PaperLatestHolder.PROVIDER; + } catch (NoSuchMethodException e) { + HOLDER_PROVIDER = (inventory, useSnapshot) -> inventory.getHolder(); + } + try { + Block.class.getMethod("getState", boolean.class); + STATE_PROVIDER = PaperLatestState.PROVIDER; + } catch (NoSuchMethodException e) { + STATE_PROVIDER = (block, useSnapshot) -> block.getState(); + } + } + + /** + * Get the inventory's holder. + * @param inventory The inventory + * @param useSnapshot Whether or not the holder should be a snapshot (if possible) + * @return The inventory's holder + */ + public static InventoryHolder getHolder(Inventory inventory, boolean useSnapshot) { + return HOLDER_PROVIDER.apply(inventory, useSnapshot); + } + + /** + * Get a block state + * @param block The block to get the state from + * @param useSnapshot Whether or not the state should be a snapshot (if possible) + * @return The block's state + */ + public static BlockState getState(Block block, boolean useSnapshot) { + return STATE_PROVIDER.apply(block, useSnapshot); + } +} diff --git a/src/main/java/com/Acrobot/Breeze/Utils/ImplementationFeatures/PaperLatestHolder.java b/src/main/java/com/Acrobot/Breeze/Utils/ImplementationFeatures/PaperLatestHolder.java new file mode 100644 index 0000000..aa59983 --- /dev/null +++ b/src/main/java/com/Acrobot/Breeze/Utils/ImplementationFeatures/PaperLatestHolder.java @@ -0,0 +1,12 @@ +package com.Acrobot.Breeze.Utils.ImplementationFeatures; + +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; + +import java.util.function.BiFunction; + +public class PaperLatestHolder { + + public static final BiFunction PROVIDER = Inventory::getHolder; + +} diff --git a/src/main/java/com/Acrobot/Breeze/Utils/ImplementationFeatures/PaperLatestState.java b/src/main/java/com/Acrobot/Breeze/Utils/ImplementationFeatures/PaperLatestState.java new file mode 100644 index 0000000..f4c5f71 --- /dev/null +++ b/src/main/java/com/Acrobot/Breeze/Utils/ImplementationFeatures/PaperLatestState.java @@ -0,0 +1,12 @@ +package com.Acrobot.Breeze.Utils.ImplementationFeatures; + +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; + +import java.util.function.BiFunction; + +public class PaperLatestState { + + public static final BiFunction PROVIDER = Block::getState; + +} diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java index 2fb280f..3e194d1 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java @@ -28,6 +28,7 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; +import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState; import static com.Acrobot.Breeze.Utils.BlockUtil.getAttachedBlock; import static com.Acrobot.Breeze.Utils.BlockUtil.isSign; import static com.Acrobot.ChestShop.Permission.OTHER_NAME_DESTROY; @@ -55,11 +56,11 @@ public class SignBreak implements Listener { return; } - Sign sign = (Sign) block.getState(); + Sign sign = (Sign) getState(block, false); Block attachedBlock = BlockUtil.getAttachedBlock(sign); if (attachedBlock.getType() == Material.AIR && ChestShopSign.isValid(sign)) { - sendShopDestroyedEvent(sign, block.hasMetadata(METADATA_NAME) + sendShopDestroyedEvent((Sign) block.getState(), block.hasMetadata(METADATA_NAME) ? (Player) block.getMetadata(METADATA_NAME).get(0).value() : null); } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemMoveListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemMoveListener.java index 4bcb998..5e50261 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemMoveListener.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemMoveListener.java @@ -7,6 +7,8 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryMoveItemEvent; +import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder; + /** * @author Acrobot */ @@ -14,11 +16,11 @@ public class ItemMoveListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public static void onItemMove(InventoryMoveItemEvent event) { - if (event.getSource() == null || event.getDestination().getHolder() instanceof BlockState) { + if (event.getSource() == null || getHolder(event.getDestination(), false) instanceof BlockState) { return; } - if (!ChestShopSign.isShopBlock(event.getSource().getHolder())) { + if (!ChestShopSign.isShopBlock(getHolder(event.getSource(), false))) { return; } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java index a47abf0..20c3c21 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java @@ -22,6 +22,7 @@ import org.bukkit.inventory.ItemStack; import java.util.IllegalFormatException; +import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder; import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE; import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE; import static com.Acrobot.ChestShop.Signs.ChestShopSign.QUANTITY_LINE; @@ -67,11 +68,11 @@ public class StockCounterModule implements Listener { @EventHandler public static void onInventoryClose(InventoryCloseEvent event) { - if (event.getInventory().getType() == InventoryType.ENDER_CHEST || event.getInventory().getLocation() == null || !ChestShopSign.isShopBlock(event.getInventory().getLocation().getBlock())) { + if (event.getInventory().getType() == InventoryType.ENDER_CHEST || event.getInventory().getLocation() == null || !uBlock.couldBeShopContainer(event.getInventory().getLocation().getBlock())) { return; } - for (Sign shopSign : uBlock.findConnectedShopSigns(event.getInventory().getHolder())) { + for (Sign shopSign : uBlock.findConnectedShopSigns(getHolder(event.getInventory(), false))) { if (!Properties.USE_STOCK_COUNTER || (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(shopSign))) { if (QuantityUtil.quantityLineContainsCounter(shopSign.getLine(QUANTITY_LINE))) { @@ -113,7 +114,7 @@ public class StockCounterModule implements Listener { return; } - for (Sign shopSign : uBlock.findConnectedShopSigns(event.getOwnerInventory().getHolder())) { + for (Sign shopSign : uBlock.findConnectedShopSigns( getHolder(event.getOwnerInventory(), false))) { updateCounterOnQuantityLine(shopSign, event.getOwnerInventory()); } } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java index c7a44d0..d4a1123 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java @@ -37,6 +37,7 @@ import java.math.MathContext; import java.util.Arrays; import java.util.logging.Level; +import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState; import static com.Acrobot.Breeze.Utils.BlockUtil.isSign; import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType; import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY; @@ -76,7 +77,7 @@ public class PlayerInteract implements Listener { if (!isSign(block) || player.getInventory().getItemInMainHand().getType().name().contains("SIGN")) // Blocking accidental sign edition return; - Sign sign = (Sign) block.getState(); + Sign sign = (Sign) getState(block, false); if (!ChestShopSign.isValid(sign)) { return; } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInventory.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInventory.java index be44411..3678c54 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInventory.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInventory.java @@ -16,6 +16,8 @@ import org.bukkit.inventory.InventoryHolder; import java.util.ArrayList; import java.util.List; +import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder; + /** * @author Acrobot */ @@ -30,7 +32,7 @@ public class PlayerInventory implements Listener { return; } - InventoryHolder holder = event.getInventory().getHolder(); + InventoryHolder holder = getHolder(event.getInventory(), false); if (!(holder instanceof BlockState) && !(holder instanceof DoubleChest)) { return; } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerTeleport.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerTeleport.java index a9012db..6e715d8 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerTeleport.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerTeleport.java @@ -9,6 +9,8 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.inventory.InventoryHolder; +import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder; + /** * A fix for a CraftBukkit bug. * @@ -18,7 +20,7 @@ public class PlayerTeleport implements Listener { @EventHandler public static void onPlayerTeleport(PlayerTeleportEvent event) { - InventoryHolder holder = event.getPlayer().getOpenInventory().getTopInventory().getHolder(); + InventoryHolder holder = getHolder(event.getPlayer().getOpenInventory().getTopInventory(), false); if (!(holder instanceof BlockState)) { return; } diff --git a/src/main/java/com/Acrobot/ChestShop/Plugins/ChestShop.java b/src/main/java/com/Acrobot/ChestShop/Plugins/ChestShop.java index 75fa233..f843e40 100644 --- a/src/main/java/com/Acrobot/ChestShop/Plugins/ChestShop.java +++ b/src/main/java/com/Acrobot/ChestShop/Plugins/ChestShop.java @@ -12,6 +12,7 @@ import org.bukkit.event.Event; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState; import static com.Acrobot.Breeze.Utils.BlockUtil.isSign; /** @@ -44,7 +45,7 @@ public class ChestShop implements Listener { } if (isSign(block)) { - Sign sign = (Sign) block.getState(); + Sign sign = (Sign) getState(block, false); if (!ChestShopSign.isValid(sign)) { return true; diff --git a/src/main/java/com/Acrobot/ChestShop/Security.java b/src/main/java/com/Acrobot/ChestShop/Security.java index 9c2b611..52f60a1 100644 --- a/src/main/java/com/Acrobot/ChestShop/Security.java +++ b/src/main/java/com/Acrobot/ChestShop/Security.java @@ -17,6 +17,8 @@ import org.bukkit.event.Event; import java.util.UUID; +import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState; + /** * @author Acrobot */ @@ -83,7 +85,7 @@ public class Security { continue; } - Sign sign = (Sign) block.getState(); + Sign sign = (Sign) getState(block, false); if (!ChestShopSign.isValid(sign) || !BlockUtil.getAttachedBlock(sign).equals(baseBlock)) { continue; diff --git a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java index 5ba5e35..6fa2831 100644 --- a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java +++ b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java @@ -22,6 +22,8 @@ import org.bukkit.inventory.InventoryHolder; import java.util.Locale; import java.util.regex.Pattern; +import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState; + /** * @author Acrobot */ @@ -66,7 +68,7 @@ public class ChestShopSign { } public static boolean isValid(Block sign) { - return BlockUtil.isSign(sign) && isValid((Sign) sign.getState()); + return BlockUtil.isSign(sign) && isValid((Sign) getState(sign, false)); } /** @@ -78,7 +80,7 @@ public class ChestShopSign { return false; } - return uBlock.getConnectedSign((Chest) chest.getState()) != null; + return uBlock.getConnectedSign(chest) != null; } public static boolean isShopBlock(Block block) { diff --git a/src/main/java/com/Acrobot/ChestShop/Signs/RestrictedSign.java b/src/main/java/com/Acrobot/ChestShop/Signs/RestrictedSign.java index d3466fe..02bd7db 100644 --- a/src/main/java/com/Acrobot/ChestShop/Signs/RestrictedSign.java +++ b/src/main/java/com/Acrobot/ChestShop/Signs/RestrictedSign.java @@ -15,6 +15,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.SignChangeEvent; +import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState; import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.SHOP_IS_RESTRICTED; import static com.Acrobot.ChestShop.Permission.ADMIN; @@ -56,7 +57,7 @@ public class RestrictedSign implements Listener { return; } - Sign sign = (Sign) connectedSign.getState(); + Sign sign = (Sign) getState(connectedSign, false); if (!ChestShopSign.hasPermission(player, Permission.OTHER_NAME_DESTROY, sign)) { dropSignAndCancelEvent(event); @@ -84,7 +85,7 @@ public class RestrictedSign implements Listener { Block currentBlock = location.getBlock(); if (BlockUtil.isSign(currentBlock)) { - Sign sign = (Sign) currentBlock.getState(); + Sign sign = (Sign) getState(currentBlock, false); if (isRestricted(sign)) { return sign; @@ -100,7 +101,7 @@ public class RestrictedSign implements Listener { continue; } - Sign sign = (Sign) relative.getState(); + Sign sign = (Sign) getState(relative, false); if (!BlockUtil.getAttachedBlock(sign).equals(currentBlock)) { continue; @@ -116,7 +117,7 @@ public class RestrictedSign implements Listener { public static boolean isRestrictedShop(Sign sign) { Block blockUp = sign.getBlock().getRelative(BlockFace.UP); - return BlockUtil.isSign(blockUp) && isRestricted(((Sign) blockUp.getState()).getLines()); + return BlockUtil.isSign(blockUp) && isRestricted(((Sign) getState(blockUp, false)).getLines()); } public static boolean isRestricted(String[] lines) { @@ -129,7 +130,7 @@ public class RestrictedSign implements Listener { public static boolean canAccess(Sign sign, Player player) { Block blockUp = sign.getBlock().getRelative(BlockFace.UP); - return !BlockUtil.isSign(blockUp) || hasPermission(player, ((Sign) blockUp.getState()).getLines()); + return !BlockUtil.isSign(blockUp) || hasPermission(player, ((Sign) getState(blockUp, false)).getLines()); } public static boolean canDestroy(Player player, Sign sign) { @@ -139,7 +140,7 @@ public class RestrictedSign implements Listener { public static Sign getAssociatedSign(Sign restricted) { Block down = restricted.getBlock().getRelative(BlockFace.DOWN); - return BlockUtil.isSign(down) ? (Sign) down.getState() : null; + return BlockUtil.isSign(down) ? (Sign) getState(down, false) : null; } public static boolean hasPermission(Player p, String[] lines) { diff --git a/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java b/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java index 1a762ba..79b5668 100644 --- a/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java +++ b/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java @@ -18,6 +18,8 @@ import org.bukkit.inventory.InventoryHolder; import java.util.ArrayList; import java.util.List; +import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState; + /** * @author Acrobot */ @@ -223,7 +225,7 @@ public class uBlock { continue; } - Sign sign = (Sign) faceBlock.getState(); + Sign sign = (Sign) getState(faceBlock, false); if (ChestShopSign.isValid(sign)) { return sign;