Created a command package and a class for each command. Note: all changes are UNTESTED!

This commit is contained in:
Tobias Ottenweller 2013-06-04 22:10:14 +02:00
parent 2502975dc1
commit cc9bd13b2e
11 changed files with 573 additions and 237 deletions

View File

@ -1,236 +0,0 @@
/* Craft Inc. BorderProtection
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.borderprotection;
import de.craftinc.borderprotection.borders.Border;
import de.craftinc.borderprotection.borders.CircBorder;
import de.craftinc.borderprotection.borders.RectBorder;
import de.craftinc.borderprotection.util.UpdateHelper;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.io.IOException;
public class Commands implements CommandExecutor
{
@Override
public boolean onCommand( CommandSender sender, Command command, String label, String[] args )
{
// Check if command comes from a player.
if ( !( sender instanceof Player ) )
{
sender.sendMessage(Messages.commandIssuedByNonPlayer);
return true;
}
// command for all actions
if ( command.getName().equalsIgnoreCase("cibp") )
{
// help
if ( args.length == 0 || ( args.length > 0 && args[0].equalsIgnoreCase("help") ) )
{
sender.sendMessage(Messages.helpGeneral);
return true;
}
// checkversion
if ( args.length > 0 && args[0].equalsIgnoreCase("checkversion") )
{
if ( !sender.hasPermission("craftinc.borderprotection.update") )
{
sender.sendMessage(Messages.noPermissionCheckversion);
return false;
}
if ( UpdateHelper.newVersionAvailable() )
{
sender.sendMessage(
Messages.updateMessage(UpdateHelper.cachedLatestVersion, UpdateHelper.getCurrentVersion()));
return true;
}
else
{
sender.sendMessage(Messages.noUpdateAvailable);
return true;
}
}
// set
if ( ( args.length == 3 || args.length == 4 ) && args[0].equalsIgnoreCase("set") )
{
if ( !sender.hasPermission("craftinc.borderprotection.set") )
{
sender.sendMessage(Messages.noPermissionSet);
return false;
}
World world = ( (Player) sender ).getWorld();
// set [r|c] <distance>
if ( args.length == 3 )
{
try
{
Double distance = Double.parseDouble(args[2]);
Border newBorder = null;
// rect border
if ( args[1].equalsIgnoreCase("r") )
{
newBorder = new RectBorder(new Location(world, distance, 0, distance),
new Location(world, -distance, 0, -distance));
}
// circ border
else if ( args[1].equalsIgnoreCase("c") )
{
newBorder = new CircBorder(new Location(world, 0, 0, 0), distance);
}
if ( newBorder != null )
{
sender.sendMessage(Messages.borderCreationSuccessful);
sender.sendMessage(
Messages.borderInfo(world.getName(), newBorder));
}
}
catch ( Exception e )
{
sender.sendMessage(e.getMessage());
}
}
// set r <point1> <point2> | set c <center> <radius>
else
{
try
{
Border newBorder = null;
// rect border
if ( args[1].equalsIgnoreCase("r") )
{
Double p1X = Double.parseDouble(args[2].split(",")[0]);
Double p1Z = Double.parseDouble(args[2].split(",")[1]);
Double p2X = Double.parseDouble(args[3].split(",")[0]);
Double p2Z = Double.parseDouble(args[3].split(",")[1]);
newBorder = new RectBorder(new Location(world, p1X, 0, p1Z),
new Location(world, p2X, 0, p2Z));
}
// circ border
else if ( args[1].equalsIgnoreCase("c") )
{
Double centerX = Double.parseDouble(args[2].split(",")[0]);
Double centerZ = Double.parseDouble(args[2].split(",")[1]);
Double radius = Double.parseDouble(args[3]);
newBorder = new CircBorder(new Location(world, centerX, 0, centerZ), radius);
}
if ( newBorder != null )
{
sender.sendMessage(Messages.borderCreationSuccessful);
sender.sendMessage(
Messages.borderInfo(world.getName(), newBorder));
}
}
catch ( Exception e )
{
sender.sendMessage(e.getMessage());
}
}
// save the new border
try
{
Border.saveBorders();
}
catch ( IOException e )
{
sender.sendMessage(Messages.borderSaveException);
}
return true;
}
// get
if ( args.length == 1 && ( args[0].equalsIgnoreCase("get") || args[0].equalsIgnoreCase("info") ) )
{
World world = ( (Player) sender ).getWorld();
// exit and send the player a message if no border is set
if ( !Border.getBorders().containsKey(world) )
{
sender.sendMessage(Messages.borderInfoNoBorderSet);
return true;
}
Border border = Border.getBorders().get(world);
sender.sendMessage(Messages.borderInfo(world.getName(), border));
return true;
}
// on
if ( args.length == 1 && ( args[0].equalsIgnoreCase("on") || args[0].equalsIgnoreCase("off") ) )
{
if ( !sender.hasPermission("craftinc.borderprotection.set") )
{
sender.sendMessage(Messages.noPermissionSet);
return false;
}
World world = ( (Player) sender ).getWorld();
Border border = Border.getBorders().get(world);
if ( border != null )
{
if ( args[0].equalsIgnoreCase("on") )
{
border.enable();
sender.sendMessage(Messages.borderEnabled);
}
else
{
border.disable();
sender.sendMessage(Messages.borderDisabled);
}
}
else
{
sender.sendMessage(Messages.borderInfoNoBorderSet);
}
// save the changed border
try
{
Border.saveBorders();
}
catch ( IOException e )
{
sender.sendMessage(Messages.borderEnableDisableException);
}
return true;
}
}
sender.sendMessage(Messages.helpGeneral);
return false;
}
}

View File

@ -84,6 +84,7 @@ public class Messages
ChatColor.WHITE + "Commands are always related to the current world." + NEWLINE + ChatColor.WHITE + "Commands are always related to the current world." + NEWLINE +
makeCmd("help", "shows this help") + makeCmd("help", "shows this help") +
makeCmd("get | info", "Shows information about the border.") + makeCmd("get | info", "Shows information about the border.") +
makeCmd("generate", "Generate all chunks inside the border", "on | off") +
makeCmd("on | off", "Enables/disables the border.") + makeCmd("on | off", "Enables/disables the border.") +
makeCmd("set", "Square border with distance (d) from 0,0.", "r", "<d>") + makeCmd("set", "Square border with distance (d) from 0,0.", "r", "<d>") +
makeCmd("set", "Rectangle defined by two points. Point=x,z.", "r", "<p1>", "<p2>") + makeCmd("set", "Rectangle defined by two points. Point=x,z.", "r", "<p1>", "<p2>") +

View File

@ -18,6 +18,7 @@ package de.craftinc.borderprotection;
import de.craftinc.borderprotection.borders.CircBorder; import de.craftinc.borderprotection.borders.CircBorder;
import de.craftinc.borderprotection.borders.RectBorder; import de.craftinc.borderprotection.borders.RectBorder;
import de.craftinc.borderprotection.commands.CommandSwitch;
import de.craftinc.borderprotection.events.PlayerLoginListener; import de.craftinc.borderprotection.events.PlayerLoginListener;
import de.craftinc.borderprotection.events.PlayerMoveListener; import de.craftinc.borderprotection.events.PlayerMoveListener;
import de.craftinc.borderprotection.events.PlayerTeleportListener; import de.craftinc.borderprotection.events.PlayerTeleportListener;
@ -52,7 +53,7 @@ public class Plugin extends JavaPlugin
PlayerLoginListener playerLoginListener = new PlayerLoginListener(); PlayerLoginListener playerLoginListener = new PlayerLoginListener();
// commands // commands
Commands commandExecutor = new Commands(); CommandSwitch commandExecutor = new CommandSwitch();
getCommand("cibp").setExecutor(commandExecutor); getCommand("cibp").setExecutor(commandExecutor);
// register listeners // register listeners

View File

@ -0,0 +1,58 @@
/* Craft Inc. BorderProtection
Copyright (C) 2013 Paul Schulze
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.borderprotection.commands;
import de.craftinc.borderprotection.Messages;
import de.craftinc.borderprotection.util.UpdateHelper;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
import java.util.List;
public class CheckVersionCommand implements SubCommand
{
@Override
public boolean execute(CommandSender sender, String[] parameters)
{
if ( !sender.hasPermission("craftinc.borderprotection.update") )
{
sender.sendMessage(Messages.noPermissionCheckversion);
return false;
}
if ( UpdateHelper.newVersionAvailable() )
{
sender.sendMessage(
Messages.updateMessage(UpdateHelper.cachedLatestVersion, UpdateHelper.getCurrentVersion()));
return true;
}
else
{
sender.sendMessage(Messages.noUpdateAvailable);
return true;
}
}
@Override
public List<String> commandNames()
{
ArrayList<String> names = new ArrayList<String>();
names.add("checkversion");
return names;
}
}

View File

@ -0,0 +1,94 @@
/* Craft Inc. BorderProtection
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.borderprotection.commands;
import de.craftinc.borderprotection.Messages;
import de.craftinc.borderprotection.borders.Border;
import de.craftinc.borderprotection.borders.CircBorder;
import de.craftinc.borderprotection.borders.RectBorder;
import de.craftinc.borderprotection.util.UpdateHelper;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class CommandSwitch implements CommandExecutor
{
protected Map<String, SubCommand> subCommandsMap = new HashMap<String, SubCommand>();
public CommandSwitch()
{
registerCommand(new CheckVersionCommand());
registerCommand(new GenerateCommand());
registerCommand(new GetCommand());
registerCommand(new HelpCommand());
registerCommand(new OnOffCommand());
registerCommand(new SetCommand());
}
protected void registerCommand(SubCommand command)
{
for (String commandName : command.commandNames())
{
subCommandsMap.put(commandName, command);
}
}
@Override
public boolean onCommand( CommandSender sender, Command command, String label, String[] args )
{
// Check if command comes from a player.
if ( !( sender instanceof Player ) )
{
sender.sendMessage(Messages.commandIssuedByNonPlayer);
return true;
}
boolean success = false;
// command for all actions
if ( command.getName().equalsIgnoreCase("cibp") )
{
if (args.length > 0)
{
String lowerCaseSubCommandName = args[0].toLowerCase();
SubCommand subCommand = subCommandsMap.get(lowerCaseSubCommandName);
if (subCommand != null)
{
success = subCommand.execute(sender, args);
}
}
if (!success)
{
subCommandsMap.get("help").execute(sender, args);
}
return success;
}
return false;
}
}

View File

@ -0,0 +1,49 @@
/* Craft Inc. BorderProtection
Copyright (C) 2013 Paul Schulze
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.borderprotection.commands;
import de.craftinc.borderprotection.Messages;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
import java.util.List;
public class GenerateCommand implements SubCommand
{
@Override
public boolean execute(CommandSender sender, String[] parameters)
{
if ( !sender.hasPermission("craftinc.borderprotection.set") ) // TODO: a new/different permission?
{
sender.sendMessage(Messages.noPermissionSet);
return false;
}
// TODO: implement me!
return false;
}
@Override
public List<String> commandNames()
{
ArrayList<String> names = new ArrayList<String>();
names.add("generate");
return names;
}
}

View File

@ -0,0 +1,57 @@
/* Craft Inc. BorderProtection
Copyright (C) 2013 Paul Schulze
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.borderprotection.commands;
import de.craftinc.borderprotection.Messages;
import de.craftinc.borderprotection.borders.Border;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
public class GetCommand implements SubCommand
{
@Override
public boolean execute(CommandSender sender, String[] parameters)
{
World world = ( (Player) sender ).getWorld();
// exit and send the player a message if no border is set
if ( !Border.getBorders().containsKey(world) )
{
sender.sendMessage(Messages.borderInfoNoBorderSet);
return true;
}
Border border = Border.getBorders().get(world);
sender.sendMessage(Messages.borderInfo(world.getName(), border));
return true;
}
@Override
public List<String> commandNames()
{
ArrayList<String> names = new ArrayList<String>();
names.add("get");
names.add("info");
return names;
}
}

View File

@ -0,0 +1,43 @@
/* Craft Inc. BorderProtection
Copyright (C) 2013 Paul Schulze
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.borderprotection.commands;
import de.craftinc.borderprotection.Messages;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
import java.util.List;
public class HelpCommand implements SubCommand
{
@Override
public boolean execute(CommandSender sender, String[] parameters)
{
sender.sendMessage(Messages.helpGeneral);
return true;
}
@Override
public List<String> commandNames()
{
ArrayList<String> names = new ArrayList<String>();
names.add("help");
return names;
}
}

View File

@ -0,0 +1,85 @@
/* Craft Inc. BorderProtection
Copyright (C) 2013 Paul Schulze
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.borderprotection.commands;
import de.craftinc.borderprotection.Messages;
import de.craftinc.borderprotection.borders.Border;
import org.bukkit.World;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class OnOffCommand implements SubCommand
{
@Override
public boolean execute(CommandSender sender, String[] parameters)
{
if ( !sender.hasPermission("craftinc.borderprotection.set") )
{
sender.sendMessage(Messages.noPermissionSet);
return false;
}
World world = ( (Player) sender ).getWorld();
Border border = Border.getBorders().get(world);
if ( border != null )
{
if ( parameters[0].equalsIgnoreCase("on") )
{
border.enable();
sender.sendMessage(Messages.borderEnabled);
}
else
{
border.disable();
sender.sendMessage(Messages.borderDisabled);
}
}
else
{
sender.sendMessage(Messages.borderInfoNoBorderSet);
}
// save the changed border
try
{
Border.saveBorders();
}
catch ( IOException e )
{
sender.sendMessage(Messages.borderEnableDisableException);
}
return true;
}
@Override
public List<String> commandNames()
{
ArrayList<String> names = new ArrayList<String>();
names.add("on");
names.add("off");
return names;
}
}

View File

@ -0,0 +1,144 @@
/* Craft Inc. BorderProtection
Copyright (C) 2013 Paul Schulze
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.borderprotection.commands;
import de.craftinc.borderprotection.Messages;
import de.craftinc.borderprotection.borders.Border;
import de.craftinc.borderprotection.borders.CircBorder;
import de.craftinc.borderprotection.borders.RectBorder;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SetCommand implements SubCommand
{
@Override
public boolean execute(CommandSender sender, String[] parameters)
{
if ( !( parameters.length == 3 || parameters.length == 4 ) )
{
return false;
}
if ( !sender.hasPermission("craftinc.borderprotection.set") )
{
sender.sendMessage(Messages.noPermissionSet);
return false;
}
World world = ( (Player) sender ).getWorld();
// set [r|c] <distance>
if ( parameters.length == 3 )
{
try
{
Double distance = Double.parseDouble(parameters[2]);
Border newBorder = null;
// rect border
if ( parameters[1].equalsIgnoreCase("r") )
{
newBorder = new RectBorder(new Location(world, distance, 0, distance),
new Location(world, -distance, 0, -distance));
}
// circ border
else if ( parameters[1].equalsIgnoreCase("c") )
{
newBorder = new CircBorder(new Location(world, 0, 0, 0), distance);
}
if ( newBorder != null )
{
sender.sendMessage(Messages.borderCreationSuccessful);
sender.sendMessage(
Messages.borderInfo(world.getName(), newBorder));
}
}
catch ( Exception e )
{
sender.sendMessage(e.getMessage());
}
}
// set r <point1> <point2> | set c <center> <radius>
else
{
try
{
Border newBorder = null;
// rect border
if ( parameters[1].equalsIgnoreCase("r") )
{
Double p1X = Double.parseDouble(parameters[2].split(",")[0]);
Double p1Z = Double.parseDouble(parameters[2].split(",")[1]);
Double p2X = Double.parseDouble(parameters[3].split(",")[0]);
Double p2Z = Double.parseDouble(parameters[3].split(",")[1]);
newBorder = new RectBorder(new Location(world, p1X, 0, p1Z),
new Location(world, p2X, 0, p2Z));
}
// circ border
else if ( parameters[1].equalsIgnoreCase("c") )
{
Double centerX = Double.parseDouble(parameters[2].split(",")[0]);
Double centerZ = Double.parseDouble(parameters[2].split(",")[1]);
Double radius = Double.parseDouble(parameters[3]);
newBorder = new CircBorder(new Location(world, centerX, 0, centerZ), radius);
}
if ( newBorder != null )
{
sender.sendMessage(Messages.borderCreationSuccessful);
sender.sendMessage(
Messages.borderInfo(world.getName(), newBorder));
}
}
catch ( Exception e )
{
sender.sendMessage(e.getMessage());
}
}
// save the new border
try
{
Border.saveBorders();
}
catch ( IOException e )
{
sender.sendMessage(Messages.borderSaveException);
}
return true;
}
@Override
public List<String> commandNames()
{
ArrayList<String> names = new ArrayList<String>();
names.add("set");
return names;
}
}

View File

@ -0,0 +1,40 @@
/* Craft Inc. BorderProtection
Copyright (C) 2013 Paul Schulze
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.borderprotection.commands;
import org.bukkit.command.CommandSender;
import java.util.List;
public interface SubCommand
{
/**
*
* @param sender will contain the command name at index 0.
* @param parameters
* @return
*/
public boolean execute(CommandSender sender, String[] parameters);
/**
*
* @return a list of names of the command. All strings should be lowercase!
*/
public List<String> commandNames();
}