diff --git a/build.gradle b/build.gradle index 28d854b21..550472316 100644 --- a/build.gradle +++ b/build.gradle @@ -24,6 +24,9 @@ dependencies { apt lombokDependency + // https://mvnrepository.com/artifact/io.netty/netty-all + compile group: 'io.netty', name: 'netty-all', version: '4.1.48.Final' + // https://mvnrepository.com/artifact/com.github.jhg023/SimpleNet implementation group: 'com.github.jhg023', name: 'SimpleNet', version: '1.6.5' // https://mvnrepository.com/artifact/it.unimi.dsi/fastutil diff --git a/src/main/java/fr/themode/minestom/MinecraftServer.java b/src/main/java/fr/themode/minestom/MinecraftServer.java index 916dd5f81..bbb3e94b0 100644 --- a/src/main/java/fr/themode/minestom/MinecraftServer.java +++ b/src/main/java/fr/themode/minestom/MinecraftServer.java @@ -54,8 +54,8 @@ public class MinecraftServer { public static final int THREAD_COUNT_SCHEDULER = 1; // Config - public static final int CHUNK_VIEW_DISTANCE = 10; - public static final int ENTITY_VIEW_DISTANCE = 5; + public static final int CHUNK_VIEW_DISTANCE = 2; + public static final int ENTITY_VIEW_DISTANCE = 2; // Can be modified at performance cost when decreased private static final int MS_TO_SEC = 1000; public static final int TICK_MS = MS_TO_SEC / 20; diff --git a/src/main/java/fr/themode/minestom/entity/Entity.java b/src/main/java/fr/themode/minestom/entity/Entity.java index 79e113d77..b894316ea 100644 --- a/src/main/java/fr/themode/minestom/entity/Entity.java +++ b/src/main/java/fr/themode/minestom/entity/Entity.java @@ -1,6 +1,5 @@ package fr.themode.minestom.entity; -import com.github.simplenet.packet.Packet; import fr.themode.minestom.MinecraftServer; import fr.themode.minestom.Viewable; import fr.themode.minestom.collision.BoundingBox; @@ -12,6 +11,7 @@ import fr.themode.minestom.event.Event; import fr.themode.minestom.instance.Chunk; import fr.themode.minestom.instance.Instance; import fr.themode.minestom.item.Material; +import fr.themode.minestom.net.packet.PacketWriter; import fr.themode.minestom.net.packet.server.play.*; import fr.themode.minestom.net.player.PlayerConnection; import fr.themode.minestom.utils.*; @@ -617,7 +617,7 @@ public abstract class Entity implements Viewable, DataContainer { return metaDataPacket; } - public Consumer getMetadataConsumer() { + public Consumer getMetadataConsumer() { return packet -> { fillMetadataIndex(packet, 0); fillMetadataIndex(packet, 1); @@ -636,7 +636,7 @@ public abstract class Entity implements Viewable, DataContainer { sendPacketToViewersAndSelf(metaDataPacket); } - private void fillMetadataIndex(Packet packet, int index) { + private void fillMetadataIndex(PacketWriter packet, int index) { switch (index) { case 0: fillStateMetadata(packet); @@ -656,9 +656,9 @@ public abstract class Entity implements Viewable, DataContainer { } } - private void fillStateMetadata(Packet packet) { - packet.putByte((byte) 0); - packet.putByte(METADATA_BYTE); + private void fillStateMetadata(PacketWriter packet) { + packet.writeByte((byte) 0); + packet.writeByte(METADATA_BYTE); byte index0 = 0; if (onFire) index0 += 1; @@ -676,31 +676,31 @@ public abstract class Entity implements Viewable, DataContainer { index0 += 64; if (usingElytra) index0 += 128; - packet.putByte(index0); + packet.writeByte(index0); } - private void fillAirTickMetaData(Packet packet) { - packet.putByte((byte) 1); - packet.putByte(METADATA_VARINT); - Utils.writeVarInt(packet, air); + private void fillAirTickMetaData(PacketWriter packet) { + packet.writeByte((byte) 1); + packet.writeByte(METADATA_VARINT); + packet.writeVarInt(air); } - private void fillCustomNameMetaData(Packet packet) { - packet.putByte((byte) 2); - packet.putByte(METADATA_CHAT); - Utils.writeString(packet, customName); + private void fillCustomNameMetaData(PacketWriter packet) { + packet.writeByte((byte) 2); + packet.writeByte(METADATA_CHAT); + packet.writeSizedString(customName); } - private void fillNoGravityMetaData(Packet packet) { - packet.putByte((byte) 5); - packet.putByte(METADATA_BOOLEAN); - packet.putBoolean(noGravity); + private void fillNoGravityMetaData(PacketWriter packet) { + packet.writeByte((byte) 5); + packet.writeByte(METADATA_BOOLEAN); + packet.writeBoolean(noGravity); } - private void fillPoseMetaData(Packet packet) { - packet.putByte((byte) 6); - packet.putByte(METADATA_POSE); - Utils.writeVarInt(packet, pose.ordinal()); + private void fillPoseMetaData(PacketWriter packet) { + packet.writeByte((byte) 6); + packet.writeByte(METADATA_POSE); + packet.writeVarInt(pose.ordinal()); } protected void sendSynchronization() { diff --git a/src/main/java/fr/themode/minestom/entity/ItemEntity.java b/src/main/java/fr/themode/minestom/entity/ItemEntity.java index 0a3ba13c5..2a491e6af 100644 --- a/src/main/java/fr/themode/minestom/entity/ItemEntity.java +++ b/src/main/java/fr/themode/minestom/entity/ItemEntity.java @@ -1,8 +1,7 @@ package fr.themode.minestom.entity; -import com.github.simplenet.packet.Packet; import fr.themode.minestom.item.ItemStack; -import fr.themode.minestom.utils.Utils; +import fr.themode.minestom.net.packet.PacketWriter; import java.util.function.Consumer; @@ -38,12 +37,12 @@ public class ItemEntity extends ObjectEntity { } @Override - public Consumer getMetadataConsumer() { + public Consumer getMetadataConsumer() { return packet -> { super.getMetadataConsumer().accept(packet); - packet.putByte((byte) 7); - packet.putByte(METADATA_SLOT); - Utils.writeItemStack(packet, itemStack); + packet.writeByte((byte) 7); + packet.writeByte(METADATA_SLOT); + packet.writeItemStack(itemStack); }; } diff --git a/src/main/java/fr/themode/minestom/entity/LivingEntity.java b/src/main/java/fr/themode/minestom/entity/LivingEntity.java index fad2e1a6a..8d3f7c786 100644 --- a/src/main/java/fr/themode/minestom/entity/LivingEntity.java +++ b/src/main/java/fr/themode/minestom/entity/LivingEntity.java @@ -1,12 +1,12 @@ package fr.themode.minestom.entity; -import com.github.simplenet.packet.Packet; import fr.themode.minestom.collision.BoundingBox; import fr.themode.minestom.entity.property.Attribute; import fr.themode.minestom.event.DeathEvent; import fr.themode.minestom.event.PickupItemEvent; import fr.themode.minestom.instance.Chunk; import fr.themode.minestom.item.ItemStack; +import fr.themode.minestom.net.packet.PacketWriter; import fr.themode.minestom.net.packet.server.play.CollectItemPacket; import fr.themode.minestom.net.packet.server.play.EntityAnimationPacket; import fr.themode.minestom.net.packet.server.play.EntityPropertiesPacket; @@ -76,11 +76,11 @@ public abstract class LivingEntity extends Entity { } @Override - public Consumer getMetadataConsumer() { + public Consumer getMetadataConsumer() { return packet -> { super.getMetadataConsumer().accept(packet); - packet.putByte((byte) 7); - packet.putByte(METADATA_BYTE); + packet.writeByte((byte) 7); + packet.writeByte(METADATA_BYTE); byte activeHandValue = 0; if (isHandActive) { activeHandValue += 1; @@ -89,7 +89,7 @@ public abstract class LivingEntity extends Entity { if (riptideSpinAttack) activeHandValue += 4; } - packet.putByte(activeHandValue); + packet.writeByte(activeHandValue); // TODO all remaining metadata }; diff --git a/src/main/java/fr/themode/minestom/net/packet/PacketWriter.java b/src/main/java/fr/themode/minestom/net/packet/PacketWriter.java index c9e0bc9e7..6327ca0e5 100644 --- a/src/main/java/fr/themode/minestom/net/packet/PacketWriter.java +++ b/src/main/java/fr/themode/minestom/net/packet/PacketWriter.java @@ -80,9 +80,9 @@ public class PacketWriter { } } - public void write(Consumer consumer) { + public void write(Consumer consumer) { if (consumer != null) - consumer.accept(packet); + consumer.accept(this); } public void writeBufferAndFree(BufferWrapper buffer) { diff --git a/src/main/java/fr/themode/minestom/net/packet/server/play/EntityMetaDataPacket.java b/src/main/java/fr/themode/minestom/net/packet/server/play/EntityMetaDataPacket.java index 9b3d79973..c939eb061 100644 --- a/src/main/java/fr/themode/minestom/net/packet/server/play/EntityMetaDataPacket.java +++ b/src/main/java/fr/themode/minestom/net/packet/server/play/EntityMetaDataPacket.java @@ -1,6 +1,5 @@ package fr.themode.minestom.net.packet.server.play; -import com.github.simplenet.packet.Packet; import fr.themode.minestom.net.packet.PacketWriter; import fr.themode.minestom.net.packet.server.ServerPacket; import fr.themode.minestom.net.packet.server.ServerPacketIdentifier; @@ -10,7 +9,7 @@ import java.util.function.Consumer; public class EntityMetaDataPacket implements ServerPacket { public int entityId; - public Consumer consumer; + public Consumer consumer; @Override public void write(PacketWriter writer) {