mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-01 13:01:32 +01:00
Remove useless synchronization blocks and fix Inventory#addItemStack
This commit is contained in:
parent
b0ccb91c31
commit
6543f17d4c
@ -223,12 +223,11 @@ public class Chunk implements Viewable {
|
|||||||
return blocksData.get(index);
|
return blocksData.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateBlocks(long time, Instance instance) {
|
public synchronized void updateBlocks(long time, Instance instance) {
|
||||||
if (updatableBlocks.isEmpty())
|
if (updatableBlocks.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Block all chunk operation during the update
|
// Block all chunk operation during the update
|
||||||
synchronized (this) {
|
|
||||||
IntIterator iterator = new IntOpenHashSet(updatableBlocks).iterator();
|
IntIterator iterator = new IntOpenHashSet(updatableBlocks).iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
int index = iterator.nextInt();
|
int index = iterator.nextInt();
|
||||||
@ -253,7 +252,6 @@ public class Chunk implements Viewable {
|
|||||||
customBlock.update(instance, blockPosition, data);
|
customBlock.update(instance, blockPosition, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public Biome[] getBiomes() {
|
public Biome[] getBiomes() {
|
||||||
return biomes;
|
return biomes;
|
||||||
|
@ -21,29 +21,23 @@ public class BlockBatch implements InstanceBatch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBlock(int x, int y, int z, short blockId, Data data) {
|
public synchronized void setBlock(int x, int y, int z, short blockId, Data data) {
|
||||||
synchronized (this) {
|
|
||||||
Chunk chunk = this.instance.getChunkAt(x, z);
|
Chunk chunk = this.instance.getChunkAt(x, z);
|
||||||
addBlockData(chunk, x, y, z, false, blockId, (short) 0, data);
|
addBlockData(chunk, x, y, z, false, blockId, (short) 0, data);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setCustomBlock(int x, int y, int z, short blockId, Data data) {
|
public synchronized void setCustomBlock(int x, int y, int z, short blockId, Data data) {
|
||||||
synchronized (this) {
|
|
||||||
Chunk chunk = this.instance.getChunkAt(x, z);
|
Chunk chunk = this.instance.getChunkAt(x, z);
|
||||||
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(blockId);
|
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(blockId);
|
||||||
addBlockData(chunk, x, y, z, true, customBlock.getBlockId(), blockId, data);
|
addBlockData(chunk, x, y, z, true, customBlock.getBlockId(), blockId, data);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setSeparateBlocks(int x, int y, int z, short blockId, short customBlockId, Data data) {
|
public synchronized void setSeparateBlocks(int x, int y, int z, short blockId, short customBlockId, Data data) {
|
||||||
synchronized (this) {
|
|
||||||
Chunk chunk = this.instance.getChunkAt(x, z);
|
Chunk chunk = this.instance.getChunkAt(x, z);
|
||||||
addBlockData(chunk, x, y, z, true, blockId, customBlockId, data);
|
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) {
|
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);
|
List<BlockData> blocksData = this.data.get(chunk);
|
||||||
|
@ -8,6 +8,7 @@ import net.minestom.server.inventory.click.InventoryClickProcessor;
|
|||||||
import net.minestom.server.inventory.click.InventoryClickResult;
|
import net.minestom.server.inventory.click.InventoryClickResult;
|
||||||
import net.minestom.server.inventory.condition.InventoryCondition;
|
import net.minestom.server.inventory.condition.InventoryCondition;
|
||||||
import net.minestom.server.item.ItemStack;
|
import net.minestom.server.item.ItemStack;
|
||||||
|
import net.minestom.server.item.StackingRule;
|
||||||
import net.minestom.server.network.PacketWriterUtils;
|
import net.minestom.server.network.PacketWriterUtils;
|
||||||
import net.minestom.server.network.packet.server.play.SetSlotPacket;
|
import net.minestom.server.network.packet.server.play.SetSlotPacket;
|
||||||
import net.minestom.server.network.packet.server.play.WindowItemsPacket;
|
import net.minestom.server.network.packet.server.play.WindowItemsPacket;
|
||||||
@ -88,7 +89,32 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,8 +187,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
return cursorPlayersItem.getOrDefault(player, ItemStack.getAirItem());
|
return cursorPlayersItem.getOrDefault(player, ItemStack.getAirItem());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void safeItemInsert(int slot, ItemStack itemStack) {
|
private synchronized void safeItemInsert(int slot, ItemStack itemStack) {
|
||||||
synchronized (this) {
|
|
||||||
itemStack = ItemStackUtils.notNull(itemStack);
|
itemStack = ItemStackUtils.notNull(itemStack);
|
||||||
this.itemStacks[slot] = itemStack;
|
this.itemStacks[slot] = itemStack;
|
||||||
SetSlotPacket setSlotPacket = new SetSlotPacket();
|
SetSlotPacket setSlotPacket = new SetSlotPacket();
|
||||||
@ -171,7 +196,6 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
setSlotPacket.itemStack = itemStack;
|
setSlotPacket.itemStack = itemStack;
|
||||||
sendPacketToViewers(setSlotPacket);
|
sendPacketToViewers(setSlotPacket);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private WindowItemsPacket createWindowItemsPacket() {
|
private WindowItemsPacket createWindowItemsPacket() {
|
||||||
WindowItemsPacket windowItemsPacket = new WindowItemsPacket();
|
WindowItemsPacket windowItemsPacket = new WindowItemsPacket();
|
||||||
@ -440,6 +464,14 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
return !clickResult.isCancel();
|
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
|
* Used to update the inventory for a specific player in order to fix his cancelled actions
|
||||||
*
|
*
|
||||||
|
@ -72,8 +72,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean addItemStack(ItemStack itemStack) {
|
public synchronized boolean addItemStack(ItemStack itemStack) {
|
||||||
synchronized (this) {
|
|
||||||
StackingRule stackingRule = itemStack.getStackingRule();
|
StackingRule stackingRule = itemStack.getStackingRule();
|
||||||
for (int i = 0; i < items.length - 10; i++) {
|
for (int i = 0; i < items.length - 10; i++) {
|
||||||
ItemStack item = items[i];
|
ItemStack item = items[i];
|
||||||
@ -99,7 +98,6 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,8 +182,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
this.cursorItem = ItemStackUtils.notNull(cursorItem);
|
this.cursorItem = ItemStackUtils.notNull(cursorItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void safeItemInsert(int slot, ItemStack itemStack) {
|
private synchronized void safeItemInsert(int slot, ItemStack itemStack) {
|
||||||
synchronized (this) {
|
|
||||||
itemStack = ItemStackUtils.notNull(itemStack);
|
itemStack = ItemStackUtils.notNull(itemStack);
|
||||||
|
|
||||||
EntityEquipmentPacket.Slot equipmentSlot;
|
EntityEquipmentPacket.Slot equipmentSlot;
|
||||||
@ -228,7 +225,6 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
update();
|
update();
|
||||||
//refreshSlot(slot); seems to break things concerning +64 stacks
|
//refreshSlot(slot); seems to break things concerning +64 stacks
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
protected void setItemStack(int slot, int offset, ItemStack itemStack) {
|
protected void setItemStack(int slot, int offset, ItemStack itemStack) {
|
||||||
slot = convertSlot(slot, offset);
|
slot = convertSlot(slot, offset);
|
||||||
|
@ -29,7 +29,7 @@ public class FakePlayerConnection extends PlayerConnection {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void flush() {
|
public void flush() {
|
||||||
throw new UnsupportedOperationException("FakePlayer does not have anything to flush");
|
// Does nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user