From de57c9384438ac4c62b49a247a9a43f5c2d739dc Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Tue, 22 Jan 2013 18:38:48 +0100 Subject: [PATCH] Working commands --- lang/messages.properties | 2 + pom.xml | 18 ++- .../FeatureSwitchGameMode.java | 113 ++++++++++++++++++ .../limitedcreative/LimitedCreative.java | 18 ++- .../limitedcreative/MainCommand.java | 54 +++++++++ .../limitedcreative/ModInventories.java | 14 +-- .../limitedcreative/Permissions.java | 24 ++-- .../SwitchGameModePermissions.java | 52 ++++++++ .../hooks/MultiVerseHooks.java | 2 +- .../hooks/WorldTypeHooker.java | 6 +- .../inventories/ArmoryConfig.java | 47 ++++---- .../inventories/InventoryPermissions.java | 7 +- 12 files changed, 301 insertions(+), 56 deletions(-) create mode 100644 src/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java create mode 100644 src/de/jaschastarke/minecraft/limitedcreative/MainCommand.java create mode 100644 src/de/jaschastarke/minecraft/limitedcreative/SwitchGameModePermissions.java diff --git a/lang/messages.properties b/lang/messages.properties index c6e6c09..4d8cfd4 100644 --- a/lang/messages.properties +++ b/lang/messages.properties @@ -4,11 +4,13 @@ basic.loaded.module: Module loaded. basic.feature.store: separated inventories basic.feature.limit: creative restrictions basic.feature.region: creative regions +basic.feature.gamemodecommands: GameMode Commands basic.conflict: Due to conflict with the plugin {0}, the feature {1} is disabled basic.warning.worldguard_not_found: WorldGuard isn't found, the feature {0} is disabled # double single-quote '' because of MessageFormater to insert {0} command.player: player +command.general: LimitedCreative: GameMode-Switch, Creative-Regions, Config and more command.switch.survival: Changes the game mode of a player to survival command.switch.creative: Changes the game mode of a player to creative command.switch.adventure: Changes the game mode of a player to adventure diff --git a/pom.xml b/pom.xml index d43cf78..352151e 100644 --- a/pom.xml +++ b/pom.xml @@ -102,7 +102,7 @@ ${project.artifactId} - + lang/ ${basedir}/lang/ - + @@ -157,14 +157,22 @@ WorldGuard WorldEdit MultiInv + xAuth + AuthMe + MultiInv + Multiverse-Inventories http://dev.bukkit.org/server-mods/limited-creative/ - de.jaschastarke.minecraft.limitedcreative.Permissions + de.jaschastarke.minecraft.limitedcreative.Permissions:CONTAINER de.jaschastarke.minecraft.limitedcreative.inventories.InventoryPermissions + de.jaschastarke.minecraft.limitedcreative.SwitchGameModePermissions:ALL + + de.jaschastarke.minecraft.limitedcreative.MainCommand + diff --git a/src/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java b/src/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java new file mode 100644 index 0000000..733d68d --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java @@ -0,0 +1,113 @@ +package de.jaschastarke.minecraft.limitedcreative; + +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.entity.Player; + +import de.jaschastarke.IHasName; +import de.jaschastarke.bukkit.lib.CoreModule; +import de.jaschastarke.bukkit.lib.commands.AbstractCommandList; +import de.jaschastarke.bukkit.lib.commands.CommandContext; +import de.jaschastarke.bukkit.lib.commands.CommandException; +import de.jaschastarke.bukkit.lib.commands.ICommand; +import de.jaschastarke.bukkit.lib.commands.MethodCommand; +import de.jaschastarke.bukkit.lib.commands.IMethodCommandContainer; +import de.jaschastarke.bukkit.lib.commands.MissingPermissionCommandException; +import de.jaschastarke.bukkit.lib.commands.NeedsPlayerCommandException; +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.minecraft.lib.permissions.IAbstractPermission; +import de.jaschastarke.minecraft.lib.permissions.IPermission; + +public class FeatureSwitchGameMode extends CoreModule { + public FeatureSwitchGameMode(LimitedCreative plugin) { + super(plugin); + } + protected Commands commands = null; + + @Override + public String getName() { + return "SwitchGameMode"; + } + + @Override + public void OnEnable() { + if (commands == null) + commands = new Commands(); + plugin.getMainCommand().getHandler().registerCommands(commands.getCommandList()); + } + + @Override + public void OnDisable() { + if (commands != null) + plugin.getMainCommand().getHandler().removeCommands(commands.getCommandList()); + } + + public class Commands extends AbstractCommandList implements IMethodCommandContainer, IHasName { + private List commands = MethodCommand.getMethodCommandsFor(this); + public List getCommandList() { + return commands; + } + + @Override + public String getName() { + return plugin.getName() + " - " + plugin.getLang().trans("basic.feature.gamemodecommands"); + } + @Override + public IPermission getPermission(String subPerm) { + return SwitchGameModePermissions.ALL.getPermission(subPerm); + } + + protected boolean changeGameMode(CommandContext context, String player, GameMode tgm, IAbstractPermission permission) throws MissingPermissionCommandException, CommandException { + Player target = null; + if (player != null && !player.isEmpty()) + target = Bukkit.getPlayer(player); + else if (context.isPlayer()) + target = context.getPlayer(); + + if (target == null) + throw new NeedsPlayerCommandException(); + + if (!target.equals(context.getSender()) && !context.checkPermission(SwitchGameModePermissions.OTHER)) + throw new MissingPermissionCommandException(SwitchGameModePermissions.OTHER); + + GameMode wgm = Hooks.DefaultWorldGameMode.get(target.getWorld()); + + if (context.checkPermission(permission) || (wgm == tgm && context.checkPermission(SwitchGameModePermissions.BACKONLY))) + throw new MissingPermissionCommandException(permission); + + target.setGameMode(tgm); + return true; + } + + @IsCommand("survival") + @Alias("s") + @Description(value = "command.switch.survival", translate = true) + @NeedsPermission(value={"survival", "backonly"}, optional = true) + @Usage("[player]") + public boolean survival(CommandContext context, String player) throws MissingPermissionCommandException, CommandException { + return changeGameMode(context, player, GameMode.SURVIVAL, SwitchGameModePermissions.SURVIVAL); + } + @IsCommand("creative") + @Alias("c") + @Description(value = "command.switch.creative", translate = true) + @NeedsPermission(value={"creative", "backonly"}, optional = true) + @Usage("[player]") + public boolean creative(CommandContext context, String player) throws MissingPermissionCommandException, CommandException { + return changeGameMode(context, player, GameMode.CREATIVE, SwitchGameModePermissions.CREATIVE); + } + @IsCommand("adventure") + @Alias("a") + @Description(value = "command.switch.adventure", translate = true) + @NeedsPermission(value={"adventure", "backonly"}, optional = true) + @Usage("[player]") + public boolean adventure(CommandContext context, String player) throws MissingPermissionCommandException, CommandException { + return changeGameMode(context, player, GameMode.ADVENTURE, SwitchGameModePermissions.ADVENTURE); + } + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java b/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java index 6138270..c6b7c39 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java @@ -5,8 +5,8 @@ import de.jaschastarke.bukkit.lib.Core; import de.jaschastarke.bukkit.lib.PluginLang; public class LimitedCreative extends Core { - private i18n lang; protected Config config = null; + protected MainCommand command = null; @Override public void OnInitialize() { @@ -14,16 +14,20 @@ public class LimitedCreative extends Core { config = new Config(this); this.debug = config.getDebug(); - lang = new PluginLang("lang/messages", this); + setLang(new PluginLang("lang/messages", this)); + + command = new MainCommand(this); + commands.registerCommand(command); Hooks.inizializeHooks(this); + addModule(new FeatureSwitchGameMode(this)); addModule(new ModInventories(this)); addModule(new ModCreativeLimits(this)); addModule(new ModRegions(this)); addModule(new ModCmdBlocker(this)); - config.save(); + config.saveDefault(); } public Config getPluginConfig() { @@ -32,9 +36,13 @@ public class LimitedCreative extends Core { @Deprecated public String L(String msg, Object... objects) { - return lang.trans(msg, objects); + return getLang().trans(msg, objects); } public i18n getLocale() { - return lang; + return getLang(); + } + + public MainCommand getMainCommand() { + return command; } } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/MainCommand.java b/src/de/jaschastarke/minecraft/limitedcreative/MainCommand.java new file mode 100644 index 0000000..0bd6b08 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/MainCommand.java @@ -0,0 +1,54 @@ +package de.jaschastarke.minecraft.limitedcreative; + +import de.jaschastarke.LocaleString; +import de.jaschastarke.bukkit.lib.commands.BukkitCommand; +import de.jaschastarke.bukkit.lib.commands.IHelpDescribed; +import de.jaschastarke.maven.ArchiveDocComments; +import de.jaschastarke.minecraft.lib.permissions.IAbstractPermission; + +/** + * LimitedCreative: GameMode-Switch, Creative-Regions, Config and more + * @usage / - displays LimitedCreative-Help + * @permission limitedcreative.command + */ +@ArchiveDocComments +public class MainCommand extends BukkitCommand implements IHelpDescribed { + private LimitedCreative plugin; + + public MainCommand() { + } + public MainCommand(LimitedCreative plugin) { + this.plugin = plugin; + } + + @Override + public String getName() { + return "limitedcreative"; + } + + @Override + public String[] getAliases() { + return new String[]{"lc"}; + } + + /** + * @internal has no effect, as not tested by any command handler + * @see IHelpDescribed + */ + @Override + public IAbstractPermission[] getRequiredPermissions() { + return new IAbstractPermission[]{Permissions.COMMAND}; + } + @Override + public String getUsage() { + return ""; + } + @Override + public CharSequence getDescription() { + return new LocaleString("command.general"); + } + @Override + public String getPackageName() { + return plugin.getName(); + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/ModInventories.java b/src/de/jaschastarke/minecraft/limitedcreative/ModInventories.java index 4e6c60a..5e73ca1 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/ModInventories.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/ModInventories.java @@ -7,10 +7,8 @@ import java.util.WeakHashMap; import org.bukkit.GameMode; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import org.bukkit.material.MaterialData; import de.jaschastarke.bukkit.lib.CoreModule; -import de.jaschastarke.bukkit.lib.permissions.PermissionManager; import de.jaschastarke.minecraft.limitedcreative.inventories.ArmoryConfig; import de.jaschastarke.minecraft.limitedcreative.inventories.Inventory; import de.jaschastarke.minecraft.limitedcreative.inventories.InventoryConfig; @@ -73,7 +71,7 @@ public class ModInventories extends CoreModule { } public void onSetGameMode(Player player, GameMode gm) { - if (PermissionManager.hasPermission(player, InventoryPermissions.KEEP_INVENTORY)) + if (plugin.getPermManager().hasPermission(player, InventoryPermissions.KEEP_INVENTORY)) return; player.closeInventory(); @@ -107,17 +105,17 @@ public class ModInventories extends CoreModule { } public void setCreativeArmor(Player player) { - Map armor = armor_config.getCreativeArmor(); + Map armor = armor_config.getCreativeArmor(); if (armor != null) { ItemStack[] is = new ItemStack[4]; if (armor.containsKey("feet")) - is[0] = armor.get("feet").toItemStack(1); + is[0] = armor.get("feet"); if (armor.containsKey("legs")) - is[1] = armor.get("legs").toItemStack(1); + is[1] = armor.get("legs"); if (armor.containsKey("chest")) - is[2] = armor.get("chest").toItemStack(1); + is[2] = armor.get("chest"); if (armor.containsKey("head")) - is[3] = armor.get("head").toItemStack(1); + is[3] = armor.get("head"); player.getInventory().setArmorContents(is); } } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/Permissions.java b/src/de/jaschastarke/minecraft/limitedcreative/Permissions.java index 0133a49..2d259fb 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/Permissions.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/Permissions.java @@ -19,26 +19,26 @@ package de.jaschastarke.minecraft.limitedcreative; import org.bukkit.permissions.PermissionDefault; -import de.jaschastarke.Singleton; import de.jaschastarke.maven.ArchiveDocComments; import de.jaschastarke.minecraft.lib.permissions.BasicPermission; import de.jaschastarke.minecraft.lib.permissions.IPermission; -import de.jaschastarke.minecraft.lib.permissions.SimplePermissionContainer; +import de.jaschastarke.minecraft.lib.permissions.SimplePermissionContainerNode; @ArchiveDocComments -public class Permissions extends SimplePermissionContainer implements Singleton { - private static Permissions instance = new Permissions(); - public static Permissions getInstance() { - return instance; +public class Permissions extends SimplePermissionContainerNode { + protected Permissions(String name) { + super(name); } + + public final static Permissions CONTAINER = new Permissions("limitedcreative"); /** * Allows changing plugin configuration ingame via commands */ - public static final IPermission CONFIG = new BasicPermission(instance, "config", PermissionDefault.OP); - - @Override - public String getFullString() { - return "limitedcreative"; - } + public static final IPermission CONFIG = new BasicPermission(CONTAINER, "config", PermissionDefault.OP); + /** + * Gives player access to the general /limitedcreative command. This permission doesn't affect the usability, but + * allows to hide the command from /help for users. + */ + public static final IPermission COMMAND = new BasicPermission(CONTAINER, "command", PermissionDefault.OP); } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/SwitchGameModePermissions.java b/src/de/jaschastarke/minecraft/limitedcreative/SwitchGameModePermissions.java new file mode 100644 index 0000000..935c157 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/SwitchGameModePermissions.java @@ -0,0 +1,52 @@ +package de.jaschastarke.minecraft.limitedcreative; + +import org.bukkit.permissions.PermissionDefault; + +import de.jaschastarke.maven.ArchiveDocComments; +import de.jaschastarke.minecraft.lib.permissions.BasicPermission; +import de.jaschastarke.minecraft.lib.permissions.IAbstractPermission; +import de.jaschastarke.minecraft.lib.permissions.IsChildPermission; +import de.jaschastarke.minecraft.lib.permissions.ParentPermissionContainerNode; + +@ArchiveDocComments +public class SwitchGameModePermissions extends ParentPermissionContainerNode { + /** + * Allows switching of own game mode to creative/adventure and back + */ + public final static SwitchGameModePermissions ALL = new SwitchGameModePermissions(Permissions.CONTAINER, "switch_gamemode"); + + protected SwitchGameModePermissions(IAbstractPermission parent, String name) { + super(parent, name); + } + + @Override + public PermissionDefault getDefault() { + return PermissionDefault.OP; + } + + /** + * Allows switching of own game mode to default of the not world he is in, but not to an other + */ + public final static BasicPermission BACKONLY = new BasicPermission(ALL, "backonly", PermissionDefault.FALSE); + + /** + * Allows switching of own game mode to survival, but not to creative/adventure + */ + @IsChildPermission + public final static BasicPermission SURVIVAL = new BasicPermission(ALL, "survival", PermissionDefault.FALSE); + /** + * Allows switching of own game mode to creative, but not to survival/adventure + */ + @IsChildPermission + public final static BasicPermission CREATIVE = new BasicPermission(ALL, "creative", PermissionDefault.FALSE); + /** + * Allows switching of own game mode to adventure, but not to creative/survival + */ + @IsChildPermission + public final static BasicPermission ADVENTURE = new BasicPermission(ALL, "adventure", PermissionDefault.FALSE); + + /** + * Allows switching of other users game mode + */ + public final static BasicPermission OTHER = new BasicPermission(ALL, "other", PermissionDefault.OP); +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/hooks/MultiVerseHooks.java b/src/de/jaschastarke/minecraft/limitedcreative/hooks/MultiVerseHooks.java index 20e903e..40ef5a2 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/hooks/MultiVerseHooks.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/hooks/MultiVerseHooks.java @@ -21,7 +21,7 @@ public class MultiVerseHooks { }); Hooks.DefaultWorldGameMode.register(new WorldTypeHooker.Check() { @Override - public GameMode test(World world) { + public GameMode get(World world) { MultiverseWorld mvWorld = getMV().getMVWorldManager().getMVWorld(world); if (mvWorld == null) return null; diff --git a/src/de/jaschastarke/minecraft/limitedcreative/hooks/WorldTypeHooker.java b/src/de/jaschastarke/minecraft/limitedcreative/hooks/WorldTypeHooker.java index de041ed..62d64ca 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/hooks/WorldTypeHooker.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/hooks/WorldTypeHooker.java @@ -8,12 +8,12 @@ import de.jaschastarke.hooking.AbstractHooker; public class WorldTypeHooker extends AbstractHooker { public interface Check { - GameMode test(World world); + GameMode get(World world); } - public GameMode test(World world) { + public GameMode get(World world) { for (Check c : hooks) { - return c.test(world); + return c.get(world); } return Bukkit.getServer().getDefaultGameMode(); } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/inventories/ArmoryConfig.java b/src/de/jaschastarke/minecraft/limitedcreative/inventories/ArmoryConfig.java index c8ec904..6e8ce8c 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/inventories/ArmoryConfig.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/inventories/ArmoryConfig.java @@ -5,12 +5,13 @@ import java.util.Map; import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.inventory.ItemStack; import org.bukkit.material.MaterialData; import de.jaschastarke.bukkit.lib.ModuleLogger; import de.jaschastarke.bukkit.lib.configuration.Configuration; import de.jaschastarke.bukkit.lib.items.MaterialDataNotRecognizedException; -import de.jaschastarke.bukkit.lib.items.MaterilNotRecognizedException; +import de.jaschastarke.bukkit.lib.items.MaterialNotRecognizedException; import de.jaschastarke.bukkit.lib.items.Utils; import de.jaschastarke.configuration.IConfigurationSubGroup; import de.jaschastarke.configuration.annotations.IsConfigurationNode; @@ -59,22 +60,26 @@ public class ArmoryConfig extends Configuration implements IConfigurationSubGrou public boolean getEnabled() { return config.getBoolean("enabled", true); } - public Map getCreativeArmor() { + public Map getCreativeArmor() { if (getEnabled()) { - Map armor = new HashMap(); + Map armor = new HashMap(); for (Map.Entry entry : config.getValues(false).entrySet()) { if (!entry.getKey().equals("enabled")) { - MaterialData md = null; - try { - md = Utils.parseMaterial((String) entry.getValue()); - } catch (MaterilNotRecognizedException e) { - getLog().warn(L("exception.config.material_not_found", entry.getValue())); - } catch (MaterialDataNotRecognizedException e) { - getLog().warn(L("exception.config.materiak_data_not_found", entry.getValue())); + if (entry instanceof ItemStack) { + armor.put(entry.getKey(), (ItemStack) entry); + } else { + MaterialData md = null; + try { + md = Utils.parseMaterial((String) entry.getValue()); + } catch (MaterialNotRecognizedException e) { + getLog().warn(L("exception.config.material_not_found", entry.getValue())); + } catch (MaterialDataNotRecognizedException e) { + getLog().warn(L("exception.config.materiak_data_not_found", entry.getValue())); + } + + if (md != null) + armor.put(entry.getKey(), md.toItemStack()); } - - if (md != null) - armor.put(entry.getKey(), md); } } return armor.size() > 0 ? armor : null; @@ -90,20 +95,20 @@ public class ArmoryConfig extends Configuration implements IConfigurationSubGrou * *see Blacklist for details on Item-Types */ @IsConfigurationNode(order = 500) - public String getHead() { - return config.getString("head", "CHAINMAIL_HELMET"); + public Object getHead() { + return config.get("head", "CHAINMAIL_HELMET"); } @IsConfigurationNode(order = 501) - public String getChest() { - return config.getString("chest", "CHAINMAIL_CHESTPLATE"); + public Object getChest() { + return config.get("chest", "CHAINMAIL_CHESTPLATE"); } @IsConfigurationNode(order = 502) - public String getLegs() { - return config.getString("legs", "CHAINMAIL_LEGGINGS"); + public Object getLegs() { + return config.get("legs", "CHAINMAIL_LEGGINGS"); } @IsConfigurationNode(order = 503) - public String getFeet() { - return config.getString("feet", "CHAINMAIL_BOOTS"); + public Object getFeet() { + return config.get("feet", "CHAINMAIL_BOOTS"); } @Deprecated diff --git a/src/de/jaschastarke/minecraft/limitedcreative/inventories/InventoryPermissions.java b/src/de/jaschastarke/minecraft/limitedcreative/inventories/InventoryPermissions.java index 358ba33..b889a8b 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/inventories/InventoryPermissions.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/inventories/InventoryPermissions.java @@ -25,11 +25,16 @@ import de.jaschastarke.minecraft.lib.permissions.IPermission; import de.jaschastarke.minecraft.lib.permissions.SimplePermissionContainer; import de.jaschastarke.minecraft.limitedcreative.Permissions; +/** + * + * @internal Doesn't represent a node, is only a list of additional permissions with an other parent. + * + */ @ArchiveDocComments public class InventoryPermissions extends SimplePermissionContainer { /** * Allows bypassing the inventory separation */ - public static final IPermission KEEP_INVENTORY = new BasicPermission(Permissions.getInstance(), "keepinventory", PermissionDefault.FALSE); + public static final IPermission KEEP_INVENTORY = new BasicPermission(Permissions.CONTAINER, "keepinventory", PermissionDefault.FALSE); }