Add ability to define type of LWC protection in config

This commit is contained in:
Phoenix616 2020-01-15 22:55:40 +01:00
parent d5198bd155
commit 4067427650
6 changed files with 85 additions and 17 deletions

View File

@ -25,7 +25,16 @@ import com.Acrobot.Breeze.Configuration.Annotations.PrecededBySpace;
*/
public class Configuration {
private static Map<String, ValueParser> 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 <T> Object parseToJava(Class<T> type, Object object) {
if (object instanceof String && type.isEnum()) {
return Enum.valueOf((Class<? extends Enum>) 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;
}

View File

@ -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 <T> Object parseToJava(Class<T> type, Object object) {
if (object instanceof ConfigurationSection) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
Map<String, List<String>> map = new HashMap<>();
for (String message : ((ConfigurationSection) object).getKeys(false)) {
map.put(message, ((ConfigurationSection) object).getStringList(message));

View File

@ -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;
@ -23,7 +24,8 @@ public class Properties {
static {
Configuration.registerParser("StringSet", new ValueParser() {
public Object parseToJava(Object object) {
@Override
public <T> Object parseToJava(Class<T> type, Object object) {
if (object instanceof Collection) {
return new LinkedHashSet<>((Collection<String>) object);
}
@ -31,7 +33,8 @@ public class Properties {
}
});
Configuration.registerParser("MaterialSet", new ValueParser() {
public Object parseToJava(Object object) {
@Override
public <T> Object parseToJava(Class<T> type, Object object) {
if (object instanceof Collection) {
EnumSet<Material> set = EnumSet.noneOf(Material.class);
for (Object o : (Collection) object) {
@ -59,7 +62,8 @@ public class Properties {
return super.parseToYAML(object);
}
public Object parseToJava(Object object) {
@Override
public <T> Object parseToJava(Class<T> 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;

View File

@ -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;
}

View File

@ -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" +

View File

@ -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
}
}