Added WorldBorder

This commit is contained in:
Felix Cravic 2020-05-26 19:22:47 +02:00
parent 182b6fbe1b
commit 4ed213249e
7 changed files with 360 additions and 11 deletions

View File

@ -1,5 +1,6 @@
package fr.themode.demo;
import fr.themode.demo.entity.ChickenCreature;
import fr.themode.demo.generator.ChunkGeneratorDemo;
import fr.themode.demo.generator.NoiseTestGenerator;
import net.minestom.server.MinecraftServer;
@ -7,7 +8,6 @@ import net.minestom.server.benchmark.BenchmarkManager;
import net.minestom.server.benchmark.ThreadResult;
import net.minestom.server.entity.*;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.entity.hologram.Hologram;
import net.minestom.server.event.entity.EntityAttackEvent;
import net.minestom.server.event.item.ItemDropEvent;
import net.minestom.server.event.item.ItemUpdateStateEvent;
@ -16,6 +16,7 @@ import net.minestom.server.event.player.*;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.WorldBorder;
import net.minestom.server.instance.block.Block;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
@ -147,10 +148,10 @@ public class PlayerInit {
p.teleport(player.getPosition());
}*/
//ChickenCreature chickenCreature = new ChickenCreature(player.getPosition());
//chickenCreature.setInstance(player.getInstance());
ChickenCreature chickenCreature = new ChickenCreature(player.getPosition());
chickenCreature.setInstance(player.getInstance());
/*FakePlayer fakePlayer = new FakePlayer(UUID.randomUUID(), "test", true);
/*FakePlayer fakePlayer = new FakePlayer(UUID.randomUUID(), "test");
fakePlayer.addEventCallback(EntityDeathEvent.class, e -> {
fakePlayer.getController().respawn();
});
@ -158,7 +159,7 @@ public class PlayerInit {
FakePlayerController controller = fakePlayer.getController();
controller.sendChatMessage("I am a bot!");*/
Hologram hologram = new Hologram(player.getInstance(), player.getPosition(), "Hey guy");
//Hologram hologram = new Hologram(player.getInstance(), player.getPosition(), "Hey guy");
});
@ -211,7 +212,7 @@ public class PlayerInit {
});
player.addEventCallback(PlayerSpawnEvent.class, event -> {
player.setGameMode(GameMode.SURVIVAL);
player.setGameMode(GameMode.CREATIVE);
player.teleport(new Position(0, 45, 0));
player.setGlowing(true);
@ -227,6 +228,11 @@ public class PlayerInit {
player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 100));
Instance instance = player.getInstance();
WorldBorder worldBorder = instance.getWorldBorder();
worldBorder.setDiameter(30);
//EntityBoat entityBoat = new EntityBoat(player.getPosition());
//entityBoat.setInstance(player.getInstance());
//entityBoat.addPassenger(player);

View File

@ -5,18 +5,15 @@ import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.vehicle.PlayerVehicleInformation;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.Vector;
public class ChickenCreature extends EntityCreature {
public ChickenCreature(Position defaultPosition) {
super(EntityType.SKELETON, defaultPosition);
super(EntityType.CHICKEN, defaultPosition);
setBoundingBox(0.4f, 0.7f, 0.4f);
setHelmet(new ItemStack(Material.DIAMOND_HELMET, (byte) 1));
}
@Override

View File

@ -39,6 +39,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
private Dimension dimension;
private WorldBorder worldBorder;
private Map<Class<? extends Event>, List<EventCallback>> eventCallbacks = new ConcurrentHashMap<>();
// Entities present in this instance
@ -56,6 +58,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
public Instance(UUID uniqueId, Dimension dimension) {
this.uniqueId = uniqueId;
this.dimension = dimension;
this.worldBorder = new WorldBorder(this);
}
public abstract void refreshBlockId(BlockPosition blockPosition, short blockId);
@ -158,6 +162,10 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
return dimension;
}
public WorldBorder getWorldBorder() {
return worldBorder;
}
public Set<Player> getPlayers() {
return Collections.unmodifiableSet(players);
}
@ -319,7 +327,9 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
boolean isPlayer = entity instanceof Player;
if (isPlayer) {
sendChunks((Player) entity);
Player player = (Player) entity;
sendChunks(player);
getWorldBorder().init(player);
}
// Send all visible entities
@ -423,6 +433,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
* @param time the current time
*/
public void tick(long time) {
worldBorder.update();
}
/**

View File

@ -0,0 +1,171 @@
package net.minestom.server.instance;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.play.WorldBorderPacket;
public class WorldBorder {
private Instance instance;
private double centerX, centerZ;
private double currentDiameter;
private double oldDiameter;
private double newDiameter;
private long lerpStartTime;
private long speed;
private int portalTeleportBoundary;
private int warningTime;
private int warningBlocks;
protected WorldBorder(Instance instance) {
this.instance = instance;
this.oldDiameter = Double.MAX_VALUE;
this.newDiameter = Double.MAX_VALUE;
this.speed = 0;
this.portalTeleportBoundary = 29999984;
}
public void setCenter(double centerX, double centerZ) {
this.centerX = centerX;
this.centerZ = centerZ;
refreshCenter();
}
public double getCenterX() {
return centerX;
}
public void setCenterX(double centerX) {
this.centerX = centerX;
refreshCenter();
}
public double getCenterZ() {
return centerZ;
}
public void setCenterZ(double centerZ) {
this.centerZ = centerZ;
refreshCenter();
}
public int getWarningTime() {
return warningTime;
}
/**
* @param warningTime In seconds as /worldborder warning time
*/
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);
}
public int getWarningBlocks() {
return warningBlocks;
}
/**
* @param warningBlocks In meters
*/
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);
}
/**
* Change the diameter to {@code diameter} in {@code speed} milliseconds (interpolation)
*
* @param diameter the diameter target
* @param speed the time it will take to reach {@code diameter} in milliseconds
*/
public void setDiameter(double diameter, long speed) {
if (speed <= 0) {
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);
}
/**
* @return the current world border diameter
* It takes lerp in consideration
*/
public double getDiameter() {
return currentDiameter;
}
/**
* Change the diameter of the world border
*
* @param diameter the new diameter of the world border
*/
public void setDiameter(double diameter) {
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);
}
protected void update() {
if (lerpStartTime == 0) {
this.currentDiameter = oldDiameter;
} else {
double diameterDelta = newDiameter - oldDiameter;
long elapsedTime = System.currentTimeMillis() - lerpStartTime;
this.currentDiameter = oldDiameter + (diameterDelta * ((double) elapsedTime / (double) speed));
}
}
protected void init(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);
}
/**
* @return the instance of this world border
*/
public Instance getInstance() {
return instance;
}
private void refreshCenter() {
WorldBorderPacket worldBorderPacket = new WorldBorderPacket();
worldBorderPacket.action = WorldBorderPacket.Action.SET_CENTER;
worldBorderPacket.wbAction = new WorldBorderPacket.WBSetCenter(centerX, centerZ);
sendPacket(worldBorderPacket);
}
private void sendPacket(WorldBorderPacket worldBorderPacket) {
instance.getPlayers().forEach(player -> player.getPlayerConnection().sendPacket(worldBorderPacket));
}
}

