55import dan200 .computercraft .api .lua .MethodResult ;
66import dan200 .computercraft .api .peripheral .IComputerAccess ;
77import dan200 .computercraft .api .peripheral .IPeripheral ;
8+ import de .srendi .advancedperipherals .common .addons .computercraft .peripheral .InventoryManagerPeripheral ;
89import de .srendi .advancedperipherals .common .util .EmptyLuaTable ;
910import de .srendi .advancedperipherals .common .util .Pair ;
1011import net .minecraft .server .level .ServerPlayer ;
2122
2223// TODO: fluid variant?
2324public class PlayerStorageItemWrapper {
24- public static final String PLAYER_INV_MAGIC_NAME = "@" ;
25-
2625 private final IComputerAccess computer ;
26+ private final WeakReference <InventoryManagerPeripheral > peripheral ;
2727 private final WeakReference <ServerPlayer > player ;
2828 private final int slot ;
2929 private final ItemStack stack ;
3030 private final IItemHandler handler ;
3131
32- protected PlayerStorageItemWrapper (IComputerAccess computer , ServerPlayer player , int slot , ItemStack stack , IItemHandler handler ) {
32+ protected PlayerStorageItemWrapper (IComputerAccess computer , InventoryManagerPeripheral peripheral , ServerPlayer player , int slot , ItemStack stack , IItemHandler handler ) {
3333 this .computer = computer ;
34+ this .peripheral = new WeakReference <>(peripheral );
3435 this .player = new WeakReference <>(player );
3536 this .slot = slot ;
3637 this .stack = stack ;
3738 this .handler = handler ;
3839 }
3940
4041 @ Nullable
41- public static PlayerStorageItemWrapper create (IComputerAccess computer , @ NotNull ServerPlayer player , int slot ) {
42+ public static PlayerStorageItemWrapper create (IComputerAccess computer , InventoryManagerPeripheral peripheral , @ NotNull ServerPlayer player , int slot ) {
4243 ItemStack stack = player .getInventory ().getItem (slot );
4344 if (stack .isEmpty ()) {
4445 return null ;
@@ -47,15 +48,22 @@ public static PlayerStorageItemWrapper create(IComputerAccess computer, @NotNull
4748 if (handler == null ) {
4849 return null ;
4950 }
50- return new PlayerStorageItemWrapper (computer , player , slot , stack , handler );
51+ return new PlayerStorageItemWrapper (computer , peripheral , player , slot , stack , handler );
5152 }
5253
5354 public boolean isValid () {
5455 ServerPlayer player = this .player .get ();
5556 if (player == null || player .isRemoved ()) {
5657 return false ;
5758 }
58- return player .getInventory ().getItem (this .slot ) == this .stack ;
59+ if (player .getInventory ().getItem (this .slot ) != this .stack ) {
60+ return false ;
61+ }
62+ InventoryManagerPeripheral peripheral = this .peripheral .get ();
63+ if (peripheral == null || !peripheral .isAccessValid (this .computer )) {
64+ return false ;
65+ }
66+ return peripheral .getOwnerPlayer () == player ;
5967 }
6068
6169 protected final void assertValid () throws LuaException {
@@ -96,22 +104,22 @@ public final MethodResult pushItems(String toName, Optional<Map<?, ?>> filterTab
96104 }
97105
98106 @ LuaFunction (mainThread = true )
99- public final MethodResult pullItems (String toName , Optional <Map <?, ?>> filterTable ) throws LuaException {
107+ public final MethodResult pullItems (String fromName , Optional <Map <?, ?>> filterTable ) throws LuaException {
100108 this .assertValid ();
101109
102110 Pair <ItemFilter , String > filter = ItemFilter .parse (EmptyLuaTable .orEmpty (filterTable .orElse (null )));
103111 if (filter .rightPresent ()) {
104112 return MethodResult .of (null , filter .right ());
105113 }
106114
107- IItemHandler inventoryFrom = this .getInventoryHandler (toName );
115+ IItemHandler inventoryFrom = this .getInventoryHandler (fromName );
108116
109117 return MethodResult .of (ItemUtil .moveItem (inventoryFrom , this .handler , filter .left ()));
110118 }
111119
112120 @ NotNull
113121 private IItemHandler getInventoryHandler (String name ) throws LuaException {
114- if (name .equals (PLAYER_INV_MAGIC_NAME )) {
122+ if (name .equals (InventoryManagerPeripheral . PLAYER_INV_MAGIC_NAME )) {
115123 ServerPlayer player = this .player .get ();
116124 if (player == null || player .isRemoved ()) {
117125 throw new LuaException ("Storage item outdate" );
0 commit comments