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