diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/ChestsPlusPlus.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/ChestsPlusPlus.java index 1014c75..5ecec88 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/ChestsPlusPlus.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/ChestsPlusPlus.java @@ -139,6 +139,7 @@ public class ChestsPlusPlus extends JavaPlugin { getServer().getPluginManager().registerEvents(new InventoryListener(), this); getServer().getPluginManager().registerEvents(new HopperListener(), this); getServer().getPluginManager().registerEvents(new WorldListener(), this); + Config.getStorageTypes().forEach(storageType -> getServer().getPluginManager().registerEvents(storageType, this)); getLogger().info("Chests++ enabled!"); }, 1); } diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java index 81d97e7..f9b1fa1 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java @@ -11,16 +11,19 @@ import com.jamesdpeters.minecraft.chests.sort.InventorySorter; import com.jamesdpeters.minecraft.chests.storage.autocraft.AutoCraftingStorage; import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorage; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.inventory.InventoryInteractEvent; import org.bukkit.event.inventory.InventoryOpenEvent; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; @@ -32,38 +35,6 @@ public class InventoryListener implements Listener { inventoryUpdate(event); } - @EventHandler(priority = EventPriority.LOWEST) - public void onInventoryOpen(InventoryOpenEvent event) { - try { - if (event.getPlayer() instanceof Player) { - if (event.getInventory().getLocation() != null) { - ChestLinkStorage storage = Config.getChestLink().getStorage(event.getInventory().getLocation()); - if (storage != null) { - event.setCancelled(true); - if (event.getPlayer().hasPermission(Permissions.OPEN) && storage.hasPermission((Player) event.getPlayer())) { - storage.getInventory().getViewers().remove(event.getPlayer()); - Utils.openChestInventory((Player) event.getPlayer(), storage, storage.getLocationInfo(event.getInventory().getLocation())); - } else { - Messages.NO_PERMISSION((Player) event.getPlayer()); - } - } else { - //If no Inventory Storage here check for AutoCraft - AutoCraftingStorage craftingStorage = Config.getAutoCraft().getStorage(event.getInventory().getLocation()); - - if (craftingStorage != null) { - event.setCancelled(true); - if (event.getPlayer().hasPermission(Permissions.AUTOCRAFT_OPEN) && craftingStorage.hasPermission((Player) event.getPlayer())) { - event.getPlayer().openInventory(craftingStorage.getInventory()); - craftingStorage.getVirtualCraftingHolder().startAnimation(); - } - } - } - } - } - } catch (NullPointerException ignore) { - } //Essentials does something weird with enderchests - shit fix but works :) - } - @EventHandler public void onInventoryClose(InventoryCloseEvent event) { try { @@ -103,7 +74,6 @@ public class InventoryListener implements Listener { craftingUpdate(event); } - //CRAFTING @EventHandler(priority = EventPriority.HIGHEST) public void inventoryDragEvent(InventoryDragEvent event) { diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java index 507abc8..c0f11ba 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java @@ -160,7 +160,7 @@ public abstract class AbstractStorage implements ConfigurationSerializable { init(); } - public abstract StorageType getStorageType(); + public abstract StorageType getStorageType(); /** * @return true if this storage should store the inventory to disk. @@ -502,7 +502,7 @@ public abstract class AbstractStorage implements ConfigurationSerializable { else removeArmorStandItem(location.getToolItemStand()); } } else { -// anchor.getState().update(); + anchor.getState().update(); removeArmorStandItem(location.getToolItemStand()); removeArmorStandItem(location.getItemStand()); removeArmorStandItem(location.getBlockStand()); diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/StorageType.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/StorageType.java index 82e6e12..2d7e0fa 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/StorageType.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/StorageType.java @@ -23,8 +23,12 @@ import org.bukkit.block.BlockState; import org.bukkit.block.Sign; import org.bukkit.block.data.type.WallSign; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataType; @@ -37,7 +41,7 @@ import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; -public abstract class StorageType { +public abstract class StorageType implements Listener { private final ConfigStorage store; private final StorageUtils, T> storageUtils; @@ -119,6 +123,15 @@ public abstract class StorageType { public abstract StorageMessages getMessages(); + @EventHandler + public void playerInteractEvent(PlayerInteractEvent event){ + if (event.getAction() == Action.RIGHT_CLICK_BLOCK && !event.getPlayer().isSneaking()) { + onBlockRightClick(event); + } + } + + public abstract void onBlockRightClick(PlayerInteractEvent event); + /* STORAGE MAP SECTION */ @@ -160,7 +173,9 @@ public abstract class StorageType { if (isValidBlockType(block)) { StorageInfo storageInfo = storageUtils.getStorageInfo(location); if (storageInfo != null) { - return storageInfo.getStorage(location); + storage = storageInfo.getStorage(location); + storageCache.put(location, storage); + return storage; } } } diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorageType.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorageType.java index 277b8ea..284e6e4 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorageType.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorageType.java @@ -10,6 +10,7 @@ import com.jamesdpeters.minecraft.chests.serialize.ConfigStorage; import com.jamesdpeters.minecraft.chests.storage.abstracts.StorageInfo; import com.jamesdpeters.minecraft.chests.storage.abstracts.StorageMessages; import com.jamesdpeters.minecraft.chests.storage.abstracts.StorageType; +import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorage; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; @@ -18,6 +19,8 @@ import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.Sign; import org.bukkit.entity.Player; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; import java.util.Arrays; import java.util.HashMap; @@ -116,6 +119,22 @@ public class AutoCraftingStorageType extends StorageType { return messages; } + @Override + public void onBlockRightClick(PlayerInteractEvent event) { + // AutoCraft Check + if (event.getClickedBlock() != null && isValidBlockType(event.getClickedBlock())) { + Location location = event.getClickedBlock().getLocation(); + AutoCraftingStorage storage = getStorage(location); + if (storage != null) { + event.setCancelled(true); + if (event.getPlayer().hasPermission(Permissions.AUTOCRAFT_OPEN) && storage.hasPermission(event.getPlayer())) { + event.getPlayer().openInventory(storage.getInventory()); + storage.getVirtualCraftingHolder().startAnimation(); + } + } + } + } + private static final AutoCraftMessages messages = new AutoCraftMessages(); private static class AutoCraftMessages extends StorageMessages { diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorageType.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorageType.java index 30e5530..8338761 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorageType.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorageType.java @@ -1,6 +1,7 @@ package com.jamesdpeters.minecraft.chests.storage.chestlink; import com.jamesdpeters.minecraft.chests.lang.Message; +import com.jamesdpeters.minecraft.chests.misc.Messages; import com.jamesdpeters.minecraft.chests.misc.Permissions; import com.jamesdpeters.minecraft.chests.misc.Utils; import com.jamesdpeters.minecraft.chests.misc.Values; @@ -9,6 +10,7 @@ import com.jamesdpeters.minecraft.chests.serialize.Config; import com.jamesdpeters.minecraft.chests.serialize.ConfigStorage; import com.jamesdpeters.minecraft.chests.storage.abstracts.StorageMessages; import com.jamesdpeters.minecraft.chests.storage.abstracts.StorageType; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.OfflinePlayer; @@ -19,6 +21,8 @@ import org.bukkit.block.Chest; import org.bukkit.block.Container; import org.bukkit.block.data.Directional; import org.bukkit.entity.Player; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; import java.util.Collections; import java.util.HashMap; @@ -118,6 +122,24 @@ public class ChestLinkStorageType extends StorageType { return messages; } + @Override + public void onBlockRightClick(PlayerInteractEvent event) { + // ChestLink Check + if (event.getClickedBlock() != null && isValidBlockType(event.getClickedBlock())) { + Location location = event.getClickedBlock().getLocation(); + ChestLinkStorage storage = getStorage(location); + if (storage != null) { + event.setCancelled(true); + if (event.getPlayer().hasPermission(Permissions.OPEN) && storage.hasPermission(event.getPlayer())) { + storage.getInventory().getViewers().remove(event.getPlayer()); + Utils.openChestInventory(event.getPlayer(), storage, storage.getLocationInfo(location)); + } else { + Messages.NO_PERMISSION(event.getPlayer()); + } + } + } + } + private static final ChestLinkMessages messages = new ChestLinkMessages(); private static class ChestLinkMessages extends StorageMessages {