55import dan200 .computercraft .api .lua .MethodResult ;
66import dan200 .computercraft .api .peripheral .IComputerAccess ;
77import dan200 .computercraft .api .peripheral .IPeripheral ;
8- import dan200 .computercraft .shared .peripheral .generic .GenericPeripheral ;
98import de .srendi .advancedperipherals .common .addons .computercraft .owner .InventoryManagerOwner ;
109import de .srendi .advancedperipherals .common .blocks .blockentities .InventoryManagerEntity ;
1110import de .srendi .advancedperipherals .common .configuration .APConfig ;
1211import de .srendi .advancedperipherals .common .util .EmptyLuaTable ;
1312import de .srendi .advancedperipherals .common .util .LuaConverter ;
1413import de .srendi .advancedperipherals .common .util .Pair ;
1514import de .srendi .advancedperipherals .common .util .inventory .ItemUtil ;
15+ import de .srendi .advancedperipherals .common .util .inventory .PlayerStorageItemWrapper ;
16+ import de .srendi .advancedperipherals .common .util .inventory .InventoryUtil ;
1617import de .srendi .advancedperipherals .common .util .inventory .ItemFilter ;
1718import de .srendi .advancedperipherals .lib .peripherals .BasePeripheral ;
18- import net .minecraft .core .Direction ;
19- import net .minecraft .world .entity .player .Inventory ;
19+ import net .minecraft .server .level .ServerPlayer ;
2020import net .minecraft .world .entity .player .Player ;
2121import net .minecraft .world .item .ItemStack ;
22- import net .minecraft .world .level .block .entity .BlockEntity ;
23- import net .neoforged .neoforge .capabilities .Capabilities ;
2422import net .neoforged .neoforge .items .IItemHandler ;
2523import net .neoforged .neoforge .items .wrapper .PlayerInvWrapper ;
24+ import org .jetbrains .annotations .NotNull ;
2625
27- import java .util .HashMap ;
2826import java .util .List ;
2927import java .util .Map ;
3028import java .util .Optional ;
@@ -49,12 +47,16 @@ protected Map<String, Object> getPeripheralConfiguration() {
4947 return configs ;
5048 }
5149
52- private Player getOwnerPlayerOrError () throws LuaException {
50+ private ServerPlayer getOwnerPlayerOrError () throws LuaException {
5351 Player player = owner .getOwner ();
5452 if (player == null ) {
5553 throw new LuaException ("The Inventory Manager doesn't have a memory card or it isn't bound to a player." );
5654 }
57- return player ;
55+ return (ServerPlayer ) player ;
56+ }
57+
58+ private IItemHandler getPlayerInventory () throws LuaException {
59+ return new PlayerInvWrapper (this .getOwnerPlayerOrError ().getInventory ());
5860 }
5961
6062 @ LuaFunction
@@ -68,127 +70,102 @@ public final MethodResult getOwner() throws LuaException {
6870
6971 @ LuaFunction (mainThread = true )
7072 public final int size () throws LuaException {
71- return getOwnerPlayerOrError ().getInventory ().getContainerSize ();
73+ return this . getOwnerPlayerOrError ().getInventory ().getContainerSize ();
7274 }
7375
7476 @ LuaFunction (mainThread = true )
7577 public final Map <Integer , Object > list () throws LuaException {
76- Inventory inventory = getOwnerPlayerOrError ().getInventory ();
77-
78- int size = inventory .getContainerSize ();
79- Map <Integer , Object > items = new HashMap <>(size * 4 / 3 + 1 );
80- for (int slot = 0 ; slot < size ; slot ++) {
81- ItemStack stack = inventory .getItem (slot );
82- if (!stack .isEmpty ()) {
83- items .put (slot + 1 , LuaConverter .itemStackToLua (stack ));
84- }
85- }
86- return items ;
78+ return InventoryUtil .list (this .getPlayerInventory ());
8779 }
8880
8981 @ LuaFunction (mainThread = true )
9082 public final MethodResult pushItems (IComputerAccess computer , String toName , Optional <Map <?, ?>> filterTable ) throws LuaException {
91- checkAllowItemTransfers ();
92-
93- IPeripheral toPeripheral = computer .getAvailablePeripheral (toName );
94- if (toPeripheral == null ) {
95- throw new LuaException ("Target '" + toName + "' does not exist" );
96- }
97- IItemHandler inventoryTo = extractItemHandler (toPeripheral );
98- if (inventoryTo == null ) {
99- throw new LuaException ("Target '" + toName + "' is not an inventory" );
100- }
83+ this .assertAllowItemTransfers ();
10184
10285 Pair <ItemFilter , String > filter = ItemFilter .parse (EmptyLuaTable .orEmpty (filterTable .orElse (null )));
10386 if (filter .rightPresent ()) {
10487 return MethodResult .of (null , filter .right ());
10588 }
10689
107- IItemHandler inventoryFrom = new PlayerInvWrapper (getOwnerPlayerOrError ().getInventory ());
90+ IItemHandler inventoryTo = this .getInventoryHandler (computer , toName );
91+ IItemHandler inventoryFrom = this .getPlayerInventory ();
10892 return MethodResult .of (ItemUtil .moveItem (inventoryFrom , inventoryTo , filter .left ()));
10993 }
11094
11195 @ LuaFunction (mainThread = true )
11296 public final MethodResult pullItems (IComputerAccess computer , String fromName , Optional <Map <?, ?>> filterTable ) throws LuaException {
113- checkAllowItemTransfers ();
114-
115- IPeripheral toPeripheral = computer .getAvailablePeripheral (fromName );
116- if (toPeripheral == null ) {
117- throw new LuaException ("Target '" + fromName + "' does not exist" );
118- }
119- IItemHandler inventoryFrom = extractItemHandler (toPeripheral );
120- if (inventoryFrom == null ) {
121- throw new LuaException ("Target '" + fromName + "' is not an inventory" );
122- }
97+ this .assertAllowItemTransfers ();
12398
12499 Pair <ItemFilter , String > filter = ItemFilter .parse (EmptyLuaTable .orEmpty (filterTable .orElse (null )));
125100 if (filter .rightPresent ()) {
126101 return MethodResult .of (null , filter .right ());
127102 }
128103
129- IItemHandler inventoryTo = new PlayerInvWrapper (getOwnerPlayerOrError ().getInventory ());
104+ IItemHandler inventoryFrom = this .getInventoryHandler (computer , fromName );
105+ IItemHandler inventoryTo = this .getPlayerInventory ();
130106 return MethodResult .of (ItemUtil .moveItem (inventoryFrom , inventoryTo , filter .left ()));
131107 }
132108
109+ @ LuaFunction (mainThread = true )
110+ public final PlayerStorageItemWrapper wrapStorageItem (IComputerAccess computer , int slot ) throws LuaException {
111+ return PlayerStorageItemWrapper .create (computer , this .getOwnerPlayerOrError (), slot - 1 );
112+ }
113+
133114 @ LuaFunction (mainThread = true )
134115 public final boolean isWearing (int index ) throws LuaException {
135116 index --;
136- List <ItemStack > armor = getOwnerPlayerOrError ().getInventory ().armor ;
117+ List <ItemStack > armor = this . getOwnerPlayerOrError ().getInventory ().armor ;
137118 return 0 <= index && index < armor .size () && !armor .get (index ).isEmpty ();
138119 }
139120
140121 @ LuaFunction (mainThread = true )
141122 public final int getEmptySlots () throws LuaException {
142123 int count = 0 ;
143- for (ItemStack stack : getOwnerPlayerOrError ().getInventory ().items ) {
124+ for (ItemStack stack : this . getOwnerPlayerOrError ().getInventory ().items ) {
144125 if (stack .isEmpty ()) {
145126 count ++;
146127 }
147128 }
148129 return count ;
149130 }
150131
151- @ LuaFunction (mainThread = true )
152- public final boolean hasAvailableSpace () throws LuaException {
153- return getOwnerPlayerOrError ().getInventory ().getFreeSlot () >= 0 ;
154- }
155-
156132 @ LuaFunction (mainThread = true )
157133 public final int getFreeSlot () throws LuaException {
158- return getOwnerPlayerOrError ().getInventory ().getFreeSlot () + 1 ;
134+ return this . getOwnerPlayerOrError ().getInventory ().getFreeSlot () + 1 ;
159135 }
160136
161137 @ LuaFunction (mainThread = true )
162138 public final int getHandSlot () throws LuaException {
163- return getOwnerPlayerOrError ().getInventory ().selected + 1 ;
139+ return this . getOwnerPlayerOrError ().getInventory ().selected + 1 ;
164140 }
165141
166142 @ LuaFunction (mainThread = true )
167143 public final Map <String , Object > getItemInHand () throws LuaException {
168- Player player = getOwnerPlayerOrError ();
144+ Player player = this . getOwnerPlayerOrError ();
169145 return LuaConverter .itemStackToLuaWithSlot (player .getMainHandItem (), player .getInventory ().selected );
170146 }
171147
172148 @ LuaFunction (mainThread = true )
173149 public final Map <String , Object > getItemInOffHand () throws LuaException {
174- return LuaConverter .itemStackToLua (getOwnerPlayerOrError ().getOffhandItem ());
150+ return LuaConverter .itemStackToLua (this . getOwnerPlayerOrError ().getOffhandItem ());
175151 }
176152
177- private void checkAllowItemTransfers () throws LuaException {
153+ private void assertAllowItemTransfers () throws LuaException {
178154 if (!APConfig .PERIPHERALS_CONFIG .enableItemsTransfer .get ()) {
179155 throw new LuaException ("This function is disabled in the config [Inventory_Manager.enableItemsTransfer]. Activate it or ask admins if they can activate it." );
180156 }
181157 }
182158
183- private static IItemHandler extractItemHandler (IPeripheral peripheral ) {
184- Object target = peripheral .getTarget ();
185- if (target instanceof IItemHandler handler ) {
186- return handler ;
159+ @ NotNull
160+ private IItemHandler getInventoryHandler (IComputerAccess computer , String name ) throws LuaException {
161+ IPeripheral toPeripheral = computer .getAvailablePeripheral (name );
162+ if (toPeripheral == null ) {
163+ throw new LuaException ("Target '" + name + "' does not exist" );
187164 }
188- if ( target instanceof BlockEntity be ) {
189- Direction side = peripheral instanceof GenericPeripheral sided ? sided . side () : null ;
190- return be . getLevel (). getCapability ( Capabilities . ItemHandler . BLOCK , be . getBlockPos (), side );
165+ IItemHandler inventory = ItemUtil . extractHandler ( toPeripheral );
166+ if ( inventory == null ) {
167+ throw new LuaException ( "Target '" + name + "' is not an inventory" );
191168 }
192- return null ;
169+ return inventory ;
193170 }
194171}
0 commit comments