From 1d047b5de3799b24337fec634b517cda05ec6d8c Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Fri, 14 Aug 2020 15:24:57 +0200 Subject: [PATCH] Removed useless synchronization --- .../minestom/server/entity/ItemEntity.java | 34 ++++++++----------- .../minestom/server/entity/LivingEntity.java | 26 +++++++------- .../net/minestom/server/entity/Player.java | 18 +++++----- .../net/minestom/server/item/ItemStack.java | 4 +-- .../listener/PlayerDiggingListener.java | 2 +- .../packet/server/play/TagsPacket.java | 2 +- .../network/player/PlayerConnection.java | 4 +-- 7 files changed, 41 insertions(+), 49 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/ItemEntity.java b/src/main/java/net/minestom/server/entity/ItemEntity.java index 6b7fbcab3..c2dddd553 100644 --- a/src/main/java/net/minestom/server/entity/ItemEntity.java +++ b/src/main/java/net/minestom/server/entity/ItemEntity.java @@ -85,31 +85,27 @@ public class ItemEntity extends ObjectEntity { if (getDistance(itemEntity) > mergeRange) continue; - // Use the class as a monitor to prevent deadlock - // Shouldn't happen too often to be an issue - synchronized (ItemEntity.class) { - final ItemStack itemStackEntity = itemEntity.getItemStack(); + final ItemStack itemStackEntity = itemEntity.getItemStack(); - final StackingRule stackingRule = itemStack.getStackingRule(); - final boolean canStack = stackingRule.canBeStacked(itemStack, itemStackEntity); + final StackingRule stackingRule = itemStack.getStackingRule(); + final boolean canStack = stackingRule.canBeStacked(itemStack, itemStackEntity); - if (!canStack) - continue; + if (!canStack) + continue; - final int totalAmount = stackingRule.getAmount(itemStack) + stackingRule.getAmount(itemStackEntity); - final boolean canApply = stackingRule.canApply(itemStack, totalAmount); + final int totalAmount = stackingRule.getAmount(itemStack) + stackingRule.getAmount(itemStackEntity); + final boolean canApply = stackingRule.canApply(itemStack, totalAmount); - if (!canApply) - continue; + if (!canApply) + continue; - final ItemStack result = stackingRule.apply(itemStack.clone(), totalAmount); + final ItemStack result = stackingRule.apply(itemStack.clone(), totalAmount); - EntityItemMergeEvent entityItemMergeEvent = new EntityItemMergeEvent(this, itemEntity, result); - callCancellableEvent(EntityItemMergeEvent.class, entityItemMergeEvent, () -> { - setItemStack(entityItemMergeEvent.getResult()); - itemEntity.remove(); - }); - } + EntityItemMergeEvent entityItemMergeEvent = new EntityItemMergeEvent(this, itemEntity, result); + callCancellableEvent(EntityItemMergeEvent.class, entityItemMergeEvent, () -> { + setItemStack(entityItemMergeEvent.getResult()); + itemEntity.remove(); + }); } } diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index f95b63792..0014b7ddb 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -100,20 +100,18 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { final BoundingBox itemBoundingBox = itemEntity.getBoundingBox(); if (expandedBoundingBox.intersect(itemBoundingBox)) { - synchronized (itemEntity) { - if (itemEntity.shouldRemove() || itemEntity.isRemoveScheduled()) - continue; - final ItemStack item = itemEntity.getItemStack(); - PickupItemEvent pickupItemEvent = new PickupItemEvent(item); - callCancellableEvent(PickupItemEvent.class, pickupItemEvent, () -> { - CollectItemPacket collectItemPacket = new CollectItemPacket(); - collectItemPacket.collectedEntityId = itemEntity.getEntityId(); - collectItemPacket.collectorEntityId = getEntityId(); - collectItemPacket.pickupItemCount = item.getAmount(); - sendPacketToViewersAndSelf(collectItemPacket); - entity.remove(); - }); - } + if (itemEntity.shouldRemove() || itemEntity.isRemoveScheduled()) + continue; + final ItemStack item = itemEntity.getItemStack(); + PickupItemEvent pickupItemEvent = new PickupItemEvent(item); + callCancellableEvent(PickupItemEvent.class, pickupItemEvent, () -> { + CollectItemPacket collectItemPacket = new CollectItemPacket(); + collectItemPacket.collectedEntityId = itemEntity.getEntityId(); + collectItemPacket.collectorEntityId = getEntityId(); + collectItemPacket.pickupItemCount = item.getAmount(); + sendPacketToViewersAndSelf(collectItemPacket); + entity.remove(); + }); } } } diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 90bf0dda2..e948c69f3 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -230,7 +230,7 @@ public class Player extends LivingEntity implements CommandSender { recipesIdentifier.add(recipe.getRecipeId()); } if (!recipesIdentifier.isEmpty()) { - String[] identifiers = recipesIdentifier.toArray(new String[0]); + final String[] identifiers = recipesIdentifier.toArray(new String[0]); UnlockRecipesPacket unlockRecipesPacket = new UnlockRecipesPacket(); unlockRecipesPacket.mode = 0; unlockRecipesPacket.recipesId = identifiers; @@ -324,15 +324,13 @@ public class Player extends LivingEntity implements CommandSender { final ExperienceOrb experienceOrb = (ExperienceOrb) entity; final BoundingBox itemBoundingBox = experienceOrb.getBoundingBox(); if (expandedBoundingBox.intersect(itemBoundingBox)) { - synchronized (experienceOrb) { - if (experienceOrb.shouldRemove() || experienceOrb.isRemoveScheduled()) - continue; - PickupExperienceEvent pickupExperienceEvent = new PickupExperienceEvent(experienceOrb); - callCancellableEvent(PickupExperienceEvent.class, pickupExperienceEvent, () -> { - short experienceCount = pickupExperienceEvent.getExperienceCount(); // TODO give to player - entity.remove(); - }); - } + if (experienceOrb.shouldRemove() || experienceOrb.isRemoveScheduled()) + continue; + PickupExperienceEvent pickupExperienceEvent = new PickupExperienceEvent(experienceOrb); + callCancellableEvent(PickupExperienceEvent.class, pickupExperienceEvent, () -> { + short experienceCount = pickupExperienceEvent.getExperienceCount(); // TODO give to player + entity.remove(); + }); } } } diff --git a/src/main/java/net/minestom/server/item/ItemStack.java b/src/main/java/net/minestom/server/item/ItemStack.java index b4b1c6b3a..e48b82235 100644 --- a/src/main/java/net/minestom/server/item/ItemStack.java +++ b/src/main/java/net/minestom/server/item/ItemStack.java @@ -128,8 +128,8 @@ public class ItemStack implements DataContainer { * @param itemStack The ItemStack to compare to * @return true if both items are similar */ - public synchronized boolean isSimilar(ItemStack itemStack) { - synchronized (itemStack) { + public boolean isSimilar(ItemStack itemStack) { + synchronized (ItemStack.class) { final ColoredText itemDisplayName = itemStack.getDisplayName(); final boolean displayNameCheck = (displayName == null && itemDisplayName == null) || (displayName != null && itemDisplayName != null && displayName.equals(itemDisplayName)); diff --git a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java index 916532af0..50dbdeb7f 100644 --- a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java +++ b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java @@ -99,7 +99,7 @@ public class PlayerDiggingListener { if (itemUpdateStateEvent == null) { player.refreshActiveHand(true, false, false); } else { - boolean isOffHand = itemUpdateStateEvent.getHand() == Player.Hand.OFF; + final boolean isOffHand = itemUpdateStateEvent.getHand() == Player.Hand.OFF; player.refreshActiveHand(itemUpdateStateEvent.hasHandAnimation(), isOffHand, false); } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java index 080a47f61..2f97da3ba 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java @@ -33,7 +33,7 @@ public class TagsPacket implements ServerPacket { // name writer.writeSizedString(tag.getName().toString()); - Set values = tag.getValues(); + final Set values = tag.getValues(); // count writer.writeVarInt(values.size()); // entries diff --git a/src/main/java/net/minestom/server/network/player/PlayerConnection.java b/src/main/java/net/minestom/server/network/player/PlayerConnection.java index d0a5e34aa..f37ded7f2 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerConnection.java @@ -49,9 +49,9 @@ public abstract class PlayerConnection { tickCounter++; if (tickCounter % 20 == 0 && tickCounter > 0) { tickCounter = 0; - int i = packetCounter.get(); + final int count = packetCounter.get(); packetCounter.set(0); - if (i > MinecraftServer.getRateLimit()) { + if (count > MinecraftServer.getRateLimit()) { if (connectionState == ConnectionState.LOGIN) { sendPacket(new LoginDisconnect("Too Many Packets")); } else {