refactoring of command create

This commit is contained in:
Tobias Ottenweller 2012-05-16 22:43:43 +02:00
parent 94404d74c6
commit 5f12bb5d99
2 changed files with 51 additions and 12 deletions

View File

@ -0,0 +1,31 @@
package org.mcteam.ancientgates.commands;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
public abstract class BaseLocationCommand extends BaseCommand
{
protected Location getValidPlayerLocation()
{
// The player might stand in a halfblock or a sign or whatever
// Therefore we load som extra locations and blocks
Block playerBlock = player.getLocation().getBlock();
Block upBlock = playerBlock.getRelative(BlockFace.UP);
if (playerBlock.getType() == Material.AIR) {
return player.getLocation();
}
else if (upBlock.getType() == Material.AIR) {
return new Location(player.getLocation().getWorld(),
player.getLocation().getX(),
player.getLocation().getY() + 1,
player.getLocation().getZ(),
player.getLocation().getYaw(),
player.getLocation().getPitch());
}
return null;
}
}

View File

@ -1,9 +1,11 @@
package org.mcteam.ancientgates.commands;
import org.bukkit.command.CommandSender;
import org.bukkit.Location;
import org.mcteam.ancientgates.Gate;
import org.mcteam.ancientgates.Plugin;
public class CommandCreate extends BaseCommand
public class CommandCreate extends BaseLocationCommand
{
public CommandCreate()
{
@ -16,6 +18,8 @@ public class CommandCreate extends BaseCommand
hasGateParam = false;
helpDescription = "Create a gate";
requiredPermission = Plugin.permissionManage;
}
@ -28,16 +32,20 @@ public class CommandCreate extends BaseCommand
return;
}
Gate.create(id);
sendMessage("Gate with id \"" + id + "\" was created. Now you should:");
sendMessage(new CommandSetFrom().getUsageTemplate(true, true));
}
@Override
public boolean hasPermission(CommandSender sender)
{
return sender.hasPermission(permissionManage);
gate = Gate.create(id);
Location playerLocation = getValidPlayerLocation();
if (playerLocation != null) {
gate.setLocation(playerLocation);
sendMessage("Gate with id \"" + id + "\" was created.\n The gates location has been set to your current location.");
}
else {
sendMessage("Gate with id \"" + id + "\" was created.\n Now you should build a frame and:");
sendMessage(new CommandSetFrom().getUsageTemplate(true, true));
}
}
}