From 97c3bfe4a1799dcb8202f35aaf455c01b4f92e17 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Tue, 17 Nov 2020 16:29:27 +0100 Subject: [PATCH 01/21] Moved sorter name complete logic in the ZSorterCommand class --- src/main/java/fr/zcraft/zsorter/ZSorter.java | 4 +++- .../java/fr/zcraft/zsorter/commands/DeleteCommand.java | 5 +---- .../java/fr/zcraft/zsorter/commands/InfoCommand.java | 5 +---- .../fr/zcraft/zsorter/commands/RemoveInputCommand.java | 5 +---- .../zcraft/zsorter/commands/RemoveOutputCommand.java | 5 +---- .../fr/zcraft/zsorter/commands/SetInputCommand.java | 5 +---- .../fr/zcraft/zsorter/commands/SetOutputCommand.java | 5 +---- .../java/fr/zcraft/zsorter/commands/SpeedCommand.java | 5 +---- .../java/fr/zcraft/zsorter/commands/ToggleCommand.java | 5 +---- .../java/fr/zcraft/zsorter/commands/UpdateCommand.java | 5 +---- .../fr/zcraft/zsorter/commands/ZSorterCommands.java | 10 ++++++++++ 11 files changed, 22 insertions(+), 37 deletions(-) diff --git a/src/main/java/fr/zcraft/zsorter/ZSorter.java b/src/main/java/fr/zcraft/zsorter/ZSorter.java index e665222..72b49f3 100644 --- a/src/main/java/fr/zcraft/zsorter/ZSorter.java +++ b/src/main/java/fr/zcraft/zsorter/ZSorter.java @@ -24,6 +24,7 @@ import fr.zcraft.zsorter.commands.DeleteCommand; import fr.zcraft.zsorter.commands.InfoCommand; import fr.zcraft.zsorter.commands.ListCommand; +import fr.zcraft.zsorter.commands.MagicCommand; import fr.zcraft.zsorter.commands.RemoveInputCommand; import fr.zcraft.zsorter.commands.RemoveOutputCommand; import fr.zcraft.zsorter.commands.SetInputCommand; @@ -135,7 +136,8 @@ public void onEnable() { SetInputCommand.class, SetOutputCommand.class, RemoveInputCommand.class, - RemoveOutputCommand.class + RemoveOutputCommand.class, + MagicCommand.class ); if(load()) { diff --git a/src/main/java/fr/zcraft/zsorter/commands/DeleteCommand.java b/src/main/java/fr/zcraft/zsorter/commands/DeleteCommand.java index 8e0e49c..e5092cf 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/DeleteCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/DeleteCommand.java @@ -38,10 +38,7 @@ protected void run() throws CommandException { @Override protected List complete() throws CommandException{ if(args.length <= 1) { - return ZSorter.getInstance().getSorterManager().getNameToSorter().keySet() - .stream() - .filter(s -> s.startsWith(args[0])) - .collect(Collectors.toList()); + return completeSorterName(args[0]); } return null; } diff --git a/src/main/java/fr/zcraft/zsorter/commands/InfoCommand.java b/src/main/java/fr/zcraft/zsorter/commands/InfoCommand.java index d4583b5..8dfe6a0 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/InfoCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/InfoCommand.java @@ -54,10 +54,7 @@ protected void run() throws CommandException { @Override protected List complete() throws CommandException{ if(args.length <= 1) { - return ZSorter.getInstance().getSorterManager().getNameToSorter().keySet() - .stream() - .filter(s -> s.startsWith(args[0])) - .collect(Collectors.toList()); + return completeSorterName(args[0]); } else if(args.length <= 2) { return Arrays.asList(DisplayMode.ITEMS, DisplayMode.OUTPUTS) diff --git a/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java index f2a6630..4520a9b 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java @@ -54,10 +54,7 @@ protected void run() throws CommandException { @Override protected List complete() throws CommandException{ if(args.length <= 1) { - return ZSorter.getInstance().getSorterManager().getNameToSorter().keySet() - .stream() - .filter(s -> s.startsWith(args[0])) - .collect(Collectors.toList()); + return completeSorterName(args[0]); } return null; } diff --git a/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java index d3407f7..d050f6a 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java @@ -54,10 +54,7 @@ protected void run() throws CommandException { @Override protected List complete() throws CommandException{ if(args.length <= 1) { - return ZSorter.getInstance().getSorterManager().getNameToSorter().keySet() - .stream() - .filter(s -> s.startsWith(args[0])) - .collect(Collectors.toList()); + return completeSorterName(args[0]); } return null; } diff --git a/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java index 11126f5..9d09d93 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java @@ -65,10 +65,7 @@ protected void run() throws CommandException { @Override protected List complete() throws CommandException{ if(args.length <= 1) { - return ZSorter.getInstance().getSorterManager().getNameToSorter().keySet() - .stream() - .filter(s -> s.startsWith(args[0])) - .collect(Collectors.toList()); + return completeSorterName(args[0]); } return null; } diff --git a/src/main/java/fr/zcraft/zsorter/commands/SetOutputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/SetOutputCommand.java index b42f9f2..c1fe482 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/SetOutputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/SetOutputCommand.java @@ -79,10 +79,7 @@ protected void run() throws CommandException { @Override protected List complete() throws CommandException{ if(args.length <= 1) { - return ZSorter.getInstance().getSorterManager().getNameToSorter().keySet() - .stream() - .filter(s -> s.startsWith(args[0])) - .collect(Collectors.toList()); + return completeSorterName(args[0]); } else if(args.length >= 3) { return Arrays.asList(Material.values()) diff --git a/src/main/java/fr/zcraft/zsorter/commands/SpeedCommand.java b/src/main/java/fr/zcraft/zsorter/commands/SpeedCommand.java index ed6b382..6961968 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/SpeedCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/SpeedCommand.java @@ -51,10 +51,7 @@ protected void run() throws CommandException { @Override protected List complete() throws CommandException{ if(args.length <= 1) { - return ZSorter.getInstance().getSorterManager().getNameToSorter().keySet() - .stream() - .filter(s -> s.startsWith(args[0])) - .collect(Collectors.toList()); + return completeSorterName(args[0]); } return null; } diff --git a/src/main/java/fr/zcraft/zsorter/commands/ToggleCommand.java b/src/main/java/fr/zcraft/zsorter/commands/ToggleCommand.java index 109d65a..c09bb1d 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/ToggleCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/ToggleCommand.java @@ -54,10 +54,7 @@ protected void run() throws CommandException { @Override protected List complete() throws CommandException{ if(args.length <= 1) { - return ZSorter.getInstance().getSorterManager().getNameToSorter().keySet() - .stream() - .filter(s -> s.startsWith(args[0])) - .collect(Collectors.toList()); + return completeSorterName(args[0]); } return null; } diff --git a/src/main/java/fr/zcraft/zsorter/commands/UpdateCommand.java b/src/main/java/fr/zcraft/zsorter/commands/UpdateCommand.java index e72ba1a..f34e0b3 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/UpdateCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/UpdateCommand.java @@ -43,10 +43,7 @@ protected void run() throws CommandException { @Override protected List complete() throws CommandException{ if(args.length <= 1) { - return ZSorter.getInstance().getSorterManager().getNameToSorter().keySet() - .stream() - .filter(s -> s.startsWith(args[0])) - .collect(Collectors.toList()); + return completeSorterName(args[0]); } return null; } diff --git a/src/main/java/fr/zcraft/zsorter/commands/ZSorterCommands.java b/src/main/java/fr/zcraft/zsorter/commands/ZSorterCommands.java index 73d5869..9bbff5d 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/ZSorterCommands.java +++ b/src/main/java/fr/zcraft/zsorter/commands/ZSorterCommands.java @@ -1,5 +1,8 @@ package fr.zcraft.zsorter.commands; +import java.util.List; +import java.util.stream.Collectors; + import org.bukkit.command.CommandSender; import fr.zcraft.quartzlib.components.commands.Command; @@ -29,6 +32,13 @@ public void checkEnable() throws CommandException { + "- Remove the file (and loose your data)"); } + public List completeSorterName(String arg){ + return ZSorter.getInstance().getSorterManager().getNameToSorter().keySet() + .stream() + .filter(s -> s.startsWith(arg)) + .collect(Collectors.toList()); + } + @Override public boolean canExecute(CommandSender sender) { From 3a1e974ad9721e6b260497960d0cff0ffef62059 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Thu, 19 Nov 2020 18:58:23 +0100 Subject: [PATCH 02/21] Removed debug log --- src/main/java/fr/zcraft/zsorter/model/Sorter.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/fr/zcraft/zsorter/model/Sorter.java b/src/main/java/fr/zcraft/zsorter/model/Sorter.java index 3b60e4b..af7eb26 100644 --- a/src/main/java/fr/zcraft/zsorter/model/Sorter.java +++ b/src/main/java/fr/zcraft/zsorter/model/Sorter.java @@ -593,7 +593,6 @@ public boolean equals(Object obj) { @Override public void postProcess() { - System.out.println("========SORTER========"); inventoryToInput = new HashMap(); for(Input input:inputs) { inventoryToInput.putIfAbsent(input.getInventory(), input); From 5d2fefc26f7bddf000b5adbefd30200f5bffe97f Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Thu, 19 Nov 2020 19:06:13 +0100 Subject: [PATCH 03/21] Close #6 Add magic effect to easily define single outputs by left clicking on the block --- src/main/java/fr/zcraft/zsorter/Config.java | 14 +- src/main/java/fr/zcraft/zsorter/ZSorter.java | 7 +- .../zcraft/zsorter/commands/MagicCommand.java | 122 ++++++++++++++++++ .../zcraft/zsorter/events/LeftClickEvent.java | 50 +++++++ .../zcraft/zsorter/model/SorterManager.java | 15 ++- src/main/resources/i18n/fr_FR.po | 8 +- 6 files changed, 198 insertions(+), 18 deletions(-) create mode 100644 src/main/java/fr/zcraft/zsorter/commands/MagicCommand.java create mode 100644 src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java diff --git a/src/main/java/fr/zcraft/zsorter/Config.java b/src/main/java/fr/zcraft/zsorter/Config.java index 96212e3..a713815 100644 --- a/src/main/java/fr/zcraft/zsorter/Config.java +++ b/src/main/java/fr/zcraft/zsorter/Config.java @@ -16,19 +16,9 @@ public class Config extends Configuration * The default language of the plugin. */ static public final ConfigurationItem LANGUAGE = item("language", Locale.class); - - /** - * The maximum number of sorter per map - */ - static public final ConfigurationItem MAX_BANKS = item("max_sorters", 100); - - /** - * The maximum inputs for one sorter. - */ - static public final ConfigurationItem MAX_INPUTS = item("max_inputs", 100); /** - * The maximum outputs for one sorter. + * The magic effect cooldown. */ - static public final ConfigurationItem MAX_OUTPUTS = item("max_ouputs", 10000); + static public final ConfigurationItem MAGIC_EFFECT_DURATION = item("magic_effect_duration", 1200); } \ No newline at end of file diff --git a/src/main/java/fr/zcraft/zsorter/ZSorter.java b/src/main/java/fr/zcraft/zsorter/ZSorter.java index 72b49f3..169f588 100644 --- a/src/main/java/fr/zcraft/zsorter/ZSorter.java +++ b/src/main/java/fr/zcraft/zsorter/ZSorter.java @@ -15,10 +15,9 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; -import fr.zcraft.quartzlib.core.QuartzLib; -import fr.zcraft.quartzlib.core.QuartzPlugin; import fr.zcraft.quartzlib.components.commands.Commands; import fr.zcraft.quartzlib.components.i18n.I18n; +import fr.zcraft.quartzlib.core.QuartzPlugin; import fr.zcraft.quartzlib.tools.PluginLogger; import fr.zcraft.zsorter.commands.CreateCommand; import fr.zcraft.zsorter.commands.DeleteCommand; @@ -35,6 +34,7 @@ import fr.zcraft.zsorter.events.HolderBreakEvent; import fr.zcraft.zsorter.events.InventoryEvent; import fr.zcraft.zsorter.events.ItemMoveEvent; +import fr.zcraft.zsorter.events.LeftClickEvent; import fr.zcraft.zsorter.model.SorterManager; import fr.zcraft.zsorter.model.serializer.InventoryAdapter; import fr.zcraft.zsorter.model.serializer.PostProcessAdapterFactory; @@ -124,7 +124,8 @@ public void onEnable() { this.getServer().getPluginManager().registerEvents(new HolderBreakEvent(), this); this.getServer().getPluginManager().registerEvents(new InventoryEvent(), this); this.getServer().getPluginManager().registerEvents(new ItemMoveEvent(), this); - + this.getServer().getPluginManager().registerEvents(new LeftClickEvent(), this); + Commands.register("sorter", ListCommand.class, CreateCommand.class, diff --git a/src/main/java/fr/zcraft/zsorter/commands/MagicCommand.java b/src/main/java/fr/zcraft/zsorter/commands/MagicCommand.java new file mode 100644 index 0000000..b2601b9 --- /dev/null +++ b/src/main/java/fr/zcraft/zsorter/commands/MagicCommand.java @@ -0,0 +1,122 @@ +package fr.zcraft.zsorter.commands; + +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Player; + +import fr.zcraft.quartzlib.components.commands.CommandException; +import fr.zcraft.quartzlib.components.commands.CommandInfo; +import fr.zcraft.quartzlib.components.i18n.I; +import fr.zcraft.zsorter.Config; +import fr.zcraft.zsorter.ZSorter; +import fr.zcraft.zsorter.model.Input; +import fr.zcraft.zsorter.model.Output; +import fr.zcraft.zsorter.model.Sorter; + +/** + * Command triggered to select a sorter.

+ * A selection is needed in order to manage inputs and outputs. + * @author Lucas + */ +@CommandInfo (name = "magic", usageParameters = "") +public class MagicCommand extends ZSorterCommands{ + + @Override + protected void run() throws CommandException { + checkEnable(); + + //Check the number of arguments + if (args.length < 1) + throwInvalidArgument(I.t("A sorter name is required.")); + + //Get the name + String name = args[0]; + + //Get the sorter from the name + Sorter sorter = ZSorter.getInstance().getSorterManager().getNameToSorter().get(name); + + if(sorter != null) { + doMagicEffectPlayer(playerSender(), sorter, Config.MAGIC_EFFECT_DURATION.get()); + + for(Input input:sorter.getInventoryToInput().values()) { + highlightGlowingBlock(playerSender(), input.getInventory().getLocation(), Config.MAGIC_EFFECT_DURATION.get()); + } + for(Output output:sorter.getInventoryToOutput().values()) { + highlightGlowingBlock(playerSender(), output.getInventory().getLocation(), Config.MAGIC_EFFECT_DURATION.get()); + } + success(I.t("The magic effect has been applied on the sorter {0} for {1} ticks.", sorter.getName(), Config.MAGIC_EFFECT_DURATION.get())); + } + else { + error(I.t("There is no sorter with this name.")); + } + + + } + + /** + * Do magic effect on the player with the given sorter.
+ * This method add the pair player and sorter to the map. It will be used to find the sorter from the player when this one will left click on a block. + * @param player - Player on which apply the effect. + * @param sorter - Sorter selected by the player. + * @param duration - Duration of the effect. + */ + public void doMagicEffectPlayer(Player player, Sorter sorter, Integer duration) { + + Bukkit.getScheduler().scheduleSyncDelayedTask(ZSorter.getInstance(), () -> { + ZSorter.getInstance().getSorterManager().getPlayerToSorter().put(player, sorter); + Bukkit.getScheduler().scheduleSyncDelayedTask(ZSorter.getInstance(), () -> { + ZSorter.getInstance().getSorterManager().getPlayerToSorter().remove(player); + success(player, I.t("The magic effect has stopped.")); + }, Config.MAGIC_EFFECT_DURATION.get()); + }, 0); + } + + /** + * Highlights a given location for a given duration. + * @param p - Player on which play the effect. + * @param loc - Location of the block to highlight. + * @param duration - Duration of the effect. + */ + public void highlightGlowingBlock(Player p, Location loc, Integer duration){ + /*Bukkit.getScheduler().scheduleSyncDelayedTask(ZSorter.getInstance(), () -> { + try { + Object connection = NMSNetwork.getPlayerConnection(p); + Object nmsScoreBoard = NMSNetwork.getPlayerHandle(p); //Ne sert à rien + + Object shulkerClass = Reflection.getMinecraftClassByName("CraftShulker"); + Object handle = Reflection.call(loc.getWorld(), "getHandle"); + Object shulker = Reflection.instantiate(shulkerClass.getClass(), handle); + + Reflection.call(shulker, "setLocation", loc.getX(), loc.getY(), loc.getZ(), 0, 0); + Reflection.call(shulker, "setFlag", 6, true); + Reflection.call(shulker, "setFlag", 5, true); + + Object spawnPacketClass = Reflection.getMinecraftClassByName("PacketPlayOutSpawnEntityLiving"); + Object spawnPacket = Reflection.instantiate(spawnPacketClass.getClass(), shulker); + Reflection.call(connection, "sendPacket", spawnPacket); + + Bukkit.getScheduler().scheduleSyncDelayedTask(ZSorter.getInstance(), () -> { + try { + Object destroyPacketClass = Reflection.getMinecraftClassByName("PacketPlayOutEntityDestroy"); + Object destroyPacket = Reflection.instantiate(destroyPacketClass.getClass(), Reflection.call(shulker, "getId")); + Reflection.call(connection, "sendPacket", destroyPacket); + }catch(InvocationTargetException | ClassNotFoundException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InstantiationException e) { + e.printStackTrace(); + } + }, duration); + }catch(InvocationTargetException | ClassNotFoundException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InstantiationException e) { + e.printStackTrace(); + } + }, 0);*/ + } + + @Override + protected List complete() throws CommandException{ + if(args.length <= 1) { + return completeSorterName(args[0]); + } + return null; + } +} diff --git a/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java b/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java new file mode 100644 index 0000000..14e4f0b --- /dev/null +++ b/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java @@ -0,0 +1,50 @@ +package fr.zcraft.zsorter.events; + +import java.util.Arrays; + +import org.bukkit.ChatColor; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; + +import fr.zcraft.quartzlib.components.i18n.I; +import fr.zcraft.zsorter.ZSorter; +import fr.zcraft.zsorter.ZSorterException; +import fr.zcraft.zsorter.model.Sorter; +import fr.zcraft.zsorter.utils.InventoryUtils; + +/** + * Event called when a block is broken. + * @author Lucas + */ +public class LeftClickEvent implements Listener{ + + /** + * Event called when a player interact with a block. + * @param e - Event. + */ + @EventHandler + public void onPlayerInteractEvent(PlayerInteractEvent e) { + if(e.getAction() == Action.LEFT_CLICK_BLOCK) { //The player left click on a block + Sorter sorter = ZSorter.getInstance().getSorterManager().getPlayerToSorter().get(e.getPlayer()); //Get the sorter the player + if(sorter != null){ //If sorter found + int priority = 1; //Defines the priority of the output + InventoryHolder holder = (InventoryHolder) e.getClickedBlock().getState(); //Get the holder + Inventory inventory = InventoryUtils.doubleInventoryToSimpleInventory(holder.getInventory()); //find the inventory from the holder + + //Try to add the output to the sorter + try { + ZSorter.getInstance().getSorterManager().setOutput(sorter.getName(), inventory, priority, Arrays.asList(e.getItem().getType())); + e.getPlayer().sendMessage(ChatColor.GREEN + I.t("This holder is now an output of priority {0}.", priority)); + e.setCancelled(true); + } + catch(ZSorterException ex) { + e.getPlayer().sendMessage(ChatColor.RED + ex.getMessage()); + } + } + } + } +} diff --git a/src/main/java/fr/zcraft/zsorter/model/SorterManager.java b/src/main/java/fr/zcraft/zsorter/model/SorterManager.java index 4d2b25c..c3da1e2 100644 --- a/src/main/java/fr/zcraft/zsorter/model/SorterManager.java +++ b/src/main/java/fr/zcraft/zsorter/model/SorterManager.java @@ -8,6 +8,7 @@ import java.util.stream.Collectors; import org.bukkit.Material; +import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import fr.zcraft.quartzlib.components.i18n.I; @@ -27,6 +28,7 @@ public class SorterManager implements Serializable{ private Map nameToSorter; private transient Map inventoryToSorter; + private transient Map playerToSorter; /** * Constructor of a sorter manager object. @@ -34,16 +36,25 @@ public class SorterManager implements Serializable{ public SorterManager() { this.inventoryToSorter = new HashMap(); this.nameToSorter = new TreeMap(); + this.playerToSorter = new HashMap(); } /** - * Returns the map linking a name to a sorters. - * @return The sorters of the plugin. + * Returns the map linking a name to a sorter. + * @return The name to sorter map. */ public Map getNameToSorter() { return nameToSorter; } + /** + * Returns the map linking a player to a sorter. + * @return The player to sorter map. + */ + public Map getPlayerToSorter() { + return playerToSorter; + } + /** * Returns the map linking a inventory to a sorter.

* Do not use this method if you need to add or remove a sorter. diff --git a/src/main/resources/i18n/fr_FR.po b/src/main/resources/i18n/fr_FR.po index e1823ed..fdf0817 100644 --- a/src/main/resources/i18n/fr_FR.po +++ b/src/main/resources/i18n/fr_FR.po @@ -166,4 +166,10 @@ msgid "This material is clogging up one of the inputs" msgstr "Ce matériaux bouche une des entrées" msgid "The display mode must be either or ." -msgstr "Le mode d'affichage peut être ou ." \ No newline at end of file +msgstr "Le mode d'affichage peut être ou ." + +msgid "The magic effect has been applied on the sorter {0} for {1} ticks." +msgstr "L'effet magique a été appliqué sur le trieur {0} pour {1} ticks." + +msgid "The magic effect has stopped." +msgstr "l'effet magique est fini." \ No newline at end of file From 02649c8b72d44ba461bd4fe7983ca756905b6558 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Fri, 20 Nov 2020 18:50:52 +0100 Subject: [PATCH 04/21] Closes #9 Commit the sorter every time an input/output is added/removed --- .../java/fr/zcraft/zsorter/model/Sorter.java | 23 +++++++------ .../zsorter/model/SorterManagerTest.java | 32 ------------------- .../fr/zcraft/zsorter/model/SorterTest.java | 31 +++--------------- 3 files changed, 15 insertions(+), 71 deletions(-) diff --git a/src/main/java/fr/zcraft/zsorter/model/Sorter.java b/src/main/java/fr/zcraft/zsorter/model/Sorter.java index af7eb26..de60e47 100644 --- a/src/main/java/fr/zcraft/zsorter/model/Sorter.java +++ b/src/main/java/fr/zcraft/zsorter/model/Sorter.java @@ -133,9 +133,7 @@ public boolean isEnable() { * @param state - {@code true} to enable the sorter, {@code false} to disable it. */ public void setEnable(boolean state) { - if(state) //If enabling the sorter - commit(); //Commit the sorter - else + if(!state) //If disabling the sorter this.toCompute = false; this.enable = state; } @@ -266,8 +264,6 @@ public Input setInput(Inventory inventory, int priority) throws ZSorterException if(output != null) //If exists throw new ZSorterException(I.t("This holder is already an output.")); //Display error message - setEnable(false); //Disable the sorter - Input existingInput = inventoryToInput.get(inventory); //Get the existing input if(existingInput == null) { //If no input exists existingInput = new Input(inventory, priority); //Create a new input @@ -276,6 +272,7 @@ public Input setInput(Inventory inventory, int priority) throws ZSorterException else { //If the input exists existingInput.setPriority(priority); //Set the new priority } + commit(); return existingInput; } @@ -285,8 +282,10 @@ public Input setInput(Inventory inventory, int priority) throws ZSorterException * @return The removed input object, {@code null} if no input found for this inventory. */ public Input removeInput(Inventory inventory) { - setEnable(false); - return inventoryToInput.remove(inventory); + Input result = inventoryToInput.remove(inventory); + if(result != null) + commit(); + return result; } /** @@ -303,8 +302,6 @@ public Output setOutput(Inventory inventory, int priority, List materi if(input != null) //If exists throw new ZSorterException(I.t("This holder is already an input.")); //Display error message - setEnable(false); //Disable the sorter - Output existingOutput = inventoryToOutput.get(inventory); //Get the existing output if(existingOutput == null) { //If no existing output existingOutput = new Output(inventory, priority); //Create a new output @@ -315,6 +312,7 @@ public Output setOutput(Inventory inventory, int priority, List materi existingOutput.setPriority(priority); //Set the new priority existingOutput.setMaterials(materials); //Set the new materials } + commit(); return existingOutput; } @@ -324,8 +322,10 @@ public Output setOutput(Inventory inventory, int priority, List materi * @return The removed output object, {@code null} if no output found at this inventory. */ public Output removeOutput(Inventory inventory) { - setEnable(false); - return inventoryToOutput.remove(inventory); + Output result = inventoryToOutput.remove(inventory); + if(result != null) + commit(); + return result; } /** @@ -423,7 +423,6 @@ public void computeSorting() { * @return Sorter as RawText. */ public RawText toRawText(DisplayMode mode) { - commit(); RawTextPart text = new RawText("") .then(name) .style(ChatColor.GOLD) diff --git a/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java b/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java index d949652..7f5aba5 100644 --- a/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java +++ b/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java @@ -122,37 +122,6 @@ public void canComputeTest() throws ZSorterException { Assert.assertEquals(Arrays.asList(), manager.canCompute()); } - /** - * Tests if a sorterManage binary serialized and deserialized. - * @throws ZSorterException - * @throws FileNotFoundException - * @throws IOException - * @throws ClassNotFoundException - */ - @Test - public void binarySerializationTest() throws ZSorterException, FileNotFoundException, IOException, ClassNotFoundException { - SorterManager manager = new SorterManager(); - Sorter sorter = manager.createSorter("test", "Simple test sorter"); - manager.setInput("test", inventory0, 1); - manager.setOutput("test", inventory1, 1, Arrays.asList(Material.IRON_INGOT)); - manager.setOutput("test", inventory2, 1, Arrays.asList()); - - sorter.setSpeed(64); - sorter.commit(); - - //Serialization - ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("serializationTest.dat"))); - oos.writeObject(manager); - oos.close(); - - //Dezerialisation - ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("serializationTest.dat"))); - SorterManager deserializedManager = (SorterManager) ois.readObject(); - ois.close(); - - Assert.assertEquals(manager, deserializedManager); - } - /** * Tests if a sorterManage object is correctly serialized and deserialized. * @throws ZSorterException @@ -169,7 +138,6 @@ public void gsonSerializationTest() throws ZSorterException, FileNotFoundExcepti manager.setOutput("test", inventory2, 1, Arrays.asList()); sorter.setSpeed(64); - sorter.commit(); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(new PostProcessAdapterFactory()); diff --git a/src/test/java/fr/zcraft/zsorter/model/SorterTest.java b/src/test/java/fr/zcraft/zsorter/model/SorterTest.java index e12c1ac..bfc0aae 100644 --- a/src/test/java/fr/zcraft/zsorter/model/SorterTest.java +++ b/src/test/java/fr/zcraft/zsorter/model/SorterTest.java @@ -48,18 +48,12 @@ public void inputSortTest() throws ZSorterException { Input i1 = sorter.setInput(inventory1, 2); Input i0 = sorter.setInput(inventory0, 1); Input i3 = sorter.setInput(inventory3, 72); - Assert.assertEquals(Arrays.asList(), sorter.getInputs()); - //Commit the sorter and test if the inputs are sorted - sorter.commit(); + //Test if the inputs are sorted Assert.assertEquals(Arrays.asList(i0,i1,i2,i3), sorter.getInputs()); - //Remove one input and test if the inputs are the same + //Remove one input and test if the inputs are sorted sorter.removeInput(inventory1); - Assert.assertEquals(Arrays.asList(i0,i1,i2,i3), sorter.getInputs()); - - //Commit the sorter and test if the inputs are sorted - sorter.commit(); Assert.assertEquals(Arrays.asList(i0,i2,i3), sorter.getInputs()); } @@ -70,24 +64,16 @@ public void inputSortTest() throws ZSorterException { @Test public void outputSortTest() throws ZSorterException { - //Create a sorter, add output inventories and test if the overflows are empty + //Create a sorter, add output inventories and test if the overflows are sorted Sorter sorter = new Sorter("outputsTestSorter", "Description"); Output o2 = sorter.setOutput(inventory2, 45, new ArrayList()); Output o1 = sorter.setOutput(inventory1, 2, new ArrayList()); Output o0 = sorter.setOutput(inventory0, 1, new ArrayList()); Output o3 = sorter.setOutput(inventory3, 72, new ArrayList()); - Assert.assertEquals(Arrays.asList(), sorter.getOverflows()); - - //Commit the sorter and test if the overflows are sorted - sorter.commit(); Assert.assertEquals(Arrays.asList(o0, o1, o2, o3), sorter.getOverflows()); - //Remove one output and test if the overflows are the same + //Remove one output and test if the overflows are sorted sorter.removeOutput(inventory1); - Assert.assertEquals(Arrays.asList(o0, o1, o2, o3), sorter.getOverflows()); - - //Commit the sorter and test if the overflows are stored - sorter.commit(); Assert.assertEquals(Arrays.asList(o0, o2, o3), sorter.getOverflows()); } @@ -101,7 +87,6 @@ public void hasOverflowTest() throws ZSorterException { //Create a sorter, add an output, commit and test if the sorter has an overflow Sorter sorter = new Sorter("hasOverflowTestSorter", "Description"); sorter.setOutput(inventory0, 1, new ArrayList()); - sorter.commit(); Assert.assertEquals(true, sorter.hasOverflow()); //Add an output, commit and test if the sorter has an overflow @@ -127,49 +112,41 @@ public void findOutputsTest() throws ZSorterException { //Test when the sorter has one output of cobblestone Output coobleStoneOutput1 = sorter.setOutput(inventory0, 2, Arrays.asList(Material.COBBLESTONE)); - sorter.commit(); Assert.assertEquals(new ArrayList(), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(coobleStoneOutput1), sorter.findOutputs(Material.COBBLESTONE)); //Test when the sorter has one output of cobblestone and one of iron_block Output ironBlockOutput = sorter.setOutput(inventory1, 1, Arrays.asList(Material.IRON_BLOCK)); - sorter.commit(); Assert.assertEquals(Arrays.asList(ironBlockOutput), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(coobleStoneOutput1), sorter.findOutputs(Material.COBBLESTONE)); //Test when the sorter has two output of cobblestone and one of iron_block Output coobleStoneOutput2 = sorter.setOutput(inventory2, 1, Arrays.asList(Material.COBBLESTONE)); - sorter.commit(); Assert.assertEquals(Arrays.asList(ironBlockOutput), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(coobleStoneOutput2, coobleStoneOutput1), sorter.findOutputs(Material.COBBLESTONE)); //Test when the sorter has two output of cobblestone, one of iron_block and one overflow Output overflow = sorter.setOutput(inventory3, 10, Arrays.asList()); - sorter.commit(); Assert.assertEquals(Arrays.asList(ironBlockOutput, overflow), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(coobleStoneOutput2, coobleStoneOutput1, overflow), sorter.findOutputs(Material.COBBLESTONE)); //Test when the sorter has two output of cobblestone and one overflow sorter.removeOutput(ironBlockOutput.getInventory()); - sorter.commit(); Assert.assertEquals(Arrays.asList(overflow), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(coobleStoneOutput2, coobleStoneOutput1, overflow), sorter.findOutputs(Material.COBBLESTONE)); //Test when the sorter has one output of cobblestone and one overflow sorter.removeOutput(coobleStoneOutput1.getInventory()); - sorter.commit(); Assert.assertEquals(Arrays.asList(overflow), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(coobleStoneOutput2, overflow), sorter.findOutputs(Material.COBBLESTONE)); //Test when the sorter has one overflow sorter.removeOutput(coobleStoneOutput2.getInventory()); - sorter.commit(); Assert.assertEquals(Arrays.asList(overflow), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(overflow), sorter.findOutputs(Material.COBBLESTONE)); //Test when the sorter has not output sorter.removeOutput(overflow.getInventory()); - sorter.commit(); Assert.assertEquals(Arrays.asList(), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(), sorter.findOutputs(Material.COBBLESTONE)); } From 3add5c39f8d6f721acd94fd835edef301de9c6cb Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Mon, 21 Dec 2020 20:33:05 +0100 Subject: [PATCH 05/21] Add magic command help --- src/main/resources/help/sorter.txt | 5 +++-- src/main/resources/help/sorter/magic.txt | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/help/sorter/magic.txt diff --git a/src/main/resources/help/sorter.txt b/src/main/resources/help/sorter.txt index bd47470..5d9fded 100644 --- a/src/main/resources/help/sorter.txt +++ b/src/main/resources/help/sorter.txt @@ -2,11 +2,12 @@ This command is used to manage sorters in game list: List all the sorters in the world create: Create a new sorter update: Update a sorter description -speed: Change the sorter speed. +speed: Change the sorter speed delete: Delete a sorter info: Display a sorter informations toggle: Enable/Disable a sorter set_input: Add an input to a sorter set_output: Add an output to a sorter remove_input: Delete a sorter input -remove_output: Delete a sorter output \ No newline at end of file +remove_output: Delete a sorter output +magic: Select a sorter to easily define the outputs \ No newline at end of file diff --git a/src/main/resources/help/sorter/magic.txt b/src/main/resources/help/sorter/magic.txt new file mode 100644 index 0000000..587e94d --- /dev/null +++ b/src/main/resources/help/sorter/magic.txt @@ -0,0 +1,6 @@ +Select a sorter to easily define the outputs. + +§6 §rThe name of the sorter to select. + +When a sorter is selected using this command, the inputs and outputs are highlighted. +By performing a left click on an holder while holding an item in the hand, defines the holder as an output for the target item. \ No newline at end of file From 74c273ad6fa2bfd2e3af08353490b7efad572270 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Mon, 21 Dec 2020 20:33:20 +0100 Subject: [PATCH 06/21] Moved to 0.4 pre-release --- dependency-reduced-pom.xml | 2 +- pom.xml | 2 +- src/main/resources/plugin.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 9ae258b..c76c007 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -3,7 +3,7 @@ 4.0.0 fr.zcraft.zSorter zSorter - 0.3 + 0.4 diff --git a/pom.xml b/pom.xml index 697052e..ba02ac2 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ fr.zcraft.zSorter zSorter - 0.3 + 0.4 jar diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0892315..2a89631 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: zSorter -version: 0.2 +version: 0.4 main: fr.zcraft.zsorter.ZSorter description: Create and manage sorters in game. author: MrDomoo From fbbc86e010cef36fda335badf8a5383d89f894f0 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Mon, 21 Dec 2020 21:17:39 +0100 Subject: [PATCH 07/21] Fix javadoc and imports --- .../zsorter/commands/DeleteCommand.java | 1 - .../zsorter/commands/RemoveInputCommand.java | 11 ++--- .../zsorter/commands/RemoveOutputCommand.java | 11 ++--- .../zsorter/commands/SetInputCommand.java | 11 ++--- .../zsorter/commands/SetOutputCommand.java | 11 ++--- .../zcraft/zsorter/commands/SpeedCommand.java | 1 - .../zsorter/commands/ToggleCommand.java | 1 - .../zsorter/commands/UpdateCommand.java | 1 - .../zsorter/commands/ZSorterCommands.java | 5 ++ .../zsorter/events/HolderBreakEvent.java | 48 ++++++++++--------- .../zcraft/zsorter/events/LeftClickEvent.java | 8 ++-- .../model/serializer/InventoryAdapter.java | 15 +++--- .../zcraft/zsorter/utils/InventoryUtils.java | 18 +++++++ .../zsorter/model/SorterManagerTest.java | 5 -- 14 files changed, 70 insertions(+), 77 deletions(-) diff --git a/src/main/java/fr/zcraft/zsorter/commands/DeleteCommand.java b/src/main/java/fr/zcraft/zsorter/commands/DeleteCommand.java index e5092cf..6e3b2b1 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/DeleteCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/DeleteCommand.java @@ -1,7 +1,6 @@ package fr.zcraft.zsorter.commands; import java.util.List; -import java.util.stream.Collectors; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; diff --git a/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java index 4520a9b..8b4744d 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java @@ -2,12 +2,10 @@ import java.util.List; import java.util.Set; -import java.util.stream.Collectors; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; @@ -36,14 +34,11 @@ protected void run() throws CommandException { //Get the inventory from location Block block = playerSender().getTargetBlock((Set) null, 15); - if(!(block.getState() instanceof InventoryHolder)) - throwInvalidArgument(I.t("An input must be a holder.")); - - InventoryHolder holder = (InventoryHolder) block.getState(); - Inventory inventory = InventoryUtils.doubleInventoryToSimpleInventory(holder.getInventory()); - //Try to remove the input from the sorter try { + Inventory inventory = InventoryUtils.findInventoryFromBlock(block); + + //Try to remove the input from the sorter ZSorter.getInstance().getSorterManager().removeInput(name, inventory); success(I.t("This holder is no longer an input.")); } catch (ZSorterException e) { diff --git a/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java index d050f6a..d3ff434 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java @@ -2,12 +2,10 @@ import java.util.List; import java.util.Set; -import java.util.stream.Collectors; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; @@ -36,14 +34,11 @@ protected void run() throws CommandException { //Get the inventory from location Block block = playerSender().getTargetBlock((Set) null, 15); - if(!(block.getState() instanceof InventoryHolder)) - throwInvalidArgument(I.t("An input must be a holder.")); - - InventoryHolder holder = (InventoryHolder) block.getState(); - Inventory inventory = InventoryUtils.doubleInventoryToSimpleInventory(holder.getInventory()); - //Try to remove the output from the sorter try { + Inventory inventory = InventoryUtils.findInventoryFromBlock(block); + + //Try to remove the output from the sorter ZSorter.getInstance().getSorterManager().removeOutput(name, inventory); success(I.t("This holder is no longer an output.")); } catch (ZSorterException e) { diff --git a/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java index 9d09d93..4f4e49e 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java @@ -2,12 +2,10 @@ import java.util.List; import java.util.Set; -import java.util.stream.Collectors; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; @@ -46,14 +44,11 @@ protected void run() throws CommandException { //Get the inventory from location Block block = playerSender().getTargetBlock((Set) null, 15); - if(!(block.getState() instanceof InventoryHolder)) - throwInvalidArgument(I.t("An input must be a holder.")); - - InventoryHolder holder = (InventoryHolder) block.getState(); - Inventory inventory = InventoryUtils.doubleInventoryToSimpleInventory(holder.getInventory()); - //Try to add the input to the sorter try { + Inventory inventory = InventoryUtils.findInventoryFromBlock(block); + + //Try to add the input to the sorter ZSorter.getInstance().getSorterManager().setInput(name, inventory, priority); success(I.t("This holder is now an input of priority {0}.", priority)); } diff --git a/src/main/java/fr/zcraft/zsorter/commands/SetOutputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/SetOutputCommand.java index c1fe482..66b2e43 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/SetOutputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/SetOutputCommand.java @@ -9,7 +9,6 @@ import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; @@ -60,14 +59,10 @@ protected void run() throws CommandException { //Get the inventory from location Block block = playerSender().getTargetBlock((Set) null, 15); - if(!(block.getState() instanceof InventoryHolder)) - throwInvalidArgument(I.t("An output must be a holder.")); - - InventoryHolder holder = (InventoryHolder) block.getState(); - Inventory inventory = InventoryUtils.doubleInventoryToSimpleInventory(holder.getInventory()); - - //Try to add the output to the sorter try { + Inventory inventory = InventoryUtils.findInventoryFromBlock(block); + + //Try to add the output to the sorter ZSorter.getInstance().getSorterManager().setOutput(name, inventory, priority, materials); success(I.t("This holder is now an output of priority {0}.", priority)); } diff --git a/src/main/java/fr/zcraft/zsorter/commands/SpeedCommand.java b/src/main/java/fr/zcraft/zsorter/commands/SpeedCommand.java index 6961968..81baa84 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/SpeedCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/SpeedCommand.java @@ -1,7 +1,6 @@ package fr.zcraft.zsorter.commands; import java.util.List; -import java.util.stream.Collectors; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; diff --git a/src/main/java/fr/zcraft/zsorter/commands/ToggleCommand.java b/src/main/java/fr/zcraft/zsorter/commands/ToggleCommand.java index c09bb1d..c8da4cd 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/ToggleCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/ToggleCommand.java @@ -1,7 +1,6 @@ package fr.zcraft.zsorter.commands; import java.util.List; -import java.util.stream.Collectors; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; diff --git a/src/main/java/fr/zcraft/zsorter/commands/UpdateCommand.java b/src/main/java/fr/zcraft/zsorter/commands/UpdateCommand.java index f34e0b3..71ac6b0 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/UpdateCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/UpdateCommand.java @@ -3,7 +3,6 @@ import java.util.List; import java.util.StringJoiner; -import java.util.stream.Collectors; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; diff --git a/src/main/java/fr/zcraft/zsorter/commands/ZSorterCommands.java b/src/main/java/fr/zcraft/zsorter/commands/ZSorterCommands.java index 9bbff5d..a7be594 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/ZSorterCommands.java +++ b/src/main/java/fr/zcraft/zsorter/commands/ZSorterCommands.java @@ -32,6 +32,11 @@ public void checkEnable() throws CommandException { + "- Remove the file (and loose your data)"); } + /** + * Complete the sorter name when typing a command. + * @param arg Current type value. + * @return List of possible sorters. + */ public List completeSorterName(String arg){ return ZSorter.getInstance().getSorterManager().getNameToSorter().keySet() .stream() diff --git a/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java b/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java index 798deac..8da5e07 100644 --- a/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java @@ -5,10 +5,10 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.i18n.I; import fr.zcraft.zsorter.ZSorter; +import fr.zcraft.zsorter.ZSorterException; import fr.zcraft.zsorter.model.Sorter; import fr.zcraft.zsorter.utils.InventoryUtils; @@ -19,26 +19,28 @@ */ public class HolderBreakEvent implements Listener{ - /** - * Event called when a block is broken. - * @param e - Event. - */ - @EventHandler - public void onBlockBreak(BlockBreakEvent e) { - if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable - if(e.getBlock().getState() instanceof InventoryHolder) { //If the block is an inventory holder - InventoryHolder holder = (InventoryHolder) e.getBlock().getState(); //Get the holder block - Inventory inventory = InventoryUtils.doubleInventoryToSimpleInventory(holder.getInventory()); //Get the inventory - Sorter sorter = ZSorter.getInstance().getSorterManager().getInventoryToSorter().get(inventory); //Get the associated sorter - if(sorter != null) { //If a sorter has been found - if(sorter.removeInput(inventory) != null){ //Try to remove the input inventory - e.getPlayer().sendMessage(ChatColor.RED + I.t("This holder was an input of the sorter {0}. It has been removed from it.", sorter.getName())); - } - if(sorter.removeOutput(inventory) != null){ //Try to remove the output inventory - e.getPlayer().sendMessage(ChatColor.RED + I.t("This holder was an output of the sorter {0}. It has been removed from it.", sorter.getName())); - } - } - } - } - } + /** + * Event called when a block is broken. + * @param e - Event. + */ + @EventHandler + public void onBlockBreak(BlockBreakEvent e) { + if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable + try { + Inventory inventory = InventoryUtils.findInventoryFromBlock(e.getBlock()); //Get the inventory + Sorter sorter = ZSorter.getInstance().getSorterManager().getInventoryToSorter().get(inventory); //Get the associated sorter + if(sorter != null) { //If a sorter has been found + if(sorter.removeInput(inventory) != null){ //Try to remove the input inventory + e.getPlayer().sendMessage(ChatColor.RED + I.t("This holder was an input of the sorter {0}. It has been removed from it.", sorter.getName())); + } + if(sorter.removeOutput(inventory) != null){ //Try to remove the output inventory + e.getPlayer().sendMessage(ChatColor.RED + I.t("This holder was an output of the sorter {0}. It has been removed from it.", sorter.getName())); + } + } + } + catch(ZSorterException ex) { + //The removed block is not an holder. + } + } + } } diff --git a/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java b/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java index 14e4f0b..6b8043b 100644 --- a/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java @@ -8,7 +8,6 @@ import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.i18n.I; import fr.zcraft.zsorter.ZSorter; @@ -32,11 +31,10 @@ public void onPlayerInteractEvent(PlayerInteractEvent e) { Sorter sorter = ZSorter.getInstance().getSorterManager().getPlayerToSorter().get(e.getPlayer()); //Get the sorter the player if(sorter != null){ //If sorter found int priority = 1; //Defines the priority of the output - InventoryHolder holder = (InventoryHolder) e.getClickedBlock().getState(); //Get the holder - Inventory inventory = InventoryUtils.doubleInventoryToSimpleInventory(holder.getInventory()); //find the inventory from the holder - - //Try to add the output to the sorter try { + Inventory inventory = InventoryUtils.findInventoryFromBlock(e.getClickedBlock()); + + //Try to add the output to the sorter ZSorter.getInstance().getSorterManager().setOutput(sorter.getName(), inventory, priority, Arrays.asList(e.getItem().getType())); e.getPlayer().sendMessage(ChatColor.GREEN + I.t("This holder is now an output of priority {0}.", priority)); e.setCancelled(true); diff --git a/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryAdapter.java b/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryAdapter.java index bec4ff4..c3c9039 100644 --- a/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryAdapter.java +++ b/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryAdapter.java @@ -5,7 +5,6 @@ import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; @@ -16,6 +15,7 @@ import com.google.gson.JsonSerializer; import fr.zcraft.zsorter.ZSorter; +import fr.zcraft.zsorter.ZSorterException; import fr.zcraft.zsorter.utils.InventoryUtils; /** @@ -56,12 +56,11 @@ public Inventory deserialize(JsonElement json, Type typeOfT, JsonDeserialization if(block == null) throw new JsonParseException(String.format("The block at the location x=%d, y=%d, z=%d in the world %s does not exist. The input/output at this location will be removed.", x, y, z, worldName)); - - if(!(block.getState() instanceof InventoryHolder)) - throw new JsonParseException(String.format("The block at the location x=%d, y=%d, z=%d in the world %s is not a holder. The input/output at this location will be removed.", x, y, z, worldName)); - - Inventory inventory = ((InventoryHolder) block.getState()).getInventory(); - return InventoryUtils.doubleInventoryToSimpleInventory(inventory); + + try { + return InventoryUtils.findInventoryFromBlock(block); + } catch (ZSorterException e) { + throw new JsonParseException(e); + } } - } diff --git a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java index d3fcb11..d80c9f0 100644 --- a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java +++ b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java @@ -1,9 +1,13 @@ package fr.zcraft.zsorter.utils; +import org.bukkit.block.Block; import org.bukkit.inventory.DoubleChestInventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; +import fr.zcraft.quartzlib.components.i18n.I; +import fr.zcraft.zsorter.ZSorterException; + /** * This class provides useful methods related to inventories. * @author Lucas @@ -38,4 +42,18 @@ public static Inventory doubleInventoryToSimpleInventory(Inventory inventory) { } return inventory; } + + /** + * Find the inventory of a block. + * @param block - Block from which get the inventory. + * @return The inventory corresponding to the block location. + * @throws ZSorterException if the block is not an instance of {@code InventoryHolder}; + */ + public static Inventory findInventoryFromBlock(Block block) throws ZSorterException { + if(!(block.getState() instanceof InventoryHolder)) + throw new ZSorterException(I.t("Thi block must be an hodler.")); + + InventoryHolder holder = (InventoryHolder) block.getState(); + return InventoryUtils.doubleInventoryToSimpleInventory(holder.getInventory()); + } } diff --git a/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java b/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java index 7f5aba5..babf7cd 100644 --- a/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java +++ b/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java @@ -1,15 +1,10 @@ package fr.zcraft.zsorter.model; import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; import java.util.Arrays; import org.bukkit.Material; From c2115806930567a881d33321d878ae0d55f4e725 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Mon, 21 Dec 2020 23:12:55 +0100 Subject: [PATCH 08/21] Fix log --- src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java index d80c9f0..c0c586b 100644 --- a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java +++ b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java @@ -51,8 +51,8 @@ public static Inventory doubleInventoryToSimpleInventory(Inventory inventory) { */ public static Inventory findInventoryFromBlock(Block block) throws ZSorterException { if(!(block.getState() instanceof InventoryHolder)) - throw new ZSorterException(I.t("Thi block must be an hodler.")); - + throw new ZSorterException(I.t("This block must be a hodler.")); + InventoryHolder holder = (InventoryHolder) block.getState(); return InventoryUtils.doubleInventoryToSimpleInventory(holder.getInventory()); } From 0ebae3e010679bdf5e09b90404fefd46d62a6a31 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sat, 26 Dec 2020 15:21:37 +0100 Subject: [PATCH 09/21] Now use holders --- src/main/java/fr/zcraft/zsorter/ZSorter.java | 5 +- .../zcraft/zsorter/commands/MagicCommand.java | 4 +- .../zsorter/commands/RemoveInputCommand.java | 4 +- .../zsorter/commands/RemoveOutputCommand.java | 4 +- .../zsorter/commands/SetInputCommand.java | 4 +- .../zsorter/commands/SetOutputCommand.java | 4 +- .../zsorter/events/HolderBreakEvent.java | 6 +- .../zcraft/zsorter/events/InventoryEvent.java | 8 +- .../zcraft/zsorter/events/ItemMoveEvent.java | 16 +-- .../zcraft/zsorter/events/LeftClickEvent.java | 50 ++++----- .../java/fr/zcraft/zsorter/model/Input.java | 4 +- .../fr/zcraft/zsorter/model/InputOutput.java | 10 +- .../java/fr/zcraft/zsorter/model/Output.java | 4 +- .../java/fr/zcraft/zsorter/model/Sorter.java | 101 +++++++++--------- .../zcraft/zsorter/model/SorterManager.java | 22 ++-- .../model/serializer/InventoryAdapter.java | 16 +-- .../serializer/SorterManagerAdapter.java | 4 +- .../zcraft/zsorter/utils/InventoryUtils.java | 44 +++++--- .../java/fr/zcraft/zsorter/ZSorterTest.java | 9 +- .../fr/zcraft/zsorter/model/SorterTest.java | 8 +- 20 files changed, 173 insertions(+), 154 deletions(-) diff --git a/src/main/java/fr/zcraft/zsorter/ZSorter.java b/src/main/java/fr/zcraft/zsorter/ZSorter.java index 169f588..7085796 100644 --- a/src/main/java/fr/zcraft/zsorter/ZSorter.java +++ b/src/main/java/fr/zcraft/zsorter/ZSorter.java @@ -8,6 +8,7 @@ import org.bukkit.event.Listener; import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPluginLoader; @@ -159,7 +160,7 @@ private void save() { try { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(new PostProcessAdapterFactory()); - gsonBuilder.registerTypeHierarchyAdapter(Inventory.class, new InventoryAdapter()); + gsonBuilder.registerTypeHierarchyAdapter(InventoryHolder.class, new InventoryAdapter()); gsonBuilder.registerTypeAdapter(SorterManager.class, new SorterManagerAdapter()); Gson customGson = gsonBuilder.create(); FileWriter fr = new FileWriter(dataPath); @@ -180,7 +181,7 @@ private boolean load() { try { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(new PostProcessAdapterFactory()); - gsonBuilder.registerTypeHierarchyAdapter(Inventory.class, new InventoryAdapter()); + gsonBuilder.registerTypeHierarchyAdapter(InventoryHolder.class, new InventoryAdapter()); gsonBuilder.registerTypeAdapter(SorterManager.class, new SorterManagerAdapter()); Gson customGson = gsonBuilder.create(); BufferedReader br = new BufferedReader(new FileReader(dataFile)); diff --git a/src/main/java/fr/zcraft/zsorter/commands/MagicCommand.java b/src/main/java/fr/zcraft/zsorter/commands/MagicCommand.java index b2601b9..844417f 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/MagicCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/MagicCommand.java @@ -41,10 +41,10 @@ protected void run() throws CommandException { doMagicEffectPlayer(playerSender(), sorter, Config.MAGIC_EFFECT_DURATION.get()); for(Input input:sorter.getInventoryToInput().values()) { - highlightGlowingBlock(playerSender(), input.getInventory().getLocation(), Config.MAGIC_EFFECT_DURATION.get()); + highlightGlowingBlock(playerSender(), input.getHolder().getInventory().getLocation(), Config.MAGIC_EFFECT_DURATION.get()); } for(Output output:sorter.getInventoryToOutput().values()) { - highlightGlowingBlock(playerSender(), output.getInventory().getLocation(), Config.MAGIC_EFFECT_DURATION.get()); + highlightGlowingBlock(playerSender(), output.getHolder().getInventory().getLocation(), Config.MAGIC_EFFECT_DURATION.get()); } success(I.t("The magic effect has been applied on the sorter {0} for {1} ticks.", sorter.getName(), Config.MAGIC_EFFECT_DURATION.get())); } diff --git a/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java index 8b4744d..6faa154 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java @@ -5,7 +5,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; @@ -36,7 +36,7 @@ protected void run() throws CommandException { Block block = playerSender().getTargetBlock((Set) null, 15); try { - Inventory inventory = InventoryUtils.findInventoryFromBlock(block); + InventoryHolder inventory = InventoryUtils.findInventoryFromBlock(block); //Try to remove the input from the sorter ZSorter.getInstance().getSorterManager().removeInput(name, inventory); diff --git a/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java index d3ff434..0dca932 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java @@ -5,7 +5,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; @@ -36,7 +36,7 @@ protected void run() throws CommandException { Block block = playerSender().getTargetBlock((Set) null, 15); try { - Inventory inventory = InventoryUtils.findInventoryFromBlock(block); + InventoryHolder inventory = InventoryUtils.findInventoryFromBlock(block); //Try to remove the output from the sorter ZSorter.getInstance().getSorterManager().removeOutput(name, inventory); diff --git a/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java index 4f4e49e..ea9aa16 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java @@ -5,7 +5,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; @@ -46,7 +46,7 @@ protected void run() throws CommandException { Block block = playerSender().getTargetBlock((Set) null, 15); try { - Inventory inventory = InventoryUtils.findInventoryFromBlock(block); + InventoryHolder inventory = InventoryUtils.findInventoryFromBlock(block); //Try to add the input to the sorter ZSorter.getInstance().getSorterManager().setInput(name, inventory, priority); diff --git a/src/main/java/fr/zcraft/zsorter/commands/SetOutputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/SetOutputCommand.java index 66b2e43..4a81123 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/SetOutputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/SetOutputCommand.java @@ -8,7 +8,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; @@ -60,7 +60,7 @@ protected void run() throws CommandException { //Get the inventory from location Block block = playerSender().getTargetBlock((Set) null, 15); try { - Inventory inventory = InventoryUtils.findInventoryFromBlock(block); + InventoryHolder inventory = InventoryUtils.findInventoryFromBlock(block); //Try to add the output to the sorter ZSorter.getInstance().getSorterManager().setOutput(name, inventory, priority, materials); diff --git a/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java b/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java index 8da5e07..00e1a2d 100644 --- a/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java @@ -4,7 +4,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.i18n.I; import fr.zcraft.zsorter.ZSorter; @@ -27,7 +27,7 @@ public class HolderBreakEvent implements Listener{ public void onBlockBreak(BlockBreakEvent e) { if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable try { - Inventory inventory = InventoryUtils.findInventoryFromBlock(e.getBlock()); //Get the inventory + InventoryHolder inventory = InventoryUtils.findInventoryFromBlock(e.getBlock()); //Get the inventory Sorter sorter = ZSorter.getInstance().getSorterManager().getInventoryToSorter().get(inventory); //Get the associated sorter if(sorter != null) { //If a sorter has been found if(sorter.removeInput(inventory) != null){ //Try to remove the input inventory @@ -39,7 +39,7 @@ public void onBlockBreak(BlockBreakEvent e) { } } catch(ZSorterException ex) { - //The removed block is not an holder. + //The removed block is not a holder. } } } diff --git a/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java b/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java index e09eee3..487822f 100644 --- a/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java @@ -3,7 +3,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import fr.zcraft.zsorter.ZSorter; import fr.zcraft.zsorter.utils.InventoryUtils; @@ -22,8 +22,10 @@ public class InventoryEvent implements Listener{ @EventHandler public void onInventoryCloseEvent(InventoryCloseEvent e) { if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable - Inventory inventory = InventoryUtils.doubleInventoryToSimpleInventory(e.getInventory()); //Get the inventory if double chest - ZSorter.getInstance().getSorterManager().computeSorter(inventory); //Try to compute the sorter with this inventory + System.out.println("onInventoryCloseEvent : " + e.getInventory().getHolder().toString()); + InventoryUtils.displayInventoryContent(e.getInventory()); + //InventoryHolder inventory = InventoryUtils.doubleInventoryToSimpleInventory(e.getInventory()).getHolder(); //Get the inventory if double chest + ZSorter.getInstance().getSorterManager().computeSorter(e.getInventory().getHolder()); //Try to compute the sorter with this inventory } } } diff --git a/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java b/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java index 8f51e20..866faec 100644 --- a/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java @@ -3,7 +3,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import fr.zcraft.zsorter.ZSorter; import fr.zcraft.zsorter.utils.InventoryUtils; @@ -21,12 +21,14 @@ public class ItemMoveEvent implements Listener{ */ @EventHandler public void onInventoryMoveItem(InventoryMoveItemEvent e) { - if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable - Inventory inputInventory = InventoryUtils.doubleInventoryToSimpleInventory(e.getDestination()); //Get the inventory if double chest - boolean computed = ZSorter.getInstance().getSorterManager().computeSorter(inputInventory); //Try to compute the sorter with this input - if(!computed) { //If no computed - Inventory outputInventory = InventoryUtils.doubleInventoryToSimpleInventory(e.getSource()); //Get the inventory if double chest - ZSorter.getInstance().getSorterManager().computeSorter(outputInventory); //Try to compute the sorter with this output + if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable + System.out.println("onInventoryMoveItem : " + e.getDestination().getHolder().toString()); + InventoryUtils.displayInventoryContent(e.getDestination()); + //InventoryHolder inputInventory = InventoryUtils.doubleInventoryToSimpleInventory(e.getDestination()).getHolder(); //Get the inventory if double chest + boolean computed = ZSorter.getInstance().getSorterManager().computeSorter(e.getDestination().getHolder()); //Try to compute the sorter with this input + if(!computed) { //If no computed + //InventoryHolder outputInventory = InventoryUtils.doubleInventoryToSimpleInventory(e.getSource()).getHolder(); //Get the inventory if double chest + ZSorter.getInstance().getSorterManager().computeSorter(e.getDestination().getHolder()); //Try to compute the sorter with this output } } } diff --git a/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java b/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java index 6b8043b..ae61e52 100644 --- a/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java @@ -7,7 +7,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.i18n.I; import fr.zcraft.zsorter.ZSorter; @@ -21,28 +21,30 @@ */ public class LeftClickEvent implements Listener{ - /** - * Event called when a player interact with a block. - * @param e - Event. - */ - @EventHandler - public void onPlayerInteractEvent(PlayerInteractEvent e) { - if(e.getAction() == Action.LEFT_CLICK_BLOCK) { //The player left click on a block - Sorter sorter = ZSorter.getInstance().getSorterManager().getPlayerToSorter().get(e.getPlayer()); //Get the sorter the player - if(sorter != null){ //If sorter found - int priority = 1; //Defines the priority of the output - try { - Inventory inventory = InventoryUtils.findInventoryFromBlock(e.getClickedBlock()); + /** + * Event called when a player interact with a block. + * @param e - Event. + */ + @EventHandler + public void onPlayerInteractEvent(PlayerInteractEvent e) { + if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable + if(e.getAction() == Action.LEFT_CLICK_BLOCK) { //The player left click on a block + Sorter sorter = ZSorter.getInstance().getSorterManager().getPlayerToSorter().get(e.getPlayer()); //Get the sorter the player + if(sorter != null){ //If sorter found + int priority = 1; //Defines the priority of the output + try { + InventoryHolder inventory = InventoryUtils.findInventoryFromBlock(e.getClickedBlock()); - //Try to add the output to the sorter - ZSorter.getInstance().getSorterManager().setOutput(sorter.getName(), inventory, priority, Arrays.asList(e.getItem().getType())); - e.getPlayer().sendMessage(ChatColor.GREEN + I.t("This holder is now an output of priority {0}.", priority)); - e.setCancelled(true); - } - catch(ZSorterException ex) { - e.getPlayer().sendMessage(ChatColor.RED + ex.getMessage()); - } - } - } - } + //Try to add the output to the sorter + ZSorter.getInstance().getSorterManager().setOutput(sorter.getName(), inventory, priority, Arrays.asList(e.getItem().getType())); + e.getPlayer().sendMessage(ChatColor.GREEN + I.t("This holder is now an output of priority {0}.", priority)); + e.setCancelled(true); + } + catch(ZSorterException ex) { + e.getPlayer().sendMessage(ChatColor.RED + ex.getMessage()); + } + } + } + } + } } diff --git a/src/main/java/fr/zcraft/zsorter/model/Input.java b/src/main/java/fr/zcraft/zsorter/model/Input.java index 1bca1ae..0a398b3 100644 --- a/src/main/java/fr/zcraft/zsorter/model/Input.java +++ b/src/main/java/fr/zcraft/zsorter/model/Input.java @@ -2,7 +2,7 @@ import java.io.Serializable; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; /** * The class {@code Input} represents an input of a sorter. @@ -26,7 +26,7 @@ public class Input extends InputOutput implements Serializable{ * @param inventory - Inventory of the output. * @param priority - Priority of the output. */ - public Input(Inventory inventory, Integer priority) { + public Input(InventoryHolder inventory, Integer priority) { super(inventory, priority); this.cloggedUp = false; } diff --git a/src/main/java/fr/zcraft/zsorter/model/InputOutput.java b/src/main/java/fr/zcraft/zsorter/model/InputOutput.java index 085aa7f..cdc9851 100644 --- a/src/main/java/fr/zcraft/zsorter/model/InputOutput.java +++ b/src/main/java/fr/zcraft/zsorter/model/InputOutput.java @@ -2,7 +2,7 @@ import java.io.Serializable; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; /** * The class {@code InputOutput} represents an input or an output of a sorter. @@ -19,7 +19,7 @@ public abstract class InputOutput implements Serializable, Comparable(); this.full = false; diff --git a/src/main/java/fr/zcraft/zsorter/model/Sorter.java b/src/main/java/fr/zcraft/zsorter/model/Sorter.java index de60e47..de19f71 100644 --- a/src/main/java/fr/zcraft/zsorter/model/Sorter.java +++ b/src/main/java/fr/zcraft/zsorter/model/Sorter.java @@ -12,6 +12,7 @@ import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import fr.zcraft.quartzlib.components.i18n.I; @@ -48,8 +49,8 @@ public class Sorter implements Serializable, PostProcessable{ private transient boolean toCompute; private int speed; - private transient Map inventoryToInput; - private transient Map inventoryToOutput; + private transient Map inventoryToInput; + private transient Map inventoryToOutput; private transient Map> materialToOutputs; private transient List overflows; @@ -78,8 +79,8 @@ public Sorter(String name, String description) { this.toCompute = false; this.speed = DEFAULT_SPEED; - this.inventoryToInput = new HashMap(); - this.inventoryToOutput = new HashMap(); + this.inventoryToInput = new HashMap(); + this.inventoryToOutput = new HashMap(); this.materialToOutputs = new TreeMap>(); this.overflows = new ArrayList(); @@ -176,7 +177,7 @@ public void setSpeed(int speed) { * Use the {@code setInput} and {@code removeInput} methods instead. * @return The sorter inputs. */ - public Map getInventoryToInput() { + public Map getInventoryToInput() { return inventoryToInput; } @@ -186,7 +187,7 @@ public Map getInventoryToInput() { * Use the {@code setOutput} and {@code removeOutput} methods instead. * @return The sorter outputs. */ - public Map getInventoryToOutput() { + public Map getInventoryToOutput() { return inventoryToOutput; } @@ -259,7 +260,7 @@ public void commit() { * @return The created input object. * @throws ZSorterException if a ZSorter exception occurs. */ - public Input setInput(Inventory inventory, int priority) throws ZSorterException { + public Input setInput(InventoryHolder inventory, int priority) throws ZSorterException { Output output = inventoryToOutput.get(inventory); //Get the existing output if(output != null) //If exists throw new ZSorterException(I.t("This holder is already an output.")); //Display error message @@ -281,7 +282,7 @@ public Input setInput(Inventory inventory, int priority) throws ZSorterException * @param inventory - Inventory of the input. * @return The removed input object, {@code null} if no input found for this inventory. */ - public Input removeInput(Inventory inventory) { + public Input removeInput(InventoryHolder inventory) { Input result = inventoryToInput.remove(inventory); if(result != null) commit(); @@ -297,7 +298,7 @@ public Input removeInput(Inventory inventory) { * @return The created output object. * @throws ZSorterException if a ZSorter exception occurs. */ - public Output setOutput(Inventory inventory, int priority, List materials) throws ZSorterException { + public Output setOutput(InventoryHolder inventory, int priority, List materials) throws ZSorterException { Input input = inventoryToInput.get(inventory); //Get the existing input if(input != null) //If exists throw new ZSorterException(I.t("This holder is already an input.")); //Display error message @@ -321,7 +322,7 @@ public Output setOutput(Inventory inventory, int priority, List materi * @param inventory - Inventory of the output. * @return The removed output object, {@code null} if no output found at this inventory. */ - public Output removeOutput(Inventory inventory) { + public Output removeOutput(InventoryHolder inventory) { Output result = inventoryToOutput.remove(inventory); if(result != null) commit(); @@ -363,50 +364,50 @@ public boolean hasOverflow() { * Compute sorter on the sorter. */ public void computeSorting() { - if(isEnable()) { //If the sorter is ON - for(Input input:inputs) { //For each input in the sorter - Inventory inputInventory = InventoryUtils.simpleInventoryToDoubleInventory(input.getInventory()); //Get the input inventory - for(ItemStack itemStack: inputInventory.getContents()) { //For each item in the input inventory - if(itemStack != null) { //If the item is not null - ItemStack itemStackToTransfer = itemStack.clone(); //Clone the item to keep the metadata - if(itemStackToTransfer.getAmount() > speed) //If the number of items in the stack is over the speed limit - itemStackToTransfer.setAmount(speed); //Set to the speed limit - List outputs = findOutputs(itemStack.getType()); //Find the outputs for this item - for(Output output:outputs) { //For each possible output - Inventory outputInventory = InventoryUtils.simpleInventoryToDoubleInventory(output.getInventory()); //Get the output inventory - int amountToTransfer = itemStackToTransfer.getAmount(); //Get the amount to transfer - HashMap couldntTransferMap = outputInventory.addItem(itemStackToTransfer); //Add the item to the output - if(couldntTransferMap.isEmpty()) { //If everything has been transfered + if(isEnable()) { //If the sorter is ON + for(Input input:inputs) { //For each input in the sorter + Inventory inputInventory = input.getHolder().getInventory(); //Get the input inventory + for(ItemStack itemStack: inputInventory.getContents()) { //For each item in the input inventory + if(itemStack != null) { //If the item is not null + ItemStack itemStackToTransfer = itemStack.clone(); //Clone the item to keep the metadata + if(itemStackToTransfer.getAmount() > speed) //If the number of items in the stack is over the speed limit + itemStackToTransfer.setAmount(speed); //Set to the speed limit + List outputs = findOutputs(itemStack.getType()); //Find the outputs for this item + for(Output output:outputs) { //For each possible output + Inventory outputInventory = output.getHolder().getInventory(); //Get the output inventory + int amountToTransfer = itemStackToTransfer.getAmount(); //Get the amount to transfer + HashMap couldntTransferMap = outputInventory.addItem(itemStackToTransfer); //Add the item to the output + if(couldntTransferMap.isEmpty()) { //If everything has been transfered //Run only if all the item fit in the output - inputInventory.removeItem(itemStackToTransfer); //Remove the item from the input - if(input.isCloggedUp()) //If the input is clogged up - input.setCloggedUp(false); //Not clogged up anymore - if(output.isFull()) //If the output was full - output.setFull(false); //Not full anymore - if(cloggingUpMaterials.contains(itemStack.getType())) //If the stored item was clogging up the inputs - cloggingUpMaterials.remove(itemStack.getType()); //Not clogging up anymore - return; //Exit + inputInventory.removeItem(itemStackToTransfer); //Remove the item from the input + if(input.isCloggedUp()) //If the input is clogged up + input.setCloggedUp(false); //Not clogged up anymore + if(output.isFull()) //If the output was full + output.setFull(false); //Not full anymore + if(cloggingUpMaterials.contains(itemStack.getType())) //If the stored item was clogging up the inputs + cloggingUpMaterials.remove(itemStack.getType()); //Not clogging up anymore + return; //Exit } //Run only if the output is full - if(!output.isFull()) //If the output was not full - output.setFull(true); //Now is it's full + if(!output.isFull()) //If the output was not full + output.setFull(true); //Now is it's full - ItemStack itemStackToRemove = itemStackToTransfer.clone(); //Create the stack to remove - itemStackToRemove.setAmount(amountToTransfer - itemStackToTransfer.getAmount()); //Set the amount to remove - inputInventory.removeItem(itemStackToRemove); //Remove the item from the input - itemStackToTransfer.setAmount(itemStackToTransfer.getAmount()); //Define the new amount to transfer + ItemStack itemStackToRemove = itemStackToTransfer.clone(); //Create the stack to remove + itemStackToRemove.setAmount(amountToTransfer - itemStackToTransfer.getAmount()); //Set the amount to remove + inputInventory.removeItem(itemStackToRemove); //Remove the item from the input + itemStackToTransfer.setAmount(itemStackToTransfer.getAmount()); //Define the new amount to transfer } //Run only if this item is clogging up. - if(!input.isCloggedUp()) //If the input is not clogging up - input.setCloggedUp(true); //Set the input to clogged up - if(!cloggingUpMaterials.contains(itemStack.getType())) //If the material is not in the list - cloggingUpMaterials.add(itemStack.getType()); //Add the material to the list + if(!input.isCloggedUp()) //If the input is not clogging up + input.setCloggedUp(true); //Set the input to clogged up + if(!cloggingUpMaterials.contains(itemStack.getType())) //If the material is not in the list + cloggingUpMaterials.add(itemStack.getType()); //Add the material to the list } } } @@ -458,7 +459,7 @@ public RawText toRawText(DisplayMode mode) { .color(input.isCloggedUp() ? ChatColor.RED : ChatColor.AQUA) .hover( new RawText() - .then(String.format("X=%1$,.0f\nY=%2$,.0f\nZ=%3$,.0f", input.getInventory().getLocation().getX(), input.getInventory().getLocation().getY(), input.getInventory().getLocation().getZ())) + .then(String.format("X=%1$,.0f\nY=%2$,.0f\nZ=%3$,.0f", input.getHolder().getInventory().getLocation().getX(), input.getHolder().getInventory().getLocation().getY(), input.getHolder().getInventory().getLocation().getZ())) .color(input.isCloggedUp() ? ChatColor.RED : ChatColor.AQUA) ); } @@ -473,7 +474,7 @@ public RawText toRawText(DisplayMode mode) { .color(overflow.isFull() ? ChatColor.RED : ChatColor.AQUA) .hover( new RawText() - .then(String.format("X=%1$,.0f\nY=%2$,.0f\nZ=%3$,.0f", overflow.getInventory().getLocation().getX(), overflow.getInventory().getLocation().getY(), overflow.getInventory().getLocation().getZ())) + .then(String.format("X=%1$,.0f\nY=%2$,.0f\nZ=%3$,.0f", overflow.getHolder().getInventory().getLocation().getX(), overflow.getHolder().getInventory().getLocation().getY(), overflow.getHolder().getInventory().getLocation().getZ())) .color(overflow.isFull() ? ChatColor.RED : ChatColor.AQUA) ); } @@ -497,7 +498,7 @@ public RawText toRawText(DisplayMode mode) { .color(output.isFull() ? ChatColor.RED : ChatColor.AQUA) .hover( new RawText() - .then(String.format("X=%1$,.0f\nY=%2$,.0f\nZ=%3$,.0f", output.getInventory().getLocation().getX(), output.getInventory().getLocation().getY(), output.getInventory().getLocation().getZ())) + .then(String.format("X=%1$,.0f\nY=%2$,.0f\nZ=%3$,.0f", output.getHolder().getInventory().getLocation().getX(), output.getHolder().getInventory().getLocation().getY(), output.getHolder().getInventory().getLocation().getZ())) .color(output.isFull() ? ChatColor.RED : ChatColor.AQUA) ); @@ -550,7 +551,7 @@ else if(mode == DisplayMode.ITEMS){ .color(output.isFull() ? ChatColor.RED : ChatColor.AQUA) .hover( new RawText() - .then(String.format("X=%1$,.0f\nY=%2$,.0f\nZ=%3$,.0f", output.getInventory().getLocation().getX(), output.getInventory().getLocation().getY(), output.getInventory().getLocation().getZ())) + .then(String.format("X=%1$,.0f\nY=%2$,.0f\nZ=%3$,.0f", output.getHolder().getInventory().getLocation().getX(), output.getHolder().getInventory().getLocation().getY(), output.getHolder().getInventory().getLocation().getZ())) .color(output.isFull() ? ChatColor.RED : ChatColor.AQUA) ); } @@ -592,13 +593,13 @@ public boolean equals(Object obj) { @Override public void postProcess() { - inventoryToInput = new HashMap(); + inventoryToInput = new HashMap(); for(Input input:inputs) { - inventoryToInput.putIfAbsent(input.getInventory(), input); + inventoryToInput.putIfAbsent(input.getHolder(), input); } - inventoryToOutput = new HashMap(); + inventoryToOutput = new HashMap(); for(Output output:outputs) { - inventoryToOutput.putIfAbsent(output.getInventory(), output); + inventoryToOutput.putIfAbsent(output.getHolder(), output); } commit(); if(enable) diff --git a/src/main/java/fr/zcraft/zsorter/model/SorterManager.java b/src/main/java/fr/zcraft/zsorter/model/SorterManager.java index c3da1e2..f05f89a 100644 --- a/src/main/java/fr/zcraft/zsorter/model/SorterManager.java +++ b/src/main/java/fr/zcraft/zsorter/model/SorterManager.java @@ -9,7 +9,7 @@ import org.bukkit.Material; import org.bukkit.entity.Player; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import fr.zcraft.quartzlib.components.i18n.I; import fr.zcraft.zsorter.ZSorterException; @@ -27,14 +27,14 @@ public class SorterManager implements Serializable{ private static final long serialVersionUID = -1782855927147248287L; private Map nameToSorter; - private transient Map inventoryToSorter; + private transient Map inventoryToSorter; private transient Map playerToSorter; /** * Constructor of a sorter manager object. */ public SorterManager() { - this.inventoryToSorter = new HashMap(); + this.inventoryToSorter = new HashMap(); this.nameToSorter = new TreeMap(); this.playerToSorter = new HashMap(); } @@ -61,7 +61,7 @@ public Map getPlayerToSorter() { * Use the {@code addSorter} and {@code deleteSorter} methods instead. * @return The sorters of the plugin. */ - public Map getInventoryToSorter() { + public Map getInventoryToSorter() { return inventoryToSorter; } @@ -92,9 +92,9 @@ public Sorter deleteSorter(String name) throws ZSorterException { throw new ZSorterException(I.t("There is no sorter with this name.")); //Display error message for(Input input:sorter.getInventoryToInput().values()) //For each input of the sorter - inventoryToSorter.remove(input.getInventory()); //Remove the input from the inventory to sorter map + inventoryToSorter.remove(input.getHolder()); //Remove the input from the inventory to sorter map for(Input input:sorter.getInventoryToInput().values()) //For each ouput of the sorter - inventoryToSorter.remove(input.getInventory()); //Remove the output from the inventory to sorter map + inventoryToSorter.remove(input.getHolder()); //Remove the output from the inventory to sorter map return sorter; } @@ -105,7 +105,7 @@ public Sorter deleteSorter(String name) throws ZSorterException { * @param priority - Priority of the input. * @throws ZSorterException if a ZSorter exception occurs. */ - public void setInput(String name, Inventory inventory, int priority) throws ZSorterException { + public void setInput(String name, InventoryHolder inventory, int priority) throws ZSorterException { Sorter sorter = nameToSorter.get(name); if(sorter == null) throw new ZSorterException(I.t("There is no sorter with this name.")); @@ -124,7 +124,7 @@ public void setInput(String name, Inventory inventory, int priority) throws ZSor * @return The removed input object, {@code null} if no input found for this inventory. * @throws ZSorterException if a ZSorter exception occurs. */ - public Input removeInput(String name, Inventory inventory) throws ZSorterException { + public Input removeInput(String name, InventoryHolder inventory) throws ZSorterException { Sorter sorter = nameToSorter.get(name); if(sorter == null) throw new ZSorterException(I.t("There is no sorter with this name.")); @@ -145,7 +145,7 @@ public Input removeInput(String name, Inventory inventory) throws ZSorterExcepti * @param materials - Materials of the output. * @throws ZSorterException if a ZSorter exception occurs. */ - public void setOutput(String name, Inventory inventory, int priority, List materials) throws ZSorterException { + public void setOutput(String name, InventoryHolder inventory, int priority, List materials) throws ZSorterException { Sorter sorter = nameToSorter.get(name); if(sorter == null) throw new ZSorterException(I.t("There is no sorter with this name.")); @@ -164,7 +164,7 @@ public void setOutput(String name, Inventory inventory, int priority, List canCompute(){ * @param inventory - Inventory of the sorter to compute. * @return {@code true} if the sorter has been computed, {@code false} otherwise. */ - public boolean computeSorter(Inventory inventory) { + public boolean computeSorter(InventoryHolder inventory) { boolean computed = false; Sorter sorter = inventoryToSorter.get(inventory); //Get the sorter associated with this inventory if(sorter != null && sorter.isEnable()) { //If sorter found and enable diff --git a/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryAdapter.java b/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryAdapter.java index c3c9039..32e0e6d 100644 --- a/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryAdapter.java +++ b/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryAdapter.java @@ -4,7 +4,7 @@ import org.bukkit.World; import org.bukkit.block.Block; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; @@ -23,20 +23,20 @@ * @author Lucas * */ -public class InventoryAdapter implements JsonSerializer, JsonDeserializer{ +public class InventoryAdapter implements JsonSerializer, JsonDeserializer{ @Override - public JsonElement serialize(Inventory src, Type typeOfSrc, JsonSerializationContext context) { + public JsonElement serialize(InventoryHolder src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonInputOutput = new JsonObject(); - jsonInputOutput.addProperty("world", src.getLocation().getWorld().getName()); - jsonInputOutput.addProperty("x", new Integer(src.getLocation().getBlockX())); - jsonInputOutput.addProperty("y", new Integer(src.getLocation().getBlockY())); - jsonInputOutput.addProperty("z", new Integer(src.getLocation().getBlockZ())); + jsonInputOutput.addProperty("world", src.getInventory().getLocation().getWorld().getName()); + jsonInputOutput.addProperty("x", new Integer(src.getInventory().getLocation().getBlockX())); + jsonInputOutput.addProperty("y", new Integer(src.getInventory().getLocation().getBlockY())); + jsonInputOutput.addProperty("z", new Integer(src.getInventory().getLocation().getBlockZ())); return jsonInputOutput; } @Override - public Inventory deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + public InventoryHolder deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); String worldName = jsonObject.get("world").getAsString(); diff --git a/src/main/java/fr/zcraft/zsorter/model/serializer/SorterManagerAdapter.java b/src/main/java/fr/zcraft/zsorter/model/serializer/SorterManagerAdapter.java index 992f92b..2323077 100644 --- a/src/main/java/fr/zcraft/zsorter/model/serializer/SorterManagerAdapter.java +++ b/src/main/java/fr/zcraft/zsorter/model/serializer/SorterManagerAdapter.java @@ -44,10 +44,10 @@ public SorterManager deserialize(JsonElement json, Type typeOfT, JsonDeserializa Sorter sorter = iterator.next(); manager.getNameToSorter().putIfAbsent(sorter.getName(), sorter); for(Input input:sorter.getInventoryToInput().values()) { - manager.getInventoryToSorter().putIfAbsent(input.getInventory(), sorter); + manager.getInventoryToSorter().putIfAbsent(input.getHolder(), sorter); } for(Output output:sorter.getInventoryToOutput().values()) { - manager.getInventoryToSorter().putIfAbsent(output.getInventory(), sorter); + manager.getInventoryToSorter().putIfAbsent(output.getHolder(), sorter); } } diff --git a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java index c0c586b..1c209ac 100644 --- a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java +++ b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java @@ -4,6 +4,7 @@ import org.bukkit.inventory.DoubleChestInventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; import fr.zcraft.quartzlib.components.i18n.I; import fr.zcraft.zsorter.ZSorterException; @@ -22,13 +23,12 @@ public class InventoryUtils { * @return The double chest inventory. */ public static Inventory simpleInventoryToDoubleInventory(Inventory inventory) { - InventoryHolder holder = (InventoryHolder) inventory.getLocation().getBlock().getState(); - if(holder.getInventory() instanceof DoubleChestInventory) { - inventory = (DoubleChestInventory) holder.getInventory(); - } - return inventory; + if(inventory instanceof DoubleChestInventory) { + inventory = (DoubleChestInventory) inventory; + } + return inventory; } - + /** * Find the left inventory if the inventory is a double chest inventory.
* Don't do anything if the inventory is not a double chest inventory. @@ -36,24 +36,36 @@ public static Inventory simpleInventoryToDoubleInventory(Inventory inventory) { * @return The left side inventory. */ public static Inventory doubleInventoryToSimpleInventory(Inventory inventory) { - if(inventory instanceof DoubleChestInventory) { - DoubleChestInventory dci = (DoubleChestInventory) inventory; - inventory = dci.getLeftSide(); - } - return inventory; + if(inventory instanceof DoubleChestInventory) { + DoubleChestInventory dci = (DoubleChestInventory) inventory; + inventory = dci.getLeftSide(); + } + return inventory; } - + /** * Find the inventory of a block. * @param block - Block from which get the inventory. * @return The inventory corresponding to the block location. * @throws ZSorterException if the block is not an instance of {@code InventoryHolder}; */ - public static Inventory findInventoryFromBlock(Block block) throws ZSorterException { - if(!(block.getState() instanceof InventoryHolder)) - throw new ZSorterException(I.t("This block must be a hodler.")); + public static InventoryHolder findInventoryFromBlock(Block block) throws ZSorterException { + if(!(block.getState() instanceof InventoryHolder)) + throw new ZSorterException(I.t("This block must be a holder.")); InventoryHolder holder = (InventoryHolder) block.getState(); - return InventoryUtils.doubleInventoryToSimpleInventory(holder.getInventory()); + //holder = InventoryUtils.doubleInventoryToSimpleInventory(holder.getInventory()).getHolder(); + System.out.println("findInventoryFromBlock : " + holder.toString()); + displayInventoryContent(holder.getInventory()); + return holder; + } + + public static void displayInventoryContent(Inventory inventory) { + System.out.println("The content of the inventory " + inventory.toString() + " is : "); + for(ItemStack itemStack: inventory.getContents()) { //For each item in the input inventory + if(itemStack != null) { //If the item is not null + System.out.println(itemStack.getAmount() + " x " + itemStack.getType().toString()); + } + } } } diff --git a/src/test/java/fr/zcraft/zsorter/ZSorterTest.java b/src/test/java/fr/zcraft/zsorter/ZSorterTest.java index 0da437c..8b20854 100644 --- a/src/test/java/fr/zcraft/zsorter/ZSorterTest.java +++ b/src/test/java/fr/zcraft/zsorter/ZSorterTest.java @@ -1,13 +1,12 @@ package fr.zcraft.zsorter; -import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import be.seeseemelk.mockbukkit.MockBukkit; import be.seeseemelk.mockbukkit.ServerMock; -import fr.zcraft.zsorter.ZSorter; import fr.zcraft.zsorter.model.SorterManager; /** @@ -21,7 +20,7 @@ public class ZSorterTest { protected ZSorter plugin; protected SorterManager manager; - protected Inventory inventory0, inventory1, inventory2, inventory3; + protected InventoryHolder inventory0, inventory1, inventory2, inventory3; /** * Load the manager and the inventories. @@ -39,10 +38,10 @@ public void setUp() server = MockBukkit.mock(); plugin = (ZSorter) MockBukkit.load(ZSorter.class); manager = new SorterManager(); - inventory0 = server.createInventory(null, 9); + /*inventory0 = server.createInventory(null, 9); inventory1 = server.createInventory(null, 9); inventory2 = server.createInventory(null, 9); - inventory3 = server.createInventory(null, 9); + inventory3 = server.createInventory(null, 9);*/ } /** diff --git a/src/test/java/fr/zcraft/zsorter/model/SorterTest.java b/src/test/java/fr/zcraft/zsorter/model/SorterTest.java index bfc0aae..7237cdc 100644 --- a/src/test/java/fr/zcraft/zsorter/model/SorterTest.java +++ b/src/test/java/fr/zcraft/zsorter/model/SorterTest.java @@ -131,22 +131,22 @@ public void findOutputsTest() throws ZSorterException { Assert.assertEquals(Arrays.asList(coobleStoneOutput2, coobleStoneOutput1, overflow), sorter.findOutputs(Material.COBBLESTONE)); //Test when the sorter has two output of cobblestone and one overflow - sorter.removeOutput(ironBlockOutput.getInventory()); + sorter.removeOutput(ironBlockOutput.getHolder()); Assert.assertEquals(Arrays.asList(overflow), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(coobleStoneOutput2, coobleStoneOutput1, overflow), sorter.findOutputs(Material.COBBLESTONE)); //Test when the sorter has one output of cobblestone and one overflow - sorter.removeOutput(coobleStoneOutput1.getInventory()); + sorter.removeOutput(coobleStoneOutput1.getHolder()); Assert.assertEquals(Arrays.asList(overflow), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(coobleStoneOutput2, overflow), sorter.findOutputs(Material.COBBLESTONE)); //Test when the sorter has one overflow - sorter.removeOutput(coobleStoneOutput2.getInventory()); + sorter.removeOutput(coobleStoneOutput2.getHolder()); Assert.assertEquals(Arrays.asList(overflow), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(overflow), sorter.findOutputs(Material.COBBLESTONE)); //Test when the sorter has not output - sorter.removeOutput(overflow.getInventory()); + sorter.removeOutput(overflow.getHolder()); Assert.assertEquals(Arrays.asList(), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(), sorter.findOutputs(Material.COBBLESTONE)); } From 20b750d24bc6b9234f80f3f323bfd3b6a92de4b9 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sat, 26 Dec 2020 19:03:08 +0100 Subject: [PATCH 10/21] Test --- .../zcraft/zsorter/events/InventoryEvent.java | 8 +++---- .../zcraft/zsorter/events/ItemMoveEvent.java | 14 +++++------- .../java/fr/zcraft/zsorter/model/Sorter.java | 6 +++-- .../zcraft/zsorter/utils/InventoryUtils.java | 22 +++++++++++-------- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java b/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java index 487822f..8f92ab1 100644 --- a/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java @@ -21,11 +21,9 @@ public class InventoryEvent implements Listener{ */ @EventHandler public void onInventoryCloseEvent(InventoryCloseEvent e) { - if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable - System.out.println("onInventoryCloseEvent : " + e.getInventory().getHolder().toString()); - InventoryUtils.displayInventoryContent(e.getInventory()); - //InventoryHolder inventory = InventoryUtils.doubleInventoryToSimpleInventory(e.getInventory()).getHolder(); //Get the inventory if double chest - ZSorter.getInstance().getSorterManager().computeSorter(e.getInventory().getHolder()); //Try to compute the sorter with this inventory + if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable + InventoryHolder holder = InventoryUtils.doubleHolderToSimpleHolder(e.getInventory().getHolder()); //Get the inventory if double chest + ZSorter.getInstance().getSorterManager().computeSorter(holder); //Try to compute the sorter with this inventory } } } diff --git a/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java b/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java index 866faec..564ba3c 100644 --- a/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java @@ -21,14 +21,12 @@ public class ItemMoveEvent implements Listener{ */ @EventHandler public void onInventoryMoveItem(InventoryMoveItemEvent e) { - if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable - System.out.println("onInventoryMoveItem : " + e.getDestination().getHolder().toString()); - InventoryUtils.displayInventoryContent(e.getDestination()); - //InventoryHolder inputInventory = InventoryUtils.doubleInventoryToSimpleInventory(e.getDestination()).getHolder(); //Get the inventory if double chest - boolean computed = ZSorter.getInstance().getSorterManager().computeSorter(e.getDestination().getHolder()); //Try to compute the sorter with this input - if(!computed) { //If no computed - //InventoryHolder outputInventory = InventoryUtils.doubleInventoryToSimpleInventory(e.getSource()).getHolder(); //Get the inventory if double chest - ZSorter.getInstance().getSorterManager().computeSorter(e.getDestination().getHolder()); //Try to compute the sorter with this output + if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable + InventoryHolder inputInventory = InventoryUtils.doubleHolderToSimpleHolder(e.getDestination().getHolder()); //Get the inventory if double chest + boolean computed = ZSorter.getInstance().getSorterManager().computeSorter(inputInventory); //Try to compute the sorter with this input + if(!computed) { //If no computed + InventoryHolder outputInventory = InventoryUtils.doubleHolderToSimpleHolder(e.getSource().getHolder()); //Get the inventory if double chest + ZSorter.getInstance().getSorterManager().computeSorter(outputInventory); //Try to compute the sorter with this output } } } diff --git a/src/main/java/fr/zcraft/zsorter/model/Sorter.java b/src/main/java/fr/zcraft/zsorter/model/Sorter.java index de19f71..7bc1257 100644 --- a/src/main/java/fr/zcraft/zsorter/model/Sorter.java +++ b/src/main/java/fr/zcraft/zsorter/model/Sorter.java @@ -366,7 +366,8 @@ public boolean hasOverflow() { public void computeSorting() { if(isEnable()) { //If the sorter is ON for(Input input:inputs) { //For each input in the sorter - Inventory inputInventory = input.getHolder().getInventory(); //Get the input inventory + System.out.println("In " + input.getHolder()); + Inventory inputInventory = InventoryUtils.doubleHolderToSimpleHolder(input.getHolder()).getInventory(); for(ItemStack itemStack: inputInventory.getContents()) { //For each item in the input inventory if(itemStack != null) { //If the item is not null ItemStack itemStackToTransfer = itemStack.clone(); //Clone the item to keep the metadata @@ -374,7 +375,8 @@ public void computeSorting() { itemStackToTransfer.setAmount(speed); //Set to the speed limit List outputs = findOutputs(itemStack.getType()); //Find the outputs for this item for(Output output:outputs) { //For each possible output - Inventory outputInventory = output.getHolder().getInventory(); //Get the output inventory + System.out.println("Out " + output.getHolder()); + Inventory outputInventory = InventoryUtils.doubleHolderToSimpleHolder(output.getHolder()).getInventory(); int amountToTransfer = itemStackToTransfer.getAmount(); //Get the amount to transfer HashMap couldntTransferMap = outputInventory.addItem(itemStackToTransfer); //Add the item to the output if(couldntTransferMap.isEmpty()) { //If everything has been transfered diff --git a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java index 1c209ac..6fd81a2 100644 --- a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java +++ b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java @@ -23,9 +23,12 @@ public class InventoryUtils { * @return The double chest inventory. */ public static Inventory simpleInventoryToDoubleInventory(Inventory inventory) { + System.out.println("simpleInventoryToDoubleInventory BEFORE : " + inventory.getHolder()); if(inventory instanceof DoubleChestInventory) { inventory = (DoubleChestInventory) inventory; - } + } + System.out.println("simpleInventoryToDoubleInventory AFTER : " + inventory.getHolder()); + displayInventoryContent(inventory); return inventory; } @@ -35,12 +38,15 @@ public static Inventory simpleInventoryToDoubleInventory(Inventory inventory) { * @param inventory - Inventory to check. * @return The left side inventory. */ - public static Inventory doubleInventoryToSimpleInventory(Inventory inventory) { - if(inventory instanceof DoubleChestInventory) { - DoubleChestInventory dci = (DoubleChestInventory) inventory; - inventory = dci.getLeftSide(); + public static InventoryHolder doubleHolderToSimpleHolder(InventoryHolder holder) { + System.out.println("doubleHolderToSimpleHolder BEFORE : " + holder); + if(holder.getInventory() instanceof DoubleChestInventory) { + DoubleChestInventory dci = (DoubleChestInventory) holder.getInventory(); + holder = dci.getLeftSide().getHolder(); } - return inventory; + System.out.println("doubleHolderToSimpleHolder AFTER : " + holder); + displayInventoryContent(holder.getInventory()); + return holder; } /** @@ -54,9 +60,7 @@ public static InventoryHolder findInventoryFromBlock(Block block) throws ZSorter throw new ZSorterException(I.t("This block must be a holder.")); InventoryHolder holder = (InventoryHolder) block.getState(); - //holder = InventoryUtils.doubleInventoryToSimpleInventory(holder.getInventory()).getHolder(); - System.out.println("findInventoryFromBlock : " + holder.toString()); - displayInventoryContent(holder.getInventory()); + holder = doubleHolderToSimpleHolder(holder); return holder; } From f6d7b93b1281e90ee4c891347549faa52e6abd70 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sat, 26 Dec 2020 20:16:41 +0100 Subject: [PATCH 11/21] Fix bug --- src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java index 6fd81a2..eb8ee57 100644 --- a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java +++ b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java @@ -43,7 +43,8 @@ public static InventoryHolder doubleHolderToSimpleHolder(InventoryHolder holder) if(holder.getInventory() instanceof DoubleChestInventory) { DoubleChestInventory dci = (DoubleChestInventory) holder.getInventory(); holder = dci.getLeftSide().getHolder(); - } + }else + holder = holder.getInventory().getHolder(); System.out.println("doubleHolderToSimpleHolder AFTER : " + holder); displayInventoryContent(holder.getInventory()); return holder; From ee04104cfbaf2241bdeb723e02db8c43d387552d Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sat, 26 Dec 2020 20:38:40 +0100 Subject: [PATCH 12/21] Removed logs --- src/main/java/fr/zcraft/zsorter/ZSorter.java | 1 - .../java/fr/zcraft/zsorter/model/Sorter.java | 9 ++-- .../zcraft/zsorter/utils/InventoryUtils.java | 41 +++---------------- 3 files changed, 8 insertions(+), 43 deletions(-) diff --git a/src/main/java/fr/zcraft/zsorter/ZSorter.java b/src/main/java/fr/zcraft/zsorter/ZSorter.java index 7085796..92865da 100644 --- a/src/main/java/fr/zcraft/zsorter/ZSorter.java +++ b/src/main/java/fr/zcraft/zsorter/ZSorter.java @@ -7,7 +7,6 @@ import java.io.IOException; import org.bukkit.event.Listener; -import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPluginLoader; diff --git a/src/main/java/fr/zcraft/zsorter/model/Sorter.java b/src/main/java/fr/zcraft/zsorter/model/Sorter.java index 7bc1257..3f8e6a1 100644 --- a/src/main/java/fr/zcraft/zsorter/model/Sorter.java +++ b/src/main/java/fr/zcraft/zsorter/model/Sorter.java @@ -23,7 +23,6 @@ import fr.zcraft.zsorter.commands.ToggleCommand; import fr.zcraft.zsorter.commands.UpdateCommand; import fr.zcraft.zsorter.model.serializer.PostProcessAdapterFactory.PostProcessable; -import fr.zcraft.zsorter.utils.InventoryUtils; /** * The class {@code Sorter} represents a sorter in the game.

@@ -366,8 +365,7 @@ public boolean hasOverflow() { public void computeSorting() { if(isEnable()) { //If the sorter is ON for(Input input:inputs) { //For each input in the sorter - System.out.println("In " + input.getHolder()); - Inventory inputInventory = InventoryUtils.doubleHolderToSimpleHolder(input.getHolder()).getInventory(); + Inventory inputInventory = input.getHolder().getInventory().getHolder().getInventory(); for(ItemStack itemStack: inputInventory.getContents()) { //For each item in the input inventory if(itemStack != null) { //If the item is not null ItemStack itemStackToTransfer = itemStack.clone(); //Clone the item to keep the metadata @@ -375,9 +373,8 @@ public void computeSorting() { itemStackToTransfer.setAmount(speed); //Set to the speed limit List outputs = findOutputs(itemStack.getType()); //Find the outputs for this item for(Output output:outputs) { //For each possible output - System.out.println("Out " + output.getHolder()); - Inventory outputInventory = InventoryUtils.doubleHolderToSimpleHolder(output.getHolder()).getInventory(); - int amountToTransfer = itemStackToTransfer.getAmount(); //Get the amount to transfer + Inventory outputInventory = output.getHolder().getInventory().getHolder().getInventory(); + int amountToTransfer = itemStackToTransfer.getAmount(); //Get the amount to transfer HashMap couldntTransferMap = outputInventory.addItem(itemStackToTransfer); //Add the item to the output if(couldntTransferMap.isEmpty()) { //If everything has been transfered diff --git a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java index eb8ee57..c07a957 100644 --- a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java +++ b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java @@ -2,9 +2,7 @@ import org.bukkit.block.Block; import org.bukkit.inventory.DoubleChestInventory; -import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; -import org.bukkit.inventory.ItemStack; import fr.zcraft.quartzlib.components.i18n.I; import fr.zcraft.zsorter.ZSorterException; @@ -17,36 +15,16 @@ public class InventoryUtils { /** - * Find the double chest inventory if the inventory is part of a double chest.
- * Don't do anything if the inventory is not part of a double chest. - * @param inventory - Inventory to check. - * @return The double chest inventory. - */ - public static Inventory simpleInventoryToDoubleInventory(Inventory inventory) { - System.out.println("simpleInventoryToDoubleInventory BEFORE : " + inventory.getHolder()); - if(inventory instanceof DoubleChestInventory) { - inventory = (DoubleChestInventory) inventory; - } - System.out.println("simpleInventoryToDoubleInventory AFTER : " + inventory.getHolder()); - displayInventoryContent(inventory); - return inventory; - } - - /** - * Find the left inventory if the inventory is a double chest inventory.
- * Don't do anything if the inventory is not a double chest inventory. - * @param inventory - Inventory to check. - * @return The left side inventory. + * Find the left holder if the holder is a double chest.
+ * Don't do anything if the holder is not a double chest. + * @param holder - Holder to check. + * @return The left side holder. */ public static InventoryHolder doubleHolderToSimpleHolder(InventoryHolder holder) { - System.out.println("doubleHolderToSimpleHolder BEFORE : " + holder); if(holder.getInventory() instanceof DoubleChestInventory) { DoubleChestInventory dci = (DoubleChestInventory) holder.getInventory(); holder = dci.getLeftSide().getHolder(); - }else - holder = holder.getInventory().getHolder(); - System.out.println("doubleHolderToSimpleHolder AFTER : " + holder); - displayInventoryContent(holder.getInventory()); + } return holder; } @@ -64,13 +42,4 @@ public static InventoryHolder findInventoryFromBlock(Block block) throws ZSorter holder = doubleHolderToSimpleHolder(holder); return holder; } - - public static void displayInventoryContent(Inventory inventory) { - System.out.println("The content of the inventory " + inventory.toString() + " is : "); - for(ItemStack itemStack: inventory.getContents()) { //For each item in the input inventory - if(itemStack != null) { //If the item is not null - System.out.println(itemStack.getAmount() + " x " + itemStack.getType().toString()); - } - } - } } From 9bed8ff0ffed6f4286e7ef0937786e15c00e0504 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sat, 26 Dec 2020 20:38:40 +0100 Subject: [PATCH 13/21] Removed logs From 44a91c982de7c9ef6d46f611b40616206fe3bab7 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sat, 26 Dec 2020 22:00:56 +0100 Subject: [PATCH 14/21] Update the full status on events --- .../fr/zcraft/zsorter/model/SorterManager.java | 7 ++++++- .../fr/zcraft/zsorter/utils/InventoryUtils.java | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/fr/zcraft/zsorter/model/SorterManager.java b/src/main/java/fr/zcraft/zsorter/model/SorterManager.java index f05f89a..2bc3160 100644 --- a/src/main/java/fr/zcraft/zsorter/model/SorterManager.java +++ b/src/main/java/fr/zcraft/zsorter/model/SorterManager.java @@ -14,6 +14,7 @@ import fr.zcraft.quartzlib.components.i18n.I; import fr.zcraft.zsorter.ZSorterException; import fr.zcraft.zsorter.tasks.SortTask; +import fr.zcraft.zsorter.utils.InventoryUtils; /** * The class {@code SorterManager} is used to manage sorters. @@ -213,7 +214,11 @@ public boolean computeSorter(InventoryHolder inventory) { .stream() .filter(sorter.getCloggingUpMaterials()::contains) .count() > 0; - + + boolean realState = InventoryUtils.isFull(output.getHolder()); + if(output.isFull() != realState) + output.setFull(realState); + if(clogging || output.isOverflow()) { //If one of the output material was clogging up the inputs or if it is an overflow sorter.setToCompute(true); //Set the sorter to compute SortTask.getInstance().start(); //Start the task diff --git a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java index c07a957..8082d49 100644 --- a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java +++ b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java @@ -2,7 +2,9 @@ import org.bukkit.block.Block; import org.bukkit.inventory.DoubleChestInventory; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; import fr.zcraft.quartzlib.components.i18n.I; import fr.zcraft.zsorter.ZSorterException; @@ -42,4 +44,18 @@ public static InventoryHolder findInventoryFromBlock(Block block) throws ZSorter holder = doubleHolderToSimpleHolder(holder); return holder; } + + /** + * Checks whether a holder is full. + * @param holder - Holder to check. + * @return {@code true} if the holder is full, {@code false} if it contains at least an empty slot. + */ + public static boolean isFull(InventoryHolder holder) { + Inventory inventory = holder.getInventory().getHolder().getInventory(); + for(ItemStack itemStack: inventory.getContents()) { //For each item in the input inventory + if(itemStack == null) + return false; + } + return true; + } } From b00cfb6e7cc209e8fb1ffa4ebdaf1ddb164ba363 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sat, 26 Dec 2020 22:01:01 +0100 Subject: [PATCH 15/21] Fix tabs --- src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java | 2 +- src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java b/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java index 8f92ab1..306b82c 100644 --- a/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java @@ -22,7 +22,7 @@ public class InventoryEvent implements Listener{ @EventHandler public void onInventoryCloseEvent(InventoryCloseEvent e) { if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable - InventoryHolder holder = InventoryUtils.doubleHolderToSimpleHolder(e.getInventory().getHolder()); //Get the inventory if double chest + InventoryHolder holder = InventoryUtils.doubleHolderToSimpleHolder(e.getInventory().getHolder()); //Get the inventory if double chest ZSorter.getInstance().getSorterManager().computeSorter(holder); //Try to compute the sorter with this inventory } } diff --git a/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java b/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java index 564ba3c..81aacaf 100644 --- a/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java @@ -22,10 +22,10 @@ public class ItemMoveEvent implements Listener{ @EventHandler public void onInventoryMoveItem(InventoryMoveItemEvent e) { if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable - InventoryHolder inputInventory = InventoryUtils.doubleHolderToSimpleHolder(e.getDestination().getHolder()); //Get the inventory if double chest + InventoryHolder inputInventory = InventoryUtils.doubleHolderToSimpleHolder(e.getDestination().getHolder()); //Get the inventory if double chest boolean computed = ZSorter.getInstance().getSorterManager().computeSorter(inputInventory); //Try to compute the sorter with this input if(!computed) { //If no computed - InventoryHolder outputInventory = InventoryUtils.doubleHolderToSimpleHolder(e.getSource().getHolder()); //Get the inventory if double chest + InventoryHolder outputInventory = InventoryUtils.doubleHolderToSimpleHolder(e.getSource().getHolder()); //Get the inventory if double chest ZSorter.getInstance().getSorterManager().computeSorter(outputInventory); //Try to compute the sorter with this output } } From 1ec55f18dddc81284308ff095e2fc130e7d86cbb Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sat, 26 Dec 2020 23:42:42 +0100 Subject: [PATCH 16/21] Fix bug When an output is connected to a hopper and the speed is more than 2, the items that couldn't fit in where stored in overflow. --- src/main/java/fr/zcraft/zsorter/model/Sorter.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/zcraft/zsorter/model/Sorter.java b/src/main/java/fr/zcraft/zsorter/model/Sorter.java index 3f8e6a1..c952343 100644 --- a/src/main/java/fr/zcraft/zsorter/model/Sorter.java +++ b/src/main/java/fr/zcraft/zsorter/model/Sorter.java @@ -392,13 +392,17 @@ public void computeSorting() { //Run only if the output is full + if(amountToTransfer != itemStackToTransfer.getAmount()) { //If partially moved + ItemStack itemStackToRemove = itemStackToTransfer.clone(); //Create the stack to remove + itemStackToRemove.setAmount(amountToTransfer - itemStackToTransfer.getAmount()); //Set the amount to remove + inputInventory.removeItem(itemStackToRemove); //Remove the item from the input + return; + } + if(!output.isFull()) //If the output was not full output.setFull(true); //Now is it's full - ItemStack itemStackToRemove = itemStackToTransfer.clone(); //Create the stack to remove - itemStackToRemove.setAmount(amountToTransfer - itemStackToTransfer.getAmount()); //Set the amount to remove - inputInventory.removeItem(itemStackToRemove); //Remove the item from the input - itemStackToTransfer.setAmount(itemStackToTransfer.getAmount()); //Define the new amount to transfer + itemStackToTransfer.setAmount(itemStackToTransfer.getAmount()); //Define the new amount to transfer*/ } //Run only if this item is clogging up. From 9c7b109dc5c3700f9836e842269de3d550e153a0 Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sun, 27 Dec 2020 00:21:42 +0100 Subject: [PATCH 17/21] Change the output flag on itemMoveEvent for hopper storages --- .../java/fr/zcraft/zsorter/events/InventoryEvent.java | 2 +- .../java/fr/zcraft/zsorter/events/ItemMoveEvent.java | 4 ++-- .../java/fr/zcraft/zsorter/model/SorterManager.java | 10 ++++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java b/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java index 306b82c..a785b68 100644 --- a/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java @@ -23,7 +23,7 @@ public class InventoryEvent implements Listener{ public void onInventoryCloseEvent(InventoryCloseEvent e) { if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable InventoryHolder holder = InventoryUtils.doubleHolderToSimpleHolder(e.getInventory().getHolder()); //Get the inventory if double chest - ZSorter.getInstance().getSorterManager().computeSorter(holder); //Try to compute the sorter with this inventory + ZSorter.getInstance().getSorterManager().computeSorter(holder, true); //Try to compute the sorter with this inventory } } } diff --git a/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java b/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java index 81aacaf..1bd0b53 100644 --- a/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java @@ -23,10 +23,10 @@ public class ItemMoveEvent implements Listener{ public void onInventoryMoveItem(InventoryMoveItemEvent e) { if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable InventoryHolder inputInventory = InventoryUtils.doubleHolderToSimpleHolder(e.getDestination().getHolder()); //Get the inventory if double chest - boolean computed = ZSorter.getInstance().getSorterManager().computeSorter(inputInventory); //Try to compute the sorter with this input + boolean computed = ZSorter.getInstance().getSorterManager().computeSorter(inputInventory, true); //Try to compute the sorter with this input if(!computed) { //If no computed InventoryHolder outputInventory = InventoryUtils.doubleHolderToSimpleHolder(e.getSource().getHolder()); //Get the inventory if double chest - ZSorter.getInstance().getSorterManager().computeSorter(outputInventory); //Try to compute the sorter with this output + ZSorter.getInstance().getSorterManager().computeSorter(outputInventory, false); //Try to compute the sorter with this output } } } diff --git a/src/main/java/fr/zcraft/zsorter/model/SorterManager.java b/src/main/java/fr/zcraft/zsorter/model/SorterManager.java index 2bc3160..fb04561 100644 --- a/src/main/java/fr/zcraft/zsorter/model/SorterManager.java +++ b/src/main/java/fr/zcraft/zsorter/model/SorterManager.java @@ -194,9 +194,10 @@ public List canCompute(){ * Compute the sorter associated with this inventory. * Don't do anything if the inventory is not an input or an output. * @param inventory - Inventory of the sorter to compute. + * @param checkContent - Defines the rule to apply for the output full flag. {@code true} to define the flag regarding the output content, {@code false} to set it to {@code false} anyway. * @return {@code true} if the sorter has been computed, {@code false} otherwise. */ - public boolean computeSorter(InventoryHolder inventory) { + public boolean computeSorter(InventoryHolder inventory, boolean checkContent) { boolean computed = false; Sorter sorter = inventoryToSorter.get(inventory); //Get the sorter associated with this inventory if(sorter != null && sorter.isEnable()) { //If sorter found and enable @@ -215,9 +216,10 @@ public boolean computeSorter(InventoryHolder inventory) { .filter(sorter.getCloggingUpMaterials()::contains) .count() > 0; - boolean realState = InventoryUtils.isFull(output.getHolder()); - if(output.isFull() != realState) - output.setFull(realState); + boolean state = false; + if(checkContent) + state = InventoryUtils.isFull(output.getHolder()); + output.setFull(state); if(clogging || output.isOverflow()) { //If one of the output material was clogging up the inputs or if it is an overflow sorter.setToCompute(true); //Set the sorter to compute From 846890ac43b7ecbeda5f1aaeda254265d58df2ea Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sun, 27 Dec 2020 00:25:43 +0100 Subject: [PATCH 18/21] Useless line --- src/main/java/fr/zcraft/zsorter/model/Sorter.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/fr/zcraft/zsorter/model/Sorter.java b/src/main/java/fr/zcraft/zsorter/model/Sorter.java index c952343..e89547d 100644 --- a/src/main/java/fr/zcraft/zsorter/model/Sorter.java +++ b/src/main/java/fr/zcraft/zsorter/model/Sorter.java @@ -401,9 +401,7 @@ public void computeSorting() { if(!output.isFull()) //If the output was not full output.setFull(true); //Now is it's full - - itemStackToTransfer.setAmount(itemStackToTransfer.getAmount()); //Define the new amount to transfer*/ - } + } //Run only if this item is clogging up. From 7f9914202a774022e2b3e221a04317fa9077bccf Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sun, 27 Dec 2020 00:46:23 +0100 Subject: [PATCH 19/21] Replaced inventory variables by holder variables --- src/main/java/fr/zcraft/zsorter/ZSorter.java | 6 +- .../zsorter/events/HolderBreakEvent.java | 2 +- .../zcraft/zsorter/events/InventoryEvent.java | 4 +- .../zcraft/zsorter/events/ItemMoveEvent.java | 4 +- .../zcraft/zsorter/events/LeftClickEvent.java | 6 +- .../java/fr/zcraft/zsorter/model/Input.java | 6 +- .../java/fr/zcraft/zsorter/model/Output.java | 6 +- .../java/fr/zcraft/zsorter/model/Sorter.java | 80 +++++++++---------- .../zcraft/zsorter/model/SorterManager.java | 70 ++++++++-------- ...apter.java => InventoryHolderAdapter.java} | 2 +- .../zcraft/zsorter/utils/InventoryUtils.java | 6 +- .../zsorter/model/SorterManagerTest.java | 4 +- 12 files changed, 98 insertions(+), 98 deletions(-) rename src/main/java/fr/zcraft/zsorter/model/serializer/{InventoryAdapter.java => InventoryHolderAdapter.java} (93%) diff --git a/src/main/java/fr/zcraft/zsorter/ZSorter.java b/src/main/java/fr/zcraft/zsorter/ZSorter.java index 92865da..7cd3474 100644 --- a/src/main/java/fr/zcraft/zsorter/ZSorter.java +++ b/src/main/java/fr/zcraft/zsorter/ZSorter.java @@ -36,7 +36,7 @@ import fr.zcraft.zsorter.events.ItemMoveEvent; import fr.zcraft.zsorter.events.LeftClickEvent; import fr.zcraft.zsorter.model.SorterManager; -import fr.zcraft.zsorter.model.serializer.InventoryAdapter; +import fr.zcraft.zsorter.model.serializer.InventoryHolderAdapter; import fr.zcraft.zsorter.model.serializer.PostProcessAdapterFactory; import fr.zcraft.zsorter.model.serializer.SorterManagerAdapter; import fr.zcraft.zsorter.tasks.SortTask; @@ -159,7 +159,7 @@ private void save() { try { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(new PostProcessAdapterFactory()); - gsonBuilder.registerTypeHierarchyAdapter(InventoryHolder.class, new InventoryAdapter()); + gsonBuilder.registerTypeHierarchyAdapter(InventoryHolder.class, new InventoryHolderAdapter()); gsonBuilder.registerTypeAdapter(SorterManager.class, new SorterManagerAdapter()); Gson customGson = gsonBuilder.create(); FileWriter fr = new FileWriter(dataPath); @@ -180,7 +180,7 @@ private boolean load() { try { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(new PostProcessAdapterFactory()); - gsonBuilder.registerTypeHierarchyAdapter(InventoryHolder.class, new InventoryAdapter()); + gsonBuilder.registerTypeHierarchyAdapter(InventoryHolder.class, new InventoryHolderAdapter()); gsonBuilder.registerTypeAdapter(SorterManager.class, new SorterManagerAdapter()); Gson customGson = gsonBuilder.create(); BufferedReader br = new BufferedReader(new FileReader(dataFile)); diff --git a/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java b/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java index 00e1a2d..019722c 100644 --- a/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java @@ -27,7 +27,7 @@ public class HolderBreakEvent implements Listener{ public void onBlockBreak(BlockBreakEvent e) { if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable try { - InventoryHolder inventory = InventoryUtils.findInventoryFromBlock(e.getBlock()); //Get the inventory + InventoryHolder inventory = InventoryUtils.findInventoryFromBlock(e.getBlock()); //Get the holder Sorter sorter = ZSorter.getInstance().getSorterManager().getInventoryToSorter().get(inventory); //Get the associated sorter if(sorter != null) { //If a sorter has been found if(sorter.removeInput(inventory) != null){ //Try to remove the input inventory diff --git a/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java b/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java index a785b68..c63a908 100644 --- a/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java @@ -22,8 +22,8 @@ public class InventoryEvent implements Listener{ @EventHandler public void onInventoryCloseEvent(InventoryCloseEvent e) { if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable - InventoryHolder holder = InventoryUtils.doubleHolderToSimpleHolder(e.getInventory().getHolder()); //Get the inventory if double chest - ZSorter.getInstance().getSorterManager().computeSorter(holder, true); //Try to compute the sorter with this inventory + InventoryHolder holder = InventoryUtils.doubleHolderToSimpleHolder(e.getInventory().getHolder()); //Get the holder if double chest + ZSorter.getInstance().getSorterManager().computeSorter(holder, true); //Try to compute the sorter with this holder } } } diff --git a/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java b/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java index 1bd0b53..2124d31 100644 --- a/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java @@ -22,10 +22,10 @@ public class ItemMoveEvent implements Listener{ @EventHandler public void onInventoryMoveItem(InventoryMoveItemEvent e) { if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable - InventoryHolder inputInventory = InventoryUtils.doubleHolderToSimpleHolder(e.getDestination().getHolder()); //Get the inventory if double chest + InventoryHolder inputInventory = InventoryUtils.doubleHolderToSimpleHolder(e.getDestination().getHolder()); //Get the holder if double chest boolean computed = ZSorter.getInstance().getSorterManager().computeSorter(inputInventory, true); //Try to compute the sorter with this input if(!computed) { //If no computed - InventoryHolder outputInventory = InventoryUtils.doubleHolderToSimpleHolder(e.getSource().getHolder()); //Get the inventory if double chest + InventoryHolder outputInventory = InventoryUtils.doubleHolderToSimpleHolder(e.getSource().getHolder()); //Get the holder if double chest ZSorter.getInstance().getSorterManager().computeSorter(outputInventory, false); //Try to compute the sorter with this output } } diff --git a/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java b/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java index ae61e52..ca422d4 100644 --- a/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java @@ -27,16 +27,16 @@ public class LeftClickEvent implements Listener{ */ @EventHandler public void onPlayerInteractEvent(PlayerInteractEvent e) { - if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable + if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable if(e.getAction() == Action.LEFT_CLICK_BLOCK) { //The player left click on a block Sorter sorter = ZSorter.getInstance().getSorterManager().getPlayerToSorter().get(e.getPlayer()); //Get the sorter the player if(sorter != null){ //If sorter found int priority = 1; //Defines the priority of the output try { - InventoryHolder inventory = InventoryUtils.findInventoryFromBlock(e.getClickedBlock()); + InventoryHolder holder = InventoryUtils.findInventoryFromBlock(e.getClickedBlock()); //Try to add the output to the sorter - ZSorter.getInstance().getSorterManager().setOutput(sorter.getName(), inventory, priority, Arrays.asList(e.getItem().getType())); + ZSorter.getInstance().getSorterManager().setOutput(sorter.getName(), holder, priority, Arrays.asList(e.getItem().getType())); e.getPlayer().sendMessage(ChatColor.GREEN + I.t("This holder is now an output of priority {0}.", priority)); e.setCancelled(true); } diff --git a/src/main/java/fr/zcraft/zsorter/model/Input.java b/src/main/java/fr/zcraft/zsorter/model/Input.java index 0a398b3..4c75067 100644 --- a/src/main/java/fr/zcraft/zsorter/model/Input.java +++ b/src/main/java/fr/zcraft/zsorter/model/Input.java @@ -23,11 +23,11 @@ public class Input extends InputOutput implements Serializable{ /** * Constructor of an output object. - * @param inventory - Inventory of the output. + * @param holder - Holder of the output. * @param priority - Priority of the output. */ - public Input(InventoryHolder inventory, Integer priority) { - super(inventory, priority); + public Input(InventoryHolder holder, Integer priority) { + super(holder, priority); this.cloggedUp = false; } diff --git a/src/main/java/fr/zcraft/zsorter/model/Output.java b/src/main/java/fr/zcraft/zsorter/model/Output.java index 13055e0..d2c4003 100644 --- a/src/main/java/fr/zcraft/zsorter/model/Output.java +++ b/src/main/java/fr/zcraft/zsorter/model/Output.java @@ -32,11 +32,11 @@ public class Output extends InputOutput implements Serializable{ /** * Constructor of an output object. - * @param inventory - Inventory of the output. + * @param holder - Holder of the output. * @param priority - Priority of the output. */ - public Output(InventoryHolder inventory, Integer priority) { - super(inventory, priority); + public Output(InventoryHolder holder, Integer priority) { + super(holder, priority); this.materials = new ArrayList(); this.full = false; } diff --git a/src/main/java/fr/zcraft/zsorter/model/Sorter.java b/src/main/java/fr/zcraft/zsorter/model/Sorter.java index e89547d..a1a4353 100644 --- a/src/main/java/fr/zcraft/zsorter/model/Sorter.java +++ b/src/main/java/fr/zcraft/zsorter/model/Sorter.java @@ -48,8 +48,8 @@ public class Sorter implements Serializable, PostProcessable{ private transient boolean toCompute; private int speed; - private transient Map inventoryToInput; - private transient Map inventoryToOutput; + private transient Map holderToInput; + private transient Map holderToOutput; private transient Map> materialToOutputs; private transient List overflows; @@ -78,8 +78,8 @@ public Sorter(String name, String description) { this.toCompute = false; this.speed = DEFAULT_SPEED; - this.inventoryToInput = new HashMap(); - this.inventoryToOutput = new HashMap(); + this.holderToInput = new HashMap(); + this.holderToOutput = new HashMap(); this.materialToOutputs = new TreeMap>(); this.overflows = new ArrayList(); @@ -177,7 +177,7 @@ public void setSpeed(int speed) { * @return The sorter inputs. */ public Map getInventoryToInput() { - return inventoryToInput; + return holderToInput; } /** @@ -187,7 +187,7 @@ public Map getInventoryToInput() { * @return The sorter outputs. */ public Map getInventoryToOutput() { - return inventoryToOutput; + return holderToOutput; } /** @@ -226,17 +226,17 @@ public List getCloggingUpMaterials() { * Sorts the input outputs by order of priority. */ public void commit() { - inputs = inventoryToInput.values().stream().collect(Collectors.toList()); + inputs = holderToInput.values().stream().collect(Collectors.toList()); Collections.sort(inputs); //Sort the inputs - outputs = inventoryToOutput.values().stream().collect(Collectors.toList()); + outputs = holderToOutput.values().stream().collect(Collectors.toList()); Collections.sort(outputs); //Sort the outputs - overflows = inventoryToOutput.values().stream().filter(o -> o.isOverflow()).collect(Collectors.toList()); + overflows = holderToOutput.values().stream().filter(o -> o.isOverflow()).collect(Collectors.toList()); Collections.sort(overflows); //Sort the overflows materialToOutputs = new HashMap>(); - for(Output output:inventoryToOutput.values()) { //For each output + for(Output output:holderToOutput.values()) { //For each output for(Material material:output.getMaterials()) { //For each material List possibleOutputs = materialToOutputs.get(material); //Get the possible outputs for the given material if(possibleOutputs == null) { //If none have been found @@ -252,22 +252,22 @@ public void commit() { } /** - * Sets the inventory has an input.

+ * Sets the holder has an input.

* If the input already exists, the priority is updated. - * @param inventory - Inventory of the input. + * @param holder - Holder of the input. * @param priority - Priority of the input. * @return The created input object. * @throws ZSorterException if a ZSorter exception occurs. */ - public Input setInput(InventoryHolder inventory, int priority) throws ZSorterException { - Output output = inventoryToOutput.get(inventory); //Get the existing output + public Input setInput(InventoryHolder holder, int priority) throws ZSorterException { + Output output = holderToOutput.get(holder); //Get the existing output if(output != null) //If exists throw new ZSorterException(I.t("This holder is already an output.")); //Display error message - Input existingInput = inventoryToInput.get(inventory); //Get the existing input + Input existingInput = holderToInput.get(holder); //Get the existing input if(existingInput == null) { //If no input exists - existingInput = new Input(inventory, priority); //Create a new input - inventoryToInput.put(inventory, existingInput); //Add the new input + existingInput = new Input(holder, priority); //Create a new input + holderToInput.put(holder, existingInput); //Add the new input } else { //If the input exists existingInput.setPriority(priority); //Set the new priority @@ -278,35 +278,35 @@ public Input setInput(InventoryHolder inventory, int priority) throws ZSorterExc /** * Remove an input from a sorter. - * @param inventory - Inventory of the input. - * @return The removed input object, {@code null} if no input found for this inventory. + * @param holder - Holder of the input. + * @return The removed input object, {@code null} if no input found for this holder. */ - public Input removeInput(InventoryHolder inventory) { - Input result = inventoryToInput.remove(inventory); + public Input removeInput(InventoryHolder holder) { + Input result = holderToInput.remove(holder); if(result != null) commit(); return result; } /** - * Sets the inventory has an output.

+ * Sets the holder has an output.

* If the output already exists, the priority and the materials are updated. - * @param inventory - Inventory of the output. + * @param holder - Holder of the output. * @param priority - Priority of the output. * @param materials - Sorted materials of the output. * @return The created output object. * @throws ZSorterException if a ZSorter exception occurs. */ - public Output setOutput(InventoryHolder inventory, int priority, List materials) throws ZSorterException { - Input input = inventoryToInput.get(inventory); //Get the existing input + public Output setOutput(InventoryHolder holder, int priority, List materials) throws ZSorterException { + Input input = holderToInput.get(holder); //Get the existing input if(input != null) //If exists throw new ZSorterException(I.t("This holder is already an input.")); //Display error message - Output existingOutput = inventoryToOutput.get(inventory); //Get the existing output + Output existingOutput = holderToOutput.get(holder); //Get the existing output if(existingOutput == null) { //If no existing output - existingOutput = new Output(inventory, priority); //Create a new output + existingOutput = new Output(holder, priority); //Create a new output existingOutput.setMaterials(materials); //Add the materials - inventoryToOutput.put(inventory, existingOutput); //Add the new output + holderToOutput.put(holder, existingOutput); //Add the new output } else { //If the output exists existingOutput.setPriority(priority); //Set the new priority @@ -318,11 +318,11 @@ public Output setOutput(InventoryHolder inventory, int priority, List /** * Remove an output from a sorter. - * @param inventory - Inventory of the output. - * @return The removed output object, {@code null} if no output found at this inventory. + * @param holder - Holder of the output. + * @return The removed output object, {@code null} if no output found at this holder. */ - public Output removeOutput(InventoryHolder inventory) { - Output result = inventoryToOutput.remove(inventory); + public Output removeOutput(InventoryHolder holder) { + Output result = holderToOutput.remove(holder); if(result != null) commit(); return result; @@ -445,10 +445,10 @@ public RawText toRawText(DisplayMode mode) { .hover(new RawText() .then(I.t("Change the sorting speed"))) .suggest(SpeedCommand.class, name) - .then("\n " + I.t("{0} input(s):", inventoryToInput.size()) + "\n ") + .then("\n " + I.t("{0} input(s):", holderToInput.size()) + "\n ") .color(ChatColor.GRAY); - List inputs = inventoryToInput + List inputs = holderToInput .values() .stream() .sorted() @@ -482,7 +482,7 @@ public RawText toRawText(DisplayMode mode) { //if display by output if(mode == DisplayMode.OUTPUTS) { - List outputs = inventoryToOutput + List outputs = holderToOutput .values() .stream() .filter(o -> !o.isOverflow()) @@ -522,7 +522,7 @@ public RawText toRawText(DisplayMode mode) { } //If display by items else if(mode == DisplayMode.ITEMS){ - List sortedMaterials = inventoryToOutput + List sortedMaterials = holderToOutput .values() .stream() .filter(o -> !o.isOverflow()) @@ -594,13 +594,13 @@ public boolean equals(Object obj) { @Override public void postProcess() { - inventoryToInput = new HashMap(); + holderToInput = new HashMap(); for(Input input:inputs) { - inventoryToInput.putIfAbsent(input.getHolder(), input); + holderToInput.putIfAbsent(input.getHolder(), input); } - inventoryToOutput = new HashMap(); + holderToOutput = new HashMap(); for(Output output:outputs) { - inventoryToOutput.putIfAbsent(output.getHolder(), output); + holderToOutput.putIfAbsent(output.getHolder(), output); } commit(); if(enable) diff --git a/src/main/java/fr/zcraft/zsorter/model/SorterManager.java b/src/main/java/fr/zcraft/zsorter/model/SorterManager.java index fb04561..2d421a8 100644 --- a/src/main/java/fr/zcraft/zsorter/model/SorterManager.java +++ b/src/main/java/fr/zcraft/zsorter/model/SorterManager.java @@ -28,14 +28,14 @@ public class SorterManager implements Serializable{ private static final long serialVersionUID = -1782855927147248287L; private Map nameToSorter; - private transient Map inventoryToSorter; + private transient Map holderToSorter; private transient Map playerToSorter; /** * Constructor of a sorter manager object. */ public SorterManager() { - this.inventoryToSorter = new HashMap(); + this.holderToSorter = new HashMap(); this.nameToSorter = new TreeMap(); this.playerToSorter = new HashMap(); } @@ -57,13 +57,13 @@ public Map getPlayerToSorter() { } /** - * Returns the map linking a inventory to a sorter.

+ * Returns the map linking a holder to a sorter.

* Do not use this method if you need to add or remove a sorter. * Use the {@code addSorter} and {@code deleteSorter} methods instead. * @return The sorters of the plugin. */ public Map getInventoryToSorter() { - return inventoryToSorter; + return holderToSorter; } /** @@ -93,88 +93,88 @@ public Sorter deleteSorter(String name) throws ZSorterException { throw new ZSorterException(I.t("There is no sorter with this name.")); //Display error message for(Input input:sorter.getInventoryToInput().values()) //For each input of the sorter - inventoryToSorter.remove(input.getHolder()); //Remove the input from the inventory to sorter map + holderToSorter.remove(input.getHolder()); //Remove the input from the holder to sorter map for(Input input:sorter.getInventoryToInput().values()) //For each ouput of the sorter - inventoryToSorter.remove(input.getHolder()); //Remove the output from the inventory to sorter map + holderToSorter.remove(input.getHolder()); //Remove the output from the holder to sorter map return sorter; } /** * Sets a new sorter input. * @param name - Name of the sorter. - * @param inventory - Input inventory. + * @param holder - Input holder. * @param priority - Priority of the input. * @throws ZSorterException if a ZSorter exception occurs. */ - public void setInput(String name, InventoryHolder inventory, int priority) throws ZSorterException { + public void setInput(String name, InventoryHolder holder, int priority) throws ZSorterException { Sorter sorter = nameToSorter.get(name); if(sorter == null) throw new ZSorterException(I.t("There is no sorter with this name.")); - Sorter existingSorter = inventoryToSorter.putIfAbsent(inventory, sorter); //Get the sorter with this input + Sorter existingSorter = holderToSorter.putIfAbsent(holder, sorter); //Get the sorter with this input if(existingSorter != null && !sorter.equals(existingSorter)) //If the sorter is not this one throw new ZSorterException(I.t("This holder is already in use by the sorter {0}.", sorter.getName())); //Display error messsage - sorter.setInput(inventory, priority); + sorter.setInput(holder, priority); } /** * Remove an input from a sorter. * @param name - Name of the sorter. - * @param inventory - Inventory of the input. - * @return The removed input object, {@code null} if no input found for this inventory. + * @param holder - Holder of the input. + * @return The removed input object, {@code null} if no input found for this holder. * @throws ZSorterException if a ZSorter exception occurs. */ - public Input removeInput(String name, InventoryHolder inventory) throws ZSorterException { + public Input removeInput(String name, InventoryHolder holder) throws ZSorterException { Sorter sorter = nameToSorter.get(name); if(sorter == null) throw new ZSorterException(I.t("There is no sorter with this name.")); - Input input = sorter.removeInput(inventory); + Input input = sorter.removeInput(holder); if(input == null) throw new ZSorterException(I.t("This holder is not an input.")); - inventoryToSorter.remove(inventory); //Unkink the sorter + holderToSorter.remove(holder); //Unkink the sorter return input; } /** * Sets a new sorter output. * @param name - Name of the sorter. - * @param inventory - Output inventory. + * @param holder - Output holder. * @param priority - Priority of the output. * @param materials - Materials of the output. * @throws ZSorterException if a ZSorter exception occurs. */ - public void setOutput(String name, InventoryHolder inventory, int priority, List materials) throws ZSorterException { + public void setOutput(String name, InventoryHolder holder, int priority, List materials) throws ZSorterException { Sorter sorter = nameToSorter.get(name); if(sorter == null) throw new ZSorterException(I.t("There is no sorter with this name.")); - Sorter existingSorter = inventoryToSorter.putIfAbsent(inventory, sorter); //Get the sorter with this input + Sorter existingSorter = holderToSorter.putIfAbsent(holder, sorter); //Get the sorter with this input if(existingSorter != null && !sorter.equals(existingSorter)) //If the sorter is not this one throw new ZSorterException(I.t("This holder is already in use by the sorter {0}.", sorter.getName())); //Display error messsage - sorter.setOutput(inventory, priority, materials); + sorter.setOutput(holder, priority, materials); } /** * Remove an output from a sorter. * @param name - Name of the sorter. - * @param inventory - Inventory of the output. - * @return The removed output object, {@code null} if no output found for this inventory. + * @param holder - Holder of the output. + * @return The removed output object, {@code null} if no output found for this holder. * @throws ZSorterException if a ZSorter exception occurs. */ - public Output removeOutput(String name, InventoryHolder inventory) throws ZSorterException { + public Output removeOutput(String name, InventoryHolder holder) throws ZSorterException { Sorter sorter = nameToSorter.get(name); if(sorter == null) throw new ZSorterException(I.t("There is no sorter with this name.")); - Output output = sorter.removeOutput(inventory); + Output output = sorter.removeOutput(holder); if(output == null) throw new ZSorterException(I.t("This holder is not an output.")); - inventoryToSorter.remove(inventory); //Unkink the sorter + holderToSorter.remove(holder); //Unkink the sorter return output; } @@ -191,24 +191,24 @@ public List canCompute(){ } /** - * Compute the sorter associated with this inventory. - * Don't do anything if the inventory is not an input or an output. - * @param inventory - Inventory of the sorter to compute. + * Compute the sorter associated with this holder. + * Don't do anything if the holder is not an input or an output. + * @param holder - Holder of the sorter to compute. * @param checkContent - Defines the rule to apply for the output full flag. {@code true} to define the flag regarding the output content, {@code false} to set it to {@code false} anyway. * @return {@code true} if the sorter has been computed, {@code false} otherwise. */ - public boolean computeSorter(InventoryHolder inventory, boolean checkContent) { + public boolean computeSorter(InventoryHolder holder, boolean checkContent) { boolean computed = false; - Sorter sorter = inventoryToSorter.get(inventory); //Get the sorter associated with this inventory + Sorter sorter = holderToSorter.get(holder); //Get the sorter associated with this holder if(sorter != null && sorter.isEnable()) { //If sorter found and enable - Input input = sorter.getInventoryToInput().get(inventory); //Get the input linked to this inventory + Input input = sorter.getInventoryToInput().get(holder); //Get the input linked to this holder if(input != null) { //If input found sorter.setToCompute(true); //Set the sorter to compute SortTask.getInstance().start(); //Start the task computed = true; } else { - Output output = sorter.getInventoryToOutput().get(inventory); //Get the output linked to this inventory + Output output = sorter.getInventoryToOutput().get(holder); //Get the output linked to this holder if(output != null) { //If output found boolean clogging = output.getMaterials() @@ -236,7 +236,7 @@ public boolean computeSorter(InventoryHolder inventory, boolean checkContent) { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + ((inventoryToSorter == null) ? 0 : inventoryToSorter.hashCode()); + result = prime * result + ((holderToSorter == null) ? 0 : holderToSorter.hashCode()); result = prime * result + ((nameToSorter == null) ? 0 : nameToSorter.hashCode()); return result; } @@ -250,10 +250,10 @@ public boolean equals(Object obj) { if (getClass() != obj.getClass()) return false; SorterManager other = (SorterManager) obj; - if (inventoryToSorter == null) { - if (other.inventoryToSorter != null) + if (holderToSorter == null) { + if (other.holderToSorter != null) return false; - } else if (!inventoryToSorter.equals(other.inventoryToSorter)) + } else if (!holderToSorter.equals(other.holderToSorter)) return false; if (nameToSorter == null) { if (other.nameToSorter != null) diff --git a/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryAdapter.java b/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryHolderAdapter.java similarity index 93% rename from src/main/java/fr/zcraft/zsorter/model/serializer/InventoryAdapter.java rename to src/main/java/fr/zcraft/zsorter/model/serializer/InventoryHolderAdapter.java index 32e0e6d..be05ebf 100644 --- a/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryAdapter.java +++ b/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryHolderAdapter.java @@ -23,7 +23,7 @@ * @author Lucas * */ -public class InventoryAdapter implements JsonSerializer, JsonDeserializer{ +public class InventoryHolderAdapter implements JsonSerializer, JsonDeserializer{ @Override public JsonElement serialize(InventoryHolder src, Type typeOfSrc, JsonSerializationContext context) { diff --git a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java index 8082d49..6fa17e5 100644 --- a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java +++ b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java @@ -31,9 +31,9 @@ public static InventoryHolder doubleHolderToSimpleHolder(InventoryHolder holder) } /** - * Find the inventory of a block. - * @param block - Block from which get the inventory. - * @return The inventory corresponding to the block location. + * Find the holder of a block. + * @param block - Block from which get the holder. + * @return Holder corresponding to the block location. * @throws ZSorterException if the block is not an instance of {@code InventoryHolder}; */ public static InventoryHolder findInventoryFromBlock(Block block) throws ZSorterException { diff --git a/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java b/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java index babf7cd..c46b025 100644 --- a/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java +++ b/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java @@ -17,7 +17,7 @@ import fr.zcraft.zsorter.ZSorterException; import fr.zcraft.zsorter.ZSorterTest; -import fr.zcraft.zsorter.model.serializer.InventoryAdapter; +import fr.zcraft.zsorter.model.serializer.InventoryHolderAdapter; import fr.zcraft.zsorter.model.serializer.PostProcessAdapterFactory; import fr.zcraft.zsorter.model.serializer.SorterManagerAdapter; @@ -136,7 +136,7 @@ public void gsonSerializationTest() throws ZSorterException, FileNotFoundExcepti GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(new PostProcessAdapterFactory()); - gsonBuilder.registerTypeHierarchyAdapter(Inventory.class, new InventoryAdapter()); + gsonBuilder.registerTypeHierarchyAdapter(Inventory.class, new InventoryHolderAdapter()); gsonBuilder.registerTypeAdapter(SorterManager.class, new SorterManagerAdapter()); Gson customGson = gsonBuilder.create(); From 25f6ec3f34cedbb9221e124e5629a5aecaee572a Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sun, 27 Dec 2020 00:53:38 +0100 Subject: [PATCH 20/21] Add /sorters shortcut --- src/main/java/fr/zcraft/zsorter/ZSorter.java | 1 + src/main/resources/plugin.yml | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/main/java/fr/zcraft/zsorter/ZSorter.java b/src/main/java/fr/zcraft/zsorter/ZSorter.java index 7cd3474..aa8cb68 100644 --- a/src/main/java/fr/zcraft/zsorter/ZSorter.java +++ b/src/main/java/fr/zcraft/zsorter/ZSorter.java @@ -140,6 +140,7 @@ public void onEnable() { RemoveOutputCommand.class, MagicCommand.class ); + Commands.registerShortcut("sorter", ListCommand.class, "sorters"); if(load()) { SortTask.getInstance().start(); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2a89631..7e062d8 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -8,6 +8,8 @@ api-version: "1.13" commands: sorter: description: Manage your sorters in game + sorters: + description: List all the sorters in game # Permissions section generated using https://amaury.carrade.eu/tools/generators/bukkit/permissions.html permissions: From baa7e441581ef47255bf34370b8f46c976378aca Mon Sep 17 00:00:00 2001 From: Luc Berge Date: Sun, 27 Dec 2020 00:58:39 +0100 Subject: [PATCH 21/21] Add magic command documentation --- README.md | 155 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 80 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 77fe066..d91c966 100644 --- a/README.md +++ b/README.md @@ -1,75 +1,80 @@ -# zSorter - -The zSorter plugin allows you to manager sorting system in game. It is an alternate solution to standard slow and laggy systems. - -## How it works - -A sorter is a sorting system composed of multiple inputs and outputs (chests, hoppers...). Inputs and outputs are ordered by priority from 1 to infinity and outputs can handle multiple items. -An output can also be defined as an overflow. - -The algorithm starts by looking for an item in the inputs. The input of priority 1 will be the first to be empty, then the 2... -If an item is found, the algorithm will list all the possible outputs and will try to put the item in the output. If the output of priority 1 is full, it will look for the output of priority 2, then 3... - -## Commands - -List all the existing sorters : -``` -/sorter list -``` - -Create a sorter: -``` -/sorter create -``` - -Update a sorter description: -``` -/sorter update -``` - -Change a sorter speed (by default set to 1): -``` -/sorter speed -``` - -Delete a sorter: -``` -/sorter delete -``` - -Display a sorter informations (By default OUTPUTS mode): -``` -/sorter info -``` - -Enable/Disable a sorter: -``` -/sorter toggle -``` - -Define the target block as an input: -``` -/sorter set_input -``` - -Define the target block as an output (no items to define it as an overflow): -``` -/sorter set_output -``` - -Remove the target block from inputs: -``` -/sorter remove_input -``` - -Remove the target block from outputs : -``` -/sorter remove_output -``` - -## Permissions - -The following permission allows you to use all the above commands. -``` -zsorter.admin -``` +# zSorter + +The zSorter plugin allows you to manager sorting system in game. It is an alternate solution to standard slow and laggy systems. + +## How it works + +A sorter is a sorting system composed of multiple inputs and outputs (chests, hoppers...). Inputs and outputs are ordered by priority from 1 to infinity and outputs can handle multiple items. +An output can also be defined as an overflow. + +The algorithm starts by looking for an item in the inputs. The input of priority 1 will be the first to be empty, then the 2... +If an item is found, the algorithm will list all the possible outputs and will try to put the item in the output. If the output of priority 1 is full, it will look for the output of priority 2, then 3... + +## Commands + +List all the existing sorters : +``` +/sorter list +``` + +Create a sorter: +``` +/sorter create +``` + +Update a sorter description: +``` +/sorter update +``` + +Change a sorter speed (by default set to 1): +``` +/sorter speed +``` + +Delete a sorter: +``` +/sorter delete +``` + +Display a sorter informations (By default OUTPUTS mode): +``` +/sorter info +``` + +Enable/Disable a sorter: +``` +/sorter toggle +``` + +Define the target block as an input: +``` +/sorter set_input +``` + +Define the target block as an output (no items to define it as an overflow): +``` +/sorter set_output +``` + +Remove the target block from inputs: +``` +/sorter remove_input +``` + +Remove the target block from outputs : +``` +/sorter remove_output +``` + +Highlight all the inputs and outputs of the sorter during 1 minute. The single outputs can also be define by left clicking on a holder. +``` +/sorter magic +``` + +## Permissions + +The following permission allows you to use all the above commands. +``` +zsorter.admin +```