LimitedCreative/src/main/java/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java

118 lines
5.3 KiB
Java
Raw Normal View History

2013-01-22 17:38:48 +00:00
package de.jaschastarke.minecraft.limitedcreative;
import java.util.Arrays;
2013-01-22 17:38:48 +00:00
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;
2013-02-01 21:25:43 +00:00
import de.jaschastarke.bukkit.lib.commands.NeedsPlayerArgumentCommandException;
2013-01-22 17:38:48 +00:00
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.Usages;
2013-01-22 17:38:48 +00:00
import de.jaschastarke.minecraft.lib.permissions.IAbstractPermission;
import de.jaschastarke.minecraft.lib.permissions.IPermission;
public class FeatureSwitchGameMode extends CoreModule<LimitedCreative> {
public FeatureSwitchGameMode(LimitedCreative plugin) {
super(plugin);
}
protected Commands commands = null;
@Override
public String getName() {
return "SwitchGameMode";
}
@Override
2013-02-01 21:25:43 +00:00
public void onEnable() {
2013-01-22 17:38:48 +00:00
if (commands == null)
commands = new Commands();
plugin.getMainCommand().getHandler().registerCommands(commands.getCommandList());
}
@Override
2013-02-01 21:25:43 +00:00
public void onDisable() {
2013-01-22 17:38:48 +00:00
if (commands != null)
plugin.getMainCommand().getHandler().removeCommands(commands.getCommandList());
}
public class Commands extends AbstractCommandList implements IMethodCommandContainer, IHasName {
private MethodCommand[] commands = MethodCommand.getMethodCommandsFor(this);
2013-01-22 17:38:48 +00:00
public List<ICommand> getCommandList() {
return Arrays.asList((ICommand[]) commands);
2013-01-22 17:38:48 +00:00
}
@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;
2013-02-01 21:25:43 +00:00
if (player != null && !player.isEmpty()) {
2013-01-22 17:38:48 +00:00
target = Bukkit.getPlayer(player);
2013-02-01 21:25:43 +00:00
if (target == null)
throw new CommandException("Player " + player + " not found");
} else if (context.isPlayer()) {
2013-01-22 17:38:48 +00:00
target = context.getPlayer();
2013-02-01 21:25:43 +00:00
}
2013-01-22 17:38:48 +00:00
if (target == null)
2013-02-01 21:25:43 +00:00
throw new NeedsPlayerArgumentCommandException();
2013-01-22 17:38:48 +00:00
if (!target.equals(context.getSender()) && !context.checkPermission(SwitchGameModePermissions.OTHER))
throw new MissingPermissionCommandException(SwitchGameModePermissions.OTHER);
GameMode wgm = Hooks.DefaultWorldGameMode.get(target.getWorld());
2013-02-10 21:12:31 +00:00
if (!context.checkPermission(permission) && (wgm != tgm || !context.checkPermission(SwitchGameModePermissions.BACKONLY)))
2013-01-22 17:38:48 +00:00
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)
@Usages("[player]")
2013-01-22 17:38:48 +00:00
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)
@Usages("[player]")
2013-01-22 17:38:48 +00:00
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)
@Usages("[player]")
2013-01-22 17:38:48 +00:00
public boolean adventure(CommandContext context, String player) throws MissingPermissionCommandException, CommandException {
return changeGameMode(context, player, GameMode.ADVENTURE, SwitchGameModePermissions.ADVENTURE);
}
}
}