From 755ca70523c6afd3f7215a3d53af706f0ed08f09 Mon Sep 17 00:00:00 2001 From: Paul Reilly Date: Wed, 8 Mar 2023 10:33:16 -0600 Subject: [PATCH] Update to fit a more secure processing system --- .../0a6146f64cde5935027cec3f5011843917c695cc | 21 ++--- .../3f45a40dab9ff1ac3dfeb0b1447c92d3fcca7e1d | 6 +- .../c98e1ff7e9323b7e41003c6492e15a73f9ff4250 | 0 .idea/sonarlint/issuestore/index.pb | 60 +++++++------- .idea/vcs.xml | 6 ++ .../simplexdevelopment/inv/AbstractGUI.java | 7 ++ .../simplexdevelopment/mob/AxolotlType.java | 7 +- .../simplexdevelopment/mob/CatType.java | 6 +- .../simplexdevelopment/mob/FoxType.java | 6 +- .../simplexdevelopment/mob/FrogType.java | 6 +- .../simplexdevelopment/mob/HorseType.java | 14 ++-- .../simplexdevelopment/mob/LlamaType.java | 6 +- .../simplexdevelopment/mob/PandaType.java | 12 +-- .../simplexdevelopment/mob/ParrotType.java | 6 +- .../simplexdevelopment/mob/RabbitType.java | 6 +- .../simplexdevelopment/mob/SheepType.java | 6 +- .../mob/TropicalFishType.java | 18 ++--- .../simplexdevelopment/mob/VillagerType.java | 12 +-- .../simplexdevelopment/util/Selector.java | 36 +++++++++ .../simplexdevelopment/util/Utility.java | 78 ++++++++++++++----- 20 files changed, 199 insertions(+), 120 deletions(-) create mode 100644 .idea/sonarlint/issuestore/c/9/c98e1ff7e9323b7e41003c6492e15a73f9ff4250 create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/io/github/simplexdevelopment/util/Selector.java diff --git a/.idea/sonarlint/issuestore/0/a/0a6146f64cde5935027cec3f5011843917c695cc b/.idea/sonarlint/issuestore/0/a/0a6146f64cde5935027cec3f5011843917c695cc index ea35756..d0445b3 100644 --- a/.idea/sonarlint/issuestore/0/a/0a6146f64cde5935027cec3f5011843917c695cc +++ b/.idea/sonarlint/issuestore/0/a/0a6146f64cde5935027cec3f5011843917c695cc @@ -1,17 +1,6 @@ -Q -java:S1066!"/Merge this if statement with the enclosing one.(80 -U -java:S1118":Add a private constructor to hide the implicit public one.( -t -java:S3776"RRefactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.(80 -C -java:S1659"!Declare "col" on a separate line.(Ϥ80 -? -java:S1659""Declare "dCol" on a separate line.(80 -f -java:S3358*"DExtract this nested ternary operation into an independent statement.(80 -f -java:S3358*"DExtract this nested ternary operation into an independent statement.(80 -O -java:S1128"-Remove this unused import 'java.util.Arrays'.(80 \ No newline at end of file +r java:S117O"QRename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(8Џ0 +< +java:S1659"!Declare "col" on a separate line.(Ϥ +8 +java:S1659 ""Declare "dCol" on a separate line.( \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/3/f/3f45a40dab9ff1ac3dfeb0b1447c92d3fcca7e1d b/.idea/sonarlint/issuestore/3/f/3f45a40dab9ff1ac3dfeb0b1447c92d3fcca7e1d index e9e06c1..b026e7d 100644 --- a/.idea/sonarlint/issuestore/3/f/3f45a40dab9ff1ac3dfeb0b1447c92d3fcca7e1d +++ b/.idea/sonarlint/issuestore/3/f/3f45a40dab9ff1ac3dfeb0b1447c92d3fcca7e1d @@ -1,5 +1,5 @@ -c java:S116"NRename this field "INV" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.( -g java:S116"RRename this field "ACTIONS" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.( +c java:S116"NRename this field "INV" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.( +g java:S116"RRename this field "ACTIONS" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.( I -java:S1611K"3Remove the parentheses around the "tuple" parameter(Ӗ \ No newline at end of file +java:S1611R"3Remove the parentheses around the "tuple" parameter(Ӗ \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/c/9/c98e1ff7e9323b7e41003c6492e15a73f9ff4250 b/.idea/sonarlint/issuestore/c/9/c98e1ff7e9323b7e41003c6492e15a73f9ff4250 new file mode 100644 index 0000000..e69de29 diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb index d77fa44..66c8eba 100644 --- a/.idea/sonarlint/issuestore/index.pb +++ b/.idea/sonarlint/issuestore/index.pb @@ -7,39 +7,41 @@ m =src/main/java/io/github/simplexdevelopment/api/GUIAction.java,3\0\30ddeaabb5082f9cf52c55066a39e20a1f7e4cde l src/main/java/io/github/simplexdevelopment/mob/ParrotType.java,c\1\c17017a8e659f282bdd6ebfdaf9293f034645a9c -m -=src/main/java/io/github/simplexdevelopment/mob/SheepType.java,f\7\f7a91356545d95e994ebce6af582f1824b8ba782 -n ->src/main/java/io/github/simplexdevelopment/mob/RabbitType.java,b\6\b6a6baf26fa767cc0299e356d0466810eb110166 -m -=src/main/java/io/github/simplexdevelopment/mob/PandaType.java,6\d\6d697c45549525f848d3b9bc2eb4094069cf0043 -t -Dsrc/main/java/io/github/simplexdevelopment/mob/TropicalFishType.java,f\b\fb9f8bc5d1d91117468f0312d09333f5af318753 -p -@src/main/java/io/github/simplexdevelopment/mob/VillagerType.java,0\f\0f39357e2b7952a9117fcd9387b5a91735f600f5 x Hsrc/main/java/io/github/simplexdevelopment/util/DisplayableSlotEnum.java,b\3\b3eafc205cf9dae5d1a7b77b5dbb74e37277308b -o -?src/main/java/io/github/simplexdevelopment/inv/AbstractGUI.java,3\f\3f45a40dab9ff1ac3dfeb0b1447c92d3fcca7e1d m =src/main/java/io/github/simplexdevelopment/api/GUIWindow.java,a\5\a5bbb1ae03235febbc6078171e3310884077d032 l src/main/java/io/github/simplexdevelopment/mob/ParrotType.java,c\1\c17017a8e659f282bdd6ebfdaf9293f034645a9c +n +>src/main/java/io/github/simplexdevelopment/mob/RabbitType.java,b\6\b6a6baf26fa767cc0299e356d0466810eb110166 +m +=src/main/java/io/github/simplexdevelopment/mob/SheepType.java,f\7\f7a91356545d95e994ebce6af582f1824b8ba782 +t +Dsrc/main/java/io/github/simplexdevelopment/mob/TropicalFishType.java,f\b\fb9f8bc5d1d91117468f0312d09333f5af318753 +p +@src/main/java/io/github/simplexdevelopment/mob/VillagerType.java,0\f\0f39357e2b7952a9117fcd9387b5a91735f600f5 \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/io/github/simplexdevelopment/inv/AbstractGUI.java b/src/main/java/io/github/simplexdevelopment/inv/AbstractGUI.java index e5ea6b3..3b69aca 100644 --- a/src/main/java/io/github/simplexdevelopment/inv/AbstractGUI.java +++ b/src/main/java/io/github/simplexdevelopment/inv/AbstractGUI.java @@ -3,6 +3,7 @@ package io.github.simplexdevelopment.inv; import io.github.simplexdevelopment.api.GUIAction; import io.github.simplexdevelopment.api.GUIWindow; import io.github.simplexdevelopment.util.DisplayableSlotEnum; +import io.github.simplexdevelopment.util.Selector; import io.github.simplexdevelopment.util.Tuple; import io.github.simplexdevelopment.util.Utility; import net.kyori.adventure.text.Component; @@ -23,11 +24,13 @@ public abstract class AbstractGUI implements GUIWindow { private final int[] slots = Utility.range(0, 44); private final Map slotMap = DisplayableSlotEnum.getSlotMaterialMap(); private final Set> tupleList = new HashSet<>(); + private final Selector selector; protected AbstractGUI(String name) { this.uuid = UUID.randomUUID(); this.INV = Bukkit.createInventory(null, 45, Component.text(name)); this.ACTIONS = new HashMap<>(); + this.selector = new Selector(22, 5, 9); GUIWindow.getInvByUUID().put(getUUID(), this); } @@ -65,6 +68,10 @@ public abstract class AbstractGUI implements GUIWindow { openInventories.put(p.getUniqueId(), getUUID()); } + public Selector getSelector() { + return selector; + } + public void createInjection(int slot, String name, GUIAction action) { tupleList.add(new Tuple<>(slot, name, action)); } diff --git a/src/main/java/io/github/simplexdevelopment/mob/AxolotlType.java b/src/main/java/io/github/simplexdevelopment/mob/AxolotlType.java index 3882dbc..385a0ba 100644 --- a/src/main/java/io/github/simplexdevelopment/mob/AxolotlType.java +++ b/src/main/java/io/github/simplexdevelopment/mob/AxolotlType.java @@ -1,6 +1,7 @@ package io.github.simplexdevelopment.mob; import io.github.simplexdevelopment.inv.AbstractGUI; +import io.github.simplexdevelopment.util.Selector; import io.github.simplexdevelopment.util.Utility; import org.bukkit.entity.Axolotl; import org.bukkit.entity.Player; @@ -17,13 +18,13 @@ public class AxolotlType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(Axolotl.Variant.values(), variant -> { - this.createInjection(x[0], variant.name(), action -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), variant.name(), action -> { axolotl.setVariant(variant); action.closeInventory(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); diff --git a/src/main/java/io/github/simplexdevelopment/mob/CatType.java b/src/main/java/io/github/simplexdevelopment/mob/CatType.java index 5ffa6d3..21f7e7f 100644 --- a/src/main/java/io/github/simplexdevelopment/mob/CatType.java +++ b/src/main/java/io/github/simplexdevelopment/mob/CatType.java @@ -17,13 +17,13 @@ public class CatType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(Cat.Type.values(), type -> { - this.createInjection(x[0], type.name(), p -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), type.name(), p -> { cat.setCatType(type); p.closeInventory(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); diff --git a/src/main/java/io/github/simplexdevelopment/mob/FoxType.java b/src/main/java/io/github/simplexdevelopment/mob/FoxType.java index a7715d1..7f8539a 100644 --- a/src/main/java/io/github/simplexdevelopment/mob/FoxType.java +++ b/src/main/java/io/github/simplexdevelopment/mob/FoxType.java @@ -17,13 +17,13 @@ public class FoxType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(Fox.Type.values(), type -> { - this.createInjection(x[0], type.name(), s -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), type.name(), s -> { fox.setFoxType(type); s.closeInventory(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); diff --git a/src/main/java/io/github/simplexdevelopment/mob/FrogType.java b/src/main/java/io/github/simplexdevelopment/mob/FrogType.java index 7da9516..485f88b 100644 --- a/src/main/java/io/github/simplexdevelopment/mob/FrogType.java +++ b/src/main/java/io/github/simplexdevelopment/mob/FrogType.java @@ -17,13 +17,13 @@ public class FrogType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(Frog.Variant.values(), type -> { - this.createInjection(x[0], type.name(), p -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), type.name(), p -> { frog.setVariant(type); p.closeInventory(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); diff --git a/src/main/java/io/github/simplexdevelopment/mob/HorseType.java b/src/main/java/io/github/simplexdevelopment/mob/HorseType.java index cc79755..52ff3eb 100644 --- a/src/main/java/io/github/simplexdevelopment/mob/HorseType.java +++ b/src/main/java/io/github/simplexdevelopment/mob/HorseType.java @@ -19,13 +19,15 @@ public class HorseType extends AbstractGUI { @Override public void openMenu() { - int[] x = {0}; Utility.forEach(Horse.Color.values(), color -> { - this.createInjection(x[0], color.name(), p -> { + if (this.getSelector().noMoreSlots()) { + return; + } + this.createInjection(this.getSelector().getSlot(), color.name(), p -> { horse.setColor(color); horseStyle.openMenu(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); @@ -44,13 +46,13 @@ public class HorseType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(Horse.Style.values(), style -> { - this.createInjection(x[0], style.name(), p -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), style.name(), p -> { horse.setStyle(style); p.closeInventory(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); diff --git a/src/main/java/io/github/simplexdevelopment/mob/LlamaType.java b/src/main/java/io/github/simplexdevelopment/mob/LlamaType.java index 431a62b..2603c57 100644 --- a/src/main/java/io/github/simplexdevelopment/mob/LlamaType.java +++ b/src/main/java/io/github/simplexdevelopment/mob/LlamaType.java @@ -17,13 +17,13 @@ public class LlamaType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(Llama.Color.values(), color -> { - this.createInjection(x[0], color.name(), p-> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), color.name(), p-> { llama.setColor(color); p.closeInventory(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); diff --git a/src/main/java/io/github/simplexdevelopment/mob/PandaType.java b/src/main/java/io/github/simplexdevelopment/mob/PandaType.java index 3f5f5a2..3c61827 100644 --- a/src/main/java/io/github/simplexdevelopment/mob/PandaType.java +++ b/src/main/java/io/github/simplexdevelopment/mob/PandaType.java @@ -19,13 +19,13 @@ public class PandaType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(Panda.Gene.values(), gene -> { - this.createInjection(x[0], gene.name(), p -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), gene.name(), p -> { panda.setMainGene(gene); diminutive.openMenu(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); @@ -42,13 +42,13 @@ public class PandaType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(Panda.Gene.values(), gene -> { - this.createInjection(x[0], gene.name(), p -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), gene.name(), p -> { panda.setHiddenGene(gene); p.closeInventory(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); diff --git a/src/main/java/io/github/simplexdevelopment/mob/ParrotType.java b/src/main/java/io/github/simplexdevelopment/mob/ParrotType.java index eebcded..e29b3fa 100644 --- a/src/main/java/io/github/simplexdevelopment/mob/ParrotType.java +++ b/src/main/java/io/github/simplexdevelopment/mob/ParrotType.java @@ -17,13 +17,13 @@ public class ParrotType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(Parrot.Variant.values(), variant -> { - this.createInjection(x[0], variant.name(), action -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), variant.name(), action -> { parrot.setVariant(variant); action.closeInventory(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); diff --git a/src/main/java/io/github/simplexdevelopment/mob/RabbitType.java b/src/main/java/io/github/simplexdevelopment/mob/RabbitType.java index 7d3125f..646dc0c 100644 --- a/src/main/java/io/github/simplexdevelopment/mob/RabbitType.java +++ b/src/main/java/io/github/simplexdevelopment/mob/RabbitType.java @@ -17,13 +17,13 @@ public class RabbitType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(Rabbit.Type.values(), type -> { - this.createInjection(x[0], type.name(), p -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), type.name(), p -> { rabbit.setRabbitType(type); p.closeInventory(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); diff --git a/src/main/java/io/github/simplexdevelopment/mob/SheepType.java b/src/main/java/io/github/simplexdevelopment/mob/SheepType.java index 49caf6c..3068147 100644 --- a/src/main/java/io/github/simplexdevelopment/mob/SheepType.java +++ b/src/main/java/io/github/simplexdevelopment/mob/SheepType.java @@ -18,13 +18,13 @@ public class SheepType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(DyeColor.values(), color -> { - this.createInjection(x[0], color.name(), p -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), color.name(), p -> { sheep.setColor(color); p.closeInventory(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); diff --git a/src/main/java/io/github/simplexdevelopment/mob/TropicalFishType.java b/src/main/java/io/github/simplexdevelopment/mob/TropicalFishType.java index 022fd8f..9acd8ff 100644 --- a/src/main/java/io/github/simplexdevelopment/mob/TropicalFishType.java +++ b/src/main/java/io/github/simplexdevelopment/mob/TropicalFishType.java @@ -22,13 +22,13 @@ public class TropicalFishType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(TropicalFish.Pattern.values(), pattern -> { - this.createInjection(x[0], pattern.name(), p -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), pattern.name(), p -> { fish.setPattern(pattern); bodyColor.openMenu(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); @@ -45,13 +45,13 @@ public class TropicalFishType extends AbstractGUI { } public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(DyeColor.values(), dyeColor -> { - this.createInjection(x[0], dyeColor.name(), p -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), dyeColor.name(), p -> { fish.setBodyColor(dyeColor); patternColor.openMenu(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); @@ -69,13 +69,13 @@ public class TropicalFishType extends AbstractGUI { } public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(DyeColor.values(), dyeColor -> { - this.createInjection(x[0], dyeColor.name(), p -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), dyeColor.name(), p -> { fish.setPatternColor(dyeColor); p.closeInventory(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); diff --git a/src/main/java/io/github/simplexdevelopment/mob/VillagerType.java b/src/main/java/io/github/simplexdevelopment/mob/VillagerType.java index f672dd4..02ba05e 100644 --- a/src/main/java/io/github/simplexdevelopment/mob/VillagerType.java +++ b/src/main/java/io/github/simplexdevelopment/mob/VillagerType.java @@ -20,13 +20,13 @@ public class VillagerType extends AbstractGUI { @Override public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(Villager.Type.values(), type -> { - this.createInjection(x[0], type.name(), p -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), type.name(), p -> { villager.setVillagerType(type); villagerProfession.openMenu(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); @@ -43,14 +43,14 @@ public class VillagerType extends AbstractGUI { } public void openMenu() { - int[] x = {Utility.getNextEmptySlot(22)}; Utility.forEach(Villager.Profession.values(), profession -> { - this.createInjection(x[0], profession.name(), p -> { + if (this.getSelector().noMoreSlots()) return; + this.createInjection(this.getSelector().getSlot(), profession.name(), p -> { villager.setProfession(profession); villager.setVillagerExperience(1); // This prevents loss of profession. p.closeInventory(); }); - x[0] = Utility.getNextEmptySlot(x[0]); + this.getSelector().nextSlot(); }); this.dynamicSlotAssignment(this.getTupleList()); this.open(player); diff --git a/src/main/java/io/github/simplexdevelopment/util/Selector.java b/src/main/java/io/github/simplexdevelopment/util/Selector.java new file mode 100644 index 0000000..c05fa60 --- /dev/null +++ b/src/main/java/io/github/simplexdevelopment/util/Selector.java @@ -0,0 +1,36 @@ +package io.github.simplexdevelopment.util; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +public class Selector { + private int slot; + private ItemStack[][] slots; + + public Selector(int slot, int length, int width) { + this.slot = slot; + this.slots = Utility.new2dItemArray(length, width); + } + + public int getSlot() { + return slot; + } + + public void nextSlot() { + dropSlot(); + this.slot = Utility.getNextEmptySlot(this.slot, this.slots); + } + + public boolean noMoreSlots() { + return this.slot == -1; + } + + public void dropSlot() { + int xPos = slot / slots.length; + int yPos = slot / slots[xPos].length; + if (Utility.isOutOfBounds(xPos, yPos, slots) || !Utility.isCurrentSlotEmpty(slots, xPos, yPos)) { + return; + } + slots[xPos][yPos] = new ItemStack(Material.BARRIER, 1); // Since we only use this for logic purposes, this can be any block other than air. + } +} \ No newline at end of file diff --git a/src/main/java/io/github/simplexdevelopment/util/Utility.java b/src/main/java/io/github/simplexdevelopment/util/Utility.java index b7c6b17..1bad822 100644 --- a/src/main/java/io/github/simplexdevelopment/util/Utility.java +++ b/src/main/java/io/github/simplexdevelopment/util/Utility.java @@ -1,11 +1,15 @@ package io.github.simplexdevelopment.util; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; -import java.util.Arrays; import java.util.function.Consumer; public class Utility { + private Utility() { + throw new AssertionError(); + } public static int[] range(int start, int end) { int[] range = new int[end - start]; for (int i = 0; i < range.length; i++) { @@ -20,33 +24,65 @@ public class Utility { } } - public static int getNextEmptySlot(int x) { - int[][] grid = new int[5][9]; // initialize the grid - - int row = 0, col = 0; - int dRow = 0, dCol = 1; + public static int getNextEmptySlot(int x, ItemStack[][] grid) { + // initialize the grid int[] dr = {0, 1, 0, -1}; int[] dc = {1, 0, -1, 0}; + int row = 0, col = 0; + int dRow = 0, dCol = 1; for (int i = 0; i < 5 * 9; i++) { - if (grid[row][col] == 0) { - if (i == x) { - return row * 9 + col; - } - } - row += dRow; - col += dCol; - if (row < 0 || row >= 5 || col < 0 || col >= 9 || grid[row][col] != 0) { - row -= dRow; - col -= dCol; - int dir = (dRow == 0) ? (dCol == 1 ? 1 : 3) : (dRow == 1 ? 2 : 0); - dRow = dr[dir]; - dCol = dc[dir]; - row += dRow; - col += dCol; + if (isCurrentSlotEmpty(grid, row, col) && i == x) { + return row * 9 + col; } + int[] nextSlot = getNextSlot(grid, row, col, dRow, dCol, dr, dc); + row = nextSlot[0]; + col = nextSlot[1]; + dRow = nextSlot[2]; + dCol = nextSlot[3]; } return -1; // no empty slot found } + + public static boolean isCurrentSlotEmpty(ItemStack[][] grid, int row, int col) { + return grid[row][col].getType().equals(Material.AIR); + } + + public static int[] getNextSlot(ItemStack[][] grid, int row, int col, int dRow, int dCol, int[] dr, int[] dc) { + int nextRow = row + dRow; + int nextCol = col + dCol; + int dir = getDirection(dRow, dCol); + + if (isOutOfBounds(nextRow, nextCol, grid) || !isCurrentSlotEmpty(grid, nextRow, nextCol)) { + dir = (dir + 1) % 4; + } + + return new int[]{row + dr[dir], col + dc[dir], dr[dir], dc[dir]}; + } + + public static int getDirection(int dRow, int dCol) { + if (dRow == 0) { + return dCol == 1 ? 1 : 3; + } else { + return dRow == 1 ? 2 : 0; + } + } + + public static boolean isOutOfBounds(int row, int col, ItemStack[][] grid) { + return row < 0 || row >= grid.length - 1 || col < 0 || col >= grid[row].length - 1; + } + + public static ItemStack[][] new2dItemArray(int length, int width) { + ItemStack[][] range = new ItemStack[length][width]; + + ItemStack BASE = new ItemStack(Material.AIR, 1); + for (int i = 0; i < length - 1; i++) { + for (int j = 0; j < width - 1; j++) { + range[i][j] = BASE; + } + } + + return range; + } }