diff --git a/src/main/java/fr/birdo/endercargo/EnderCargo.java b/src/main/java/fr/birdo/endercargo/EnderCargo.java index f9132de..23cfbc2 100644 --- a/src/main/java/fr/birdo/endercargo/EnderCargo.java +++ b/src/main/java/fr/birdo/endercargo/EnderCargo.java @@ -1,5 +1,10 @@ package fr.birdo.endercargo; +import fr.birdo.endercargo.Utils.EnderCargoData; +import fr.birdo.endercargo.items.EnderCargoAdvancedOutput; +import fr.birdo.endercargo.items.EnderCargoInput; +import fr.birdo.endercargo.items.EnderCargoLinker; +import fr.birdo.endercargo.items.EnderCargoOutput; import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; @@ -8,10 +13,13 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.NamespacedKey; +import org.bukkit.block.Container; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; import java.io.File; import java.io.IOException; @@ -24,47 +32,27 @@ public class EnderCargo extends JavaPlugin implements SlimefunAddon { public void onEnable() { getServer().getPluginManager().registerEvents(new EnderCargoInput(this), this); getServer().getPluginManager().registerEvents(new EnderCargoOutput(this), this); + getServer().getPluginManager().registerEvents(new EnderCargoAdvancedOutput(this), this); getServer().getPluginManager().registerEvents(new EnderCargoLinker(this), this); - /* - * 1. Creating a new Category - * This Category will use the following ItemStack - */ + //Creating a new category ItemStack itemGroupItem = new CustomItemStack(Material.ENDER_EYE, "&5Ender Cargo", "", "&a> Click to open"); - - // Give your Category a unique id. NamespacedKey itemGroupId = new NamespacedKey(this, "endercargo_category"); ItemGroup itemGroup = new ItemGroup(itemGroupId, itemGroupItem); - /* - * 2. Create a new SlimefunItemStack - * This class has many constructors, it is very important - * that you give each item a unique id. - */ - SlimefunItemStack ender_input = new SlimefunItemStack("ENDER_INPUT", Material.DISPENSER, EnderCargoInput.blockName, "&7Use the ender force to teleport", "&7items between worlds"); - SlimefunItemStack ender_output = new SlimefunItemStack("ENDER_OUTPUT", Material.DISPENSER, EnderCargoOutput.blockName, "&7Use the ender force to teleport", "&7items between worlds"); - SlimefunItemStack advanced_ender_output = new SlimefunItemStack("ADVANCED_ENDER_OUTPUT", Material.DISPENSER, EnderCargoAdvancedOutput.blockName, "&7Use the ender force to teleport", "&7items between worlds"); - SlimefunItemStack ender_linker = new SlimefunItemStack("ENDER_LINKER", Material.ENDER_EYE, "&3Ender Cargo Linker", "&7&eShift + Right Click&7 to link", "&7&eLeft Click&7 to reset"); + //Create Slimefun Itemstacks + SlimefunItemStack ender_input = new SlimefunItemStack("ENDER_INPUT", EnderCargoInput.blockMaterial, EnderCargoInput.blockName, "&7Use the ender force to teleport", "&7items between worlds"); + SlimefunItemStack ender_output = new SlimefunItemStack("ENDER_OUTPUT", EnderCargoOutput.blockMaterial, EnderCargoOutput.blockName, "&7Use the ender force to teleport", "&7items between worlds"); + SlimefunItemStack advanced_ender_output = new SlimefunItemStack("ADVANCED_ENDER_OUTPUT", EnderCargoAdvancedOutput.blockMaterial, EnderCargoAdvancedOutput.blockName, "&7Use the ender force to teleport", "&7items between worlds"); + SlimefunItemStack ender_linker = new SlimefunItemStack("ENDER_LINKER", EnderCargoLinker.itemMaterial, EnderCargoLinker.itemName, "&7&eShift + Right Click&7 to link", "&7&eLeft Click&7 to reset"); - /* - * 3. Creating a Recipe - * The Recipe is an ItemStack Array with a length of 9. - * It represents a Shaped Recipe in a 3x3 crafting grid. - * The machine in which this recipe is crafted in is specified - * further down as the RecipeType. - */ + //Creating Recipes ItemStack[] ender_input_recipe = {null, SlimefunItems.INFUSED_HOPPER, null, SlimefunItems.ENDER_LUMP_3, SlimefunItems.CARGO_CONNECTOR_NODE, SlimefunItems.ENDER_LUMP_3, null, SlimefunItems.INFUSED_HOPPER, null}; ItemStack[] ender_output_recipe = {null, SlimefunItems.INFUSED_HOPPER, null, SlimefunItems.MAGIC_LUMP_3, SlimefunItems.CARGO_CONNECTOR_NODE, SlimefunItems.MAGIC_LUMP_3, null, SlimefunItems.INFUSED_HOPPER, null}; ItemStack[] advanced_ender_output_recipe = {null, SlimefunItems.CARGO_MOTOR, null, SlimefunItems.REINFORCED_PLATE, ender_output, SlimefunItems.REINFORCED_PLATE, null, SlimefunItems.CARGO_MOTOR, null}; ItemStack[] ender_linker_recipe = {SlimefunItems.HARDENED_METAL_INGOT, SlimefunItems.ESSENCE_OF_AFTERLIFE, SlimefunItems.HARDENED_METAL_INGOT, SlimefunItems.HARDENED_METAL_INGOT, new ItemStack(Material.STICK), SlimefunItems.HARDENED_METAL_INGOT, null, new ItemStack(Material.STICK), null}; - /* - * 4. Registering the Item - * Now you just have to register the item. - * RecipeType.ENHANCED_CRAFTING_TABLE refers to the machine in - * which this item is crafted in. - * Recipe Types from Slimefun itself will automatically add the recipe to that machine. - */ + //Registering Items SlimefunItem ender_input_item = new SlimefunItem(itemGroup, ender_input, RecipeType.ENHANCED_CRAFTING_TABLE, ender_input_recipe); ender_input_item.register(this); SlimefunItem ender_output_item = new SlimefunItem(itemGroup, ender_output, RecipeType.ENHANCED_CRAFTING_TABLE, ender_output_recipe); @@ -74,6 +62,7 @@ public void onEnable() { SlimefunItem ender_linker_item = new SlimefunItem(itemGroup, ender_linker, RecipeType.ENHANCED_CRAFTING_TABLE, ender_linker_recipe); ender_linker_item.register(this); + //Creating data file and folder dataFolderPath = getDataFolder().getAbsolutePath(); File folder = new File(dataFolderPath); if (!folder.exists()) @@ -86,8 +75,10 @@ public void onEnable() { ex.printStackTrace(); } } + + //Run ticker Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> { - EnderCargoOutput.setContent(); + setContent(); }, 0L, 1L); } @@ -111,4 +102,48 @@ public JavaPlugin getJavaPlugin() { return this; } + public void setContent() { + if (EnderCargoData.getLinkedCargo() != null) { + for (String string : EnderCargoData.getLinkedCargo()) { + + String[] str = string.split(" "); + Location location = new Location(Bukkit.getWorld(str[0]), Integer.parseInt(str[1]), Integer.parseInt(str[2]), Integer.parseInt(str[3])); + + Container outputContainer = (Container) location.getWorld().getBlockAt(location).getState(); + ItemStack[] outputContent = outputContainer.getInventory().getStorageContents(); + + outputContainer.update(); + + BukkitRunnable task = new BukkitRunnable() { + @Override + public void run() { + if (location.getBlock().getType() == Material.DISPENSER && EnderCargoData.getInputCargo(location) != null && EnderCargoData.getInputCargo(location).getBlock().getType() == EnderCargoInput.blockMaterial) { + + Container outputContainerTick = (Container) location.getWorld().getBlockAt(location).getState(); + ItemStack[] outputContentTick = outputContainerTick.getInventory().getStorageContents(); + + Container inputContainer = (Container) EnderCargoData.getInputCargo(location).getWorld().getBlockAt(EnderCargoData.getInputCargo(location)).getState(); + ItemStack[] inputContent = inputContainer.getInventory().getStorageContents(); + + for (int i = 0; i < 9; i++) { + int nb; + if (outputContent[i] != null) { + if (outputContentTick[i] == null) { + nb = outputContent[i].getAmount(); + } else { + nb = outputContent[i].getAmount() - outputContentTick[i].getAmount(); + } + ItemStack item = inputContent[i]; + item.setAmount(inputContent[i].getAmount() - nb); + inputContainer.getInventory().setItem(i, item); + } + } + outputContainerTick.getInventory().setContents(inputContainer.getInventory().getContents()); + } + } + }; + task.runTaskLater(this, 1); + } + } + } } diff --git a/src/main/java/fr/birdo/endercargo/EnderCargoAdvancedOutput.java b/src/main/java/fr/birdo/endercargo/EnderCargoAdvancedOutput.java deleted file mode 100644 index db16f6b..0000000 --- a/src/main/java/fr/birdo/endercargo/EnderCargoAdvancedOutput.java +++ /dev/null @@ -1,6 +0,0 @@ -package fr.birdo.endercargo; - -public class EnderCargoAdvancedOutput { - - public static final String blockName = "§3Advanced Ender Cargo Node §b(Output)"; -} diff --git a/src/main/java/fr/birdo/endercargo/EnderCargoInput.java b/src/main/java/fr/birdo/endercargo/EnderCargoInput.java deleted file mode 100644 index 74eab5e..0000000 --- a/src/main/java/fr/birdo/endercargo/EnderCargoInput.java +++ /dev/null @@ -1,25 +0,0 @@ -package fr.birdo.endercargo; - -import org.bukkit.Material; -import org.bukkit.block.Dispenser; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockDispenseEvent; - -public class EnderCargoInput implements Listener { - - public static final String blockName = "§3Ender Cargo Node §b(Input)"; - - public EnderCargoInput(EnderCargo enderCargo) { - } - - @EventHandler - public static void onDispense(BlockDispenseEvent event) { - if (event.getBlock().getType() == Material.DISPENSER) { - Dispenser d = (Dispenser) event.getBlock().getState(); - if (d.getCustomName().equalsIgnoreCase(blockName)) { - event.setCancelled(true); - } - } - } -} \ No newline at end of file diff --git a/src/main/java/fr/birdo/endercargo/EnderCargoOutput.java b/src/main/java/fr/birdo/endercargo/EnderCargoOutput.java deleted file mode 100644 index c334a31..0000000 --- a/src/main/java/fr/birdo/endercargo/EnderCargoOutput.java +++ /dev/null @@ -1,166 +0,0 @@ -package fr.birdo.endercargo; - -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.block.Container; -import org.bukkit.block.Dispenser; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.BlockDispenseEvent; -import org.bukkit.event.inventory.*; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; -import org.bukkit.scheduler.BukkitRunnable; - -public class EnderCargoOutput implements Listener { - - public static final String blockName = "§3Ender Cargo Node §b(Output)"; - - private static EnderCargo instance; - - public EnderCargoOutput(EnderCargo pluginInstance) { - instance = pluginInstance; - } - - @EventHandler - public void onDispense(BlockDispenseEvent event) { - if (event.getBlock().getType() == Material.DISPENSER) { - Dispenser d = (Dispenser) event.getBlock().getState(); - if (d.getCustomName().equalsIgnoreCase(blockName) || d.getCustomName().equalsIgnoreCase(EnderCargoAdvancedOutput.blockName)) { - event.setCancelled(true); - } - } - } - - @EventHandler - public void onBreak(BlockBreakEvent event) { - if (event.getBlock().getType() == Material.DISPENSER) { - Dispenser d = (Dispenser) event.getBlock().getState(); - if (d.getCustomName().equalsIgnoreCase(blockName) || d.getCustomName().equalsIgnoreCase(EnderCargoAdvancedOutput.blockName)) { - Container container = (Container) event.getBlock().getState(); - container.getInventory().clear(); - EnderCargoData.unlinkCargo(event.getBlock().getLocation()); - } else if (d.getCustomName().equalsIgnoreCase(EnderCargoInput.blockName)) { - for (String string : EnderCargoData.getLinkedCargo()) { - String[] str = string.split(" "); - Location location = new Location(Bukkit.getWorld(str[0]), Integer.parseInt(str[1]), Integer.parseInt(str[2]), Integer.parseInt(str[3])); - Location loc = event.getBlock().getLocation(); - Location loc2 = EnderCargoData.getInputCargo(location); - if (loc.getWorld() == loc2.getWorld() && loc.getX() == loc2.getX() && loc.getY() == loc2.getY() && loc.getZ() == loc2.getZ()) { - EnderCargoData.unlinkCargo(location); - } - } - } - } - } - - @EventHandler - public void move(InventoryMoveItemEvent event) { - Container sourceContainer = (Container) event.getSource().getHolder(); - Container destinationContainer = (Container) event.getDestination().getHolder(); - - if (event.getSource().getType() == InventoryType.HOPPER) { - if (destinationContainer.getCustomName().equalsIgnoreCase(EnderCargoAdvancedOutput.blockName) || destinationContainer.getCustomName().equalsIgnoreCase(blockName) || destinationContainer.getCustomName().equalsIgnoreCase(EnderCargoInput.blockName)) { - event.setCancelled(true); - } - } else if (event.getDestination().getType() == InventoryType.HOPPER) { - if (sourceContainer.getCustomName().equalsIgnoreCase(EnderCargoAdvancedOutput.blockName) || sourceContainer.getCustomName().equalsIgnoreCase(blockName) || sourceContainer.getCustomName().equalsIgnoreCase(EnderCargoInput.blockName)) { - event.setCancelled(true); - } - } - } - - @EventHandler - public void onInteract(PlayerInteractEvent event) { - if (event.getClickedBlock() != null) { - if (event.getClickedBlock().getType() == Material.DISPENSER) { - if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { - if (!event.getPlayer().isSneaking() || event.getItem() == null) { - Container container = (Container) event.getClickedBlock().getState(); - if (container.getCustomName().equalsIgnoreCase(EnderCargoAdvancedOutput.blockName)) { - event.setCancelled(true); - Inventory inventory = Bukkit.createInventory(null, InventoryType.DISPENSER, "§3Ender Cargo Node view (Output)"); - inventory.setContents(container.getInventory().getContents()); - event.getPlayer().openInventory(inventory); - } else if (container.getCustomName().equalsIgnoreCase(EnderCargoInput.blockName)) { - event.setCancelled(true); - Inventory inventory = Bukkit.createInventory(null, InventoryType.DISPENSER, "§3Ender Cargo Node view (Input)"); - inventory.setContents(container.getInventory().getContents()); - event.getPlayer().openInventory(inventory); - } else if (container.getCustomName().equalsIgnoreCase(blockName)) { - event.setCancelled(true); - } - } else if (event.getPlayer().isSneaking() && event.getItem() != null) { - if (event.getItem().hasItemMeta() && event.getItem().getItemMeta().hasDisplayName()) { - String displayName = event.getItem().getItemMeta().getDisplayName(); - Container container = (Container) event.getClickedBlock().getState(); - if (container.getCustomName().equalsIgnoreCase(EnderCargoAdvancedOutput.blockName) || container.getCustomName().equalsIgnoreCase(blockName)) { - if (displayName.equalsIgnoreCase("§7Cargo Node §c(Output)") || displayName.equalsIgnoreCase("§6Advanced Cargo Node §c(Output)")) - event.setCancelled(true); - } else if (container.getCustomName().equalsIgnoreCase(EnderCargoInput.blockName)) { - if (displayName.equalsIgnoreCase("§7Cargo Node §c(Input)")) - event.setCancelled(true); - } - } - } - } - } - } - } - - @EventHandler - public void onInv(InventoryClickEvent event) { - if (event.getView().getTitle().equalsIgnoreCase("§3Ender Cargo Node view (Input)") || event.getView().getTitle().equalsIgnoreCase("§3Ender Cargo Node view (Output)")) { - event.setCancelled(true); - } - } - - public static void setContent() { - if (EnderCargoData.getLinkedCargo() != null) { - for (String string : EnderCargoData.getLinkedCargo()) { - - String[] str = string.split(" "); - Location location = new Location(Bukkit.getWorld(str[0]), Integer.parseInt(str[1]), Integer.parseInt(str[2]), Integer.parseInt(str[3])); - - Container outputContainer = (Container) location.getWorld().getBlockAt(location).getState(); - ItemStack[] outputContent = outputContainer.getInventory().getStorageContents(); - - outputContainer.update(); - - BukkitRunnable task = new BukkitRunnable() { - @Override - public void run() { - if (location.getBlock().getType() == Material.DISPENSER && EnderCargoData.getInputCargo(location) != null && EnderCargoData.getInputCargo(location).getBlock().getType() == Material.DISPENSER) { - - Container outputContainerTick = (Container) location.getWorld().getBlockAt(location).getState(); - ItemStack[] outputContentTick = outputContainerTick.getInventory().getStorageContents(); - - Container inputContainer = (Container) EnderCargoData.getInputCargo(location).getWorld().getBlockAt(EnderCargoData.getInputCargo(location)).getState(); - ItemStack[] inputContent = inputContainer.getInventory().getStorageContents(); - - for (int i = 0; i < 9; i++) { - int nb; - if (outputContent[i] != null) { - if (outputContentTick[i] == null) { - nb = outputContent[i].getAmount(); - } else { - nb = outputContent[i].getAmount() - outputContentTick[i].getAmount(); - } - ItemStack item = inputContent[i]; - item.setAmount(inputContent[i].getAmount() - nb); - inputContainer.getInventory().setItem(i, item); - } - } - outputContainerTick.getInventory().setContents(inputContainer.getInventory().getContents()); - } - } - }; - task.runTaskLater(instance, 1); - } - } - } -} diff --git a/src/main/java/fr/birdo/endercargo/EnderCargoData.java b/src/main/java/fr/birdo/endercargo/Utils/EnderCargoData.java similarity index 97% rename from src/main/java/fr/birdo/endercargo/EnderCargoData.java rename to src/main/java/fr/birdo/endercargo/Utils/EnderCargoData.java index 7153f94..abca5a8 100644 --- a/src/main/java/fr/birdo/endercargo/EnderCargoData.java +++ b/src/main/java/fr/birdo/endercargo/Utils/EnderCargoData.java @@ -1,5 +1,6 @@ -package fr.birdo.endercargo; +package fr.birdo.endercargo.Utils; +import fr.birdo.endercargo.EnderCargo; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.configuration.file.FileConfiguration; diff --git a/src/main/java/fr/birdo/endercargo/items/EnderCargoAdvancedOutput.java b/src/main/java/fr/birdo/endercargo/items/EnderCargoAdvancedOutput.java new file mode 100644 index 0000000..d1977ac --- /dev/null +++ b/src/main/java/fr/birdo/endercargo/items/EnderCargoAdvancedOutput.java @@ -0,0 +1,94 @@ +package fr.birdo.endercargo.items; + +import fr.birdo.endercargo.EnderCargo; +import fr.birdo.endercargo.Utils.EnderCargoData; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.block.Container; +import org.bukkit.block.Dispenser; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockDispenseEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryMoveItemEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.Inventory; + +public class EnderCargoAdvancedOutput implements Listener { + + public static final String blockName = "§3Advanced Ender Cargo Node §b(Output)"; + public static final Material blockMaterial = Material.DISPENSER; + + public EnderCargoAdvancedOutput(EnderCargo enderCargo) { + } + + @EventHandler + public void onDispense(BlockDispenseEvent event) { + if (event.getBlock().getType() == blockMaterial) { + Dispenser d = (Dispenser) event.getBlock().getState(); + if (d.getCustomName() != null && d.getCustomName().equalsIgnoreCase(blockName)) + event.setCancelled(true); + } + } + + @EventHandler + public void onBreak(BlockBreakEvent event) { + if (event.getBlock().getType() == blockMaterial) { + Dispenser d = (Dispenser) event.getBlock().getState(); + if (d.getCustomName().equalsIgnoreCase(blockName)) { + Container container = (Container) event.getBlock().getState(); + container.getInventory().clear(); + EnderCargoData.unlinkCargo(event.getBlock().getLocation()); + } + } + } + + @EventHandler + public void onMove(InventoryMoveItemEvent event) { + if (event.getSource().getType() == InventoryType.HOPPER && event.getDestination().getType() == InventoryType.DISPENSER) { + Container destinationContainer = (Container) event.getDestination().getHolder(); + if (destinationContainer.getCustomName().equalsIgnoreCase(blockName)) + event.setCancelled(true); + } else if (event.getDestination().getType() == InventoryType.HOPPER && event.getSource().getType() == InventoryType.DISPENSER) { + Container sourceContainer = (Container) event.getSource().getHolder(); + if (sourceContainer.getCustomName().equalsIgnoreCase(blockName)) + event.setCancelled(true); + } + } + + @EventHandler + public void onClickInv(InventoryClickEvent event) { + if (event.getView().getTitle().equalsIgnoreCase("§3Ender Cargo Node view (Output)")) + event.setCancelled(true); + } + + @EventHandler + public void onInteract(PlayerInteractEvent event) { + if (event.getClickedBlock() != null) { + if (event.getClickedBlock().getType() == blockMaterial) { + if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { + if (!event.getPlayer().isSneaking() || event.getItem() == null) { + Container container = (Container) event.getClickedBlock().getState(); + if (container.getCustomName().equalsIgnoreCase(blockName)) { + event.setCancelled(true); + Inventory inventory = Bukkit.createInventory(null, InventoryType.DISPENSER, "§3Ender Cargo Node view (Output)"); + inventory.setContents(container.getInventory().getContents()); + event.getPlayer().openInventory(inventory); + } + } else if (event.getPlayer().isSneaking() && event.getItem() != null) { + if (event.getItem().hasItemMeta() && event.getItem().getItemMeta().hasDisplayName()) { + String displayName = event.getItem().getItemMeta().getDisplayName(); + Container container = (Container) event.getClickedBlock().getState(); + if (container.getCustomName().equalsIgnoreCase(blockName)) + if (displayName.equalsIgnoreCase("§7Cargo Node §c(Output)") || displayName.equalsIgnoreCase("§6Advanced Cargo Node §c(Output)")) + event.setCancelled(true); + } + } + } + } + } + } +} diff --git a/src/main/java/fr/birdo/endercargo/items/EnderCargoInput.java b/src/main/java/fr/birdo/endercargo/items/EnderCargoInput.java new file mode 100644 index 0000000..ddce3b3 --- /dev/null +++ b/src/main/java/fr/birdo/endercargo/items/EnderCargoInput.java @@ -0,0 +1,101 @@ +package fr.birdo.endercargo.items; + +import fr.birdo.endercargo.EnderCargo; +import fr.birdo.endercargo.Utils.EnderCargoData; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Container; +import org.bukkit.block.Dispenser; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockDispenseEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryMoveItemEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.Inventory; + +public class EnderCargoInput implements Listener { + + public static final String blockName = "§3Ender Cargo Node §b(Input)"; + public static final Material blockMaterial = Material.DISPENSER; + + public EnderCargoInput(EnderCargo enderCargo) { + } + + @EventHandler + public void onDispense(BlockDispenseEvent event) { + if (event.getBlock().getType() == blockMaterial) { + Dispenser d = (Dispenser) event.getBlock().getState(); + if (d.getCustomName() != null && d.getCustomName().equalsIgnoreCase(blockName)) + event.setCancelled(true); + } + } + + @EventHandler + public void onBreak(BlockBreakEvent event) { + if (event.getBlock().getType() == blockMaterial) { + Dispenser d = (Dispenser) event.getBlock().getState(); + if (d.getCustomName().equalsIgnoreCase(blockName)) { + for (String string : EnderCargoData.getLinkedCargo()) { + String[] str = string.split(" "); + Location location = new Location(Bukkit.getWorld(str[0]), Integer.parseInt(str[1]), Integer.parseInt(str[2]), Integer.parseInt(str[3])); + Location loc = event.getBlock().getLocation(); + Location loc2 = EnderCargoData.getInputCargo(location); + if (loc.getWorld() == loc2.getWorld() && loc.getX() == loc2.getX() && loc.getY() == loc2.getY() && loc.getZ() == loc2.getZ()) { + EnderCargoData.unlinkCargo(location); + } + } + } + } + } + + @EventHandler + public void onMove(InventoryMoveItemEvent event) { + if (event.getSource().getType() == InventoryType.HOPPER && event.getDestination().getType() == InventoryType.DISPENSER) { + Container destinationContainer = (Container) event.getDestination().getHolder(); + if (destinationContainer.getCustomName().equalsIgnoreCase(blockName)) + event.setCancelled(true); + } else if (event.getDestination().getType() == InventoryType.HOPPER && event.getSource().getType() == InventoryType.DISPENSER) { + Container sourceContainer = (Container) event.getSource().getHolder(); + if (sourceContainer.getCustomName().equalsIgnoreCase(blockName)) + event.setCancelled(true); + } + } + + @EventHandler + public void onClickInv(InventoryClickEvent event) { + if (event.getView().getTitle().equalsIgnoreCase("§3Ender Cargo Node view (Input)")) + event.setCancelled(true); + } + + @EventHandler + public void onInteract(PlayerInteractEvent event) { + if (event.getClickedBlock() != null) { + if (event.getClickedBlock().getType() == Material.DISPENSER) { + if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { + if (!event.getPlayer().isSneaking() || event.getItem() == null) { + Container container = (Container) event.getClickedBlock().getState(); + if (container.getCustomName().equalsIgnoreCase(blockName)) { + event.setCancelled(true); + Inventory inventory = Bukkit.createInventory(null, InventoryType.DISPENSER, "§3Ender Cargo Node view (Input)"); + inventory.setContents(container.getInventory().getContents()); + event.getPlayer().openInventory(inventory); + } + } else if (event.getPlayer().isSneaking() && event.getItem() != null) { + if (event.getItem().hasItemMeta() && event.getItem().getItemMeta().hasDisplayName()) { + String displayName = event.getItem().getItemMeta().getDisplayName(); + Container container = (Container) event.getClickedBlock().getState(); + if (container.getCustomName().equalsIgnoreCase(blockName)) + if (displayName.equalsIgnoreCase("§7Cargo Node §c(Input)")) + event.setCancelled(true); + } + } + } + } + } + } +} \ No newline at end of file diff --git a/src/main/java/fr/birdo/endercargo/EnderCargoLinker.java b/src/main/java/fr/birdo/endercargo/items/EnderCargoLinker.java similarity index 92% rename from src/main/java/fr/birdo/endercargo/EnderCargoLinker.java rename to src/main/java/fr/birdo/endercargo/items/EnderCargoLinker.java index 7a8a379..ef9fb66 100644 --- a/src/main/java/fr/birdo/endercargo/EnderCargoLinker.java +++ b/src/main/java/fr/birdo/endercargo/items/EnderCargoLinker.java @@ -1,5 +1,7 @@ -package fr.birdo.endercargo; +package fr.birdo.endercargo.items; +import fr.birdo.endercargo.EnderCargo; +import fr.birdo.endercargo.Utils.EnderCargoData; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -16,17 +18,18 @@ public class EnderCargoLinker implements Listener { + public static final String itemName = "§3Ender Cargo Linker"; + public static final Material itemMaterial = Material.ENDER_EYE; private static Map input = new HashMap<>(); public EnderCargoLinker(EnderCargo enderCargo) { } @EventHandler - public static void onClick(PlayerInteractEvent event) { + public void onClick(PlayerInteractEvent event) { if (event.getItem() != null) { - if (event.getItem().getType() == Material.ENDER_EYE) { - System.out.println(event.getItem().getItemMeta().getDisplayName()); - if (event.getItem().hasItemMeta() && event.getItem().getItemMeta().hasDisplayName() && event.getItem().getItemMeta().getDisplayName().equalsIgnoreCase("§3Ender Cargo Linker")) { + if (event.getItem().getType() == itemMaterial) { + if (event.getItem().hasItemMeta() && event.getItem().getItemMeta().hasDisplayName() && event.getItem().getItemMeta().getDisplayName().equalsIgnoreCase(itemName)) { event.setCancelled(true); if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getPlayer().isSneaking()) { if (event.getClickedBlock().getType() == Material.DISPENSER) { diff --git a/src/main/java/fr/birdo/endercargo/items/EnderCargoOutput.java b/src/main/java/fr/birdo/endercargo/items/EnderCargoOutput.java new file mode 100644 index 0000000..bbcf4ba --- /dev/null +++ b/src/main/java/fr/birdo/endercargo/items/EnderCargoOutput.java @@ -0,0 +1,85 @@ +package fr.birdo.endercargo.items; + +import fr.birdo.endercargo.EnderCargo; +import fr.birdo.endercargo.Utils.EnderCargoData; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Container; +import org.bukkit.block.Dispenser; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockDispenseEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryMoveItemEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.Inventory; + +public class EnderCargoOutput implements Listener { + + public static final String blockName = "§3Ender Cargo Node §b(Output)"; + public static final Material blockMaterial = Material.DISPENSER; + + public EnderCargoOutput(EnderCargo enderCargo) { + } + + @EventHandler + public void onDispense(BlockDispenseEvent event) { + if (event.getBlock().getType() == blockMaterial) { + Dispenser d = (Dispenser) event.getBlock().getState(); + if (d.getCustomName() != null && d.getCustomName().equalsIgnoreCase(blockName)) + event.setCancelled(true); + } + } + + @EventHandler + public void onBreak(BlockBreakEvent event) { + if (event.getBlock().getType() == blockMaterial) { + Dispenser d = (Dispenser) event.getBlock().getState(); + if (d.getCustomName().equalsIgnoreCase(blockName)) { + Container container = (Container) event.getBlock().getState(); + container.getInventory().clear(); + EnderCargoData.unlinkCargo(event.getBlock().getLocation()); + } + } + } + + @EventHandler + public void onMove(InventoryMoveItemEvent event) { + if (event.getSource().getType() == InventoryType.HOPPER && event.getDestination().getType() == InventoryType.DISPENSER) { + Container destinationContainer = (Container) event.getDestination().getHolder(); + if (destinationContainer.getCustomName().equalsIgnoreCase(blockName)) + event.setCancelled(true); + } else if (event.getDestination().getType() == InventoryType.HOPPER && event.getSource().getType() == InventoryType.DISPENSER) { + Container sourceContainer = (Container) event.getSource().getHolder(); + if (sourceContainer.getCustomName().equalsIgnoreCase(blockName)) + event.setCancelled(true); + } + } + + @EventHandler + public void onInteract(PlayerInteractEvent event) { + if (event.getClickedBlock() != null) { + if (event.getClickedBlock().getType() == Material.DISPENSER) { + if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { + if (!event.getPlayer().isSneaking() || event.getItem() == null) { + Container container = (Container) event.getClickedBlock().getState(); + if (container.getCustomName().equalsIgnoreCase(blockName)) + event.setCancelled(true); + } else if (event.getPlayer().isSneaking() && event.getItem() != null) { + if (event.getItem().hasItemMeta() && event.getItem().getItemMeta().hasDisplayName()) { + String displayName = event.getItem().getItemMeta().getDisplayName(); + Container container = (Container) event.getClickedBlock().getState(); + if (container.getCustomName().equalsIgnoreCase(blockName)) + if (displayName.equalsIgnoreCase("§7Cargo Node §c(Output)") || displayName.equalsIgnoreCase("§6Advanced Cargo Node §c(Output)")) + event.setCancelled(true); + } + } + } + } + } + } +}