From ea99bd5fd454246db201a50afa2ac48cd0fa5ecb Mon Sep 17 00:00:00 2001 From: Jikoo Date: Wed, 28 Jul 2021 11:28:08 -0400 Subject: [PATCH] Extract duplicate API-only code --- .../openinv/internal/IAnySilentContainer.java | 110 +++++++++++++++++- .../internal/v1_16_R3/AnySilentContainer.java | 98 ---------------- .../internal/v1_17_R1/AnySilentContainer.java | 95 --------------- 3 files changed, 107 insertions(+), 196 deletions(-) diff --git a/api/src/main/java/com/lishid/openinv/internal/IAnySilentContainer.java b/api/src/main/java/com/lishid/openinv/internal/IAnySilentContainer.java index d5e586b..c042777 100644 --- a/api/src/main/java/com/lishid/openinv/internal/IAnySilentContainer.java +++ b/api/src/main/java/com/lishid/openinv/internal/IAnySilentContainer.java @@ -16,8 +16,19 @@ package com.lishid.openinv.internal; +import org.bukkit.Material; +import org.bukkit.block.Barrel; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.BlockState; +import org.bukkit.block.EnderChest; +import org.bukkit.block.ShulkerBox; +import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.Directional; +import org.bukkit.block.data.type.Chest; +import org.bukkit.entity.Cat; import org.bukkit.entity.Player; +import org.bukkit.util.BoundingBox; import org.jetbrains.annotations.NotNull; public interface IAnySilentContainer { @@ -41,14 +52,99 @@ public interface IAnySilentContainer { */ void deactivateContainer(@NotNull Player player); + /** + * @deprecated use {@link #isAnyContainerNeeded(Block)} + * @param player the player opening the container + * @param block the block + * @return true if the container is blocked + */ + @Deprecated + default boolean isAnyContainerNeeded(@NotNull Player player, @NotNull Block block) { + return isAnyContainerNeeded(block); + } + /** * Checks if the container at the given coordinates is blocked. * - * @param player the Player opening the container * @param block the Block * @return true if the container is blocked */ - boolean isAnyContainerNeeded(@NotNull Player player, @NotNull Block block); + default boolean isAnyContainerNeeded(@NotNull Block block) { + BlockState blockState = block.getState(); + + // Barrels do not require AnyContainer. + if (blockState instanceof Barrel) { + return false; + } + + // Enderchests require a non-occluding block on top to open. + if (blockState instanceof EnderChest) { + return block.getRelative(0, 1, 0).getType().isOccluding(); + } + + // Shulker boxes require 1/2 a block clear in the direction they open. + if (blockState instanceof ShulkerBox) { + BoundingBox boundingBox = block.getBoundingBox(); + if (boundingBox.getVolume() > 1) { + // Shulker box is already open. + return false; + } + + BlockData blockData = block.getBlockData(); + if (!(blockData instanceof Directional)) { + // Shouldn't be possible. Just in case, demand AnyChest. + return true; + } + + Directional directional = (Directional) blockData; + BlockFace face = directional.getFacing(); + boundingBox.shift(face.getDirection()); + // Return whether or not bounding boxes overlap. + return block.getRelative(face, 1).getBoundingBox().overlaps(boundingBox); + } + + + if (!(blockState instanceof org.bukkit.block.Chest)) { + return false; + } + + if (isChestBlocked(block)) { + return true; + } + + BlockData blockData = block.getBlockData(); + if (!(blockData instanceof Chest) || ((Chest) blockData).getType() == Chest.Type.SINGLE) { + return false; + } + + Chest chest = (Chest) blockData; + int ordinal = (chest.getFacing().ordinal() + 4 + (chest.getType() == Chest.Type.RIGHT ? -1 : 1)) % 4; + BlockFace relativeFace = BlockFace.values()[ordinal]; + org.bukkit.block.Block relative = block.getRelative(relativeFace); + + if (relative.getType() != block.getType()) { + return false; + } + + BlockData relativeData = relative.getBlockData(); + if (!(relativeData instanceof Chest)) { + return false; + } + + Chest relativeChest = (Chest) relativeData; + if (relativeChest.getFacing() != chest.getFacing() + || relativeChest.getType() != (chest.getType() == Chest.Type.RIGHT ? Chest.Type.LEFT : Chest.Type.RIGHT)) { + return false; + } + + return isChestBlocked(relative); + } + + default boolean isChestBlocked(Block chest) { + org.bukkit.block.Block relative = chest.getRelative(0, 1, 0); + return relative.getType().isOccluding() + || chest.getWorld().getNearbyEntities(BoundingBox.of(relative), entity -> entity instanceof Cat).size() > 0; + } /** * Checks if the given block is a container which can be unblocked or silenced. @@ -56,6 +152,14 @@ public interface IAnySilentContainer { * @param block the BlockState * @return true if the Block is a supported container */ - boolean isAnySilentContainer(@NotNull Block block); + default boolean isAnySilentContainer(@NotNull Block block) { + if (block.getType() == Material.ENDER_CHEST) { + return true; + } + BlockState state = block.getState(); + return state instanceof org.bukkit.block.Chest + || state instanceof org.bukkit.block.ShulkerBox + || state instanceof org.bukkit.block.Barrel; + } } diff --git a/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/AnySilentContainer.java b/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/AnySilentContainer.java index 36281c2..3e099c0 100644 --- a/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/AnySilentContainer.java +++ b/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/AnySilentContainer.java @@ -48,18 +48,8 @@ import net.minecraft.server.v1_16_R3.TileInventory; import net.minecraft.server.v1_16_R3.World; import org.bukkit.Material; import org.bukkit.Statistic; -import org.bukkit.block.Barrel; -import org.bukkit.block.BlockFace; -import org.bukkit.block.BlockState; -import org.bukkit.block.EnderChest; -import org.bukkit.block.ShulkerBox; -import org.bukkit.block.data.BlockData; -import org.bukkit.block.data.Directional; -import org.bukkit.block.data.type.Chest; -import org.bukkit.entity.Cat; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryView; -import org.bukkit.util.BoundingBox; import org.jetbrains.annotations.NotNull; public class AnySilentContainer implements IAnySilentContainer { @@ -76,94 +66,6 @@ public class AnySilentContainer implements IAnySilentContainer { } } - @Override - public boolean isAnySilentContainer(@NotNull final org.bukkit.block.Block bukkitBlock) { - if (bukkitBlock.getType() == Material.ENDER_CHEST) { - return true; - } - BlockState state = bukkitBlock.getState(); - return state instanceof org.bukkit.block.Chest - || state instanceof org.bukkit.block.ShulkerBox - || state instanceof org.bukkit.block.Barrel; - } - - @Override - public boolean isAnyContainerNeeded(@NotNull final Player p, @NotNull final org.bukkit.block.Block block) { - BlockState blockState = block.getState(); - - // Barrels do not require AnyContainer. - if (blockState instanceof Barrel) { - return false; - } - - // Enderchests require a non-occluding block on top to open. - if (blockState instanceof EnderChest) { - return block.getRelative(0, 1, 0).getType().isOccluding(); - } - - // Shulker boxes require 1/2 a block clear in the direction they open. - if (blockState instanceof ShulkerBox) { - BoundingBox boundingBox = block.getBoundingBox(); - if (boundingBox.getVolume() > 1) { - // Shulker box is already open. - return false; - } - - BlockData blockData = block.getBlockData(); - if (!(blockData instanceof Directional)) { - // Shouldn't be possible. Just in case, demand AnyChest. - return true; - } - - Directional directional = (Directional) blockData; - BlockFace face = directional.getFacing(); - boundingBox.shift(face.getDirection()); - // Return whether or not bounding boxes overlap. - return block.getRelative(face, 1).getBoundingBox().overlaps(boundingBox); - } - - if (!(blockState instanceof org.bukkit.block.Chest)) { - return false; - } - - if (isBlockedChest(block)) { - return true; - } - - BlockData blockData = block.getBlockData(); - if (!(blockData instanceof Chest) || ((Chest) blockData).getType() == Chest.Type.SINGLE) { - return false; - } - - Chest chest = (Chest) blockData; - int ordinal = (chest.getFacing().ordinal() + 4 + (chest.getType() == Chest.Type.RIGHT ? -1 : 1)) % 4; - BlockFace relativeFace = BlockFace.values()[ordinal]; - org.bukkit.block.Block relative = block.getRelative(relativeFace); - - if (relative.getType() != block.getType()) { - return false; - } - - BlockData relativeData = relative.getBlockData(); - if (!(relativeData instanceof Chest)) { - return false; - } - - Chest relativeChest = (Chest) relativeData; - if (relativeChest.getFacing() != chest.getFacing() - || relativeChest.getType() != (chest.getType() == Chest.Type.RIGHT ? Chest.Type.LEFT : Chest.Type.RIGHT)) { - return false; - } - - return isBlockedChest(relative); - } - - private boolean isBlockedChest(org.bukkit.block.Block block) { - org.bukkit.block.Block relative = block.getRelative(0, 1, 0); - return relative.getType().isOccluding() - || block.getWorld().getNearbyEntities(BoundingBox.of(relative), entity -> entity instanceof Cat).size() > 0; - } - @Override public boolean activateContainer(@NotNull final Player bukkitPlayer, final boolean silentchest, @NotNull final org.bukkit.block.Block bukkitBlock) { diff --git a/internal/v1_17_R1/src/main/java/com/lishid/openinv/internal/v1_17_R1/AnySilentContainer.java b/internal/v1_17_R1/src/main/java/com/lishid/openinv/internal/v1_17_R1/AnySilentContainer.java index 4e8091d..26a6266 100644 --- a/internal/v1_17_R1/src/main/java/com/lishid/openinv/internal/v1_17_R1/AnySilentContainer.java +++ b/internal/v1_17_R1/src/main/java/com/lishid/openinv/internal/v1_17_R1/AnySilentContainer.java @@ -50,18 +50,8 @@ import net.minecraft.world.level.block.state.IBlockData; import net.minecraft.world.level.block.state.properties.BlockPropertyChestType; import org.bukkit.Material; import org.bukkit.Statistic; -import org.bukkit.block.Barrel; -import org.bukkit.block.BlockFace; -import org.bukkit.block.BlockState; -import org.bukkit.block.EnderChest; -import org.bukkit.block.ShulkerBox; -import org.bukkit.block.data.BlockData; -import org.bukkit.block.data.Directional; -import org.bukkit.block.data.type.Chest; -import org.bukkit.entity.Cat; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryView; -import org.bukkit.util.BoundingBox; import org.jetbrains.annotations.NotNull; public class AnySilentContainer implements IAnySilentContainer { @@ -79,91 +69,6 @@ public class AnySilentContainer implements IAnySilentContainer { } } - @Override - public boolean isAnySilentContainer(@NotNull final org.bukkit.block.Block bukkitBlock) { - if (bukkitBlock.getType() == Material.ENDER_CHEST) { - return true; - } - BlockState state = bukkitBlock.getState(); - return state instanceof org.bukkit.block.Chest - || state instanceof org.bukkit.block.ShulkerBox - || state instanceof org.bukkit.block.Barrel; - } - - @Override - public boolean isAnyContainerNeeded(@NotNull final Player p, @NotNull final org.bukkit.block.Block block) { - BlockState blockState = block.getState(); - - // Barrels do not require AnyContainer. - if (blockState instanceof Barrel) { - return false; - } - - // Enderchests require a non-occluding block on top to open. - if (blockState instanceof EnderChest) { - return block.getRelative(0, 1, 0).getType().isOccluding(); - } - - // Shulker boxes require 1/2 a block clear in the direction they open. - if (blockState instanceof ShulkerBox) { - BoundingBox boundingBox = block.getBoundingBox(); - if (boundingBox.getVolume() > 1) { - // Shulker box is already open. - return false; - } - - BlockData blockData = block.getBlockData(); - if (!(blockData instanceof Directional directional)) { - // Shouldn't be possible. Just in case, demand AnyChest. - return true; - } - - BlockFace face = directional.getFacing(); - boundingBox.shift(face.getDirection()); - // Return whether or not bounding boxes overlap. - return block.getRelative(face, 1).getBoundingBox().overlaps(boundingBox); - } - - if (!(blockState instanceof org.bukkit.block.Chest)) { - return false; - } - - if (isBlockedChest(block)) { - return true; - } - - BlockData blockData = block.getBlockData(); - if (!(blockData instanceof Chest chest) || ((Chest) blockData).getType() == Chest.Type.SINGLE) { - return false; - } - - int ordinal = (chest.getFacing().ordinal() + 4 + (chest.getType() == Chest.Type.RIGHT ? -1 : 1)) % 4; - BlockFace relativeFace = BlockFace.values()[ordinal]; - org.bukkit.block.Block relative = block.getRelative(relativeFace); - - if (relative.getType() != block.getType()) { - return false; - } - - BlockData relativeData = relative.getBlockData(); - if (!(relativeData instanceof Chest relativeChest)) { - return false; - } - - if (relativeChest.getFacing() != chest.getFacing() - || relativeChest.getType() != (chest.getType() == Chest.Type.RIGHT ? Chest.Type.LEFT : Chest.Type.RIGHT)) { - return false; - } - - return isBlockedChest(relative); - } - - private boolean isBlockedChest(org.bukkit.block.Block block) { - org.bukkit.block.Block relative = block.getRelative(0, 1, 0); - return relative.getType().isOccluding() - || block.getWorld().getNearbyEntities(BoundingBox.of(relative), entity -> entity instanceof Cat).size() > 0; - } - @Override public boolean activateContainer( @NotNull final Player bukkitPlayer,