Remove useless synchronization blocks and fix Inventory#addItemStack

This commit is contained in:
Felix Cravic 2020-05-29 19:41:08 +02:00
parent b0ccb91c31
commit 6543f17d4c
5 changed files with 131 additions and 111 deletions

View File

@ -223,12 +223,11 @@ public class Chunk implements Viewable {
return blocksData.get(index);
}
public void updateBlocks(long time, Instance instance) {
public synchronized void updateBlocks(long time, Instance instance) {
if (updatableBlocks.isEmpty())
return;
// Block all chunk operation during the update
synchronized (this) {
IntIterator iterator = new IntOpenHashSet(updatableBlocks).iterator();
while (iterator.hasNext()) {
int index = iterator.nextInt();
@ -253,7 +252,6 @@ public class Chunk implements Viewable {
customBlock.update(instance, blockPosition, data);
}
}
}
public Biome[] getBiomes() {
return biomes;

View File

@ -21,29 +21,23 @@ public class BlockBatch implements InstanceBatch {
}
@Override
public void setBlock(int x, int y, int z, short blockId, Data data) {
synchronized (this) {
public synchronized void setBlock(int x, int y, int z, short blockId, Data data) {
Chunk chunk = this.instance.getChunkAt(x, z);
addBlockData(chunk, x, y, z, false, blockId, (short) 0, data);
}
}
@Override
public void setCustomBlock(int x, int y, int z, short blockId, Data data) {
synchronized (this) {
public synchronized void setCustomBlock(int x, int y, int z, short blockId, Data data) {
Chunk chunk = this.instance.getChunkAt(x, z);
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(blockId);
addBlockData(chunk, x, y, z, true, customBlock.getBlockId(), blockId, data);
}
}
@Override
public void setSeparateBlocks(int x, int y, int z, short blockId, short customBlockId, Data data) {
synchronized (this) {
public synchronized void setSeparateBlocks(int x, int y, int z, short blockId, short customBlockId, Data data) {
Chunk chunk = this.instance.getChunkAt(x, z);
addBlockData(chunk, x, y, z, true, blockId, customBlockId, data);
}
}
private void addBlockData(Chunk chunk, int x, int y, int z, boolean customBlock, short blockId, short customBlockId, Data data) {
List<BlockData> blocksData = this.data.get(chunk);

View File

@ -8,6 +8,7 @@ import net.minestom.server.inventory.click.InventoryClickProcessor;
import net.minestom.server.inventory.click.InventoryClickResult;
import net.minestom.server.inventory.condition.InventoryCondition;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.StackingRule;
import net.minestom.server.network.PacketWriterUtils;
import net.minestom.server.network.packet.server.play.SetSlotPacket;
import net.minestom.server.network.packet.server.play.WindowItemsPacket;
@ -88,7 +89,32 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
}
@Override
public boolean addItemStack(ItemStack itemStack) {
public synchronized boolean addItemStack(ItemStack itemStack) {
StackingRule stackingRule = itemStack.getStackingRule();
for (int i = 0; i < getItemStacks().length; i++) {
ItemStack item = getItemStacks()[i];
StackingRule itemStackingRule = item.getStackingRule();
if (itemStackingRule.canBeStacked(itemStack, item)) {
int itemAmount = itemStackingRule.getAmount(item);
if (itemAmount == stackingRule.getMaxSize())
continue;
int itemStackAmount = itemStackingRule.getAmount(itemStack);
int totalAmount = itemStackAmount + itemAmount;
if (!stackingRule.canApply(itemStack, totalAmount)) {
item = itemStackingRule.apply(item, itemStackingRule.getMaxSize());
sendSlotRefresh((short) i, item);
itemStack = stackingRule.apply(itemStack, totalAmount - stackingRule.getMaxSize());
} else {
item.setAmount((byte) totalAmount);
sendSlotRefresh((short) i, item);
return true;
}
} else if (item.isAir()) {
setItemStack(i, itemStack);
return true;
}
}
return false;
}
@ -161,8 +187,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
return cursorPlayersItem.getOrDefault(player, ItemStack.getAirItem());
}
private void safeItemInsert(int slot, ItemStack itemStack) {
synchronized (this) {
private synchronized void safeItemInsert(int slot, ItemStack itemStack) {
itemStack = ItemStackUtils.notNull(itemStack);
this.itemStacks[slot] = itemStack;
SetSlotPacket setSlotPacket = new SetSlotPacket();
@ -171,7 +196,6 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
setSlotPacket.itemStack = itemStack;
sendPacketToViewers(setSlotPacket);
}
}
private WindowItemsPacket createWindowItemsPacket() {
WindowItemsPacket windowItemsPacket = new WindowItemsPacket();
@ -440,6 +464,14 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
return !clickResult.isCancel();
}
private void sendSlotRefresh(short slot, ItemStack itemStack) {
SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = getWindowId();
setSlotPacket.slot = slot;
setSlotPacket.itemStack = itemStack;
sendPacketToViewers(setSlotPacket);
}
/**
* Used to update the inventory for a specific player in order to fix his cancelled actions
*

View File

@ -72,8 +72,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
}
@Override
public boolean addItemStack(ItemStack itemStack) {
synchronized (this) {
public synchronized boolean addItemStack(ItemStack itemStack) {
StackingRule stackingRule = itemStack.getStackingRule();
for (int i = 0; i < items.length - 10; i++) {
ItemStack item = items[i];
@ -99,7 +98,6 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
return true;
}
}
}
return false;
}
@ -184,8 +182,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
this.cursorItem = ItemStackUtils.notNull(cursorItem);
}
private void safeItemInsert(int slot, ItemStack itemStack) {
synchronized (this) {
private synchronized void safeItemInsert(int slot, ItemStack itemStack) {
itemStack = ItemStackUtils.notNull(itemStack);
EntityEquipmentPacket.Slot equipmentSlot;
@ -228,7 +225,6 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
update();
//refreshSlot(slot); seems to break things concerning +64 stacks
}
}
protected void setItemStack(int slot, int offset, ItemStack itemStack) {
slot = convertSlot(slot, offset);

View File

@ -29,7 +29,7 @@ public class FakePlayerConnection extends PlayerConnection {
@Override
public void flush() {
throw new UnsupportedOperationException("FakePlayer does not have anything to flush");
// Does nothing
}
@Override