diff --git a/pom.xml b/pom.xml index f7bdbff..b64f558 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.loohp Limbo - 0.3.4-ALPHA + 0.3.5-ALPHA src diff --git a/src/com/loohp/limbo/Events/PlayerChatEvent.java b/src/com/loohp/limbo/Events/PlayerChatEvent.java index 2b28017..9cc7e69 100644 --- a/src/com/loohp/limbo/Events/PlayerChatEvent.java +++ b/src/com/loohp/limbo/Events/PlayerChatEvent.java @@ -9,17 +9,12 @@ public class PlayerChatEvent extends PlayerEvent implements Cancellable { private boolean cancelled; public PlayerChatEvent(Player player, String prefix, String message, boolean cancelled) { - this.player = player; + super(player); this.prefix = prefix; this.message = message; this.cancelled = cancelled; } - @Override - public Player getPlayer() { - return player; - } - public String getPrefix() { return prefix; } diff --git a/src/com/loohp/limbo/Events/PlayerEvent.java b/src/com/loohp/limbo/Events/PlayerEvent.java index 888dd3d..e45dea6 100644 --- a/src/com/loohp/limbo/Events/PlayerEvent.java +++ b/src/com/loohp/limbo/Events/PlayerEvent.java @@ -2,10 +2,16 @@ package com.loohp.limbo.Events; import com.loohp.limbo.Player.Player; -public abstract class PlayerEvent extends Event { +public class PlayerEvent extends Event { - protected Player player; + private Player player; - public abstract Player getPlayer(); + public PlayerEvent(Player player) { + this.player = player; + } + + public Player getPlayer() { + return player; + } } diff --git a/src/com/loohp/limbo/Events/PlayerJoinEvent.java b/src/com/loohp/limbo/Events/PlayerJoinEvent.java index 5698234..d1ffc65 100644 --- a/src/com/loohp/limbo/Events/PlayerJoinEvent.java +++ b/src/com/loohp/limbo/Events/PlayerJoinEvent.java @@ -5,12 +5,7 @@ import com.loohp.limbo.Player.Player; public class PlayerJoinEvent extends PlayerEvent { public PlayerJoinEvent(Player player) { - this.player = player; - } - - @Override - public Player getPlayer() { - return player; + super(player); } } diff --git a/src/com/loohp/limbo/Events/PlayerMoveEvent.java b/src/com/loohp/limbo/Events/PlayerMoveEvent.java new file mode 100644 index 0000000..39f6721 --- /dev/null +++ b/src/com/loohp/limbo/Events/PlayerMoveEvent.java @@ -0,0 +1,86 @@ +package com.loohp.limbo.Events; + +import com.loohp.limbo.Location.Location; +import com.loohp.limbo.Player.Player; + +/** + * Holds information for player movement events + */ +public class PlayerMoveEvent extends PlayerEvent implements Cancellable { + + private boolean cancel = false; + private Location from; + private Location to; + + public PlayerMoveEvent(Player player, Location from, Location to) { + super(player); + this.from = from; + this.to = to; + } + + /** + * Gets the cancellation state of this event. A cancelled event will not + * be executed in the server, but will still pass to other plugins + *

+ * If a move or teleport event is cancelled, the player will be moved or + * teleported back to the Location as defined by getFrom(). This will not + * fire an event + * + * @return true if this event is cancelled + */ + @Override + public boolean isCancelled() { + return cancel; + } + + /** + * Sets the cancellation state of this event. A cancelled event will not + * be executed in the server, but will still pass to other plugins + *

+ * If a move or teleport event is cancelled, the player will be moved or + * teleported back to the Location as defined by getFrom(). This will not + * fire an event + * + * @param cancel true if you wish to cancel this event + */ + @Override + public void setCancelled(boolean cancel) { + this.cancel = cancel; + } + + /** + * Gets the location this player moved from + * + * @return Location the player moved from + */ + public Location getFrom() { + return from; + } + + /** + * Sets the location to mark as where the player moved from + * + * @param from New location to mark as the players previous location + */ + public void setFrom(Location from) { + this.from = from; + } + + /** + * Gets the location this player moved to + * + * @return Location the player moved to + */ + public Location getTo() { + return to; + } + + /** + * Sets the location that this player will move to + * + * @param to New Location this player will move to + */ + public void setTo(Location to) { + this.to = to; + } +} diff --git a/src/com/loohp/limbo/Events/PlayerQuitEvent.java b/src/com/loohp/limbo/Events/PlayerQuitEvent.java index 26760c8..88de2eb 100644 --- a/src/com/loohp/limbo/Events/PlayerQuitEvent.java +++ b/src/com/loohp/limbo/Events/PlayerQuitEvent.java @@ -5,12 +5,7 @@ import com.loohp.limbo.Player.Player; public class PlayerQuitEvent extends PlayerEvent { public PlayerQuitEvent(Player player) { - this.player = player; - } - - @Override - public Player getPlayer() { - return player; + super(player); } } diff --git a/src/com/loohp/limbo/Events/PlayerTeleportEvent.java b/src/com/loohp/limbo/Events/PlayerTeleportEvent.java new file mode 100644 index 0000000..e1a63ed --- /dev/null +++ b/src/com/loohp/limbo/Events/PlayerTeleportEvent.java @@ -0,0 +1,12 @@ +package com.loohp.limbo.Events; + +import com.loohp.limbo.Location.Location; +import com.loohp.limbo.Player.Player; + +public class PlayerTeleportEvent extends PlayerMoveEvent { + + public PlayerTeleportEvent(Player player, Location from, Location to) { + super(player, from, to); + } + +} diff --git a/src/com/loohp/limbo/Player/Player.java b/src/com/loohp/limbo/Player/Player.java index 45a0ba8..29f4c97 100644 --- a/src/com/loohp/limbo/Player/Player.java +++ b/src/com/loohp/limbo/Player/Player.java @@ -6,6 +6,7 @@ import java.util.UUID; import com.loohp.limbo.Limbo; import com.loohp.limbo.Commands.CommandSender; import com.loohp.limbo.Events.PlayerChatEvent; +import com.loohp.limbo.Events.PlayerTeleportEvent; import com.loohp.limbo.Location.Location; import com.loohp.limbo.Server.ClientConnection; import com.loohp.limbo.Server.Packets.PacketPlayOutChat; @@ -92,15 +93,19 @@ public class Player implements CommandSender { } public void teleport(Location location) { - try { - if (!this.location.getWorld().equals(location.getWorld())) { - PacketPlayOutRespawn respawn = new PacketPlayOutRespawn(location.getWorld(), Limbo.getInstance().getDimensionRegistry().getCodec(), 0, gamemode, false, false, true); - clientConnection.sendPacket(respawn); + PlayerTeleportEvent event = Limbo.getInstance().getEventsManager().callEvent(new PlayerTeleportEvent(this, getLocation(), location)); + if (!event.isCancelled()) { + location = event.getTo(); + try { + if (!this.location.getWorld().equals(location.getWorld())) { + PacketPlayOutRespawn respawn = new PacketPlayOutRespawn(location.getWorld(), Limbo.getInstance().getDimensionRegistry().getCodec(), 0, gamemode, false, false, true); + clientConnection.sendPacket(respawn); + } + PacketPlayOutPositionAndLook positionLook = new PacketPlayOutPositionAndLook(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch(), 1); + clientConnection.sendPacket(positionLook); + } catch (IOException e) { + e.printStackTrace(); } - PacketPlayOutPositionAndLook positionLook = new PacketPlayOutPositionAndLook(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch(), 1); - clientConnection.sendPacket(positionLook); - } catch (IOException e) { - e.printStackTrace(); } } diff --git a/src/com/loohp/limbo/Server/ClientConnection.java b/src/com/loohp/limbo/Server/ClientConnection.java index 8d234cb..6c29737 100644 --- a/src/com/loohp/limbo/Server/ClientConnection.java +++ b/src/com/loohp/limbo/Server/ClientConnection.java @@ -19,6 +19,7 @@ import com.loohp.limbo.DeclareCommands; import com.loohp.limbo.Limbo; import com.loohp.limbo.Events.PlayerJoinEvent; import com.loohp.limbo.Events.PlayerLoginEvent; +import com.loohp.limbo.Events.PlayerMoveEvent; import com.loohp.limbo.Events.PlayerQuitEvent; import com.loohp.limbo.Events.StatusPingEvent; import com.loohp.limbo.File.ServerProperties; @@ -356,22 +357,49 @@ public class ClientConnection extends Thread { input.skipBytes(size - DataTypeIO.getVarIntLength(packetId)); } else if (packetType.equals(PacketPlayInPositionAndLook.class)) { PacketPlayInPositionAndLook pos = new PacketPlayInPositionAndLook(input); - player.setLocation(new Location(player.getWorld(), pos.getX(), pos.getY(), pos.getZ(), pos.getYaw(), pos.getPitch())); + Location from = player.getLocation(); + Location to = new Location(player.getWorld(), pos.getX(), pos.getY(), pos.getZ(), pos.getYaw(), pos.getPitch()); - PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) player.getLocation().getX() >> 4, (int) player.getLocation().getZ() >> 4); - sendPacket(response); + PlayerMoveEvent event = Limbo.getInstance().getEventsManager().callEvent(new PlayerMoveEvent(player, from, to)); + if (event.isCancelled()) { + Location returnTo = event.getFrom(); + PacketPlayOutPositionAndLook cancel = new PacketPlayOutPositionAndLook(returnTo.getX(), returnTo.getY(), returnTo.getZ(), returnTo.getYaw(), returnTo.getPitch(), 1); + sendPacket(cancel); + } else { + player.setLocation(event.getTo()); + PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) player.getLocation().getX() >> 4, (int) player.getLocation().getZ() >> 4); + sendPacket(response); + } } else if (packetType.equals(PacketPlayInPosition.class)) { PacketPlayInPosition pos = new PacketPlayInPosition(input); - player.setLocation(new Location(player.getWorld(), pos.getX(), pos.getY(), pos.getZ(), player.getLocation().getYaw(), player.getLocation().getPitch())); + Location from = player.getLocation(); + Location to = new Location(player.getWorld(), pos.getX(), pos.getY(), pos.getZ(), player.getLocation().getYaw(), player.getLocation().getPitch()); - PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) player.getLocation().getX() >> 4, (int) player.getLocation().getZ() >> 4); - sendPacket(response); + PlayerMoveEvent event = Limbo.getInstance().getEventsManager().callEvent(new PlayerMoveEvent(player, from, to)); + if (event.isCancelled()) { + Location returnTo = event.getFrom(); + PacketPlayOutPositionAndLook cancel = new PacketPlayOutPositionAndLook(returnTo.getX(), returnTo.getY(), returnTo.getZ(), returnTo.getYaw(), returnTo.getPitch(), 1); + sendPacket(cancel); + } else { + player.setLocation(event.getTo()); + PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) player.getLocation().getX() >> 4, (int) player.getLocation().getZ() >> 4); + sendPacket(response); + } } else if (packetType.equals(PacketPlayInRotation.class)) { PacketPlayInRotation pos = new PacketPlayInRotation(input); - player.setLocation(new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), pos.getYaw(), pos.getPitch())); + Location from = player.getLocation(); + Location to = new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), pos.getYaw(), pos.getPitch()); - PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) player.getLocation().getX() >> 4, (int) player.getLocation().getZ() >> 4); - sendPacket(response); + PlayerMoveEvent event = Limbo.getInstance().getEventsManager().callEvent(new PlayerMoveEvent(player, from, to)); + if (event.isCancelled()) { + Location returnTo = event.getFrom(); + PacketPlayOutPositionAndLook cancel = new PacketPlayOutPositionAndLook(returnTo.getX(), returnTo.getY(), returnTo.getZ(), returnTo.getYaw(), returnTo.getPitch(), 1); + sendPacket(cancel); + } else { + player.setLocation(event.getTo()); + PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) player.getLocation().getX() >> 4, (int) player.getLocation().getZ() >> 4); + sendPacket(response); + } } else if (packetType.equals(PacketPlayInKeepAlive.class)) { PacketPlayInKeepAlive alive = new PacketPlayInKeepAlive(input); if (alive.getPayload() != lastKeepAlivePayLoad) {