Skip to content

Commit

Permalink
Merge pull request #69 from OneLiteFeatherNET/feature/upstream
Browse files Browse the repository at this point in the history
Update from Upstream
  • Loading branch information
TheMeinerLP committed Aug 19, 2024
2 parents acb9b86 + bb96585 commit e145ce3
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 294 deletions.
11 changes: 7 additions & 4 deletions src/main/java/net/minestom/server/ServerFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ public final class ServerFlag {
public static final int MAX_PACKET_SIZE = intProperty("minestom.max-packet-size", 2_097_151); // 3 bytes var-int
public static final int SOCKET_SEND_BUFFER_SIZE = intProperty("minestom.send-buffer-size", 262_143);
public static final int SOCKET_RECEIVE_BUFFER_SIZE = intProperty("minestom.receive-buffer-size", 32_767);
public static final boolean SOCKET_NO_DELAY = booleanProperty("minestom.tcp-no-delay", true);
public static final int POOLED_BUFFER_SIZE = intProperty("minestom.pooled-buffer-size", 262_143);
public static final int PLAYER_PACKET_PER_TICK = intProperty("minestom.packet-per-tick", 20);
public static final int PLAYER_PACKET_QUEUE_SIZE = intProperty("minestom.packet-queue-size", 1000);
public static final int SEND_LIGHT_AFTER_BLOCK_PLACEMENT_DELAY = intProperty("minestom.send-light-after-block-placement-delay", 100);
public static final long KEEP_ALIVE_DELAY = longProperty("minestom.keep-alive-delay", 10_000);
public static final long KEEP_ALIVE_KICK = longProperty("minestom.keep-alive-kick", 30_000);
public static final long LOGIN_PLUGIN_MESSAGE_TIMEOUT = longProperty("minestom.login-plugin-message-timeout", 5_000);

// Network rate limiting
public static final int PLAYER_PACKET_PER_TICK = intProperty("minestom.packet-per-tick", 50);
public static final int PLAYER_PACKET_QUEUE_SIZE = intProperty("minestom.packet-queue-size", 1000);
public static final long KEEP_ALIVE_DELAY = longProperty("minestom.keep-alive-delay", 10_000);
public static final long KEEP_ALIVE_KICK = longProperty("minestom.keep-alive-kick", 15_000);

// Chunk update
public static final float MIN_CHUNKS_PER_TICK = floatProperty("minestom.chunk-queue.min-per-tick", 0.01f);
public static final float MAX_CHUNKS_PER_TICK = floatProperty("minestom.chunk-queue.max-per-tick", 64.0f);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/minestom/server/coordinate/Vec.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public record Vec(double x, double y, double z) implements Point {
public static final Vec ZERO = new Vec(0);
public static final Vec ONE = new Vec(1);
public static final Vec SECTION = new Vec(16);

public static final double EPSILON = 0.000001;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/minestom/server/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,7 @@ public boolean isOccluded(@NotNull Shape shape, @NotNull BlockFace face) {

@Override
public boolean intersectBox(@NotNull Point positionRelative, @NotNull BoundingBox boundingBox) {
return boundingBox.intersectBox(positionRelative, boundingBox);
return this.boundingBox.intersectBox(positionRelative, boundingBox);
}

@Override
Expand Down
128 changes: 32 additions & 96 deletions src/main/java/net/minestom/server/entity/LivingEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minestom.server.entity.attribute.AttributeOperation;
import net.minestom.server.entity.damage.Damage;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.entity.metadata.EntityMeta;
import net.minestom.server.entity.metadata.LivingEntityMeta;
import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.entity.EntityDamageEvent;
Expand Down Expand Up @@ -83,36 +84,25 @@ public class LivingEntity extends Entity implements EquipmentHandler {
private float health = 1F;

// Equipments
private ItemStack mainHandItem;
private ItemStack offHandItem;
private ItemStack mainHandItem = ItemStack.AIR;
private ItemStack offHandItem = ItemStack.AIR;

private ItemStack helmet;
private ItemStack chestplate;
private ItemStack leggings;
private ItemStack boots;
private ItemStack helmet = ItemStack.AIR;
private ItemStack chestplate = ItemStack.AIR;
private ItemStack leggings = ItemStack.AIR;
private ItemStack boots = ItemStack.AIR;

/**
* Constructor which allows to specify an UUID. Only use if you know what you are doing!
*/
public LivingEntity(@NotNull EntityType entityType, @NotNull UUID uuid) {
super(entityType, uuid);
initEquipments();
}

public LivingEntity(@NotNull EntityType entityType) {
this(entityType, UUID.randomUUID());
}

private void initEquipments() {
this.mainHandItem = ItemStack.AIR;
this.offHandItem = ItemStack.AIR;

this.helmet = ItemStack.AIR;
this.chestplate = ItemStack.AIR;
this.leggings = ItemStack.AIR;
this.boots = ItemStack.AIR;
}

@Override
public void setSprinting(boolean sprinting) {
super.setSprinting(sprinting);
Expand All @@ -125,91 +115,37 @@ public void setSprinting(boolean sprinting) {
else speed.removeModifier(SPRINTING_SPEED_MODIFIER);
}

@NotNull
@Override
public ItemStack getItemInMainHand() {
return mainHandItem;
}

@Override
public void setItemInMainHand(@NotNull ItemStack itemStack) {
ItemStack oldItem = this.mainHandItem;
this.mainHandItem = getEquipmentItem(itemStack, EquipmentSlot.MAIN_HAND);
syncEquipment(EquipmentSlot.MAIN_HAND);
updateEquipmentAttributes(oldItem, this.mainHandItem, EquipmentSlot.MAIN_HAND);
}

@NotNull
@Override
public ItemStack getItemInOffHand() {
return offHandItem;
public @NotNull ItemStack getEquipment(@NotNull EquipmentSlot slot) {
return switch (slot) {
case MAIN_HAND -> mainHandItem;
case OFF_HAND -> offHandItem;
case BOOTS -> boots;
case LEGGINGS -> leggings;
case CHESTPLATE -> chestplate;
case HELMET -> helmet;
};
}

@Override
public void setItemInOffHand(@NotNull ItemStack itemStack) {
ItemStack oldItem = this.offHandItem;
this.offHandItem = getEquipmentItem(itemStack, EquipmentSlot.OFF_HAND);
syncEquipment(EquipmentSlot.OFF_HAND);
updateEquipmentAttributes(oldItem, this.offHandItem, EquipmentSlot.OFF_HAND);
}

@NotNull
@Override
public ItemStack getHelmet() {
return helmet;
}

@Override
public void setHelmet(@NotNull ItemStack itemStack) {
ItemStack oldItem = this.helmet;
this.helmet = getEquipmentItem(itemStack, EquipmentSlot.HELMET);
syncEquipment(EquipmentSlot.HELMET);
updateEquipmentAttributes(oldItem, this.helmet, EquipmentSlot.HELMET);
}

@NotNull
@Override
public ItemStack getChestplate() {
return chestplate;
}

@Override
public void setChestplate(@NotNull ItemStack itemStack) {
ItemStack oldItem = this.chestplate;
this.chestplate = getEquipmentItem(itemStack, EquipmentSlot.CHESTPLATE);
syncEquipment(EquipmentSlot.CHESTPLATE);
updateEquipmentAttributes(oldItem, this.chestplate, EquipmentSlot.CHESTPLATE);
}

@NotNull
@Override
public ItemStack getLeggings() {
return leggings;
}

@Override
public void setLeggings(@NotNull ItemStack itemStack) {
ItemStack oldItem = this.leggings;
this.leggings = getEquipmentItem(itemStack, EquipmentSlot.LEGGINGS);
syncEquipment(EquipmentSlot.LEGGINGS);
updateEquipmentAttributes(oldItem, this.leggings, EquipmentSlot.LEGGINGS);
}

@NotNull
@Override
public ItemStack getBoots() {
return boots;
}
public void setEquipment(@NotNull EquipmentSlot slot, @NotNull ItemStack itemStack) {
ItemStack oldItem = getEquipment(slot);
ItemStack newItem = slotChangeEvent(itemStack, slot);

switch (slot) {
case MAIN_HAND -> mainHandItem = newItem;
case OFF_HAND -> offHandItem = newItem;
case BOOTS -> boots = newItem;
case LEGGINGS -> leggings = newItem;
case CHESTPLATE -> chestplate = newItem;
case HELMET -> helmet = newItem;
}

@Override
public void setBoots(@NotNull ItemStack itemStack) {
ItemStack oldItem = this.boots;
this.boots = getEquipmentItem(itemStack, EquipmentSlot.BOOTS);
syncEquipment(EquipmentSlot.BOOTS);
updateEquipmentAttributes(oldItem, this.boots, EquipmentSlot.BOOTS);
syncEquipment(slot);
updateEquipmentAttributes(oldItem, newItem, slot);
}

private ItemStack getEquipmentItem(@NotNull ItemStack itemStack, @NotNull EquipmentSlot slot) {
private ItemStack slotChangeEvent(@NotNull ItemStack itemStack, @NotNull EquipmentSlot slot) {
EntityEquipEvent entityEquipEvent = new EntityEquipEvent(this, itemStack, slot);
EventDispatcher.call(entityEquipEvent);
return entityEquipEvent.getEquippedItem();
Expand Down Expand Up @@ -720,7 +656,7 @@ public void setTeam(@Nullable Team team) {
}

/**
* Gets {@link net.minestom.server.entity.metadata.EntityMeta} of this entity casted to {@link LivingEntityMeta}.
* Gets {@link EntityMeta} of this entity casted to {@link LivingEntityMeta}.
*
* @return null if meta of this entity does not inherit {@link LivingEntityMeta}, casted value otherwise.
*/
Expand Down
72 changes: 10 additions & 62 deletions src/main/java/net/minestom/server/entity/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.validate.Check;
import net.minestom.server.world.DimensionType;
import org.jctools.queues.MessagePassingQueue;
import org.jctools.queues.MpscUnboundedXaddArrayQueue;
import org.jctools.queues.MpscArrayQueue;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -175,7 +174,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
private final AtomicInteger teleportId = new AtomicInteger();
private int receivedTeleportId;

private final MessagePassingQueue<ClientPacket> packets = new MpscUnboundedXaddArrayQueue<>(32);
private final MpscArrayQueue<ClientPacket> packets = new MpscArrayQueue<>(ServerFlag.PLAYER_PACKET_QUEUE_SIZE);
private final boolean levelFlat;
private final PlayerSettings settings;
private float exp;
Expand Down Expand Up @@ -2127,15 +2126,14 @@ protected void refreshAbilities() {
* @param packet the packet to add in the queue
*/
public void addPacketToQueue(@NotNull ClientPacket packet) {
this.packets.offer(packet);
final boolean success = packets.offer(packet);
if (!success) {
kick(Component.text("Too Many Packets", NamedTextColor.RED));
}
}

@ApiStatus.Internal
public void interpretPacketQueue() {
if (this.packets.size() >= ServerFlag.PLAYER_PACKET_QUEUE_SIZE) {
kick(Component.text("Too Many Packets", NamedTextColor.RED));
return;
}
final PacketListenerManager manager = MinecraftServer.getPacketListenerManager();
// This method is NOT thread-safe
this.packets.drain(packet -> manager.processClientPacket(packet, playerConnection), ServerFlag.PLAYER_PACKET_PER_TICK);
Expand Down Expand Up @@ -2291,63 +2289,13 @@ protected void showPlayer(@NotNull PlayerConnection connection) {
}

@Override
public @NotNull ItemStack getItemInMainHand() {
return inventory.getItemInMainHand();
}

@Override
public void setItemInMainHand(@NotNull ItemStack itemStack) {
inventory.setItemInMainHand(itemStack);
}

@Override
public @NotNull ItemStack getItemInOffHand() {
return inventory.getItemInOffHand();
}

@Override
public void setItemInOffHand(@NotNull ItemStack itemStack) {
inventory.setItemInOffHand(itemStack);
}

@Override
public @NotNull ItemStack getHelmet() {
return inventory.getHelmet();
}

@Override
public void setHelmet(@NotNull ItemStack itemStack) {
inventory.setHelmet(itemStack);
}

@Override
public @NotNull ItemStack getChestplate() {
return inventory.getChestplate();
}

@Override
public void setChestplate(@NotNull ItemStack itemStack) {
inventory.setChestplate(itemStack);
}

@Override
public @NotNull ItemStack getLeggings() {
return inventory.getLeggings();
}

@Override
public void setLeggings(@NotNull ItemStack itemStack) {
inventory.setLeggings(itemStack);
}

@Override
public @NotNull ItemStack getBoots() {
return inventory.getBoots();
public @NotNull ItemStack getEquipment(@NotNull EquipmentSlot slot) {
return inventory.getEquipment(slot);
}

@Override
public void setBoots(@NotNull ItemStack itemStack) {
inventory.setBoots(itemStack);
public void setEquipment(@NotNull EquipmentSlot slot, @NotNull ItemStack itemStack) {
inventory.setEquipment(slot, itemStack);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class Navigator {

private double minimumDistance;

NodeGenerator nodeGenerator = new GroundNodeGenerator();
NodeGenerator nodeGenerator = new GroundNodeGenerator();
private NodeFollower nodeFollower;

public Navigator(@NotNull Entity entity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

public class PathGenerator {
private static final ExecutorService pool = Executors.newWorkStealingPool();
private static final PNode repathNode = new PNode(0, 0, 0, 0, 0, PNode.NodeType.REPATH, null);
private static final Comparator<PNode> pNodeComparator = (s1, s2) -> (int) (((s1.g() + s1.h()) - (s2.g() + s2.h())) * 1000);

public static @NotNull PPath generate(@NotNull Instance instance, @NotNull Pos orgStart, @NotNull Point orgTarget, double closeDistance, double maxDistance, double pathVariance, @NotNull BoundingBox boundingBox, boolean isOnGround, @NotNull NodeGenerator generator, @Nullable Runnable onComplete) {
Expand All @@ -34,6 +33,10 @@ public class PathGenerator {
return path;
}

private static PNode buildRepathNode(PNode parent) {
return new PNode(0, 0, 0, 0, 0, PNode.NodeType.REPATH, parent);
}

private static void computePath(Instance instance, Point start, Point target, double closeDistance, double maxDistance, double pathVariance, BoundingBox boundingBox, PPath path, NodeGenerator generator) {
double closestDistance = Double.MAX_VALUE;
double straightDistance = generator.heuristic(start, target);
Expand Down Expand Up @@ -93,8 +96,7 @@ private static void computePath(Instance instance, Point start, Point target, do
current = closestFoundNodes.get(0);

if (!open.isEmpty()) {
repathNode.setParent(current);
current = repathNode;
current = buildRepathNode(current);
}
}

Expand Down
Loading

0 comments on commit e145ce3

Please sign in to comment.