From 40674276501b7d28933724754e18a012f2d25069 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Wed, 15 Jan 2020 22:55:40 +0100 Subject: [PATCH] Add ability to define type of LWC protection in config --- .../Breeze/Configuration/Configuration.java | 18 +++++++++-- .../Breeze/Configuration/ValueParser.java | 5 +-- .../ChestShop/Configuration/Properties.java | 22 +++++++++---- .../Events/Protection/ProtectBlockEvent.java | 13 +++++++- .../Plugins/LightweightChestProtection.java | 31 ++++++++++++++++--- .../java/com/Acrobot/ChestShop/Security.java | 13 +++++++- 6 files changed, 85 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/Acrobot/Breeze/Configuration/Configuration.java b/src/main/java/com/Acrobot/Breeze/Configuration/Configuration.java index c594b5f..e62be3b 100644 --- a/src/main/java/com/Acrobot/Breeze/Configuration/Configuration.java +++ b/src/main/java/com/Acrobot/Breeze/Configuration/Configuration.java @@ -25,7 +25,16 @@ import com.Acrobot.Breeze.Configuration.Annotations.PrecededBySpace; */ public class Configuration { private static Map parsers = new HashMap<>(); - public static ValueParser DEFAULT_PARSER = new ValueParser(); + private static final ValueParser DEFAULT_PARSER = new ValueParser(); + private static final ValueParser ENUM_PARSER = new ValueParser() { + @Override + public Object parseToJava(Class type, Object object) { + if (object instanceof String && type.isEnum()) { + return Enum.valueOf((Class) type, ((String) object).toUpperCase()); + } + return object; + } + }; /** * Loads a YAML-formatted file into a class and modifies the file if some of class's fields are missing @@ -52,9 +61,9 @@ public class Configuration { try { if (config.isSet(path)) { - field.set(null, getParser(field).parseToJava(config.get(path))); + field.set(null, getParser(field).parseToJava(field.getType(), config.get(path))); } else if (config.isSet(path.toLowerCase())) { - field.set(null, getParser(field).parseToJava(config.get(path.toLowerCase()))); + field.set(null, getParser(field).parseToJava(field.getType(), config.get(path.toLowerCase()))); } else { if (field.isAnnotationPresent(PrecededBySpace.class)) { writer.newLine(); @@ -147,6 +156,9 @@ public class Configuration { if (parser == null) { parser = Configuration.getParser(field.getType().getSimpleName()); } + if (parser == null && field.getType().isEnum()) { + parser = Configuration.ENUM_PARSER; + } if (parser == null) { parser = Configuration.DEFAULT_PARSER; } diff --git a/src/main/java/com/Acrobot/Breeze/Configuration/ValueParser.java b/src/main/java/com/Acrobot/Breeze/Configuration/ValueParser.java index d69a1f3..b9a3588 100644 --- a/src/main/java/com/Acrobot/Breeze/Configuration/ValueParser.java +++ b/src/main/java/com/Acrobot/Breeze/Configuration/ValueParser.java @@ -34,12 +34,13 @@ public class ValueParser { /** * Parses a YAML "object" to Java-compatible object * + * @param type The type of the returned object * @param object Object to parse * @return Java-compatible object */ - public Object parseToJava(Object object) { + public Object parseToJava(Class type, Object object) { if (object instanceof ConfigurationSection) { - Map> map = new HashMap>(); + Map> map = new HashMap<>(); for (String message : ((ConfigurationSection) object).getKeys(false)) { map.put(message, ((ConfigurationSection) object).getStringList(message)); diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java index 6069669..1120cf2 100644 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java +++ b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java @@ -6,6 +6,7 @@ import com.Acrobot.Breeze.Configuration.Annotations.PrecededBySpace; import com.Acrobot.Breeze.Configuration.Configuration; import com.Acrobot.Breeze.Configuration.ValueParser; import com.Acrobot.ChestShop.ChestShop; +import com.Acrobot.ChestShop.Security; import org.bukkit.Material; import java.math.BigDecimal; @@ -22,16 +23,18 @@ import java.util.logging.Level; public class Properties { static { - Configuration.registerParser("StringSet", new ValueParser(){ - public Object parseToJava(Object object) { + Configuration.registerParser("StringSet", new ValueParser() { + @Override + public Object parseToJava(Class type, Object object) { if (object instanceof Collection) { return new LinkedHashSet<>((Collection) object); } return object; } }); - Configuration.registerParser("MaterialSet", new ValueParser(){ - public Object parseToJava(Object object) { + Configuration.registerParser("MaterialSet", new ValueParser() { + @Override + public Object parseToJava(Class type, Object object) { if (object instanceof Collection) { EnumSet set = EnumSet.noneOf(Material.class); for (Object o : (Collection) object) { @@ -50,7 +53,7 @@ public class Properties { return object; } }); - Configuration.registerParser("BigDecimal", new ValueParser(){ + Configuration.registerParser("BigDecimal", new ValueParser() { @Override public String parseToYAML(Object object) { if (object instanceof BigDecimal) { @@ -59,7 +62,8 @@ public class Properties { return super.parseToYAML(object); } - public Object parseToJava(Object object) { + @Override + public Object parseToJava(Class type, Object object) { if (object instanceof Double) { return BigDecimal.valueOf((Double) object); } else if (object instanceof Long) { @@ -215,9 +219,15 @@ public class Properties { @ConfigurationComment("Do you want to protect shop chests with LWC?") public static boolean PROTECT_CHEST_WITH_LWC = false; + @ConfigurationComment("Of which type should the container protection be? Possible type: public, private, donate and on some LWC versions display") + public static Security.Type LWC_CHEST_PROTECTION_TYPE = Security.Type.PRIVATE; + @ConfigurationComment("Do you want to protect shop signs with LWC?") public static boolean PROTECT_SIGN_WITH_LWC = false; + @ConfigurationComment("Of which type should the sign protection be? Possible type: public, private, donate and on some LWC versions display") + public static Security.Type LWC_SIGN_PROTECTION_TYPE = Security.Type.PRIVATE; + @ConfigurationComment("Should the chest's LWC protection be removed once the shop sign is destroyed? ") public static boolean REMOVE_LWC_PROTECTION_AUTOMATICALLY = true; diff --git a/src/main/java/com/Acrobot/ChestShop/Events/Protection/ProtectBlockEvent.java b/src/main/java/com/Acrobot/ChestShop/Events/Protection/ProtectBlockEvent.java index 6c2aaea..508ea09 100644 --- a/src/main/java/com/Acrobot/ChestShop/Events/Protection/ProtectBlockEvent.java +++ b/src/main/java/com/Acrobot/ChestShop/Events/Protection/ProtectBlockEvent.java @@ -1,5 +1,6 @@ package com.Acrobot.ChestShop.Events.Protection; +import com.Acrobot.ChestShop.Security; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.Event; @@ -16,7 +17,8 @@ public class ProtectBlockEvent extends Event { private final Player player; private final UUID protectionOwner; - private Block block; + private final Block block; + private final Security.Type type; boolean isProtected = false; @@ -25,9 +27,14 @@ public class ProtectBlockEvent extends Event { } public ProtectBlockEvent(Block block, Player player, UUID protectionOwner) { + this(block, player, protectionOwner, Security.Type.PRIVATE); + } + + public ProtectBlockEvent(Block block, Player player, UUID protectionOwner, Security.Type type) { this.block = block; this.player = player; this.protectionOwner = protectionOwner; + this.type = type; } public boolean isProtected() { @@ -50,6 +57,10 @@ public class ProtectBlockEvent extends Event { return protectionOwner; } + public Security.Type getType() { + return type; + } + public HandlerList getHandlers() { return handlers; } diff --git a/src/main/java/com/Acrobot/ChestShop/Plugins/LightweightChestProtection.java b/src/main/java/com/Acrobot/ChestShop/Plugins/LightweightChestProtection.java index c345f2f..fcaf930 100644 --- a/src/main/java/com/Acrobot/ChestShop/Plugins/LightweightChestProtection.java +++ b/src/main/java/com/Acrobot/ChestShop/Plugins/LightweightChestProtection.java @@ -27,6 +27,14 @@ public class LightweightChestProtection implements Listener { public LightweightChestProtection() { this.lwc = LWC.getInstance(); + try { + if (Properties.PROTECT_SIGN_WITH_LWC) + Protection.Type.valueOf(Properties.LWC_SIGN_PROTECTION_TYPE.name()); + if (Properties.PROTECT_CHEST_WITH_LWC) + Protection.Type.valueOf(Properties.LWC_CHEST_PROTECTION_TYPE.name()); + } catch (IllegalArgumentException e) { + ChestShop.getBukkitLogger().warning("Your installed LWC version doesn't seem to support the configured protection type! " + e.getMessage()); + } } @EventHandler @@ -36,13 +44,13 @@ public class LightweightChestProtection implements Listener { Container connectedContainer = event.getContainer(); if (Properties.PROTECT_SIGN_WITH_LWC) { - if (!Security.protect(player, sign.getBlock(), event.getOwnerAccount() != null ? event.getOwnerAccount().getUuid() : player.getUniqueId())) { + if (!Security.protect(player, sign.getBlock(), event.getOwnerAccount() != null ? event.getOwnerAccount().getUuid() : player.getUniqueId(), Properties.LWC_SIGN_PROTECTION_TYPE)) { player.sendMessage(Messages.prefix(Messages.NOT_ENOUGH_PROTECTIONS)); } } if (Properties.PROTECT_CHEST_WITH_LWC && connectedContainer != null - && Security.protect(player, connectedContainer.getBlock(), event.getOwnerAccount() != null ? event.getOwnerAccount().getUuid() : player.getUniqueId())) { + && Security.protect(player, connectedContainer.getBlock(), event.getOwnerAccount() != null ? event.getOwnerAccount().getUuid() : player.getUniqueId(), Properties.LWC_CHEST_PROTECTION_TYPE)) { player.sendMessage(Messages.prefix(Messages.PROTECTED_SHOP)); } } @@ -100,16 +108,31 @@ public class LightweightChestProtection implements Listener { return; } + Protection.Type type = Protection.Type.PRIVATE; + switch (event.getType()) { + case PUBLIC: + type = Protection.Type.PUBLIC; + break; + case DONATION: + type = Protection.Type.DONATION; + break; + case DISPLAY: + try { + type = Protection.Type.valueOf("DISPLAY"); + } catch (IllegalArgumentException ignored) {} + break; + } + Protection protection = null; try { - protection = lwc.getPhysicalDatabase().registerProtection(block.getType(), Protection.Type.PRIVATE, worldName, event.getProtectionOwner().toString(), "", x, y, z); + protection = lwc.getPhysicalDatabase().registerProtection(block.getType(), type, worldName, event.getProtectionOwner().toString(), "", x, y, z); } catch (LinkageError e) { try { int blockId = com.griefcraft.cache.BlockCache.getInstance().getBlockId(block); if (blockId < 0) { return; } - protection = lwc.getPhysicalDatabase().registerProtection(blockId, Protection.Type.PRIVATE, worldName, event.getProtectionOwner().toString(), "", x, y, z); + protection = lwc.getPhysicalDatabase().registerProtection(blockId, type, worldName, event.getProtectionOwner().toString(), "", x, y, z); } catch (LinkageError e2) { ChestShop.getBukkitLogger().warning( "Incompatible LWC version installed! (" + lwc.getPlugin().getName() + " v" + lwc.getVersion() + ") \n" + diff --git a/src/main/java/com/Acrobot/ChestShop/Security.java b/src/main/java/com/Acrobot/ChestShop/Security.java index d361769..9c2b611 100644 --- a/src/main/java/com/Acrobot/ChestShop/Security.java +++ b/src/main/java/com/Acrobot/ChestShop/Security.java @@ -29,7 +29,11 @@ public class Security { } public static boolean protect(Player player, Block block, UUID protectionOwner) { - ProtectBlockEvent event = new ProtectBlockEvent(block, player, protectionOwner); + return protect(player, block, protectionOwner, Type.PRIVATE); + } + + public static boolean protect(Player player, Block block, UUID protectionOwner, Type type) { + ProtectBlockEvent event = new ProtectBlockEvent(block, player, protectionOwner, type); ChestShop.callEvent(event); return event.isProtected(); @@ -94,4 +98,11 @@ public class Security { } return false; } + + public enum Type { + PUBLIC, + PRIVATE, + DONATION, + DISPLAY + } }