Cleanup of the /tree commands

This commit is contained in:
snowleo 2011-11-15 22:50:09 +01:00
parent cccebdd62e
commit 27ee7efde5
3 changed files with 24 additions and 15 deletions

View File

@ -28,6 +28,8 @@ import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
public class Util public class Util
@ -221,6 +223,7 @@ public class Util
// The player can stand inside these materials // The player can stand inside these materials
private static final Set<Integer> AIR_MATERIALS = new HashSet<Integer>(); private static final Set<Integer> AIR_MATERIALS = new HashSet<Integer>();
private static final HashSet<Byte> AIR_MATERIALS_TARGET = new HashSet<Byte>();
static { static {
AIR_MATERIALS.add(Material.AIR.getId()); AIR_MATERIALS.add(Material.AIR.getId());
@ -257,6 +260,20 @@ public class Util
AIR_MATERIALS.add(Material.VINE.getId()); AIR_MATERIALS.add(Material.VINE.getId());
//TODO: Add 1.9 materials //TODO: Add 1.9 materials
for (Integer integer : AIR_MATERIALS)
{
AIR_MATERIALS_TARGET.add(integer.byteValue());
}
AIR_MATERIALS_TARGET.add((byte)Material.WATER.getId());
AIR_MATERIALS_TARGET.add((byte)Material.STATIONARY_WATER.getId());
}
public static Location getTarget(final LivingEntity entity) throws Exception {
final Block block = entity.getTargetBlock(AIR_MATERIALS_TARGET, 300);
if (block == null) {
throw new Exception("Not targeting a block");
}
return block.getLocation();
} }
public static Location getSafeDestination(final Location loc) throws Exception public static Location getSafeDestination(final Location loc) throws Exception

View File

@ -1,6 +1,5 @@
package com.earth2me.essentials.commands; package com.earth2me.essentials.commands;
import com.earth2me.essentials.TargetBlock;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.TreeType; import org.bukkit.TreeType;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
@ -32,13 +31,9 @@ public class Commandbigtree extends EssentialsCommand
throw new NotEnoughArgumentsException(); throw new NotEnoughArgumentsException();
} }
final int[] ignore = final Location loc = Util.getTarget(user);
{
8, 9
};
final Location loc = (new TargetBlock(user, 300, 0.2, ignore)).getTargetBlock().getLocation();
final Location safeLocation = Util.getSafeDestination(loc); final Location safeLocation = Util.getSafeDestination(loc);
final boolean success = user.getWorld().generateTree(safeLocation, (TreeType)tree); final boolean success = user.getWorld().generateTree(safeLocation, tree);
if (success) if (success)
{ {
user.sendMessage(Util.i18n("bigTreeSuccess")); user.sendMessage(Util.i18n("bigTreeSuccess"));

View File

@ -1,13 +1,10 @@
package com.earth2me.essentials.commands; package com.earth2me.essentials.commands;
import com.earth2me.essentials.TargetBlock;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.TreeType; import org.bukkit.TreeType;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.Util; import com.earth2me.essentials.Util;
import org.bukkit.Material;
import org.bukkit.block.Block;
public class Commandtree extends EssentialsCommand public class Commandtree extends EssentialsCommand
@ -20,7 +17,7 @@ public class Commandtree extends EssentialsCommand
@Override @Override
public void run(Server server, User user, String commandLabel, String[] args) throws Exception public void run(Server server, User user, String commandLabel, String[] args) throws Exception
{ {
Object tree = new Object(); TreeType tree;
if (args.length < 1) if (args.length < 1)
{ {
throw new NotEnoughArgumentsException(); throw new NotEnoughArgumentsException();
@ -46,9 +43,9 @@ public class Commandtree extends EssentialsCommand
{ {
8, 9 8, 9
}; };
final Location loc = (new TargetBlock(user, 300, 0.2, ignore)).getTargetBlock().getLocation(); final Location loc = Util.getTarget(user);
final Location safeLocation = Util.getSafeDestination(loc); final Location safeLocation = Util.getSafeDestination(loc);
final boolean success = user.getWorld().generateTree(safeLocation, (TreeType)tree); final boolean success = user.getWorld().generateTree(safeLocation, tree);
if (success) if (success)
{ {
user.sendMessage(Util.i18n("treeSpawned")); user.sendMessage(Util.i18n("treeSpawned"));