Removed useless synchronization

This commit is contained in:
Felix Cravic 2020-08-14 15:24:57 +02:00
parent 0dd454985f
commit 1d047b5de3
7 changed files with 41 additions and 49 deletions

View File

@ -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();
});
}
}

View File

@ -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();
});
}
}
}

View File

@ -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();
});
}
}
}

View File

@ -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));

View File

@ -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);
}

View File

@ -33,7 +33,7 @@ public class TagsPacket implements ServerPacket {
// name
writer.writeSizedString(tag.getName().toString());
Set<NamespaceID> values = tag.getValues();
final Set<NamespaceID> values = tag.getValues();
// count
writer.writeVarInt(values.size());
// entries

View File

@ -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 {