Add /cbtool and /setlever, commands geared toward Command Blocks.

This commit is contained in:
StevenLawson 2013-09-14 22:00:11 -04:00
parent 1416429910
commit ded31e4640
4 changed files with 287 additions and 5 deletions

View File

@ -1,5 +1,5 @@
#Fri, 13 Sep 2013 22:11:21 -0400 #Sat, 14 Sep 2013 21:58:26 -0400
program.VERSION=3.2 program.VERSION=3.2
program.BUILDNUM=579 program.BUILDNUM=594
program.BUILDDATE=09/13/2013 10\:11 PM program.BUILDDATE=09/14/2013 09\:58 PM

View File

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit! #Build Number for ANT. Do not edit!
#Fri Sep 13 22:11:21 EDT 2013 #Sat Sep 14 21:58:26 EDT 2013
build.number=580 build.number=595

View File

@ -0,0 +1,206 @@
package me.StevenLawson.TotalFreedomMod.Commands;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import me.StevenLawson.TotalFreedomMod.TFM_Log;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@CommandPermissions(level = AdminLevel.ALL, source = SourceType.BOTH)
@CommandParameters(description = "No Description Yet", usage = "/<command>")
public class Command_cbtool extends TFM_Command
{
@Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
if (args.length < 1)
{
return false;
}
if ("targetblock".equalsIgnoreCase(args[0]) && sender instanceof Player)
{
Block targetBlock = sender_p.getTargetBlock(null, 100);
playerMsg("Your target block: " + targetBlock.getLocation().toString());
return true;
}
try
{
final StringBuffer generatedCommand = new StringBuffer();
final Matcher matcher = Pattern.compile("\\[(.+?)\\]").matcher(StringUtils.join(args, " ").trim());
while (matcher.find())
{
matcher.appendReplacement(generatedCommand, processSubCommand(matcher.group(1)));
}
matcher.appendTail(generatedCommand);
server.dispatchCommand(sender, generatedCommand.toString());
}
catch (SubCommandFailureException ex)
{
}
catch (Exception ex)
{
TFM_Log.severe(ex);
}
return true;
}
private String processSubCommand(final String subcommand) throws SubCommandFailureException
{
final String[] args = StringUtils.split(subcommand, " ");
if (args.length == 1)
{
throw new SubCommandFailureException("Invalid subcommand name.");
}
return SubCommand.getByName(args[0]).getExecutable().execute(ArrayUtils.remove(args, 0));
}
private static enum SubCommand
{
PLAYER_DETECT("playerdetect", new SubCommandExecutable()
{
@Override
public String execute(String[] args) throws SubCommandFailureException
{
if (args.length != 5)
{
throw new SubCommandFailureException("Invalid # of arguments.");
}
double x, y, z;
try
{
x = Double.parseDouble(args[0].trim());
y = Double.parseDouble(args[1].trim());
z = Double.parseDouble(args[2].trim());
}
catch (NumberFormatException ex)
{
throw new SubCommandFailureException("Invalid coordinates.");
}
World world = null;
final String needleWorldName = args[3].trim();
final List<World> worlds = Bukkit.getWorlds();
for (final World testWorld : worlds)
{
if (testWorld.getName().trim().equalsIgnoreCase(needleWorldName))
{
world = testWorld;
break;
}
}
if (world == null)
{
throw new SubCommandFailureException("Invalid world name.");
}
final Location testLocation = new Location(world, x, y, z);
double radius;
try
{
radius = Double.parseDouble(args[4].trim());
}
catch (NumberFormatException ex)
{
throw new SubCommandFailureException("Invalid radius.");
}
final double radiusSq = radius * radius;
final List<Player> worldPlayers = testLocation.getWorld().getPlayers();
for (final Player testPlayer : worldPlayers)
{
if (testPlayer.getLocation().distanceSquared(testLocation) < radiusSq)
{
return testPlayer.getName();
}
}
throw new SubCommandFailureException("No player found in range.");
}
}),
PLAYER_DETECT_BOOLEAN("playerdetectboolean", new SubCommandExecutable()
{
@Override
public String execute(String[] args) throws SubCommandFailureException
{
try
{
PLAYER_DETECT.getExecutable().execute(args);
}
catch (SubCommandFailureException ex)
{
return "0";
}
return "1";
}
});
//
private final String name;
private final SubCommandExecutable executable;
private SubCommand(String subCommandName, SubCommandExecutable subCommandImpl)
{
this.name = subCommandName;
this.executable = subCommandImpl;
}
public SubCommandExecutable getExecutable()
{
return executable;
}
public String getName()
{
return name;
}
public static SubCommand getByName(String needle) throws SubCommandFailureException
{
needle = needle.trim();
for (SubCommand subCommand : values())
{
if (subCommand.getName().equalsIgnoreCase(needle))
{
return subCommand;
}
}
throw new SubCommandFailureException("Invalid subcommand name.");
}
}
private interface SubCommandExecutable
{
public String execute(String[] args) throws SubCommandFailureException;
}
private static class SubCommandFailureException extends Exception
{
public SubCommandFailureException()
{
}
public SubCommandFailureException(String message)
{
super(message);
}
}
}

View File

@ -0,0 +1,76 @@
package me.StevenLawson.TotalFreedomMod.Commands;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@CommandPermissions(level = AdminLevel.ALL, source = SourceType.BOTH)
@CommandParameters(description = "Set the on/off state of the lever at position x, y, z in world 'worldname'.", usage = "/<command> <x> <y> <z> <worldname> <on|off>")
public class Command_setlever extends TFM_Command
{
@Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
if (args.length != 5)
{
return false;
}
double x, y, z;
try
{
x = Double.parseDouble(args[0]);
y = Double.parseDouble(args[1]);
z = Double.parseDouble(args[2]);
}
catch (NumberFormatException ex)
{
playerMsg("Invalid coordinates.");
return true;
}
World world = null;
final String needleWorldName = args[3].trim();
final List<World> worlds = server.getWorlds();
for (final World testWorld : worlds)
{
if (testWorld.getName().trim().equalsIgnoreCase(needleWorldName))
{
world = testWorld;
break;
}
}
if (world == null)
{
playerMsg("Invalid world name.");
return true;
}
final Location leverLocation = new Location(world, x, y, z);
final boolean leverOn = (args[4].trim().equalsIgnoreCase("on") || args[4].trim().equalsIgnoreCase("1")) ? true : false;
final Block targetBlock = leverLocation.getBlock();
if (targetBlock.getType() == Material.LEVER)
{
org.bukkit.material.Lever lever = new org.bukkit.material.Lever(Material.LEVER, targetBlock.getData());
lever.setPowered(leverOn);
targetBlock.setData(lever.getData());
targetBlock.getState().update();
}
else
{
playerMsg("Target block " + targetBlock + " is not a lever.");
return true;
}
return true;
}
}