Added PlayerMoveEvent and PlayerTeleportEvent

This commit is contained in:
LOOHP 2020-10-31 00:23:04 +08:00
parent 5c69de1637
commit 5ec6eaf6e2
9 changed files with 161 additions and 39 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.loohp</groupId> <groupId>com.loohp</groupId>
<artifactId>Limbo</artifactId> <artifactId>Limbo</artifactId>
<version>0.3.4-ALPHA</version> <version>0.3.5-ALPHA</version>
<build> <build>
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>
<resources> <resources>

View File

@ -9,17 +9,12 @@ public class PlayerChatEvent extends PlayerEvent implements Cancellable {
private boolean cancelled; private boolean cancelled;
public PlayerChatEvent(Player player, String prefix, String message, boolean cancelled) { public PlayerChatEvent(Player player, String prefix, String message, boolean cancelled) {
this.player = player; super(player);
this.prefix = prefix; this.prefix = prefix;
this.message = message; this.message = message;
this.cancelled = cancelled; this.cancelled = cancelled;
} }
@Override
public Player getPlayer() {
return player;
}
public String getPrefix() { public String getPrefix() {
return prefix; return prefix;
} }

View File

@ -2,10 +2,16 @@ package com.loohp.limbo.Events;
import com.loohp.limbo.Player.Player; 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;
}
} }

View File

@ -5,12 +5,7 @@ import com.loohp.limbo.Player.Player;
public class PlayerJoinEvent extends PlayerEvent { public class PlayerJoinEvent extends PlayerEvent {
public PlayerJoinEvent(Player player) { public PlayerJoinEvent(Player player) {
this.player = player; super(player);
}
@Override
public Player getPlayer() {
return player;
} }
} }

View File

@ -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
* <p>
* 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
* <p>
* 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;
}
}

View File

@ -5,12 +5,7 @@ import com.loohp.limbo.Player.Player;
public class PlayerQuitEvent extends PlayerEvent { public class PlayerQuitEvent extends PlayerEvent {
public PlayerQuitEvent(Player player) { public PlayerQuitEvent(Player player) {
this.player = player; super(player);
}
@Override
public Player getPlayer() {
return player;
} }
} }

View File

@ -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);
}
}

View File

@ -6,6 +6,7 @@ import java.util.UUID;
import com.loohp.limbo.Limbo; import com.loohp.limbo.Limbo;
import com.loohp.limbo.Commands.CommandSender; import com.loohp.limbo.Commands.CommandSender;
import com.loohp.limbo.Events.PlayerChatEvent; import com.loohp.limbo.Events.PlayerChatEvent;
import com.loohp.limbo.Events.PlayerTeleportEvent;
import com.loohp.limbo.Location.Location; import com.loohp.limbo.Location.Location;
import com.loohp.limbo.Server.ClientConnection; import com.loohp.limbo.Server.ClientConnection;
import com.loohp.limbo.Server.Packets.PacketPlayOutChat; import com.loohp.limbo.Server.Packets.PacketPlayOutChat;
@ -92,15 +93,19 @@ public class Player implements CommandSender {
} }
public void teleport(Location location) { public void teleport(Location location) {
try { PlayerTeleportEvent event = Limbo.getInstance().getEventsManager().callEvent(new PlayerTeleportEvent(this, getLocation(), location));
if (!this.location.getWorld().equals(location.getWorld())) { if (!event.isCancelled()) {
PacketPlayOutRespawn respawn = new PacketPlayOutRespawn(location.getWorld(), Limbo.getInstance().getDimensionRegistry().getCodec(), 0, gamemode, false, false, true); location = event.getTo();
clientConnection.sendPacket(respawn); 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();
} }
} }

View File

@ -19,6 +19,7 @@ import com.loohp.limbo.DeclareCommands;
import com.loohp.limbo.Limbo; import com.loohp.limbo.Limbo;
import com.loohp.limbo.Events.PlayerJoinEvent; import com.loohp.limbo.Events.PlayerJoinEvent;
import com.loohp.limbo.Events.PlayerLoginEvent; import com.loohp.limbo.Events.PlayerLoginEvent;
import com.loohp.limbo.Events.PlayerMoveEvent;
import com.loohp.limbo.Events.PlayerQuitEvent; import com.loohp.limbo.Events.PlayerQuitEvent;
import com.loohp.limbo.Events.StatusPingEvent; import com.loohp.limbo.Events.StatusPingEvent;
import com.loohp.limbo.File.ServerProperties; import com.loohp.limbo.File.ServerProperties;
@ -356,22 +357,49 @@ public class ClientConnection extends Thread {
input.skipBytes(size - DataTypeIO.getVarIntLength(packetId)); input.skipBytes(size - DataTypeIO.getVarIntLength(packetId));
} else if (packetType.equals(PacketPlayInPositionAndLook.class)) { } else if (packetType.equals(PacketPlayInPositionAndLook.class)) {
PacketPlayInPositionAndLook pos = new PacketPlayInPositionAndLook(input); 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); PlayerMoveEvent event = Limbo.getInstance().getEventsManager().callEvent(new PlayerMoveEvent(player, from, to));
sendPacket(response); 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)) { } else if (packetType.equals(PacketPlayInPosition.class)) {
PacketPlayInPosition pos = new PacketPlayInPosition(input); 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); PlayerMoveEvent event = Limbo.getInstance().getEventsManager().callEvent(new PlayerMoveEvent(player, from, to));
sendPacket(response); 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)) { } else if (packetType.equals(PacketPlayInRotation.class)) {
PacketPlayInRotation pos = new PacketPlayInRotation(input); 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); PlayerMoveEvent event = Limbo.getInstance().getEventsManager().callEvent(new PlayerMoveEvent(player, from, to));
sendPacket(response); 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)) { } else if (packetType.equals(PacketPlayInKeepAlive.class)) {
PacketPlayInKeepAlive alive = new PacketPlayInKeepAlive(input); PacketPlayInKeepAlive alive = new PacketPlayInKeepAlive(input);
if (alive.getPayload() != lastKeepAlivePayLoad) { if (alive.getPayload() != lastKeepAlivePayLoad) {