Duplicate: "true,1" grabs commands as well.

This commit is contained in:
TinyTank800 2025-03-23 14:27:38 -07:00
parent 3e62132444
commit 51e35b1c7a
2 changed files with 89 additions and 0 deletions

View File

@ -99,6 +99,34 @@ public class Utils implements Listener {
break;
}
}
// If we didn't find the slot directly, check if it's a duplicate
if(foundSlot == null){
// Loop through all items to check their duplicate configurations
for(String item : Objects.requireNonNull(panel.getConfig().getConfigurationSection("item")).getKeys(false)){
String section = plugin.has.hasSection(panel, position, panel.getConfig().getConfigurationSection("item." + item), p);
// Check if this item has a duplicate configuration
if(panel.getConfig().contains("item." + item + section + ".duplicate")) {
String duplicateValue = panel.getConfig().getString("item." + item + section + ".duplicate");
// Check if the clicked slot is in the duplicate configuration
DuplicateResult result = isSlotInDuplicateConfig(clickedSlot, duplicateValue);
if(result.isInDuplicate) {
foundSlot = item;
// If commands shouldn't be duplicated, return early after cancelling the event
if(!result.shouldDuplicateCommands) {
e.setCancelled(true);
return;
}
break;
}
}
}
}
if(foundSlot == null){
e.setCancelled(true);
return;
@ -178,4 +206,60 @@ public class Utils implements Listener {
}
return false;
}
// Class to hold the result of duplicate check
private static class DuplicateResult {
public boolean isInDuplicate;
public boolean shouldDuplicateCommands;
public DuplicateResult(boolean isInDuplicate, boolean shouldDuplicateCommands) {
this.isInDuplicate = isInDuplicate;
this.shouldDuplicateCommands = shouldDuplicateCommands;
}
}
// Helper method to check if a slot is included in the duplicate configuration and if commands should be duplicated
private DuplicateResult isSlotInDuplicateConfig(int slot, String duplicateConfig) {
if(duplicateConfig == null) return new DuplicateResult(false, false);
boolean shouldDuplicateCommands = false;
String[] dupeItems = duplicateConfig.split(",");
// First check if "true" is included in the config
for(String item : dupeItems) {
if(item.trim().equalsIgnoreCase("true")) {
shouldDuplicateCommands = true;
break;
}
}
// Then check if the slot is included
for(String dupeItem : dupeItems) {
dupeItem = dupeItem.trim(); // Remove any whitespace
// Skip the "true" flag, it's not a slot designation
if(dupeItem.equalsIgnoreCase("true")) continue;
if(dupeItem.contains("-")) {
// This is a range
String[] range = dupeItem.split("-");
int min = Integer.parseInt(range[0]);
int max = Integer.parseInt(range[1]);
if(slot >= min && slot <= max) {
return new DuplicateResult(true, shouldDuplicateCommands);
}
} else {
// This is a single slot
try {
int dupeSlot = Integer.parseInt(dupeItem);
if(dupeSlot == slot) {
return new DuplicateResult(true, shouldDuplicateCommands);
}
} catch(NumberFormatException ignored) {}
}
}
return new DuplicateResult(false, shouldDuplicateCommands);
}
}

View File

@ -85,6 +85,11 @@ public class OpenGUI {
try {
String[] duplicateItems = pconfig.getString("item." + item + section + ".duplicate").split(",");
for (String tempDupe : duplicateItems) {
//Skip the "slot" if it is just the command setting
if(tempDupe.equalsIgnoreCase("true")){
continue;
}
if (tempDupe.contains("-")) {
//if there is multiple dupe items, convert numbers to ints
int[] bothNumbers = new int[]{Integer.parseInt(tempDupe.split("-")[0]), Integer.parseInt(tempDupe.split("-")[1])};