Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Make the slot in simple guis optional (it will search for next slot)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeandroPA committed Jul 1, 2017
1 parent 85fbe63 commit 25878d0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
26 changes: 24 additions & 2 deletions src/me/tuke/sktuke/manager/gui/GUIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class GUIManager {
public GUIManager(TuSKe tuske) {
this.tuske = tuske;
}
private HashMap<Inventory, HashMap<Integer, GUI[]>> invs = new HashMap<>();
private Map<Inventory, HashMap<Integer, GUI[]>> invs = new HashMap<>();
private Map<Inventory, Integer> lastSlots = new HashMap<>();

public boolean isGUI(Inventory inv, int slot){
return invs.containsKey(inv) && invs.get(inv).containsKey(slot);
Expand All @@ -48,6 +49,22 @@ private GUI getGUI(GUI[] guis, ClickType ct){
private void addToListener(Inventory inv, int slot, ItemStack item, GUI gui){
GUI[] guis2 = null;
HashMap<Integer,GUI[]> guislot2 = new HashMap<>();
if (slot == -1) {
Integer s = lastSlots.get(inv);
if (s != null)
slot = s + 1;
else
slot = 0;
} else if (slot == -2) {
slot = 0;
for (int x = 0; x < inv.getSize(); x++)
if (!guislot2.containsKey(x)) {
slot = x;
break;
}
}
if (slot >= inv.getSize())
return;
boolean firstSlot = invs.containsKey(inv);
if (firstSlot && (guislot2 = invs.get(inv)).containsKey(slot)){
guislot2.get(slot)[getIndex(gui.getClickType())] = gui;
Expand All @@ -59,6 +76,7 @@ private void addToListener(Inventory inv, int slot, ItemStack item, GUI gui){
guislot2.put(slot, guis2);
}
invs.put(inv, guislot2);
lastSlots.put(inv, slot);
inv.setItem(slot, item);
}
public void remove(Inventory inv, int slot){
Expand All @@ -67,15 +85,18 @@ public void remove(Inventory inv, int slot){
map.remove(slot);
if (map.size() > 0)
invs.put(inv, map);
else
else {
invs.remove(inv);
lastSlots.remove(inv);
}
}
public void removeAll(Inventory inv){
Map<Integer, GUI[]> map = invs.get(inv);
if (map != null) {
for (int slot : map.keySet())
inv.setItem(slot, new ItemStack(Material.AIR));
invs.remove(inv);
lastSlots.remove(inv);
}

}
Expand All @@ -84,6 +105,7 @@ public void clearAll(){
for (Integer slot : invs.get(inv).keySet())
inv.setItem(slot, new ItemStack(Material.AIR));
invs.clear();
lastSlots.clear();
}
public boolean isAllowedType(ClickType ct){
if (ct != null)
Expand Down
20 changes: 10 additions & 10 deletions src/me/tuke/sktuke/sections/gui/EffFormatGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public class EffFormatGUI extends EffectSection {
static {
String cr = "string/" + Classes.getExactClassInfo(ClickType.class).getCodeName();
Registry.newEffect(EffFormatGUI.class,
"(format|create|make) [a] gui slot %numbers% of %players% with %itemstack% [to [do] nothing]",
"(format|create|make) [a] gui slot %numbers% of %players% with %itemstack% to (1¦close|2¦open %-inventory%) [(using|with) %-" + cr + "% [(button|click|action)]]",
"(format|create|make) [a] gui slot %numbers% of %players% with %itemstack% to (run|exe[cute]) [(using|with) %-" + cr + "% [(button|click|action)]]",
"(format|create|make) [a] gui slot %numbers% of %players% with %itemstack% to [(1¦close|2¦open %-inventory%) then] (run|exe[cute]) %commandsender% command %string% [(using|with) perm[ission] %-string%][[(,| and)] (using|with) %-" + cr + "% [(button|click|action)]][[(,| and)] (using|with) cursor [item] %-itemstack%]",
"(format|create|make) [a] gui slot %numbers% of %players% with %itemstack% to [(1¦close|2¦open %-inventory%) then] (run|exe[cute]) function <(.+)>\\([<.*?>]\\)[[(,| and)] (using|with) %-" + cr + "% [(button|click|action)]][[(,| and)] (using|with) cursor [item] %-itemstack%]",
"(format|create|make) [a] gui slot [%-numbers%] of %players% with %itemstack% [to [do] nothing]",
"(format|create|make) [a] gui slot [%-numbers%] of %players% with %itemstack% to (1¦close|2¦open %-inventory%) [(using|with) %-" + cr + "% [(button|click|action)]]",
"(format|create|make) [a] gui slot [%-numbers%] of %players% with %itemstack% to (run|exe[cute]) [(using|with) %-" + cr + "% [(button|click|action)]]",
"(format|create|make) [a] gui slot [%-numbers%] of %players% with %itemstack% to [(1¦close|2¦open %-inventory%) then] (run|exe[cute]) %commandsender% command %string% [(using|with) perm[ission] %-string%][[(,| and)] (using|with) %-" + cr + "% [(button|click|action)]][[(,| and)] (using|with) cursor [item] %-itemstack%]",
"(format|create|make) [a] gui slot [%-numbers%] of %players% with %itemstack% to [(1¦close|2¦open %-inventory%) then] (run|exe[cute]) function <(.+)>\\([<.*?>]\\)[[(,| and)] (using|with) %-" + cr + "% [(button|click|action)]][[(,| and)] (using|with) cursor [item] %-itemstack%]",
//"(format|create|make) [a] gui slot %numbers% of %players% with %itemstack% to [(1¦close|2¦open %-inventoy%) then] (run|exe[cute]) function <(.+)>\\([%-objects%[, %-objects%][, %-objects%][, %-objects%][, %-objects%][, %-objects%][, %-objects%][, %-objects%][, %-objects%][, %-objects%]]\\)[[(,| and)] (using|with) %-" + cr + "% [(button|click|action)]][[(,| and)] (using|with) cursor [item] %-itemstack%]",
"(format|create|make) [a] gui slot %numbers% of %players% with %itemstack% to (run|exe[cute]) [gui [click]] event");
"(format|create|make) [a] gui slot [%-numbers%] of %players% with %itemstack% to (run|exe[cute]) [gui [click]] event");
}

public static EffFormatGUI lastInstance = null;
Expand Down Expand Up @@ -112,20 +112,20 @@ public boolean init(Expression<?>[] arg, int arg1, Kleenean arg2, ParseResult ar

@Override
public String toString(@Nullable Event e, boolean arg1) {
return "format a gui slot " +s.toString(e, arg1) + " of " + p.toString(e, arg1) + " with " + i.toString(e, arg1);
return "format a gui slot " + (s != null ? s.toString(e, arg1) : -1) + " of " + p.toString(e, arg1) + " with " + i.toString(e, arg1);
}

@Override
protected void execute(Event e) {
if (this.p.getArray(e) != null && this.s.getArray(e) != null && this.i.getSingle(e) != null){
if (this.p.getArray(e) != null && this.i.getSingle(e) != null){
Player[] p = this.p.getArray(e);
Number[] slots = this.s.getArray(e);
Number[] slots = s != null ? s.getArray(e) : new Number[]{-2};
for (int x = 0; x < slots.length; x++)
for(int y = 0; y < p.length; y++){
if (p[y] == null || slots[x] == null)
continue;
Inventory inv = p[y].getOpenInventory().getTopInventory();
if (slots[x].intValue() >= 0 && slots[x].intValue() < inv.getSize() && !inv.getType().equals(InventoryType.CRAFTING)) {
if (slots[x].intValue() >= -2 && slots[x].intValue() < inv.getSize() && !inv.getType().equals(InventoryType.CRAFTING)) {
Object rn = null;
switch (Type) {
case 3:
Expand Down

0 comments on commit 25878d0

Please sign in to comment.