Extract duplicate API-only code
This commit is contained in:
		@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user