View File

@ -90,6 +90,10 @@ public class PacketWriter {
Utils.writeVarInt(this, i);
}
public void writeVarLong(long l) {
Utils.writeVarLong(this, l);
}
public void writeSizedString(String string) {
byte[] bytes;
bytes = string.getBytes(StandardCharsets.UTF_8);

View File

@ -0,0 +1,149 @@
package net.minestom.server.network.packet.server.play;
import net.minestom.server.network.packet.PacketWriter;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
public class WorldBorderPacket implements ServerPacket {
public Action action;
public WBAction wbAction;
@Override
public void write(PacketWriter writer) {
writer.writeVarInt(action.ordinal());
wbAction.write(writer);
}
@Override
public int getId() {
return ServerPacketIdentifier.WORLD_BORDER;
}
public enum Action {
SET_SIZE,
LERP_SIZE,
SET_CENTER,
INITIALIZE,
SET_WARNING_TIME,
SET_WARNING_BLOCKS
}
public static abstract class WBAction {
public abstract void write(PacketWriter writer);
}
public static class WBSetSize extends WBAction {
public double diameter;
public WBSetSize(double diameter) {
this.diameter = diameter;
}
@Override
public void write(PacketWriter writer) {
writer.writeDouble(diameter);
}
}
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(PacketWriter writer) {
writer.writeDouble(oldDiameter);
writer.writeDouble(newDiameter);
writer.writeVarLong(speed);
}
}
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(PacketWriter writer) {
writer.writeDouble(x);
writer.writeDouble(z);
}
}
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(PacketWriter 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);
}
}
public static class WBSetWarningTime extends WBAction {
public int warningTime;
public WBSetWarningTime(int warningTime) {
this.warningTime = warningTime;
}
@Override
public void write(PacketWriter writer) {
writer.writeVarInt(warningTime);
}
}
public static class WBSetWarningBlocks extends WBAction {
public int warningBlocks;
public WBSetWarningBlocks(int warningBlocks) {
this.warningBlocks = warningBlocks;
}
@Override
public void write(PacketWriter writer) {
writer.writeVarInt(warningBlocks);
}
}
}

View File

@ -78,6 +78,17 @@ public class Utils {
return result;
}
public static void writeVarLong(PacketWriter writer, long value) {
do {
byte temp = (byte) (value & 0b01111111);
value >>>= 7;
if (value != 0) {
temp |= 0b10000000;
}
writer.writeByte(temp);
} while (value != 0);
}
public static void writeItemStack(PacketWriter packet, ItemStack itemStack) {
if (itemStack == null || itemStack.isAir()) {
packet.writeBoolean(false);