22
33import cn .nukkit .Player ;
44import cn .nukkit .Server ;
5- import cn .nukkit .block .BlockID ;
65import cn .nukkit .event .inventory .InventoryTransactionEvent ;
76import cn .nukkit .inventory .BaseInventory ;
87import cn .nukkit .inventory .InventoryType ;
1514import me .iwareq .fakeinventories .block .FakeBlock ;
1615import me .iwareq .fakeinventories .util .ItemHandler ;
1716
18- import java .util .*;
17+ import java .util .HashMap ;
18+ import java .util .Map ;
1919
2020public 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 ) {
0 commit comments