Turn Target into a real class.

This commit is contained in:
KHobbits 2013-06-08 21:40:02 +01:00
parent 7276bcccab
commit ac6b74887f
7 changed files with 126 additions and 90 deletions

View File

@ -0,0 +1,10 @@
package com.earth2me.essentials;
import org.bukkit.Location;
import org.bukkit.Server;
public interface ITarget
{
public Location getLocation();
}

View File

@ -0,0 +1,28 @@
package com.earth2me.essentials;
import org.bukkit.Location;
import org.bukkit.Server;
public class LocationTarget implements ITarget
{
private final Location location;
private final String name;
LocationTarget(Location location)
{
this.location = location;
this.name = null;
}
@Override
public Location getLocation(Server server)
{
if (this.name != null)
{
return server.getPlayerExact(name).getLocation();
}
return location;
}
}

View File

@ -0,0 +1,28 @@
package com.earth2me.essentials;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public class PlayerTarget implements ITarget
{
private final Location location;
private final String name;
PlayerTarget(Player entity)
{
this.name = entity.getName();
this.location = null;
}
@Override
public Location getLocation()
{
if (this.name != null)
{
return Bukkit.getServer().getPlayerExact(name).getLocation();
}
return location;
}
}

View File

@ -11,34 +11,7 @@ import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
public class Teleport implements ITeleport
{
public class Target
{
private final Location location;
private final String name;
Target(Location location)
{
this.location = location;
this.name = null;
}
Target(Player entity)
{
this.name = entity.getName();
this.location = null;
}
public Location getLocation()
{
if (this.name != null)
{
return ess.getServer().getPlayerExact(name).getLocation();
}
return location;
}
}
{
private final IUser teleportOwner;
private final IEssentials ess;
private TimedTeleport timedTeleport;
@ -103,7 +76,7 @@ public class Teleport implements ITeleport
{
cooldown(false);
}
now(teleportOwner, new Target(loc), cause);
now(teleportOwner, new LocationTarget(loc), cause);
}
@Override
@ -113,10 +86,10 @@ public class Teleport implements ITeleport
{
cooldown(false);
}
now(teleportOwner, new Target(entity), cause);
now(teleportOwner, new PlayerTarget(entity), cause);
}
protected void now(IUser teleportee, Target target, TeleportCause cause) throws Exception
protected void now(IUser teleportee, ITarget target, TeleportCause cause) throws Exception
{
cancel(false);
teleportee.setLastLocation();
@ -134,28 +107,28 @@ public class Teleport implements ITeleport
@Override
public void teleport(Location loc, Trade chargeFor, TeleportCause cause) throws Exception
{
teleport(teleportOwner, new Target(loc), chargeFor, cause);
teleport(teleportOwner, new LocationTarget(loc), chargeFor, cause);
}
@Override
public void teleport(Player entity, Trade chargeFor, TeleportCause cause) throws Exception
{
teleport(teleportOwner, new Target(entity), chargeFor, cause);
teleport(teleportOwner, new PlayerTarget(entity), chargeFor, cause);
}
@Override
public void teleportPlayer(IUser teleportee, Location loc, Trade chargeFor, TeleportCause cause) throws Exception
{
teleport(teleportee, new Target(loc), chargeFor, cause);
teleport(teleportee, new LocationTarget(loc), chargeFor, cause);
}
@Override
public void teleportPlayer(IUser teleportee, Player entity, Trade chargeFor, TeleportCause cause) throws Exception
{
teleport(teleportee, new Target(entity), chargeFor, cause);
teleport(teleportee, new PlayerTarget(entity), chargeFor, cause);
}
private void teleport(IUser teleportee, Target target, Trade chargeFor, TeleportCause cause) throws Exception
private void teleport(IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause) throws Exception
{
double delay = ess.getSettings().getTeleportDelay();
@ -185,7 +158,7 @@ public class Teleport implements ITeleport
@Override
public void teleportToMe(IUser otherUser, Trade chargeFor, TeleportCause cause) throws Exception
{
Target target = new Target(teleportOwner);
ITarget target = new PlayerTarget(teleportOwner);
teleport(otherUser, target, chargeFor, cause);
}
@ -221,7 +194,7 @@ public class Teleport implements ITeleport
Location bed = player.getBedSpawnLocation();
if (bed != null)
{
now(teleportee, new Target(bed), cause);
now(teleportee, new LocationTarget(bed), cause);
}
else
{
@ -231,7 +204,7 @@ public class Teleport implements ITeleport
}
final PlayerRespawnEvent pre = new PlayerRespawnEvent(player, player.getWorld().getSpawnLocation(), false);
ess.getServer().getPluginManager().callEvent(pre);
now(teleportee, new Target(pre.getRespawnLocation()), cause);
now(teleportee, new LocationTarget(pre.getRespawnLocation()), cause);
}
}
@ -241,28 +214,28 @@ public class Teleport implements ITeleport
{
Location loc = ess.getWarps().getWarp(warp);
teleportee.sendMessage(_("warpingTo", warp));
teleport(teleportee, new Target(loc), chargeFor, cause);
teleport(teleportee, new LocationTarget(loc), chargeFor, cause);
}
//The back function is a wrapper used to teleportPlayer a player /back to their previous location.
@Override
public void back(Trade chargeFor) throws Exception
{
teleport(teleportOwner, new Target(teleportOwner.getLastLocation()), chargeFor, TeleportCause.COMMAND);
teleport(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()), chargeFor, TeleportCause.COMMAND);
}
//This function is used to throw a user back after a jail sentence
@Override
public void back() throws Exception
{
now(teleportOwner, new Target(teleportOwner.getLastLocation()), TeleportCause.COMMAND);
now(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()), TeleportCause.COMMAND);
}
//This function handles teleporting to /home
@Override
public void home(Location loc, Trade chargeFor) throws Exception
{
teleport(teleportOwner, new Target(loc), chargeFor, TeleportCause.COMMAND);
teleport(teleportOwner, new LocationTarget(loc), chargeFor, TeleportCause.COMMAND);
}
//If we need to cancelTimer a pending teleportPlayer call this method
@ -275,7 +248,7 @@ public class Teleport implements ITeleport
}
}
private void initTimer(long delay, IUser teleportUser, Target target, Trade chargeFor, TeleportCause cause, boolean respawn)
private void initTimer(long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn)
{
timedTeleport = new TimedTeleport(teleportOwner, ess, this, delay, teleportUser, target, chargeFor, cause, respawn);
}

