Update world border packets

This commit is contained in:
TheMode 2021-05-31 18:45:46 +02:00
parent b3692f4f3f
commit 7a9826fc64
10 changed files with 58 additions and 262 deletions

View File

@ -2,7 +2,8 @@ package net.minestom.server.instance;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.play.WorldBorderPacket;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.play.*;
import net.minestom.server.utils.PacketUtils;
import net.minestom.server.utils.Position;
import org.jetbrains.annotations.NotNull;
@ -100,11 +101,7 @@ public class WorldBorder {
*/
public void setWarningTime(int warningTime) {
this.warningTime = warningTime;
WorldBorderPacket worldBorderPacket = new WorldBorderPacket();
worldBorderPacket.action = WorldBorderPacket.Action.SET_WARNING_TIME;
worldBorderPacket.wbAction = new WorldBorderPacket.WBSetWarningTime(warningTime);
sendPacket(worldBorderPacket);
sendPacket(WorldBorderWarningDelayPacket.of(warningTime));
}
public int getWarningBlocks() {
@ -116,11 +113,7 @@ public class WorldBorder {
*/
public void setWarningBlocks(int warningBlocks) {
this.warningBlocks = warningBlocks;
WorldBorderPacket worldBorderPacket = new WorldBorderPacket();
worldBorderPacket.action = WorldBorderPacket.Action.SET_WARNING_BLOCKS;
worldBorderPacket.wbAction = new WorldBorderPacket.WBSetWarningBlocks(warningBlocks);
sendPacket(worldBorderPacket);
sendPacket(WorldBorderWarningReachPacket.of(warningBlocks));
}
/**
@ -134,16 +127,10 @@ public class WorldBorder {
setDiameter(diameter);
return;
}
this.newDiameter = diameter;
this.speed = speed;
this.lerpStartTime = System.currentTimeMillis();
WorldBorderPacket worldBorderPacket = new WorldBorderPacket();
worldBorderPacket.action = WorldBorderPacket.Action.LERP_SIZE;
worldBorderPacket.wbAction = new WorldBorderPacket.WBLerpSize(oldDiameter, newDiameter, speed);
sendPacket(worldBorderPacket);
sendPacket(WorldBorderLerpSizePacket.of(oldDiameter, newDiameter, speed));
}
/**
@ -166,11 +153,7 @@ public class WorldBorder {
this.oldDiameter = diameter;
this.newDiameter = diameter;
this.lerpStartTime = 0;
WorldBorderPacket worldBorderPacket = new WorldBorderPacket();
worldBorderPacket.action = WorldBorderPacket.Action.SET_SIZE;
worldBorderPacket.wbAction = new WorldBorderPacket.WBSetSize(diameter);
sendPacket(worldBorderPacket);
sendPacket(WorldBorderSizePacket.of(diameter));
}
/**
@ -243,11 +226,9 @@ public class WorldBorder {
* @param player the player to send the packet to
*/
protected void init(@NotNull Player player) {
WorldBorderPacket worldBorderPacket = new WorldBorderPacket();
worldBorderPacket.action = WorldBorderPacket.Action.INITIALIZE;
worldBorderPacket.wbAction = new WorldBorderPacket.WBInitialize(centerX, centerZ, oldDiameter, newDiameter, speed,
portalTeleportBoundary, warningTime, warningBlocks);
player.getPlayerConnection().sendPacket(worldBorderPacket);
player.getPlayerConnection().sendPacket(
InitializeWorldBorderPacket.of(centerX, centerZ, oldDiameter, newDiameter, speed,
portalTeleportBoundary, warningTime, warningBlocks));
}
/**
@ -264,18 +245,10 @@ public class WorldBorder {
* Sends the new world border centers to all instance players.
*/
private void refreshCenter() {
WorldBorderPacket worldBorderPacket = new WorldBorderPacket();
worldBorderPacket.action = WorldBorderPacket.Action.SET_CENTER;
worldBorderPacket.wbAction = new WorldBorderPacket.WBSetCenter(centerX, centerZ);
sendPacket(worldBorderPacket);
sendPacket(WorldBorderCenterPacket.of(centerX, centerZ));
}
/**
* Sends a {@link WorldBorderPacket} to all the instance players.
*
* @param packet the packet to send
*/
private void sendPacket(@NotNull WorldBorderPacket packet) {
private void sendPacket(@NotNull ServerPacket packet) {
PacketUtils.sendGroupedPacket(instance.getPlayers(), packet);
}

View File

@ -11,7 +11,6 @@ public class ClientPlayPacketsHandler extends ClientPacketsHandler {
register(0x04, ClientStatusPacket::new);
register(0x05, ClientSettingsPacket::new);
register(0x06, ClientTabCompletePacket::new);
//register(0x07, ClientWindowConfirmationPacket::new);
register(0x08, ClientClickWindowButtonPacket::new);
register(0x09, ClientClickWindowPacket::new);
register(0x0A, ClientCloseWindowPacket::new);

View File

@ -27,7 +27,6 @@ public class ServerPacketIdentifier {
public static final int CLEAR_TITLES = 0x10;
public static final int TAB_COMPLETE = 0x11;
public static final int DECLARE_COMMANDS = 0x12;
//public static final int WINDOW_CONFIRMATION = 0x11;
public static final int CLOSE_WINDOW = 0x13;
public static final int WINDOW_ITEMS = 0x14;
public static final int WINDOW_PROPERTY = 0x15;
@ -53,7 +52,6 @@ public class ServerPacketIdentifier {
public static final int ENTITY_POSITION = 0x29;
public static final int ENTITY_POSITION_AND_ROTATION = 0x2A;
public static final int ENTITY_ROTATION = 0x2B;
//public static final int ENTITY_MOVEMENT = 0x2A;
public static final int VEHICLE_MOVE = 0x2C;
public static final int OPEN_BOOK = 0x2D;
public static final int OPEN_WINDOW = 0x2E;
@ -100,7 +98,6 @@ public class ServerPacketIdentifier {
public static final int TIME_UPDATE = 0x57;
public static final int SET_TITLE_TEXT = 0x58;
public static final int SET_TITLE_TIME = 0x59;
//public static final int TITLE = 0x4F;
public static final int ENTITY_SOUND_EFFECT = 0x5A;
public static final int SOUND_EFFECT = 0x5B;
public static final int STOP_SOUND = 0x5C;

View File

@ -16,6 +16,20 @@ public class InitializeWorldBorderPacket implements ServerPacket {
public int warningTime;
public int warningBlocks;
public static InitializeWorldBorderPacket of(double x, double z, double oldDiameter, double newDiameter, long speed,
int portalTeleportBoundary, int warningTime, int warningBlocks) {
InitializeWorldBorderPacket packet = new InitializeWorldBorderPacket();
packet.x = x;
packet.z = z;
packet.oldDiameter = oldDiameter;
packet.newDiameter = newDiameter;
packet.speed = speed;
packet.portalTeleportBoundary = portalTeleportBoundary;
packet.warningTime = warningTime;
packet.warningBlocks = warningBlocks;
return packet;
}
@Override
public void write(BinaryWriter writer) {
writer.writeDouble(x);

View File

@ -11,6 +11,13 @@ public class WorldBorderCenterPacket implements ServerPacket {
public double x;
public double z;
public static WorldBorderCenterPacket of(double x, double z) {
WorldBorderCenterPacket packet = new WorldBorderCenterPacket();
packet.x = x;
packet.z = z;
return packet;
}
@Override
public void read(@NotNull BinaryReader reader) {
this.x = reader.readDouble();

View File

@ -12,6 +12,14 @@ public class WorldBorderLerpSizePacket implements ServerPacket {
public double newDiameter;
public long speed;
public static WorldBorderLerpSizePacket of(double oldDiameter, double newDiameter, long speed) {
WorldBorderLerpSizePacket packet = new WorldBorderLerpSizePacket();
packet.oldDiameter = oldDiameter;
packet.newDiameter = newDiameter;
packet.speed = speed;
return packet;
}
@Override
public void read(@NotNull BinaryReader reader) {
this.oldDiameter = reader.readDouble();

View File

@ -1,220 +0,0 @@
package net.minestom.server.network.packet.server.play;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.binary.Readable;
import net.minestom.server.utils.binary.Writeable;
import org.jetbrains.annotations.NotNull;
import java.util.function.Supplier;
public class WorldBorderPacket implements ServerPacket {
public Action action;
public WBAction wbAction;
private static final WBAction DEFAULT_ACTION = new WBSetSize(0.0);
/**
* Default constructor, required for reflection operations.
*/
public WorldBorderPacket() {
action = Action.SET_SIZE;
wbAction = DEFAULT_ACTION;
}
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeVarInt(action.ordinal());
wbAction.write(writer);
}
@Override
public void read(@NotNull BinaryReader reader) {
action = Action.values()[reader.readVarInt()];
wbAction = action.generateNewInstance();
wbAction.read(reader);
}
@Override
public int getId() {
return ServerPacketIdentifier.WORLD_BORDER;
}
public enum Action {
SET_SIZE(() -> new WBSetSize(0.0)),
LERP_SIZE(() -> new WBLerpSize(0.0, 0.0, 0)),
SET_CENTER(() -> new WBSetCenter(0.0, 0.0)),
INITIALIZE(() -> new WBInitialize(0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0)),
SET_WARNING_TIME(() -> new WBSetWarningTime(0)),
SET_WARNING_BLOCKS(() -> new WBSetWarningBlocks(0));
private Supplier<WBAction> generator;
Action(Supplier<WBAction> generator) {
this.generator = generator;
}
public WBAction generateNewInstance() {
return generator.get();
}
}
public static abstract class WBAction implements Writeable, Readable {}
public static class WBSetSize extends WBAction {
public double diameter;
public WBSetSize(double diameter) {
this.diameter = diameter;
}
@Override
public void write(BinaryWriter writer) {
writer.writeDouble(diameter);
}
@Override
public void read(@NotNull BinaryReader reader) {
diameter = reader.readDouble();
}
}
public static class WBLerpSize extends WBAction {
public double oldDiameter;
public double newDiameter;
public long speed;
public WBLerpSize(double oldDiameter, double newDiameter, long speed) {
this.oldDiameter = oldDiameter;
this.newDiameter = newDiameter;
this.speed = speed;
}
@Override
public void write(BinaryWriter writer) {
writer.writeDouble(oldDiameter);
writer.writeDouble(newDiameter);
writer.writeVarLong(speed);
}
@Override
public void read(@NotNull BinaryReader reader) {
oldDiameter = reader.readDouble();
newDiameter = reader.readDouble();
speed = reader.readVarLong();
}
}
public static class WBSetCenter extends WBAction {
public double x, z;
public WBSetCenter(double x, double z) {
this.x = x;
this.z = z;
}
@Override
public void write(BinaryWriter writer) {
writer.writeDouble(x);
writer.writeDouble(z);
}
@Override
public void read(@NotNull BinaryReader reader) {
x = reader.readDouble();
z = reader.readDouble();
}
}
public static class WBInitialize extends WBAction {
public double x, z;
public double oldDiameter;
public double newDiameter;
public long speed;
public int portalTeleportBoundary;
public int warningTime;
public int warningBlocks;
public WBInitialize(double x, double z, double oldDiameter, double newDiameter, long speed,
int portalTeleportBoundary, int warningTime, int warningBlocks) {
this.x = x;
this.z = z;
this.oldDiameter = oldDiameter;
this.newDiameter = newDiameter;
this.speed = speed;
this.portalTeleportBoundary = portalTeleportBoundary;
this.warningTime = warningTime;
this.warningBlocks = warningBlocks;
}
@Override
public void write(BinaryWriter writer) {
writer.writeDouble(x);
writer.writeDouble(z);
writer.writeDouble(oldDiameter);
writer.writeDouble(newDiameter);
writer.writeVarLong(speed);
writer.writeVarInt(portalTeleportBoundary);
writer.writeVarInt(warningTime);
writer.writeVarInt(warningBlocks);
}
@Override
public void read(@NotNull BinaryReader reader) {
x = reader.readDouble();
z = reader.readDouble();
oldDiameter = reader.readDouble();
newDiameter = reader.readDouble();
speed = reader.readVarLong();
portalTeleportBoundary = reader.readVarInt();
warningTime = reader.readVarInt();
warningBlocks = reader.readVarInt();
}
}
public static class WBSetWarningTime extends WBAction {
public int warningTime;
public WBSetWarningTime(int warningTime) {
this.warningTime = warningTime;
}
@Override
public void write(BinaryWriter writer) {
writer.writeVarInt(warningTime);
}
@Override
public void read(@NotNull BinaryReader reader) {
warningTime = reader.readVarInt();
}
}
public static class WBSetWarningBlocks extends WBAction {
public int warningBlocks;
public WBSetWarningBlocks(int warningBlocks) {
this.warningBlocks = warningBlocks;
}
@Override
public void write(BinaryWriter writer) {
writer.writeVarInt(warningBlocks);
}
@Override
public void read(@NotNull BinaryReader reader) {
warningBlocks = reader.readVarInt();
}
}
}

View File

@ -10,6 +10,12 @@ public class WorldBorderSizePacket implements ServerPacket {
public double diameter;
public static WorldBorderSizePacket of(double diameter) {
WorldBorderSizePacket packet = new WorldBorderSizePacket();
packet.diameter = diameter;
return packet;
}
@Override
public void read(@NotNull BinaryReader reader) {
this.diameter = reader.readDouble();

View File

@ -10,6 +10,12 @@ public class WorldBorderWarningDelayPacket implements ServerPacket {
public int warningTime;
public static WorldBorderWarningDelayPacket of(int warningTime) {
WorldBorderWarningDelayPacket packet = new WorldBorderWarningDelayPacket();
packet.warningTime = warningTime;
return packet;
}
@Override
public void read(@NotNull BinaryReader reader) {
this.warningTime = reader.readVarInt();

View File

@ -10,6 +10,12 @@ public class WorldBorderWarningReachPacket implements ServerPacket {
public int warningBlocks;
public static WorldBorderWarningReachPacket of(int warningBlocks) {
WorldBorderWarningReachPacket packet = new WorldBorderWarningReachPacket();
packet.warningBlocks = warningBlocks;
return packet;
}
@Override
public void read(@NotNull BinaryReader reader) {
this.warningBlocks = reader.readVarInt();