diff --git a/resource/plugin.yml b/resource/plugin.yml index 32b757a..b7e1656 100644 --- a/resource/plugin.yml +++ b/resource/plugin.yml @@ -1,4 +1,4 @@ -version: 3.14.1.3 +version: 3.14.1.4 main: me.rockyhawk.commandpanels.CommandPanels name: CommandPanels author: RockyHawk diff --git a/src/me/rockyhawk/commandpanels/CommandPanels.java b/src/me/rockyhawk/commandpanels/CommandPanels.java index b3ba166..7e02a32 100644 --- a/src/me/rockyhawk/commandpanels/CommandPanels.java +++ b/src/me/rockyhawk/commandpanels/CommandPanels.java @@ -32,6 +32,7 @@ import me.rockyhawk.commandpanels.legacy.PlayerHeads; import me.rockyhawk.commandpanels.openpanelsmanager.OpenGUI; import me.rockyhawk.commandpanels.openpanelsmanager.OpenPanelsLoader; import me.rockyhawk.commandpanels.openpanelsmanager.UtilsPanelsLoader; +import me.rockyhawk.commandpanels.openwithitem.HotbarItemLoader; import me.rockyhawk.commandpanels.openwithitem.SwapItemEvent; import me.rockyhawk.commandpanels.openwithitem.UtilsOpenWithItem; import me.rockyhawk.commandpanels.panelblocks.BlocksTabComplete; @@ -79,6 +80,7 @@ public class CommandPanels extends JavaPlugin { public OpenPanelsLoader openPanels = new OpenPanelsLoader(this); public OpenGUI createGUI = new OpenGUI(this); public CommandPlaceholderLoader customCommand = new CommandPlaceholderLoader(this); + public HotbarItemLoader hotbar = new HotbarItemLoader(this); public File panelsf; public YamlConfiguration blockConfig; //where panel block locations are stored @@ -191,6 +193,9 @@ public class CommandPanels extends JavaPlugin { //load panelFiles reloadPanelFiles(); + //do hotbar items + hotbar.reloadHotbarSlots(); + //add custom charts bStats Metrics metrics = new Metrics(this); metrics.addCustomChart(new Metrics.SingleLineChart("panels_amount", new Callable() { diff --git a/src/me/rockyhawk/commandpanels/classresources/OpenEditorGuis.java b/src/me/rockyhawk/commandpanels/classresources/OpenEditorGuis.java index 435217e..472b8da 100644 --- a/src/me/rockyhawk/commandpanels/classresources/OpenEditorGuis.java +++ b/src/me/rockyhawk/commandpanels/classresources/OpenEditorGuis.java @@ -314,10 +314,9 @@ public class OpenEditorGuis { temp = new ItemStack(Material.BEDROCK, 1); lore.clear(); lore.add(ChatColor.GRAY + "Hotbar location for the item"); - lore.add(ChatColor.GRAY + "choose a number from 1 to 9"); + lore.add(ChatColor.GRAY + "choose a number from 0 to 33"); if (cf.contains("open-with-item.stationary")) { lore.add(ChatColor.WHITE + "-------------------------"); - //in the editor, change the value of 0-8 to 1-9 for simplicity int location = cf.getInt("open-with-item.stationary") + 1; lore.add(ChatColor.WHITE + String.valueOf(location)); } diff --git a/src/me/rockyhawk/commandpanels/commands/Commandpanelsreload.java b/src/me/rockyhawk/commandpanels/commands/Commandpanelsreload.java index cb3cc36..04bd224 100644 --- a/src/me/rockyhawk/commandpanels/commands/Commandpanelsreload.java +++ b/src/me/rockyhawk/commandpanels/commands/Commandpanelsreload.java @@ -32,6 +32,9 @@ public class Commandpanelsreload implements CommandExecutor { //check for duplicates plugin.checkDuplicatePanel(sender); + //reloadHotbarSlots + plugin.hotbar.reloadHotbarSlots(); + //add custom commands registerCommands(); @@ -50,7 +53,14 @@ public class Commandpanelsreload implements CommandExecutor { public void registerCommands(){ ConfigurationSection tempFile; File commandsLoc = new File("commands.yml"); - YamlConfiguration cmdCF = YamlConfiguration.loadConfiguration(commandsLoc); + YamlConfiguration cmdCF; + try { + cmdCF = YamlConfiguration.loadConfiguration(commandsLoc); + }catch(Exception e){ + //could not access the commands.yml file + plugin.debug(e); + return; + } //remove old commandpanels commands for(String existingCommands : cmdCF.getConfigurationSection("aliases").getKeys(false)){ if(cmdCF.getStringList("aliases." + existingCommands).get(0).equals("commandpanel")){ diff --git a/src/me/rockyhawk/commandpanels/ingameeditor/EditorUserInput.java b/src/me/rockyhawk/commandpanels/ingameeditor/EditorUserInput.java index 90418d8..8366f7f 100644 --- a/src/me/rockyhawk/commandpanels/ingameeditor/EditorUserInput.java +++ b/src/me/rockyhawk/commandpanels/ingameeditor/EditorUserInput.java @@ -375,9 +375,9 @@ public class EditorUserInput implements Listener { } try { int loc = Integer.parseInt(e.getMessage()); - if (loc >= 10 || loc <= 0) { + if (loc >= 34 || loc <= -1) { //if the number isn't between 1-9 - p.sendMessage(plugin.papi( plugin.tag + ChatColor.GREEN + "Choose an integer between 1 to 9!")); + p.sendMessage(plugin.papi( plugin.tag + ChatColor.GREEN + "Choose an integer between 0 to 33!")); return; } p.sendMessage(plugin.papi( plugin.tag + ChatColor.GREEN + "Set Hotbar Location to " + loc + "!")); diff --git a/src/me/rockyhawk/commandpanels/openwithitem/HotbarItemLoader.java b/src/me/rockyhawk/commandpanels/openwithitem/HotbarItemLoader.java new file mode 100644 index 0000000..681e5eb --- /dev/null +++ b/src/me/rockyhawk/commandpanels/openwithitem/HotbarItemLoader.java @@ -0,0 +1,79 @@ +package me.rockyhawk.commandpanels.openwithitem; + +import me.rockyhawk.commandpanels.CommandPanels; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import java.io.File; +import java.util.ArrayList; +import java.util.Objects; + +public class HotbarItemLoader { + CommandPanels plugin; + public HotbarItemLoader(CommandPanels pl) { + this.plugin = pl; + } + + //stationary slots 0-8 are the hotbar, using 9-27 for inside the inventory + ArrayList stationaryItems = new ArrayList<>(); //{slot 0-33, index of panelNames} + + //will compile the ArrayList {slot 0-4, index of panelNames} + public void reloadHotbarSlots() { + stationaryItems = new ArrayList<>(); + int i = 0; + for (String[] panelName : plugin.panelNames) { + ConfigurationSection tempFile = YamlConfiguration.loadConfiguration(new File(plugin.panelsf + File.separator + plugin.panelFiles.get(Integer.parseInt(panelName[1])))).getConfigurationSection("panels." + panelName[0]); + if(tempFile.contains("open-with-item.stationary")){ + stationaryItems.add(new int[]{tempFile.getInt("open-with-item.stationary"),i}); + } + i++; + } + } + + //return true if found + public boolean stationaryExecute(int slot, Player p, boolean openPanel){ + for(int[] temp : stationaryItems){ + if(slot == temp[0]){ + if(openPanel) { + String panelName = plugin.panelNames.get(temp[1])[0]; + ConfigurationSection tempFile = YamlConfiguration.loadConfiguration(new File(plugin.panelsf + File.separator + plugin.panelFiles.get(Integer.parseInt(plugin.panelNames.get(temp[1])[1])))).getConfigurationSection("panels." + panelName); + if (plugin.openPanels.hasPanelOpen(p.getName())) { + plugin.openPanels.skipPanels.add(p.getName()); + } + plugin.openVoids.openCommandPanel(p, p, panelName, tempFile, false); + } + return true; + } + } + return false; + } + + //return true if found + public boolean itemCheckExecute(ItemStack invItem, Player p, boolean openPanel){ + for(String[] panelName : plugin.panelNames) { + ConfigurationSection tempFile = YamlConfiguration.loadConfiguration(new File(plugin.panelsf + File.separator + plugin.panelFiles.get(Integer.parseInt(panelName[1])))).getConfigurationSection("panels." + panelName[0]); + String tempName = panelName[0]; + if(tempFile.contains("open-with-item")){ + ItemStack panelItem = plugin.itemCreate.makeItemFromConfig(Objects.requireNonNull(tempFile.getConfigurationSection("open-with-item")), p, false, true); + if(invItem != null && panelItem != null) { + panelItem.setAmount(1); + invItem.setAmount(1); + }else{ + return false; + } + if(panelItem.isSimilar(invItem)){ + if(openPanel) { + if (plugin.openPanels.hasPanelOpen(p.getName())) { + plugin.openPanels.skipPanels.add(p.getName()); + } + plugin.openVoids.openCommandPanel(p, p, tempName, tempFile, false); + } + return true; + } + } + } + return false; + } +} \ No newline at end of file diff --git a/src/me/rockyhawk/commandpanels/openwithitem/SwapItemEvent.java b/src/me/rockyhawk/commandpanels/openwithitem/SwapItemEvent.java index 3e768ed..ad59427 100644 --- a/src/me/rockyhawk/commandpanels/openwithitem/SwapItemEvent.java +++ b/src/me/rockyhawk/commandpanels/openwithitem/SwapItemEvent.java @@ -1,17 +1,10 @@ package me.rockyhawk.commandpanels.openwithitem; import me.rockyhawk.commandpanels.CommandPanels; -import org.bukkit.Material; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerSwapHandItemsEvent; -import org.bukkit.inventory.ItemStack; - -import java.io.File; -import java.util.Iterator; -import java.util.Objects; public class SwapItemEvent implements Listener { CommandPanels plugin; @@ -25,47 +18,9 @@ public class SwapItemEvent implements Listener { return; } Player p = e.getPlayer(); - try { - if (plugin.panelFiles == null) { - return; - } - }catch(Exception b){ - return; - } - String tpanels; //tpanels is the temp to check through the files - ItemStack clicked = e.getOffHandItem(); - for(String fileName : plugin.panelFiles) { //will loop through all the files in folder - YamlConfiguration temp = YamlConfiguration.loadConfiguration(new File(plugin.panelsf + File.separator + fileName)); - String key; - tpanels = ""; - if(!plugin.checkPanels(temp)){ - continue; - } - for (Iterator var10 = Objects.requireNonNull(temp.getConfigurationSection("panels")).getKeys(false).iterator(); var10.hasNext(); tpanels = tpanels + key + " ") { - key = (String) var10.next(); - if(temp.contains("panels." + key + ".open-with-item")){ - assert clicked != null; - if(clicked.getType() != Material.AIR) { - //if loop has material first to stop 1.12.2 from spitting errors - //try and catch loop to stop errors with the same material type but different name - try { - if (clicked.getType() == new ItemStack(Objects.requireNonNull(Material.matchMaterial(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.material")))), 1).getType()) { - if ((plugin.papi( Objects.requireNonNull(clicked.getItemMeta()).getDisplayName()).equals(plugin.papi( Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.name")))))) { - //cancel the click item event - if(temp.contains("panels." + key + ".open-with-item.stationary")){ - if(p.getInventory().getHeldItemSlot() == Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary")))){ - e.setCancelled(true); - } - } - return; - } - } - }catch(NullPointerException | IllegalArgumentException n){ - plugin.debug(n); - } - } - } - } + if(plugin.hotbar.itemCheckExecute(e.getOffHandItem(),p,false)){ + e.setCancelled(true); + p.updateInventory(); } } } diff --git a/src/me/rockyhawk/commandpanels/openwithitem/UtilsOpenWithItem.java b/src/me/rockyhawk/commandpanels/openwithitem/UtilsOpenWithItem.java index 587cb8a..aa444ac 100644 --- a/src/me/rockyhawk/commandpanels/openwithitem/UtilsOpenWithItem.java +++ b/src/me/rockyhawk/commandpanels/openwithitem/UtilsOpenWithItem.java @@ -3,10 +3,8 @@ package me.rockyhawk.commandpanels.openwithitem; import me.rockyhawk.commandpanels.CommandPanels; import me.rockyhawk.commandpanels.ioclasses.GetItemInHand; import me.rockyhawk.commandpanels.ioclasses.GetItemInHand_Legacy; -import me.rockyhawk.commandpanels.ioclasses.Sequence_1_14; import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -15,7 +13,6 @@ import org.bukkit.event.block.Action; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.player.*; import org.bukkit.inventory.ItemStack; @@ -36,39 +33,17 @@ public class UtilsOpenWithItem implements Listener { //if none of the panels have open-with-item return; } - ItemStack clicked = e.getCurrentItem(); Player p = (Player)e.getWhoClicked(); //get the item clicked, then loop through panel names after action isn't nothing if(e.getAction() == InventoryAction.NOTHING){return;} - if (e.getRawSlot() == -999) {return;} - if (e.getSlotType() != InventoryType.SlotType.QUICKBAR) {return;} - for(String[] panelName : plugin.panelNames){ - ConfigurationSection tempFile = YamlConfiguration.loadConfiguration(new File(plugin.panelsf + File.separator + plugin.panelFiles.get(Integer.parseInt(panelName[1])))).getConfigurationSection("panels." + panelName[0]); - String tempName = panelName[0]; - if(tempFile.contains("open-with-item") && Objects.requireNonNull(e.getClickedInventory()).getType() == InventoryType.PLAYER) { - try{ - assert clicked != null; - if (clicked.getType() == new ItemStack(Objects.requireNonNull(plugin.itemCreate.makeItemFromConfig(Objects.requireNonNull(tempFile.getConfigurationSection("open-with-item")), p, false, true).getType()), 1).getType()) { - if ((plugin.papi( Objects.requireNonNull(clicked.getItemMeta()).getDisplayName()).equals(plugin.papi( Objects.requireNonNull(tempFile.getString("open-with-item.name")))))) { - //cancel the click item event - if (tempFile.contains("open-with-item.stationary")) { - if (e.getSlot() == Integer.parseInt(Objects.requireNonNull(tempFile.getString("open-with-item.stationary")))) { - e.setCancelled(true); - p.updateInventory(); - if(plugin.openPanels.hasPanelOpen(p.getName())) { - plugin.openPanels.skipPanels.add(p.getName()); - } - plugin.openVoids.openCommandPanel(p,p,tempName,tempFile,false); - return; - } - } - return; - } - } - }catch(NullPointerException cancel){ - //do nothing skip item - } - } + if(plugin.hotbar.stationaryExecute(e.getSlot(),p,true)){ + e.setCancelled(true); + p.updateInventory(); + return; + } + if(plugin.hotbar.itemCheckExecute(e.getCurrentItem(),p,false) || plugin.hotbar.itemCheckExecute(e.getCursor(),p,false) || plugin.hotbar.stationaryExecute(e.getHotbarButton(),p,false)){ + e.setCancelled(true); + p.updateInventory(); } } @EventHandler @@ -82,40 +57,13 @@ public class UtilsOpenWithItem implements Listener { if(e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK || Objects.requireNonNull(e.getItem()).getType() == Material.AIR){ return; } - if (plugin.panelFiles == null) { - return; - } }catch(Exception b){ return; } - ItemStack clicked = e.getItem(); Player p = e.getPlayer(); - ConfigurationSection tempFile; - String tempName; - for(String[] panelName : plugin.panelNames){ - tempFile = YamlConfiguration.loadConfiguration(new File(plugin.panelsf + File.separator + plugin.panelFiles.get(Integer.parseInt(panelName[1])))).getConfigurationSection("panels." + panelName[0]); - tempName = panelName[0]; - if(tempFile.contains("open-with-item")) { - try{ - assert clicked != null; - if (clicked.getType() == new ItemStack(Objects.requireNonNull(plugin.itemCreate.makeItemFromConfig(Objects.requireNonNull(tempFile.getConfigurationSection("open-with-item")), p, false, true).getType()), 1).getType()) { - if ((plugin.papi( Objects.requireNonNull(clicked.getItemMeta()).getDisplayName()).equals(plugin.papi(Objects.requireNonNull(tempFile.getString("open-with-item.name")))))) { - //cancel the click item event - if (tempFile.contains("open-with-item.stationary")) { - if (p.getInventory().getHeldItemSlot() != Integer.parseInt(Objects.requireNonNull(tempFile.getString("open-with-item.stationary")))) { - return; - } - } - e.setCancelled(true); - p.updateInventory(); - plugin.openVoids.openCommandPanel(p,p,tempName,tempFile,false); - return; - } - } - }catch(NullPointerException cancel){ - //do nothing skip item - } - } + if(plugin.hotbar.itemCheckExecute(e.getItem(),p,true)){ + e.setCancelled(true); + p.updateInventory(); } } @EventHandler @@ -162,7 +110,7 @@ public class UtilsOpenWithItem implements Listener { if (p.hasPermission("commandpanel.panel." + temp.getString("panels." + key + ".perm")) && temp.contains("panels." + key + ".open-with-item")) { ItemStack s = plugin.itemCreate.makeItemFromConfig(Objects.requireNonNull(temp.getConfigurationSection("panels." + key + ".open-with-item")), p, false, true); if(temp.contains("panels." + key + ".open-with-item.stationary")) { - if (0 <= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))) && 8 >= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary")))) { + if (0 <= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))) && 33 >= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary")))) { p.getInventory().setItem(Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))), s); } } @@ -203,7 +151,7 @@ public class UtilsOpenWithItem implements Listener { } if (p.hasPermission("commandpanel.panel." + temp.getString("panels." + key + ".perm")) && temp.contains("panels." + key + ".open-with-item")) { ItemStack s = plugin.itemCreate.makeItemFromConfig(Objects.requireNonNull(temp.getConfigurationSection("panels." + key + ".open-with-item")), p, false, true); - if(temp.contains("panels." + key + ".open-with-item.stationary") && 0 <= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))) && 8 >= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary")))){ + if(temp.contains("panels." + key + ".open-with-item.stationary") && 0 <= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))) && 33 >= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary")))){ p.getInventory().setItem(Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))), s); } } @@ -275,7 +223,7 @@ public class UtilsOpenWithItem implements Listener { } } ItemStack s = plugin.itemCreate.makeItemFromConfig(Objects.requireNonNull(temp.getConfigurationSection("panels." + key + ".open-with-item")), p, false, true); - if(temp.contains("panels." + key + ".open-with-item.stationary") && 0 <= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))) && 8 >= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary")))){ + if(temp.contains("panels." + key + ".open-with-item.stationary") && 0 <= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))) && 33 >= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary")))){ p.getInventory().setItem(Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))), s); } }else{ @@ -287,7 +235,7 @@ public class UtilsOpenWithItem implements Listener { continue; } plugin.setName(s, temp.getString("panels." + key + ".open-with-item.name"), temp.getStringList("panels." + key + ".open-with-item.lore"),p,true, true); - if(temp.contains("panels." + key + ".open-with-item.stationary") && 0 <= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))) && 8 >= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary")))){ + if(temp.contains("panels." + key + ".open-with-item.stationary") && 0 <= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))) && 33 >= Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary")))){ try { if (Objects.requireNonNull(p.getInventory().getItem(Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))))).isSimilar(s)) { p.getInventory().setItem(Integer.parseInt(Objects.requireNonNull(temp.getString("panels." + key + ".open-with-item.stationary"))), null); @@ -347,53 +295,15 @@ public class UtilsOpenWithItem implements Listener { } //cancel everything if holding item (item frames eg) Player p = e.getPlayer(); - try { - if (plugin.panelFiles == null) { - return; - } - }catch(Exception b){ - return; - } - ItemStack clicked; if(Bukkit.getVersion().contains("1.8")){ clicked = new GetItemInHand_Legacy(plugin).itemInHand(p); }else{ clicked = new GetItemInHand(plugin).itemInHand(p); } - - String tempName; - ConfigurationSection tempFile; - for(String[] panelName : plugin.panelNames){ - tempName = panelName[0]; - tempFile = YamlConfiguration.loadConfiguration(new File(plugin.panelsf + File.separator + plugin.panelFiles.get(Integer.parseInt(panelName[1])))).getConfigurationSection("panels." + tempName); - - if (tempFile.contains("open-with-item")) { - if (clicked.getType() != Material.AIR) { - //if loop has material first to stop 1.12.2 from spitting errors - //try and catch loop to stop errors with the same material type but different name - try { - if (clicked.getType() == new ItemStack(Objects.requireNonNull(Material.matchMaterial(Objects.requireNonNull(tempFile.getString("open-with-item.material")))), 1).getType()) { - if ((plugin.papi( Objects.requireNonNull(clicked.getItemMeta()).getDisplayName()).equals(plugin.papi( Objects.requireNonNull(tempFile.getString("open-with-item.name")))))) { - //cancel the click item event - if (tempFile.contains("open-with-item.stationary")) { - if (p.getInventory().getHeldItemSlot() == Integer.parseInt(Objects.requireNonNull(tempFile.getString("open-with-item.stationary")))) { - e.setCancelled(true); - p.updateInventory(); - if(plugin.openPanels.hasPanelOpen(p.getName())) { - plugin.openPanels.skipPanels.add(p.getName()); - } - plugin.openVoids.openCommandPanel(p,p,tempName,tempFile,false); - } - } - return; - } - } - } catch (NullPointerException | IllegalArgumentException n) { - plugin.debug(n); - } - } - } + if(plugin.hotbar.itemCheckExecute(clicked,p,true)){ + e.setCancelled(true); + p.updateInventory(); } } }