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 +``` 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/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 e665222..aa8cb68 100644 --- a/src/main/java/fr/zcraft/zsorter/ZSorter.java +++ b/src/main/java/fr/zcraft/zsorter/ZSorter.java @@ -7,7 +7,7 @@ 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; @@ -15,15 +15,15 @@ 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; 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; @@ -34,8 +34,9 @@ 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.InventoryHolderAdapter; import fr.zcraft.zsorter.model.serializer.PostProcessAdapterFactory; import fr.zcraft.zsorter.model.serializer.SorterManagerAdapter; import fr.zcraft.zsorter.tasks.SortTask; @@ -123,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, @@ -135,8 +137,10 @@ public void onEnable() { SetInputCommand.class, SetOutputCommand.class, RemoveInputCommand.class, - RemoveOutputCommand.class + RemoveOutputCommand.class, + MagicCommand.class ); + Commands.registerShortcut("sorter", ListCommand.class, "sorters"); if(load()) { SortTask.getInstance().start(); @@ -156,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 InventoryHolderAdapter()); gsonBuilder.registerTypeAdapter(SorterManager.class, new SorterManagerAdapter()); Gson customGson = gsonBuilder.create(); FileWriter fr = new FileWriter(dataPath); @@ -177,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 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/commands/DeleteCommand.java b/src/main/java/fr/zcraft/zsorter/commands/DeleteCommand.java index 8e0e49c..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; @@ -38,10 +37,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/MagicCommand.java b/src/main/java/fr/zcraft/zsorter/commands/MagicCommand.java new file mode 100644 index 0000000..844417f --- /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.getHolder().getInventory().getLocation(), Config.MAGIC_EFFECT_DURATION.get()); + } + for(Output output:sorter.getInventoryToOutput().values()) { + 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())); + } + 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/commands/RemoveInputCommand.java b/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java index f2a6630..6faa154 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/RemoveInputCommand.java @@ -2,11 +2,9 @@ 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; @@ -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 { + InventoryHolder 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) { @@ -54,10 +49,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..0dca932 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/RemoveOutputCommand.java @@ -2,11 +2,9 @@ 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; @@ -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 { + InventoryHolder 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) { @@ -54,10 +49,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..ea9aa16 100644 --- a/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java +++ b/src/main/java/fr/zcraft/zsorter/commands/SetInputCommand.java @@ -2,11 +2,9 @@ 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; @@ -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 { + InventoryHolder 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)); } @@ -65,10 +60,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..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,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; @@ -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 { + InventoryHolder 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)); } @@ -79,10 +74,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..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; @@ -51,10 +50,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..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; @@ -54,10 +53,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..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; @@ -43,10 +42,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..a7be594 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,18 @@ 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() + .filter(s -> s.startsWith(arg)) + .collect(Collectors.toList()); + } + @Override public boolean canExecute(CommandSender sender) { diff --git a/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java b/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java index 798deac..019722c 100644 --- a/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java +++ b/src/main/java/fr/zcraft/zsorter/events/HolderBreakEvent.java @@ -4,11 +4,11 @@ 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; +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 { + 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 + 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 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..c63a908 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; @@ -21,9 +21,9 @@ 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 + if(ZSorter.getInstance().isEnable()) { //If the plugin is not enable + 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 8f51e20..2124d31 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,12 @@ 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 + 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 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 new file mode 100644 index 0000000..ca422d4 --- /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.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(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 holder = InventoryUtils.findInventoryFromBlock(e.getClickedBlock()); + + //Try to add the output to the sorter + 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); + } + 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..4c75067 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. @@ -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(Inventory 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/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 3b60e4b..a1a4353 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; @@ -22,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.

@@ -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(); @@ -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; } @@ -178,8 +176,8 @@ public void setSpeed(int speed) { * Use the {@code setInput} and {@code removeInput} methods instead. * @return The sorter inputs. */ - public Map getInventoryToInput() { - return inventoryToInput; + public Map getInventoryToInput() { + return holderToInput; } /** @@ -188,8 +186,8 @@ public Map getInventoryToInput() { * Use the {@code setOutput} and {@code removeOutput} methods instead. * @return The sorter outputs. */ - public Map getInventoryToOutput() { - return inventoryToOutput; + public Map getInventoryToOutput() { + return holderToOutput; } /** @@ -228,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 @@ -254,78 +252,80 @@ 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(Inventory 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 - setEnable(false); //Disable the sorter - - 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 } + commit(); return existingInput; } /** * 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(Inventory inventory) { - setEnable(false); - return 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(Inventory 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 - setEnable(false); //Disable the sorter - - 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 existingOutput.setMaterials(materials); //Set the new materials } + commit(); return existingOutput; } /** * 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(Inventory inventory) { - setEnable(false); - return inventoryToOutput.remove(inventory); + public Output removeOutput(InventoryHolder holder) { + Output result = holderToOutput.remove(holder); + if(result != null) + commit(); + return result; } /** @@ -363,50 +363,52 @@ 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().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 + 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().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 //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 - - 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 - } + 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 + } //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 } } } @@ -423,7 +425,6 @@ public void computeSorting() { * @return Sorter as RawText. */ public RawText toRawText(DisplayMode mode) { - commit(); RawTextPart text = new RawText("") .then(name) .style(ChatColor.GOLD) @@ -444,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() @@ -459,7 +460,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) ); } @@ -474,14 +475,14 @@ 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) ); } //if display by output if(mode == DisplayMode.OUTPUTS) { - List outputs = inventoryToOutput + List outputs = holderToOutput .values() .stream() .filter(o -> !o.isOverflow()) @@ -498,7 +499,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) ); @@ -521,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()) @@ -551,7 +552,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) ); } @@ -593,14 +594,13 @@ public boolean equals(Object obj) { @Override public void postProcess() { - System.out.println("========SORTER========"); - inventoryToInput = new HashMap(); + holderToInput = new HashMap(); for(Input input:inputs) { - inventoryToInput.putIfAbsent(input.getInventory(), input); + holderToInput.putIfAbsent(input.getHolder(), input); } - inventoryToOutput = new HashMap(); + holderToOutput = new HashMap(); for(Output output:outputs) { - inventoryToOutput.putIfAbsent(output.getInventory(), 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 4d2b25c..2d421a8 100644 --- a/src/main/java/fr/zcraft/zsorter/model/SorterManager.java +++ b/src/main/java/fr/zcraft/zsorter/model/SorterManager.java @@ -8,11 +8,13 @@ import java.util.stream.Collectors; import org.bukkit.Material; -import org.bukkit.inventory.Inventory; +import org.bukkit.entity.Player; +import org.bukkit.inventory.InventoryHolder; 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. @@ -26,32 +28,42 @@ 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(); } /** - * 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 inventory to a sorter.

+ * 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 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; + public Map getInventoryToSorter() { + return holderToSorter; } /** @@ -81,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.getInventory()); //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.getInventory()); //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, Inventory 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, Inventory 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, Inventory 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, Inventory 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; } @@ -179,30 +191,36 @@ 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(Inventory inventory) { + 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() .stream() .filter(sorter.getCloggingUpMaterials()::contains) .count() > 0; - + + 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 SortTask.getInstance().start(); //Start the task @@ -218,7 +236,7 @@ public boolean computeSorter(Inventory inventory) { 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; } @@ -232,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 58% 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 bec4ff4..be05ebf 100644 --- a/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryAdapter.java +++ b/src/main/java/fr/zcraft/zsorter/model/serializer/InventoryHolderAdapter.java @@ -4,7 +4,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; @@ -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; /** @@ -23,20 +23,20 @@ * @author Lucas * */ -public class InventoryAdapter implements JsonSerializer, JsonDeserializer{ +public class InventoryHolderAdapter 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(); @@ -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/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 d3fcb11..6fa17e5 100644 --- a/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java +++ b/src/main/java/fr/zcraft/zsorter/utils/InventoryUtils.java @@ -1,8 +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 org.bukkit.inventory.ItemStack; + +import fr.zcraft.quartzlib.components.i18n.I; +import fr.zcraft.zsorter.ZSorterException; /** * This class provides useful methods related to inventories. @@ -12,30 +17,45 @@ 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. + * 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) { + if(holder.getInventory() instanceof DoubleChestInventory) { + DoubleChestInventory dci = (DoubleChestInventory) holder.getInventory(); + holder = dci.getLeftSide().getHolder(); + } + return holder; + } + + /** + * 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 Inventory simpleInventoryToDoubleInventory(Inventory inventory) { - InventoryHolder holder = (InventoryHolder) inventory.getLocation().getBlock().getState(); - if(holder.getInventory() instanceof DoubleChestInventory) { - inventory = (DoubleChestInventory) holder.getInventory(); - } - return inventory; + 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(); + holder = doubleHolderToSimpleHolder(holder); + return holder; } /** - * 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. + * 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 Inventory doubleInventoryToSimpleInventory(Inventory inventory) { - if(inventory instanceof DoubleChestInventory) { - DoubleChestInventory dci = (DoubleChestInventory) inventory; - inventory = dci.getLeftSide(); - } - return inventory; + 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; } } 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 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 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0892315..7e062d8 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 @@ -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: 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/SorterManagerTest.java b/src/test/java/fr/zcraft/zsorter/model/SorterManagerTest.java index d949652..c46b025 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; @@ -22,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; @@ -122,37 +117,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,11 +133,10 @@ 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()); - gsonBuilder.registerTypeHierarchyAdapter(Inventory.class, new InventoryAdapter()); + gsonBuilder.registerTypeHierarchyAdapter(Inventory.class, new InventoryHolderAdapter()); gsonBuilder.registerTypeAdapter(SorterManager.class, new SorterManagerAdapter()); Gson customGson = gsonBuilder.create(); diff --git a/src/test/java/fr/zcraft/zsorter/model/SorterTest.java b/src/test/java/fr/zcraft/zsorter/model/SorterTest.java index e12c1ac..7237cdc 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(); + 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.commit(); + 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.commit(); + 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.commit(); + sorter.removeOutput(overflow.getHolder()); Assert.assertEquals(Arrays.asList(), sorter.findOutputs(Material.IRON_BLOCK)); Assert.assertEquals(Arrays.asList(), sorter.findOutputs(Material.COBBLESTONE)); }