View File

@ -1,7 +1,6 @@
package com.earth2me.essentials;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Teleport.Target;
import org.bukkit.Location;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
@ -23,16 +22,15 @@ public class TimedTeleport implements Runnable
private long timer_initX;
private long timer_initY;
private long timer_initZ;
private Target timer_teleportTarget;
private ITarget timer_teleportTarget;
private boolean timer_respawn;
private boolean timer_canMove;
private Trade timer_chargeFor;
private TeleportCause timer_cause;
public TimedTeleport(IUser user, IEssentials ess, Teleport teleport, long delay, IUser teleportUser, Target target, Trade chargeFor, TeleportCause cause, boolean respawn)
public TimedTeleport(IUser user, IEssentials ess, Teleport teleport, long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn)
{
this.teleportOwner = user;
this.ess = ess;
this.teleport = teleport;

View File

@ -234,48 +234,48 @@ public class Util
return c.getTimeInMillis();
}
// The player can stand inside these materials
private static final Set<Integer> UNSAFE_MATERIALS = new HashSet<Integer>();
private static final Set<Integer> HOLLOW_MATERIALS = new HashSet<Integer>();
private static final HashSet<Byte> TRANSPARENT_MATERIALS = new HashSet<Byte>();
static
{
UNSAFE_MATERIALS.add(Material.AIR.getId());
UNSAFE_MATERIALS.add(Material.SAPLING.getId());
UNSAFE_MATERIALS.add(Material.POWERED_RAIL.getId());
UNSAFE_MATERIALS.add(Material.DETECTOR_RAIL.getId());
UNSAFE_MATERIALS.add(Material.LONG_GRASS.getId());
UNSAFE_MATERIALS.add(Material.DEAD_BUSH.getId());
UNSAFE_MATERIALS.add(Material.YELLOW_FLOWER.getId());
UNSAFE_MATERIALS.add(Material.RED_ROSE.getId());
UNSAFE_MATERIALS.add(Material.BROWN_MUSHROOM.getId());
UNSAFE_MATERIALS.add(Material.RED_MUSHROOM.getId());
UNSAFE_MATERIALS.add(Material.TORCH.getId());
UNSAFE_MATERIALS.add(Material.REDSTONE_WIRE.getId());
UNSAFE_MATERIALS.add(Material.SEEDS.getId());
UNSAFE_MATERIALS.add(Material.SIGN_POST.getId());
UNSAFE_MATERIALS.add(Material.WOODEN_DOOR.getId());
UNSAFE_MATERIALS.add(Material.LADDER.getId());
UNSAFE_MATERIALS.add(Material.RAILS.getId());
UNSAFE_MATERIALS.add(Material.WALL_SIGN.getId());
UNSAFE_MATERIALS.add(Material.LEVER.getId());
UNSAFE_MATERIALS.add(Material.STONE_PLATE.getId());
UNSAFE_MATERIALS.add(Material.IRON_DOOR_BLOCK.getId());
UNSAFE_MATERIALS.add(Material.WOOD_PLATE.getId());
UNSAFE_MATERIALS.add(Material.REDSTONE_TORCH_OFF.getId());
UNSAFE_MATERIALS.add(Material.REDSTONE_TORCH_ON.getId());
UNSAFE_MATERIALS.add(Material.STONE_BUTTON.getId());
UNSAFE_MATERIALS.add(Material.SNOW.getId());
UNSAFE_MATERIALS.add(Material.SUGAR_CANE_BLOCK.getId());
UNSAFE_MATERIALS.add(Material.DIODE_BLOCK_OFF.getId());
UNSAFE_MATERIALS.add(Material.DIODE_BLOCK_ON.getId());
UNSAFE_MATERIALS.add(Material.PUMPKIN_STEM.getId());
UNSAFE_MATERIALS.add(Material.MELON_STEM.getId());
UNSAFE_MATERIALS.add(Material.VINE.getId());
UNSAFE_MATERIALS.add(Material.FENCE_GATE.getId());
UNSAFE_MATERIALS.add(Material.WATER_LILY.getId());
UNSAFE_MATERIALS.add(Material.NETHER_WARTS.getId());
HOLLOW_MATERIALS.add(Material.AIR.getId());
HOLLOW_MATERIALS.add(Material.SAPLING.getId());
HOLLOW_MATERIALS.add(Material.POWERED_RAIL.getId());
HOLLOW_MATERIALS.add(Material.DETECTOR_RAIL.getId());
HOLLOW_MATERIALS.add(Material.LONG_GRASS.getId());
HOLLOW_MATERIALS.add(Material.DEAD_BUSH.getId());
HOLLOW_MATERIALS.add(Material.YELLOW_FLOWER.getId());
HOLLOW_MATERIALS.add(Material.RED_ROSE.getId());
HOLLOW_MATERIALS.add(Material.BROWN_MUSHROOM.getId());
HOLLOW_MATERIALS.add(Material.RED_MUSHROOM.getId());
HOLLOW_MATERIALS.add(Material.TORCH.getId());
HOLLOW_MATERIALS.add(Material.REDSTONE_WIRE.getId());
HOLLOW_MATERIALS.add(Material.SEEDS.getId());
HOLLOW_MATERIALS.add(Material.SIGN_POST.getId());
HOLLOW_MATERIALS.add(Material.WOODEN_DOOR.getId());
HOLLOW_MATERIALS.add(Material.LADDER.getId());
HOLLOW_MATERIALS.add(Material.RAILS.getId());
HOLLOW_MATERIALS.add(Material.WALL_SIGN.getId());
HOLLOW_MATERIALS.add(Material.LEVER.getId());
HOLLOW_MATERIALS.add(Material.STONE_PLATE.getId());
HOLLOW_MATERIALS.add(Material.IRON_DOOR_BLOCK.getId());
HOLLOW_MATERIALS.add(Material.WOOD_PLATE.getId());
HOLLOW_MATERIALS.add(Material.REDSTONE_TORCH_OFF.getId());
HOLLOW_MATERIALS.add(Material.REDSTONE_TORCH_ON.getId());
HOLLOW_MATERIALS.add(Material.STONE_BUTTON.getId());
HOLLOW_MATERIALS.add(Material.SNOW.getId());
HOLLOW_MATERIALS.add(Material.SUGAR_CANE_BLOCK.getId());
HOLLOW_MATERIALS.add(Material.DIODE_BLOCK_OFF.getId());
HOLLOW_MATERIALS.add(Material.DIODE_BLOCK_ON.getId());
HOLLOW_MATERIALS.add(Material.PUMPKIN_STEM.getId());
HOLLOW_MATERIALS.add(Material.MELON_STEM.getId());
HOLLOW_MATERIALS.add(Material.VINE.getId());
HOLLOW_MATERIALS.add(Material.FENCE_GATE.getId());
HOLLOW_MATERIALS.add(Material.WATER_LILY.getId());
HOLLOW_MATERIALS.add(Material.NETHER_WARTS.getId());
for (Integer integer : UNSAFE_MATERIALS)
for (Integer integer : HOLLOW_MATERIALS)
{
TRANSPARENT_MATERIALS.add(integer.byteValue());
}
@ -407,7 +407,7 @@ public class Util
private static boolean isBlockAboveAir(final World world, final int x, final int y, final int z)
{
return UNSAFE_MATERIALS.contains(world.getBlockAt(x, y - 1, z).getType().getId());
return HOLLOW_MATERIALS.contains(world.getBlockAt(x, y - 1, z).getType().getId());
}
public static boolean isBlockUnsafe(final World world, final int x, final int y, final int z)
@ -437,8 +437,8 @@ public class Util
return true;
}
if ((!UNSAFE_MATERIALS.contains(world.getBlockAt(x, y, z).getType().getId()))
|| (!UNSAFE_MATERIALS.contains(world.getBlockAt(x, y + 1, z).getType().getId())))
if ((!HOLLOW_MATERIALS.contains(world.getBlockAt(x, y, z).getType().getId()))
|| (!HOLLOW_MATERIALS.contains(world.getBlockAt(x, y + 1, z).getType().getId())))
{
return true;
}

View File

@ -1,7 +1,6 @@
package com.earth2me.essentials.api;
import com.earth2me.essentials.IUser;
import com.earth2me.essentials.Teleport.Target;
import com.earth2me.essentials.Trade;
import org.bukkit.Location;
import org.bukkit.entity.Player;