2013-01-22 17:38:48 +00:00
|
|
|
package de.jaschastarke.minecraft.limitedcreative;
|
|
|
|
|
2013-01-23 15:31:33 +00:00
|
|
|
import java.util.Arrays;
|
2013-01-22 17:38:48 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.GameMode;
|
2013-07-02 09:43:13 +00:00
|
|
|
import org.bukkit.World;
|
2013-01-22 17:38:48 +00:00
|
|
|
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;
|
2013-02-05 20:04:05 +00:00
|
|
|
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;
|
2013-07-02 09:43:13 +00:00
|
|
|
import de.jaschastarke.minecraft.limitedcreative.regions.Flags;
|
|
|
|
import de.jaschastarke.minecraft.limitedcreative.regions.worldguard.ApplicableRegions;
|
2013-01-22 17:38:48 +00:00
|
|
|
|
|
|
|
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-03-23 16:59:14 +00:00
|
|
|
super.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-03-23 16:59:14 +00:00
|
|
|
super.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 {
|
2013-01-23 15:31:33 +00:00
|
|
|
private MethodCommand[] commands = MethodCommand.getMethodCommandsFor(this);
|
2013-01-22 17:38:48 +00:00
|
|
|
public List<ICommand> getCommandList() {
|
2013-01-23 15:31:33 +00:00
|
|
|
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);
|
|
|
|
|
2013-07-02 09:43:13 +00:00
|
|
|
GameMode wgm = this.getDefaultGameMode(target.getWorld());
|
2013-01-22 17:38:48 +00:00
|
|
|
|
2013-07-02 09:43:13 +00:00
|
|
|
if (!this.regionOptional(target, tgm)) {
|
|
|
|
if (!context.checkPermission(permission) && (wgm != tgm || !context.checkPermission(SwitchGameModePermissions.BACKONLY)))
|
|
|
|
throw new MissingPermissionCommandException(permission);
|
|
|
|
}
|
2013-01-22 17:38:48 +00:00
|
|
|
|
2013-03-20 16:11:32 +00:00
|
|
|
if (target.getGameMode() != tgm) {
|
|
|
|
target.setGameMode(tgm);
|
|
|
|
if (!context.isPlayer() || !target.equals(context.getPlayer())) {
|
|
|
|
context.response(context.getFormatter().getString("command.gamemode.changed", target.getName()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
context.response(context.getFormatter().getString("command.gamemode.no_change"));
|
|
|
|
}
|
2013-01-22 17:38:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-07-02 09:43:13 +00:00
|
|
|
|
|
|
|
protected GameMode getDefaultGameMode(World world) {
|
|
|
|
return Hooks.DefaultWorldGameMode.get(world);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean regionOptional(Player player, GameMode tgm) {
|
|
|
|
ModRegions mod = plugin.getModule(ModRegions.class);
|
|
|
|
if (mod != null) {
|
|
|
|
ApplicableRegions rs = mod.getRegionManager().getRegionSet(player.getLocation());
|
|
|
|
if (rs.allows(Flags.GAMEMODE_OPTIONAL)) {
|
|
|
|
if ((tgm == rs.getFlag(Flags.GAMEMODE, player)) || (tgm == this.getDefaultGameMode(player.getWorld())))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-22 17:38:48 +00:00
|
|
|
|
|
|
|
@IsCommand("survival")
|
|
|
|
@Alias("s")
|
|
|
|
@Description(value = "command.switch.survival", translate = true)
|
|
|
|
@NeedsPermission(value={"survival", "backonly"}, optional = true)
|
2013-02-05 20:04:05 +00:00
|
|
|
@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)
|
2013-02-05 20:04:05 +00:00
|
|
|
@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)
|
2013-02-05 20:04:05 +00:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|