From 729c186b759bb5c82e7b48b1690bcc73124b5682 Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Tue, 5 Feb 2013 21:04:05 +0100 Subject: [PATCH] Better Configuration implementation and better working Limit Features --- .../minecraft/limitedcreative/Config.java | 15 ++----- .../FeatureSwitchGameMode.java | 8 ++-- .../limitedcreative/LimitedCreative.java | 2 +- .../limitedcreative/MainCommand.java | 4 +- .../limitedcreative/ModCreativeLimits.java | 37 +++++++++++++--- .../inventories/ArmoryConfig.java | 5 --- .../inventories/InventoryConfig.java | 9 +--- .../limitedcreative/limits/BlackList.java | 42 ++++++++++++++----- .../limitedcreative/limits/LimitConfig.java | 26 ++++++------ .../limits/PlayerListener.java | 8 +++- src/main/resources/lang/messages.properties | 9 ++-- 11 files changed, 99 insertions(+), 66 deletions(-) diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/Config.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/Config.java index 38c4c69..8009338 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/Config.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/Config.java @@ -1,32 +1,23 @@ package de.jaschastarke.minecraft.limitedcreative; -import java.io.IOException; - import de.jaschastarke.bukkit.lib.Core; import de.jaschastarke.bukkit.lib.configuration.PluginConfiguration; import de.jaschastarke.configuration.annotations.IsConfigurationNode; import de.jaschastarke.maven.ArchiveDocComments; -import de.jaschastarke.utils.ClassDescriptorStorage; /** * Limited Creative - Configuration * * (YAML-Syntax: http://en.wikipedia.org/wiki/YAML) + * + * This configuration-file is automatically written when changed via ingame-commands. So any manual added comments are + * removed. */ @ArchiveDocComments public class Config extends PluginConfiguration { public Config(Core plugin) { super(plugin); } - @Override - public void save() { - try { - ClassDescriptorStorage.load(plugin.getResource("META-INF/descriptions.jos")); - } catch (IOException e) { - plugin.getLog().severe("Failed to load ConfigNode-Descriptions"); - } - super.save(); - } /** * Metrics diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java index cae9fdb..db52a9a 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java @@ -21,7 +21,7 @@ import de.jaschastarke.bukkit.lib.commands.annotations.Alias; import de.jaschastarke.bukkit.lib.commands.annotations.Description; import de.jaschastarke.bukkit.lib.commands.annotations.IsCommand; import de.jaschastarke.bukkit.lib.commands.annotations.NeedsPermission; -import de.jaschastarke.bukkit.lib.commands.annotations.Usage; +import de.jaschastarke.bukkit.lib.commands.annotations.Usages; import de.jaschastarke.minecraft.lib.permissions.IAbstractPermission; import de.jaschastarke.minecraft.lib.permissions.IPermission; @@ -93,7 +93,7 @@ public class FeatureSwitchGameMode extends CoreModule { @Alias("s") @Description(value = "command.switch.survival", translate = true) @NeedsPermission(value={"survival", "backonly"}, optional = true) - @Usage("[player]") + @Usages("[player]") public boolean survival(CommandContext context, String player) throws MissingPermissionCommandException, CommandException { return changeGameMode(context, player, GameMode.SURVIVAL, SwitchGameModePermissions.SURVIVAL); } @@ -101,7 +101,7 @@ public class FeatureSwitchGameMode extends CoreModule { @Alias("c") @Description(value = "command.switch.creative", translate = true) @NeedsPermission(value={"creative", "backonly"}, optional = true) - @Usage("[player]") + @Usages("[player]") public boolean creative(CommandContext context, String player) throws MissingPermissionCommandException, CommandException { return changeGameMode(context, player, GameMode.CREATIVE, SwitchGameModePermissions.CREATIVE); } @@ -109,7 +109,7 @@ public class FeatureSwitchGameMode extends CoreModule { @Alias("a") @Description(value = "command.switch.adventure", translate = true) @NeedsPermission(value={"adventure", "backonly"}, optional = true) - @Usage("[player]") + @Usages("[player]") public boolean adventure(CommandContext context, String player) throws MissingPermissionCommandException, CommandException { return changeGameMode(context, player, GameMode.ADVENTURE, SwitchGameModePermissions.ADVENTURE); } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java index 70d72ed..59a92db 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java @@ -3,7 +3,7 @@ package de.jaschastarke.minecraft.limitedcreative; import de.jaschastarke.I18n; import de.jaschastarke.bukkit.lib.Core; import de.jaschastarke.bukkit.lib.PluginLang; -import de.jaschastarke.bukkit.lib.configuration.ConfigCommand; +import de.jaschastarke.bukkit.lib.configuration.command.ConfigCommand; public class LimitedCreative extends Core { protected Config config = null; diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/MainCommand.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/MainCommand.java index 0bd6b08..c1b1bcd 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/MainCommand.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/MainCommand.java @@ -40,8 +40,8 @@ public class MainCommand extends BukkitCommand implements IHelpDescribed { return new IAbstractPermission[]{Permissions.COMMAND}; } @Override - public String getUsage() { - return ""; + public String[] getUsages() { + return null; } @Override public CharSequence getDescription() { diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java index ff47961..d744f25 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java @@ -1,9 +1,13 @@ package de.jaschastarke.minecraft.limitedcreative; import de.jaschastarke.bukkit.lib.CoreModule; +import de.jaschastarke.minecraft.limitedcreative.limits.BlockListener; +import de.jaschastarke.minecraft.limitedcreative.limits.EntityListener; import de.jaschastarke.minecraft.limitedcreative.limits.LimitConfig; +import de.jaschastarke.minecraft.limitedcreative.limits.PlayerListener; import de.jaschastarke.modularize.IModule; import de.jaschastarke.modularize.ModuleEntry; +import de.jaschastarke.modularize.ModuleEntry.ModuleState; public class ModCreativeLimits extends CoreModule { protected LimitConfig config; @@ -12,19 +16,40 @@ public class ModCreativeLimits extends CoreModule { super(plugin); } - public LimitConfig getConfig() { - return config; - } - protected FeatureBlockItemSpawn blockDrops = null; + + @Override + public String getName() { + return "Limits"; + } @Override public void initialize(ModuleEntry entry) { super.initialize(entry); - blockDrops = plugin.getModule(FeatureBlockItemSpawn.class); + listeners.addListener(new PlayerListener(this)); + listeners.addListener(new EntityListener(this)); + listeners.addListener(new BlockListener(this)); + config = plugin.getPluginConfig().registerSection(new LimitConfig(this, entry)); + + /*blockDrops = plugin.getModule(FeatureBlockItemSpawn.class); if (blockDrops == null) blockDrops = plugin.addModule(new FeatureBlockItemSpawn(plugin)).getModule(); - + */ + + if (!config.getEnabled()) { + entry.initialState = ModuleState.DISABLED; + return; + } + } + + @Override + public void onEnable() { + super.onEnable(); + getLog().info(plugin.getLocale().trans("basic.loaded.module")); + } + + public LimitConfig getConfig() { + return config; } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/ArmoryConfig.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/ArmoryConfig.java index 42f353d..2553513 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/ArmoryConfig.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/ArmoryConfig.java @@ -28,11 +28,6 @@ public class ArmoryConfig extends Configuration implements IConfigurationSubGrou public ArmoryConfig(ModInventories modInventories) { mod = modInventories; } - - @Override - public boolean isReadOnly() { - return false; - } @Override public void setValues(ConfigurationSection sect) { diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/InventoryConfig.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/InventoryConfig.java index 60f46a8..8ca3d8d 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/InventoryConfig.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/InventoryConfig.java @@ -28,17 +28,12 @@ public class InventoryConfig extends Configuration implements IConfigurationSubG entry = modEntry; } - @Override - public boolean isReadOnly() { - return false; - } - @Override public void setValue(IConfigurationNode node, Object pValue) throws InvalidValueException { super.setValue(node, pValue); if (node.getName().equals("enabled")) { - if ((Boolean) pValue) { - entry.activate(); + if (getEnabled()) { + entry.enable(); } else { entry.disable(); } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackList.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackList.java index f691b24..6bd5aef 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackList.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackList.java @@ -10,11 +10,13 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.material.MaterialData; import de.jaschastarke.bukkit.lib.configuration.ConfigurableList; +import de.jaschastarke.bukkit.lib.configuration.IToGeneric; import de.jaschastarke.bukkit.lib.items.ItemUtils; import de.jaschastarke.bukkit.lib.items.MaterialDataNotRecognizedException; import de.jaschastarke.bukkit.lib.items.MaterialNotRecognizedException; +import de.jaschastarke.configuration.InvalidValueException; -public class BlackList extends ArrayList implements ConfigurableList { +public class BlackList extends ArrayList implements ConfigurableList, IToGeneric { private static final long serialVersionUID = -3701659163474405152L; public static class Blacklisted { @@ -22,15 +24,15 @@ public class BlackList extends ArrayList implements Confi private MaterialData md; private boolean hasData = false; - public Blacklisted(String rep) { + public Blacklisted(String rep) throws InvalidValueException { stringRep = rep; try { md = ItemUtils.parseMaterial(rep); hasData = rep.contains(ItemUtils.MATERIAL_DATA_SEP); } catch (MaterialNotRecognizedException e) { - throw new IllegalArgumentException(e); + throw new InvalidValueException(e); } catch (MaterialDataNotRecognizedException e) { - throw new IllegalArgumentException(e); + throw new InvalidValueException(e); } } public Blacklisted(Material m) { @@ -51,7 +53,7 @@ public class BlackList extends ArrayList implements Confi } public boolean matches(Block block) { if (hasData) { - return md.equals(block.getData()); + return md.equals(new MaterialData(block.getType(), block.getData())); } else { return block.getType().equals(md.getItemType()); } @@ -68,17 +70,22 @@ public class BlackList extends ArrayList implements Confi public BlackList(List list) { if (list != null) { for (Object el : list) { - if (el instanceof Blacklisted) + if (el instanceof Blacklisted) { add((Blacklisted) el); - else - add(el.toString()); + } else { + try { + add(el.toString()); + } catch (InvalidValueException e) { + System.err.println(e.getCause().getMessage()); + } + } } } } public boolean contains(String e) { for (Blacklisted bl : this) { - if (bl.toString().equals(e)) + if (bl.toString().equalsIgnoreCase(e)) return true; } return false; @@ -100,7 +107,7 @@ public class BlackList extends ArrayList implements Confi } @Override // ConfigurableList, not List - public void add(String e) { + public void add(String e) throws InvalidValueException { if (!contains(e)) { add(new Blacklisted(e)); } @@ -110,11 +117,24 @@ public class BlackList extends ArrayList implements Confi public boolean remove(String e) { Iterator it = iterator(); while (it.hasNext()) { - if (it.next().toString().equals(e)) { + if (it.next().toString().equalsIgnoreCase(e)) { it.remove(); return true; } } return false; } + + public List toStringList() { + List list = new ArrayList(size()); + for (Blacklisted bl : this) { + list.add(bl.toString()); + } + return list; + } + + @Override + public List toGeneric() { + return toStringList(); + } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/LimitConfig.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/LimitConfig.java index 7b477ba..5e11c9a 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/LimitConfig.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/LimitConfig.java @@ -4,6 +4,7 @@ import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import de.jaschastarke.bukkit.lib.configuration.Configuration; +import de.jaschastarke.bukkit.lib.configuration.IToGeneric; import de.jaschastarke.configuration.IConfigurationNode; import de.jaschastarke.configuration.IConfigurationSubGroup; import de.jaschastarke.configuration.InvalidValueException; @@ -29,23 +30,19 @@ public class LimitConfig extends Configuration implements IConfigurationSubGroup entry = modEntry; } - @Override - public boolean isReadOnly() { - return false; - } - @Override public void setValue(IConfigurationNode node, Object pValue) throws InvalidValueException { - super.setValue(node, pValue); + if (!(pValue instanceof BlackList)) + super.setValue(node, pValue); if (node.getName().equals("enabled")) { - if ((Boolean) pValue) { - entry.activate(); + if (getEnabled()) { + entry.enable(); } else { entry.disable(); } } } - + @Override public void setValues(ConfigurationSection sect) { super.setValues(sect); @@ -54,7 +51,7 @@ public class LimitConfig extends Configuration implements IConfigurationSubGroup if (!sect.contains("interact") && sect.contains("sign")) { interactList = new BlackList(); if (config.getBoolean("sign", true)) { - interactList.add(new BlackList.Blacklisted(Material.SIGN)); + interactList.add(new BlackList.Blacklisted(Material.WALL_SIGN)); interactList.add(new BlackList.Blacklisted(Material.SIGN_POST)); } if (config.getBoolean("button", false)) { @@ -114,9 +111,14 @@ public class LimitConfig extends Configuration implements IConfigurationSubGroup } - public static enum BlockPickup { + public static enum BlockPickup implements IToGeneric { PREVENT, REMOVE; + + @Override + public Object toGeneric() { + return name().toLowerCase(); + } } /** @@ -176,7 +178,7 @@ public class LimitConfig extends Configuration implements IConfigurationSubGroup if (interactList == null) { interactList = new BlackList(config.getList("interact")); if (!config.contains("interact")) { - interactList.add(new BlackList.Blacklisted(Material.SIGN)); + interactList.add(new BlackList.Blacklisted(Material.WALL_SIGN)); interactList.add(new BlackList.Blacklisted(Material.SIGN_POST)); interactList.add(new BlackList.Blacklisted(Material.LEVER)); interactList.add(new BlackList.Blacklisted(Material.STONE_BUTTON)); diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/PlayerListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/PlayerListener.java index 2f09666..f8e396e 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/PlayerListener.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/PlayerListener.java @@ -104,7 +104,7 @@ public class PlayerListener implements Listener { @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { if (!isCancelled(event) && event.getPlayer().getGameMode() == GameMode.CREATIVE) { - if (mod.getConfig().getBlockUse().isListed(event.getItem())) { + if (event.getItem() != null && mod.getConfig().getBlockUse().isListed(event.getItem())) { if (!checkPermission(event, NoLimitPermissions.USE(event.getItem().getData()))) { event.setCancelled(true); event.setUseItemInHand(Event.Result.DENY); @@ -112,6 +112,11 @@ public class PlayerListener implements Listener { return; } } + + // Well, the action could be ignored, if the player is sneaking, as MC now let you place block on workbench + // and other while crouching. + // But we don't trust other plugins, like chest-shops that do something while right-clicking a block even + // when crouching. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { Block block = event.getClickedBlock(); if (isChest(block)) { @@ -130,6 +135,7 @@ public class PlayerListener implements Listener { } } } + @EventHandler public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { if (!event.isCancelled() && event.getPlayer().getGameMode() == GameMode.CREATIVE) { diff --git a/src/main/resources/lang/messages.properties b/src/main/resources/lang/messages.properties index 4d8cfd4..43bbc19 100644 --- a/src/main/resources/lang/messages.properties +++ b/src/main/resources/lang/messages.properties @@ -43,16 +43,15 @@ exception.storage.load: Failed to load your Inventory. Ask your Admin to enable exception.region.not_optional: You can not be {0} in that area blocked.chest: Access to chests is not allowed in creative mode -blocked.sign: To interact with signs is not allowed in creative mode -blocked.button: To interact with buttons is not allowed in creative mode -blocked.lever: To interact with levers is not allowed in creative mode blocked.survival_flying: You should stay on ground, when leaving a creative-area blocked.outside_place: You can not place blocks outside of the gamemode-area blocked.outside_break: You can not destroy blocks outside of the gamemode-area blocked.inside_place: You can not place blocks inside of the gamemode-area blocked.inside_break: You can not destroy blocks inside of the gamemode-area +blocked.interact: You are not allowed to interact with this type of blocks blocked.use: You are not allowed to use this type of item blocked.place: You are not allowed to place this type of block blocked.break: You are not allowed to break this type of block -blocked.piston: Moving {0} block out of creative area was blocked at {1} -blocked.piston_in: Moving {0} block into creative area was blocked at {1} + +blocked.region.piston: Moving {0} block out of creative area was blocked at {1} +blocked.region.piston_in: Moving {0} block into creative area was blocked at {1}