diff --git a/src/org/mcteam/ancientgates/commands/BaseLocationCommand.java b/src/org/mcteam/ancientgates/commands/BaseLocationCommand.java new file mode 100644 index 0000000..0282ca5 --- /dev/null +++ b/src/org/mcteam/ancientgates/commands/BaseLocationCommand.java @@ -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; + } +} diff --git a/src/org/mcteam/ancientgates/commands/CommandCreate.java b/src/org/mcteam/ancientgates/commands/CommandCreate.java index 2c5c7a9..255bd6b 100644 --- a/src/org/mcteam/ancientgates/commands/CommandCreate.java +++ b/src/org/mcteam/ancientgates/commands/CommandCreate.java @@ -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)); + } } + }