From c3b22783778e71a24734f5b210f146cc5d72770d Mon Sep 17 00:00:00 2001 From: Aleksey Terzi Date: Tue, 24 May 2022 21:57:40 -0600 Subject: [PATCH] Added possibility to set lore or name for snitch items --- .../jukealert/gui/SnitchOverviewGUI.java | 2 +- .../listener/SnitchLifeCycleListener.java | 14 ++-- .../untamedears/jukealert/model/Snitch.java | 2 +- .../jukealert/model/SnitchFactoryType.java | 75 ++++++++++++++++++- .../jukealert/model/SnitchTypeManager.java | 45 ++++++++--- paper/src/main/resources/config.yml | 30 ++++++-- 6 files changed, 140 insertions(+), 28 deletions(-) diff --git a/paper/src/main/java/com/untamedears/jukealert/gui/SnitchOverviewGUI.java b/paper/src/main/java/com/untamedears/jukealert/gui/SnitchOverviewGUI.java index c8e68240..90af7e12 100644 --- a/paper/src/main/java/com/untamedears/jukealert/gui/SnitchOverviewGUI.java +++ b/paper/src/main/java/com/untamedears/jukealert/gui/SnitchOverviewGUI.java @@ -33,7 +33,7 @@ private List constructSnitchClickables() { final List clickables = new LinkedList<>(); for (final Snitch snitch : this.snitches) { // Base the snitch icon on the snitch type - final var icon = snitch.getType().getItem().clone(); + final var icon = snitch.getType().getItemRepresentation(); ItemUtils.handleItemMeta(icon, (ItemMeta meta) -> { meta.setDisplayName(ChatColor.GOLD + snitch.getName()); final var location = snitch.getLocation(); diff --git a/paper/src/main/java/com/untamedears/jukealert/listener/SnitchLifeCycleListener.java b/paper/src/main/java/com/untamedears/jukealert/listener/SnitchLifeCycleListener.java index ec71e249..1d8da3e2 100644 --- a/paper/src/main/java/com/untamedears/jukealert/listener/SnitchLifeCycleListener.java +++ b/paper/src/main/java/com/untamedears/jukealert/listener/SnitchLifeCycleListener.java @@ -1,5 +1,6 @@ package com.untamedears.jukealert.listener; +import com.untamedears.jukealert.JukeAlert; import com.untamedears.jukealert.SnitchManager; import com.untamedears.jukealert.model.Snitch; import com.untamedears.jukealert.model.SnitchFactoryType; @@ -31,6 +32,7 @@ public class SnitchLifeCycleListener implements Listener { private SnitchTypeManager configManager; private SnitchManager snitchManager; private Map pendingSnitches; + private Map deletedSnitches; private Logger logger; public SnitchLifeCycleListener(SnitchManager snitchManager, SnitchTypeManager configManager, Logger logger) { @@ -38,6 +40,7 @@ public SnitchLifeCycleListener(SnitchManager snitchManager, SnitchTypeManager co this.snitchManager = snitchManager; this.logger = logger; this.pendingSnitches = new HashMap<>(); + this.deletedSnitches = new HashMap<>(); } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @@ -66,12 +69,12 @@ public void onBlockPlaceCancelled(BlockPlaceEvent event) { public void onBlockBreak(BlockBreakEvent event) { Block block = event.getBlock(); SnitchFactoryType snitchConfig = pendingSnitches.remove(block.getLocation()); - if (snitchConfig == null) { - return; - } - if (block.getType() == snitchConfig.getItem().getType()) { + if (snitchConfig == null) + snitchConfig = this.deletedSnitches.remove(block.getLocation()); + + if (snitchConfig != null && block.getType() == snitchConfig.getItemMaterial()) { event.setDropItems(false); - block.getWorld().dropItemNaturally(block.getLocation(), snitchConfig.getItem()); + block.getWorld().dropItemNaturally(block.getLocation(), snitchConfig.getItemRepresentation()); } } @@ -118,6 +121,7 @@ private void reinforcementGone(Reinforcement rein, Player source) { UUID uuid = source != null ? source.getUniqueId() : null; String name = source != null ? source.getName() : "ENVIRONMENT"; snitch.destroy(uuid, Cause.PLAYER); + this.deletedSnitches.put(rein.getLocation(), snitch.getType()); logger.info(String.format("%s destroyed snitch of type %s at %s", name, snitch.getType().getName(), snitch.getLocation().toString())); } diff --git a/paper/src/main/java/com/untamedears/jukealert/model/Snitch.java b/paper/src/main/java/com/untamedears/jukealert/model/Snitch.java index c2dce3a2..c6d6d418 100644 --- a/paper/src/main/java/com/untamedears/jukealert/model/Snitch.java +++ b/paper/src/main/java/com/untamedears/jukealert/model/Snitch.java @@ -242,7 +242,7 @@ public boolean checkPhysicalIntegrity() { return false; } Block block = getLocation().getBlock(); - if (block.getType() != this.type.getItem().getType()) { + if (block.getType() != this.type.getItemMaterial()) { //block is no longer a snitch destroy(null, Cause.CLEANUP); return false; diff --git a/paper/src/main/java/com/untamedears/jukealert/model/SnitchFactoryType.java b/paper/src/main/java/com/untamedears/jukealert/model/SnitchFactoryType.java index 3333858e..91c837ef 100644 --- a/paper/src/main/java/com/untamedears/jukealert/model/SnitchFactoryType.java +++ b/paper/src/main/java/com/untamedears/jukealert/model/SnitchFactoryType.java @@ -1,29 +1,47 @@ package com.untamedears.jukealert.model; +import com.untamedears.jukealert.JukeAlert; import com.untamedears.jukealert.model.appender.AbstractSnitchAppender; import com.untamedears.jukealert.model.field.FieldManager; import com.untamedears.jukealert.model.field.SingleCuboidRangeManager; + +import java.util.ArrayList; import java.util.List; import java.util.function.Function; + +import net.kyori.adventure.text.Component; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; public class SnitchFactoryType { + public static class ItemInfo { + public Material material; + public String name; + public List lore; + } private final int id; - private final ItemStack item; + private final ItemInfo item; private final String name; private final Function fieldGenerator; private final List> appenders; - public SnitchFactoryType(ItemStack item, Function fieldGenerator, String name, int id, + public SnitchFactoryType(ItemInfo item, Function fieldGenerator, String name, int id, List> appenders) { this.item = item; this.name = name; this.id = id; this.fieldGenerator = fieldGenerator; this.appenders = appenders; + + if (this.item.name != null && this.item.name.length() == 0) + this.item.name = null; + + if (this.item.lore != null && this.item.lore.isEmpty()) + this.item.lore = null; } public Snitch create(int snitchID, Location location, String name, int groupID, boolean isNew) { @@ -53,10 +71,59 @@ public String getName() { return name; } + /** + * @return Material of item used to create instances of this snitch + */ + public Material getItemMaterial() { + return this.item.material; + } + /** * @return Item used to create instances of this snitch */ - public ItemStack getItem() { - return item.clone(); + public ItemStack getItemRepresentation() { + ItemStack item = new ItemStack(this.item.material); + + if (this.item.name != null && this.item.name.length() > 0 || this.item.lore != null && this.item.lore.size() > 0) { + ItemMeta meta = item.getItemMeta(); + + if (this.item.name != null && this.item.name.length() > 0) + meta.displayName(Component.text(this.item.name)); + + if (this.item.lore != null && this.item.lore.size() > 0) { + List loreList = new ArrayList<>(); + for (String loreLine : this.item.lore) + loreList.add(Component.text(loreLine)); + + meta.lore(loreList); + } + + item.setItemMeta(meta); + } + + return item; + } + + public boolean isSame(ItemStack itemStack) { + if (itemStack == null || this.item.material != itemStack.getType()) + return false; + + ItemMeta meta = itemStack.getItemMeta(); + + String name = meta.getDisplayName(); + if (name != null && name.length() == 0) + name = null; + + List lore; + if (meta.hasLore()) { + lore = meta.getLore(); + if (lore != null && lore.isEmpty()) + lore = null; + } else { + lore = null; + } + + return (this.item.name == null && name == null || this.item.name != null && this.item.name.equals(name)) + && (this.item.lore == null && lore == null || this.item.lore != null && this.item.lore.equals(lore)); } } diff --git a/paper/src/main/java/com/untamedears/jukealert/model/SnitchTypeManager.java b/paper/src/main/java/com/untamedears/jukealert/model/SnitchTypeManager.java index 50e8741f..99e526e5 100644 --- a/paper/src/main/java/com/untamedears/jukealert/model/SnitchTypeManager.java +++ b/paper/src/main/java/com/untamedears/jukealert/model/SnitchTypeManager.java @@ -19,18 +19,22 @@ import java.util.Map; import java.util.function.Function; import java.util.logging.Logger; + +import net.kyori.adventure.text.Component; +import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; public class SnitchTypeManager { private Map> appenderClasses; - private Map configFactoriesByItem; + private List configFactories; private Map configFactoriesById; public SnitchTypeManager() { appenderClasses = new HashMap<>(); - configFactoriesByItem = new HashMap<>(); + configFactories = new ArrayList<>(); configFactoriesById = new HashMap<>(); registerAppenderTypes(); } @@ -49,7 +53,7 @@ private void registerAppenderType(String id, Class> appenderInstanciations = new ArrayList<>(); if (config.isConfigurationSection("appender")) { @@ -105,10 +113,25 @@ public boolean parseFromConfig(ConfigurationSection config) { } SnitchFactoryType configFactory = new SnitchFactoryType(item, fieldGenerator, name, id, appenderInstanciations); configFactoriesById.put(configFactory.getID(), configFactory); - configFactoriesByItem.put(configFactory.getItem(), configFactory); + configFactories.add(configFactory); logger.info(sb.toString()); return true; } + + private static SnitchFactoryType.ItemInfo getItemInfo(ConfigurationSection config) { + String type = config.getString("item.material"); + String name = config.getString("item.name"); + List lore = config.getStringList("item.lore"); + + Material material = Material.getMaterial(type); + + SnitchFactoryType.ItemInfo item = new SnitchFactoryType.ItemInfo(); + item.material = material; + item.name = name; + item.lore = lore; + + return item; + } private Function getFieldInstanciation(ConfigurationSection config) { Logger logger = JukeAlert.getInstance().getLogger(); @@ -191,12 +214,12 @@ private Function getAppenderInstantiation( * exists */ public SnitchFactoryType getConfig(ItemStack is) { - if (is == null) { - return null; + for (SnitchFactoryType f : this.configFactories) { + if (f.isSame(is)) + return f; } - ItemStack copy = is.clone(); - copy.setAmount(1); - return configFactoriesByItem.get(copy); + + return null; } public SnitchFactoryType getConfig(int id) { diff --git a/paper/src/main/resources/config.yml b/paper/src/main/resources/config.yml index 5c058274..339c85fb 100644 --- a/paper/src/main/resources/config.yml +++ b/paper/src/main/resources/config.yml @@ -14,9 +14,7 @@ database: snitchConfigs: note: item: - ==: org.bukkit.inventory.ItemStack - type: NOTE_BLOCK - amount: 1 + material: NOTE_BLOCK id: 0 name: Snitch range: @@ -35,9 +33,7 @@ snitchConfigs: showownerondestroy: {} juke: item: - ==: org.bukkit.inventory.ItemStack - type: JUKEBOX - amount: 1 + material: JUKEBOX id: 1 name: Logsnitch range: 11 @@ -69,3 +65,25 @@ snitchConfigs: acceptAll: true lifeTime: 4 weeks hardCap: 100000 + tallsnitch: + item: + material: NOTE_BLOCK + name: Tall Snitch + lore: + - This snitch is better than regular + id: 2 + name: Tall Snitch + range: + type: cuboid + width: 11 + height: 40 + appender: + broadcast: + trigger: + - ENTRY + - LOGIN + - LOGOUT + dormantcull: + lifeTime: 2 weeks + dormantTime: 2 weeks + showownerondestroy: {}