Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private List<IClickable> constructSnitchClickables() {
final List<IClickable> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -31,13 +32,15 @@ public class SnitchLifeCycleListener implements Listener {
private SnitchTypeManager configManager;
private SnitchManager snitchManager;
private Map<Location, SnitchFactoryType> pendingSnitches;
private Map<Location, SnitchFactoryType> deletedSnitches;
private Logger logger;

public SnitchLifeCycleListener(SnitchManager snitchManager, SnitchTypeManager configManager, Logger logger) {
this.configManager = configManager;
this.snitchManager = snitchManager;
this.logger = logger;
this.pendingSnitches = new HashMap<>();
this.deletedSnitches = new HashMap<>();
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> lore;
}

private final int id;
private final ItemStack item;
private final ItemInfo item;
private final String name;
private final Function<Snitch, FieldManager> fieldGenerator;

private final List<Function<Snitch, AbstractSnitchAppender>> appenders;

public SnitchFactoryType(ItemStack item, Function<Snitch, FieldManager> fieldGenerator, String name, int id,
public SnitchFactoryType(ItemInfo item, Function<Snitch, FieldManager> fieldGenerator, String name, int id,
List<Function<Snitch, AbstractSnitchAppender>> 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringUtils.isEmpty(this.item.name)

this.item.name = null;

if (this.item.lore != null && this.item.lore.isEmpty())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CollectionUtils.isEmpty(this.item.lore)

this.item.lore = null;
}

public Snitch create(int snitchID, Location location, String name, int groupID, boolean isNew) {
Expand Down Expand Up @@ -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<Component> 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<String> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Class<? extends AbstractSnitchAppender>> appenderClasses;
private Map<ItemStack, SnitchFactoryType> configFactoriesByItem;
private List<SnitchFactoryType> configFactories;
private Map<Integer, SnitchFactoryType> configFactoriesById;

public SnitchTypeManager() {
appenderClasses = new HashMap<>();
configFactoriesByItem = new HashMap<>();
configFactories = new ArrayList<>();
configFactoriesById = new HashMap<>();
registerAppenderTypes();
}
Expand All @@ -49,7 +53,7 @@ private void registerAppenderType(String id, Class<? extends AbstractSnitchAppen

public boolean parseFromConfig(ConfigurationSection config) {
Logger logger = JukeAlert.getInstance().getLogger();
ItemStack item = config.getItemStack("item", null);
SnitchFactoryType.ItemInfo item = getItemInfo(config);
StringBuilder sb = new StringBuilder();
if (item == null) {
logger.warning("Snitch type at " + config.getCurrentPath() + " had no item specified");
Expand All @@ -74,8 +78,12 @@ public boolean parseFromConfig(ConfigurationSection config) {
sb.append(name);
sb.append(" with id: ");
sb.append(id);
sb.append(", item: ");
sb.append(item.toString());
sb.append(", item.material: ");
sb.append(item.material);
sb.append(", item.name: ");
sb.append(item.name);
sb.append(", item.lore: ");
sb.append(item.lore);
sb.append(", appenders: ");
List<Function<Snitch, AbstractSnitchAppender>> appenderInstanciations = new ArrayList<>();
if (config.isConfigurationSection("appender")) {
Expand Down Expand Up @@ -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<String> 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<Snitch, FieldManager> getFieldInstanciation(ConfigurationSection config) {
Logger logger = JukeAlert.getInstance().getLogger();
Expand Down Expand Up @@ -191,12 +214,12 @@ private Function<Snitch, AbstractSnitchAppender> 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) {
Expand Down
30 changes: 24 additions & 6 deletions paper/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -35,9 +33,7 @@ snitchConfigs:
showownerondestroy: {}
juke:
item:
==: org.bukkit.inventory.ItemStack
type: JUKEBOX
amount: 1
material: JUKEBOX
id: 1
name: Logsnitch
range: 11
Expand Down Expand Up @@ -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: {}