mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-11-25 19:45:36 +01:00
Add ability to define type of LWC protection in config
This commit is contained in:
parent
d5198bd155
commit
4067427650
@ -25,7 +25,16 @@ import com.Acrobot.Breeze.Configuration.Annotations.PrecededBySpace;
|
|||||||
*/
|
*/
|
||||||
public class Configuration {
|
public class Configuration {
|
||||||
private static Map<String, ValueParser> parsers = new HashMap<>();
|
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
|
* 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 {
|
try {
|
||||||
if (config.isSet(path)) {
|
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())) {
|
} 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 {
|
} else {
|
||||||
if (field.isAnnotationPresent(PrecededBySpace.class)) {
|
if (field.isAnnotationPresent(PrecededBySpace.class)) {
|
||||||
writer.newLine();
|
writer.newLine();
|
||||||
@ -147,6 +156,9 @@ public class Configuration {
|
|||||||
if (parser == null) {
|
if (parser == null) {
|
||||||
parser = Configuration.getParser(field.getType().getSimpleName());
|
parser = Configuration.getParser(field.getType().getSimpleName());
|
||||||
}
|
}
|
||||||
|
if (parser == null && field.getType().isEnum()) {
|
||||||
|
parser = Configuration.ENUM_PARSER;
|
||||||
|
}
|
||||||
if (parser == null) {
|
if (parser == null) {
|
||||||
parser = Configuration.DEFAULT_PARSER;
|
parser = Configuration.DEFAULT_PARSER;
|
||||||
}
|
}
|
||||||
|
@ -34,12 +34,13 @@ public class ValueParser {
|
|||||||
/**
|
/**
|
||||||
* Parses a YAML "object" to Java-compatible object
|
* Parses a YAML "object" to Java-compatible object
|
||||||
*
|
*
|
||||||
|
* @param type The type of the returned object
|
||||||
* @param object Object to parse
|
* @param object Object to parse
|
||||||
* @return Java-compatible object
|
* @return Java-compatible object
|
||||||
*/
|
*/
|
||||||
public Object parseToJava(Object object) {
|
public <T> Object parseToJava(Class<T> type, Object object) {
|
||||||
if (object instanceof ConfigurationSection) {
|
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)) {
|
for (String message : ((ConfigurationSection) object).getKeys(false)) {
|
||||||
map.put(message, ((ConfigurationSection) object).getStringList(message));
|
map.put(message, ((ConfigurationSection) object).getStringList(message));
|
||||||
|
@ -6,6 +6,7 @@ import com.Acrobot.Breeze.Configuration.Annotations.PrecededBySpace;
|
|||||||
import com.Acrobot.Breeze.Configuration.Configuration;
|
import com.Acrobot.Breeze.Configuration.Configuration;
|
||||||
import com.Acrobot.Breeze.Configuration.ValueParser;
|
import com.Acrobot.Breeze.Configuration.ValueParser;
|
||||||
import com.Acrobot.ChestShop.ChestShop;
|
import com.Acrobot.ChestShop.ChestShop;
|
||||||
|
import com.Acrobot.ChestShop.Security;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
@ -22,16 +23,18 @@ import java.util.logging.Level;
|
|||||||
public class Properties {
|
public class Properties {
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Configuration.registerParser("StringSet", new ValueParser(){
|
Configuration.registerParser("StringSet", new ValueParser() {
|
||||||
public Object parseToJava(Object object) {
|
@Override
|
||||||
|
public <T> Object parseToJava(Class<T> type, Object object) {
|
||||||
if (object instanceof Collection) {
|
if (object instanceof Collection) {
|
||||||
return new LinkedHashSet<>((Collection<String>) object);
|
return new LinkedHashSet<>((Collection<String>) object);
|
||||||
}
|
}
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Configuration.registerParser("MaterialSet", new ValueParser(){
|
Configuration.registerParser("MaterialSet", new ValueParser() {
|
||||||
public Object parseToJava(Object object) {
|
@Override
|
||||||
|
public <T> Object parseToJava(Class<T> type, Object object) {
|
||||||
if (object instanceof Collection) {
|
if (object instanceof Collection) {
|
||||||
EnumSet<Material> set = EnumSet.noneOf(Material.class);
|
EnumSet<Material> set = EnumSet.noneOf(Material.class);
|
||||||
for (Object o : (Collection) object) {
|
for (Object o : (Collection) object) {
|
||||||
@ -50,7 +53,7 @@ public class Properties {
|
|||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Configuration.registerParser("BigDecimal", new ValueParser(){
|
Configuration.registerParser("BigDecimal", new ValueParser() {
|
||||||
@Override
|
@Override
|
||||||
public String parseToYAML(Object object) {
|
public String parseToYAML(Object object) {
|
||||||
if (object instanceof BigDecimal) {
|
if (object instanceof BigDecimal) {
|
||||||
@ -59,7 +62,8 @@ public class Properties {
|
|||||||
return super.parseToYAML(object);
|
return super.parseToYAML(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object parseToJava(Object object) {
|
@Override
|
||||||
|
public <T> Object parseToJava(Class<T> type, Object object) {
|
||||||
if (object instanceof Double) {
|
if (object instanceof Double) {
|
||||||
return BigDecimal.valueOf((Double) object);
|
return BigDecimal.valueOf((Double) object);
|
||||||
} else if (object instanceof Long) {
|
} else if (object instanceof Long) {
|
||||||
@ -215,9 +219,15 @@ public class Properties {
|
|||||||
@ConfigurationComment("Do you want to protect shop chests with LWC?")
|
@ConfigurationComment("Do you want to protect shop chests with LWC?")
|
||||||
public static boolean PROTECT_CHEST_WITH_LWC = false;
|
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?")
|
@ConfigurationComment("Do you want to protect shop signs with LWC?")
|
||||||
public static boolean PROTECT_SIGN_WITH_LWC = false;
|
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? ")
|
@ConfigurationComment("Should the chest's LWC protection be removed once the shop sign is destroyed? ")
|
||||||
public static boolean REMOVE_LWC_PROTECTION_AUTOMATICALLY = true;
|
public static boolean REMOVE_LWC_PROTECTION_AUTOMATICALLY = true;
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.Acrobot.ChestShop.Events.Protection;
|
package com.Acrobot.ChestShop.Events.Protection;
|
||||||
|
|
||||||
|
import com.Acrobot.ChestShop.Security;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.Event;
|
import org.bukkit.event.Event;
|
||||||
@ -16,7 +17,8 @@ public class ProtectBlockEvent extends Event {
|
|||||||
|
|
||||||
private final Player player;
|
private final Player player;
|
||||||
private final UUID protectionOwner;
|
private final UUID protectionOwner;
|
||||||
private Block block;
|
private final Block block;
|
||||||
|
private final Security.Type type;
|
||||||
|
|
||||||
boolean isProtected = false;
|
boolean isProtected = false;
|
||||||
|
|
||||||
@ -25,9 +27,14 @@ public class ProtectBlockEvent extends Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ProtectBlockEvent(Block block, Player player, UUID protectionOwner) {
|
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.block = block;
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.protectionOwner = protectionOwner;
|
this.protectionOwner = protectionOwner;
|
||||||
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isProtected() {
|
public boolean isProtected() {
|
||||||
@ -50,6 +57,10 @@ public class ProtectBlockEvent extends Event {
|
|||||||
return protectionOwner;
|
return protectionOwner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Security.Type getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
public HandlerList getHandlers() {
|
public HandlerList getHandlers() {
|
||||||
return handlers;
|
return handlers;
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,14 @@ public class LightweightChestProtection implements Listener {
|
|||||||
|
|
||||||
public LightweightChestProtection() {
|
public LightweightChestProtection() {
|
||||||
this.lwc = LWC.getInstance();
|
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
|
@EventHandler
|
||||||
@ -36,13 +44,13 @@ public class LightweightChestProtection implements Listener {
|
|||||||
Container connectedContainer = event.getContainer();
|
Container connectedContainer = event.getContainer();
|
||||||
|
|
||||||
if (Properties.PROTECT_SIGN_WITH_LWC) {
|
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));
|
player.sendMessage(Messages.prefix(Messages.NOT_ENOUGH_PROTECTIONS));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Properties.PROTECT_CHEST_WITH_LWC && connectedContainer != null
|
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));
|
player.sendMessage(Messages.prefix(Messages.PROTECTED_SHOP));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -100,16 +108,31 @@ public class LightweightChestProtection implements Listener {
|
|||||||
return;
|
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;
|
Protection protection = null;
|
||||||
try {
|
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) {
|
} catch (LinkageError e) {
|
||||||
try {
|
try {
|
||||||
int blockId = com.griefcraft.cache.BlockCache.getInstance().getBlockId(block);
|
int blockId = com.griefcraft.cache.BlockCache.getInstance().getBlockId(block);
|
||||||
if (blockId < 0) {
|
if (blockId < 0) {
|
||||||
return;
|
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) {
|
} catch (LinkageError e2) {
|
||||||
ChestShop.getBukkitLogger().warning(
|
ChestShop.getBukkitLogger().warning(
|
||||||
"Incompatible LWC version installed! (" + lwc.getPlugin().getName() + " v" + lwc.getVersion() + ") \n" +
|
"Incompatible LWC version installed! (" + lwc.getPlugin().getName() + " v" + lwc.getVersion() + ") \n" +
|
||||||
|
@ -29,7 +29,11 @@ public class Security {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean protect(Player player, Block block, UUID protectionOwner) {
|
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);
|
ChestShop.callEvent(event);
|
||||||
|
|
||||||
return event.isProtected();
|
return event.isProtected();
|
||||||
@ -94,4 +98,11 @@ public class Security {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum Type {
|
||||||
|
PUBLIC,
|
||||||
|
PRIVATE,
|
||||||
|
DONATION,
|
||||||
|
DISPLAY
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user