Skip to content

Commit 8d5d7e1

Browse files
committed
fix: inventory opening
1 parent c035511 commit 8d5d7e1

3 files changed

Lines changed: 7 additions & 73 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = "me.iwareq.fakeinventories"
7-
version = "1.1.8-MOT"
7+
version = "1.1.9-MOT"
88

99
java {
1010
toolchain {

src/main/java/me/iwareq/fakeinventories/FakeInventory.java

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import cn.nukkit.Player;
44
import cn.nukkit.Server;
5-
import cn.nukkit.block.BlockID;
65
import cn.nukkit.event.inventory.InventoryTransactionEvent;
76
import cn.nukkit.inventory.BaseInventory;
87
import cn.nukkit.inventory.InventoryType;
@@ -15,7 +14,8 @@
1514
import me.iwareq.fakeinventories.block.FakeBlock;
1615
import me.iwareq.fakeinventories.util.ItemHandler;
1716

18-
import java.util.*;
17+
import java.util.HashMap;
18+
import java.util.Map;
1919

2020
public class FakeInventory extends BaseInventory {
2121

@@ -57,7 +57,7 @@ public void onOpen(Player player) {
5757
super.onOpen(player);
5858

5959
this.sendContents(player);
60-
}, 5);
60+
}, 2);
6161
}
6262

6363
@Override
@@ -68,73 +68,7 @@ public void onClose(Player player) {
6868
player.dataPacket(packet);
6969

7070
super.onClose(player);
71-
72-
Server.getInstance().getScheduler().scheduleDelayedTask(FakeInventories.getInstance(), () -> {
73-
this.fakeBlock.remove(player);
74-
}, 5);
75-
}
76-
77-
public Item[] addItem(ItemHandler handler, Item... slots) {
78-
List<Item> itemSlots = new ArrayList<>();
79-
for (Item slot : slots) {
80-
if (slot.getId() != 0 && slot.getCount() > 0) {
81-
itemSlots.add(slot.clone());
82-
}
83-
}
84-
85-
List<Integer> emptySlots = new ArrayList<>();
86-
87-
for (int i = 0; i < this.getSize(); ++i) {
88-
Item item = this.getItem(i);
89-
if (item.getId() == BlockID.AIR || item.getCount() <= 0) {
90-
emptySlots.add(i);
91-
}
92-
93-
for (Item slot : Collections.unmodifiableList(itemSlots)) {
94-
if (slot.equals(item) && item.getCount() < item.getMaxStackSize()) {
95-
int amount = Math.min(item.getMaxStackSize() - item.getCount(), slot.getCount());
96-
amount = Math.min(amount, this.getMaxStackSize());
97-
if (amount > 0) {
98-
slot.setCount(slot.getCount() - amount);
99-
item.setCount(item.getCount() + amount);
100-
101-
this.setItem(i, item, handler);
102-
103-
if (slot.getCount() <= 0) {
104-
itemSlots.remove(slot);
105-
}
106-
}
107-
}
108-
}
109-
110-
if (itemSlots.isEmpty()) {
111-
break;
112-
}
113-
}
114-
115-
if (!itemSlots.isEmpty() && !emptySlots.isEmpty()) {
116-
for (int slotIndex : emptySlots) {
117-
if (!itemSlots.isEmpty()) {
118-
Item slot = itemSlots.get(0);
119-
120-
int amount = Math.min(slot.getMaxStackSize(), slot.getCount());
121-
amount = Math.min(amount, this.getMaxStackSize());
122-
123-
slot.setCount(slot.getCount() - amount);
124-
125-
Item item = slot.clone();
126-
item.setCount(amount);
127-
128-
this.setItem(slotIndex, item, handler);
129-
130-
if (slot.getCount() <= 0) {
131-
itemSlots.remove(slot);
132-
}
133-
}
134-
}
135-
}
136-
137-
return itemSlots.toArray(new Item[0]);
71+
this.fakeBlock.remove(player);
13872
}
13973

14074
public void setItem(int index, Item item, ItemHandler handler) {

src/main/java/me/iwareq/fakeinventories/block/SingleFakeBlock.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void create(Player player, String title) {
3131
positions.forEach(position -> {
3232
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
3333
updateBlockPacket.blockRuntimeId = GlobalBlockPalette.getOrCreateRuntimeId(player.protocol, this.blockId, 0);
34-
updateBlockPacket.flags = UpdateBlockPacket.FLAG_ALL;
34+
updateBlockPacket.flags = UpdateBlockPacket.FLAG_ALL_PRIORITY;
3535
updateBlockPacket.x = position.getFloorX();
3636
updateBlockPacket.y = position.getFloorY();
3737
updateBlockPacket.z = position.getFloorZ();
@@ -56,7 +56,7 @@ public void remove(Player player) {
5656
this.lastPositions.forEach(position -> {
5757
UpdateBlockPacket packet = new UpdateBlockPacket();
5858
packet.blockRuntimeId = GlobalBlockPalette.getOrCreateRuntimeId(player.protocol, player.getLevel().getBlock(position).getFullId());
59-
packet.flags = UpdateBlockPacket.FLAG_ALL;
59+
packet.flags = UpdateBlockPacket.FLAG_ALL_PRIORITY;
6060
packet.x = position.getFloorX();
6161
packet.y = position.getFloorY();
6262
packet.z = position.getFloorZ();

0 commit comments

Comments
 (0)