Skip to content

Commit

Permalink
Replaced inventory variables by holder variables
Browse files Browse the repository at this point in the history
  • Loading branch information
LucBerge committed Dec 26, 2020
1 parent 846890a commit 7f99142
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 98 deletions.
6 changes: 3 additions & 3 deletions src/main/java/fr/zcraft/zsorter/ZSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/fr/zcraft/zsorter/events/InventoryEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/fr/zcraft/zsorter/events/ItemMoveEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/fr/zcraft/zsorter/events/LeftClickEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/fr/zcraft/zsorter/model/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/fr/zcraft/zsorter/model/Output.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Material>();
this.full = false;
}
Expand Down
80 changes: 40 additions & 40 deletions src/main/java/fr/zcraft/zsorter/model/Sorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public class Sorter implements Serializable, PostProcessable{
private transient boolean toCompute;
private int speed;

private transient Map<InventoryHolder, Input> inventoryToInput;
private transient Map<InventoryHolder, Output> inventoryToOutput;
private transient Map<InventoryHolder, Input> holderToInput;
private transient Map<InventoryHolder, Output> holderToOutput;
private transient Map<Material, List<Output>> materialToOutputs;
private transient List<Output> overflows;

Expand Down Expand Up @@ -78,8 +78,8 @@ public Sorter(String name, String description) {
this.toCompute = false;
this.speed = DEFAULT_SPEED;

this.inventoryToInput = new HashMap<InventoryHolder, Input>();
this.inventoryToOutput = new HashMap<InventoryHolder, Output>();
this.holderToInput = new HashMap<InventoryHolder, Input>();
this.holderToOutput = new HashMap<InventoryHolder, Output>();
this.materialToOutputs = new TreeMap<Material, List<Output>>();
this.overflows = new ArrayList<Output>();

Expand Down Expand Up @@ -177,7 +177,7 @@ public void setSpeed(int speed) {
* @return The sorter inputs.
*/
public Map<InventoryHolder, Input> getInventoryToInput() {
return inventoryToInput;
return holderToInput;
}

/**
Expand All @@ -187,7 +187,7 @@ public Map<InventoryHolder, Input> getInventoryToInput() {
* @return The sorter outputs.
*/
public Map<InventoryHolder, Output> getInventoryToOutput() {
return inventoryToOutput;
return holderToOutput;
}

/**
Expand Down Expand Up @@ -226,17 +226,17 @@ public List<Material> 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<Material, List<Output>>();
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<Output> possibleOutputs = materialToOutputs.get(material); //Get the possible outputs for the given material
if(possibleOutputs == null) { //If none have been found
Expand All @@ -252,22 +252,22 @@ public void commit() {
}

/**
* Sets the inventory has an input.<br><br>
* Sets the holder has an input.<br><br>
* 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
Expand All @@ -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.<br><br>
* Sets the holder has an output.<br><br>
* 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<Material> materials) throws ZSorterException {
Input input = inventoryToInput.get(inventory); //Get the existing input
public Output setOutput(InventoryHolder holder, int priority, List<Material> 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
Expand All @@ -318,11 +318,11 @@ public Output setOutput(InventoryHolder inventory, int priority, List<Material>

/**
* 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;
Expand Down Expand Up @@ -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<Input> inputs = inventoryToInput
List<Input> inputs = holderToInput
.values()
.stream()
.sorted()
Expand Down Expand Up @@ -482,7 +482,7 @@ public RawText toRawText(DisplayMode mode) {

//if display by output
if(mode == DisplayMode.OUTPUTS) {
List<Output> outputs = inventoryToOutput
List<Output> outputs = holderToOutput
.values()
.stream()
.filter(o -> !o.isOverflow())
Expand Down Expand Up @@ -522,7 +522,7 @@ public RawText toRawText(DisplayMode mode) {
}
//If display by items
else if(mode == DisplayMode.ITEMS){
List<Material> sortedMaterials = inventoryToOutput
List<Material> sortedMaterials = holderToOutput
.values()
.stream()
.filter(o -> !o.isOverflow())
Expand Down Expand Up @@ -594,13 +594,13 @@ public boolean equals(Object obj) {

@Override
public void postProcess() {
inventoryToInput = new HashMap<InventoryHolder, Input>();
holderToInput = new HashMap<InventoryHolder, Input>();
for(Input input:inputs) {
inventoryToInput.putIfAbsent(input.getHolder(), input);
holderToInput.putIfAbsent(input.getHolder(), input);
}
inventoryToOutput = new HashMap<InventoryHolder, Output>();
holderToOutput = new HashMap<InventoryHolder, Output>();
for(Output output:outputs) {
inventoryToOutput.putIfAbsent(output.getHolder(), output);
holderToOutput.putIfAbsent(output.getHolder(), output);
}
commit();
if(enable)
Expand Down
Loading

0 comments on commit 7f99142

Please sign in to comment.