Merge pull request #29 from Chew/chore/move-gui-code

Move createGuiItem methods to their own util class
This commit is contained in:
SeanOMik 2021-09-27 12:52:25 -04:00 committed by GitHub
commit d7bb2ac0ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 83 deletions

View File

@ -1,7 +1,6 @@
package net.seanomik.energeticstorage.gui; package net.seanomik.energeticstorage.gui;
import de.tr7zw.changeme.nbtapi.NBTItem; import de.tr7zw.changeme.nbtapi.NBTItem;
import net.seanomik.energeticstorage.EnergeticStorage;
import net.seanomik.energeticstorage.files.PlayersFile; import net.seanomik.energeticstorage.files.PlayersFile;
import net.seanomik.energeticstorage.objects.ESDrive; import net.seanomik.energeticstorage.objects.ESDrive;
import net.seanomik.energeticstorage.objects.ESSystem; import net.seanomik.energeticstorage.objects.ESSystem;
@ -17,9 +16,14 @@ import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.*; import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import static net.seanomik.energeticstorage.utils.GUIHelper.createGuiItem;
public class ESDriveGUI implements InventoryHolder, Listener { public class ESDriveGUI implements InventoryHolder, Listener {
private final Inventory globalInv; private final Inventory globalInv;
@ -65,34 +69,6 @@ public class ESDriveGUI implements InventoryHolder, Listener {
} }
} }
private ItemStack createGuiItem(Material material, String name, List<String> description) {
ItemStack item = new ItemStack(material, 1);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
meta.setLore(description);
item.setItemMeta(meta);
return item;
}
private ItemStack createGuiItem(Material material, List<String> description) {
ItemStack item = new ItemStack(material, 1);
ItemMeta meta = item.getItemMeta();
meta.setLore(description);
item.setItemMeta(meta);
return item;
}
private ItemStack createGuiItem(Material material, String name) {
ItemStack item = new ItemStack(material, 1);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
item.setItemMeta(meta);
return item;
}
// You can open the inventory with this // You can open the inventory with this
public void openInventory(Player p, ESSystem esSystem) { public void openInventory(Player p, ESSystem esSystem) {
p.openInventory(globalInv); p.openInventory(globalInv);

View File

@ -18,10 +18,15 @@ import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.inventory.meta.SkullMeta;
import java.util.*; import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static net.seanomik.energeticstorage.utils.GUIHelper.createGuiItem;
public class ESSystemSecurityGUI implements InventoryHolder, Listener { public class ESSystemSecurityGUI implements InventoryHolder, Listener {
private final Inventory inv; private final Inventory inv;
@ -115,34 +120,6 @@ public class ESSystemSecurityGUI implements InventoryHolder, Listener {
} }
} }
private ItemStack createGuiItem(Material material, String name, List<String> description) {
ItemStack item = new ItemStack(material, 1);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
meta.setLore(description);
item.setItemMeta(meta);
return item;
}
private ItemStack createGuiItem(Material material, List<String> description) {
ItemStack item = new ItemStack(material, 1);
ItemMeta meta = item.getItemMeta();
meta.setLore(description);
item.setItemMeta(meta);
return item;
}
private ItemStack createGuiItem(Material material, String name) {
ItemStack item = new ItemStack(material, 1);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
item.setItemMeta(meta);
return item;
}
public void openInventory(Player p, ESSystem esSystem) { public void openInventory(Player p, ESSystem esSystem) {
if (openSystems.containsKey(p)) { if (openSystems.containsKey(p)) {
openSystems.replace(p, esSystem); openSystems.replace(p, esSystem);

View File

@ -10,8 +10,6 @@ import net.wesjd.anvilgui.AnvilGUI;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -24,7 +22,15 @@ import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static net.seanomik.energeticstorage.utils.GUIHelper.createGuiItem;
public class ESTerminalGUI implements InventoryHolder, Listener { public class ESTerminalGUI implements InventoryHolder, Listener {
private final Inventory globalInv; private final Inventory globalInv;
@ -208,25 +214,6 @@ public class ESTerminalGUI implements InventoryHolder, Listener {
} }
} }
private ItemStack createGuiItem(Material material, String name) {
ItemStack item = new ItemStack(material, 1);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
item.setItemMeta(meta);
return item;
}
private ItemStack createGuiItem(Material material, String name, List<String> lore) {
ItemStack item = new ItemStack(material, 1);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
meta.setLore(lore);
item.setItemMeta(meta);
return item;
}
private enum ClickType { private enum ClickType {
NONE, NONE,
SWAP, SWAP,

View File

@ -0,0 +1,57 @@
package net.seanomik.energeticstorage.utils;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class GUIHelper {
/**
* Creates a GUI Item with a given material, display name, and optional lore
*
* @param material The material to use
* @param name The name of the item
* @param lore Optional lore to add to the item
* @return An item stack to be used in the GUI.
*/
public static ItemStack createGuiItem(Material material, String name, String... lore) {
ItemStack item = new ItemStack(material, 1);
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(ChatColor.RESET + name);
if (lore.length > 0) {
ArrayList<String> metaLore = new ArrayList<>(Arrays.asList(lore));
meta.setLore(metaLore);
}
item.setItemMeta(meta);
}
return item;
}
/**
* Creates a GUI item with given material, display name, and required lore
*
* @param material The material to use
* @param name The name of the item
* @param lore Optional lore to add to the item
* @return An item stack to be used in the GUI.
*/
public static ItemStack createGuiItem(Material material, String name, List<String> lore) {
ItemStack item = new ItemStack(material, 1);
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(ChatColor.RESET + name);
meta.setLore(lore);
}
item.setItemMeta(meta);
return item;
}
}