Better Configuration implementation and better working Limit Features

This commit is contained in:
Jascha Starke 2013-02-05 21:04:05 +01:00
parent 0d0a9bfaba
commit 729c186b75
11 changed files with 99 additions and 66 deletions

View file

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

View file

@ -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<LimitedCreative> {
@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<LimitedCreative> {
@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<LimitedCreative> {
@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);
}

View file

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

View file

@ -40,8 +40,8 @@ public class MainCommand extends BukkitCommand implements IHelpDescribed {
return new IAbstractPermission[]{Permissions.COMMAND};
}
@Override
public String getUsage() {
return "<subcommand>";
public String[] getUsages() {
return null;
}
@Override
public CharSequence getDescription() {

View file

@ -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<LimitedCreative> {
protected LimitConfig config;
@ -12,19 +16,40 @@ public class ModCreativeLimits extends CoreModule<LimitedCreative> {
super(plugin);
}
public LimitConfig getConfig() {
return config;
}
protected FeatureBlockItemSpawn blockDrops = null;
@Override
public String getName() {
return "Limits";
}
@Override
public void initialize(ModuleEntry<IModule> 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;
}
}

View file

@ -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) {

View file

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

View file

@ -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<BlackList.Blacklisted> implements ConfigurableList {
public class BlackList extends ArrayList<BlackList.Blacklisted> implements ConfigurableList<BlackList.Blacklisted>, IToGeneric {
private static final long serialVersionUID = -3701659163474405152L;
public static class Blacklisted {
@ -22,15 +24,15 @@ public class BlackList extends ArrayList<BlackList.Blacklisted> 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<BlackList.Blacklisted> 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<BlackList.Blacklisted> 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<BlackList.Blacklisted> implements Confi
}
@Override // ConfigurableList, not List<E>
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<BlackList.Blacklisted> implements Confi
public boolean remove(String e) {
Iterator<Blacklisted> 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<String> toStringList() {
List<String> list = new ArrayList<String>(size());
for (Blacklisted bl : this) {
list.add(bl.toString());
}
return list;
}
@Override
public List<String> toGeneric() {
return toStringList();
}
}

View file

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

View file

@ -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) {

View file

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