33import dan200 .computercraft .api .lua .LuaException ;
44import dan200 .computercraft .api .lua .LuaFunction ;
55import dan200 .computercraft .api .lua .MethodResult ;
6- import dan200 .computercraft .api .lua .ObjectLuaTable ;
6+ import dan200 .computercraft .api .peripheral .IComputerAccess ;
7+ import dan200 .computercraft .api .peripheral .IPeripheral ;
8+ import dan200 .computercraft .shared .peripheral .generic .GenericPeripheral ;
79import de .srendi .advancedperipherals .common .addons .computercraft .owner .InventoryManagerOwner ;
810import de .srendi .advancedperipherals .common .blocks .blockentities .InventoryManagerEntity ;
911import de .srendi .advancedperipherals .common .configuration .APConfig ;
12+ import de .srendi .advancedperipherals .common .util .EmptyLuaTable ;
1013import de .srendi .advancedperipherals .common .util .LuaConverter ;
1114import de .srendi .advancedperipherals .common .util .Pair ;
1215import de .srendi .advancedperipherals .common .util .inventory .InventoryUtil ;
1619import net .minecraft .world .entity .player .Inventory ;
1720import net .minecraft .world .entity .player .Player ;
1821import net .minecraft .world .item .ItemStack ;
22+ import net .minecraft .world .level .block .entity .BlockEntity ;
1923import net .neoforged .neoforge .capabilities .Capabilities ;
2024import net .neoforged .neoforge .items .IItemHandler ;
2125import net .neoforged .neoforge .items .wrapper .PlayerInvWrapper ;
2226
2327import java .util .HashMap ;
2428import java .util .List ;
2529import java .util .Map ;
30+ import java .util .Optional ;
2631
2732public class InventoryManagerPeripheral extends BasePeripheral <InventoryManagerOwner > {
2833
@@ -37,6 +42,21 @@ public boolean isEnabled() {
3742 return APConfig .PERIPHERALS_CONFIG .enableInventoryManager .get ();
3843 }
3944
45+ @ Override
46+ protected Map <String , Object > getPeripheralConfiguration () {
47+ Map <String , Object > configs = super .getPeripheralConfiguration ();
48+ configs .put ("itemsTransferEnabled" , APConfig .PERIPHERALS_CONFIG .enableItemsTransfer .get ());
49+ return configs ;
50+ }
51+
52+ private Player getOwnerPlayerOrError () throws LuaException {
53+ Player player = owner .getOwner ();
54+ if (player == null ) {
55+ throw new LuaException ("The Inventory Manager doesn't have a memory card or it isn't bound to a player." );
56+ }
57+ return player ;
58+ }
59+
4060 @ LuaFunction
4161 public final MethodResult getOwner () throws LuaException {
4262 Player player = owner .getOwner ();
@@ -46,53 +66,9 @@ public final MethodResult getOwner() throws LuaException {
4666 return MethodResult .of (player .getUUID ().toString (), player .getGameProfile ().getName ());
4767 }
4868
49-
50- // Add the specified item to the player
51- // The item is specified the same as with the RS/ME bridge:
52- // {name="minecraft:enchanted_book", count=1, nbt="ae70053c97f877de546b0248b9ddf525"}
53- @ LuaFunction (mainThread = true )
54- public final MethodResult addItemToPlayer (String invDirection , Map <?, ?> item ) throws LuaException {
55- Pair <ItemFilter , String > filter = ItemFilter .parse (new ObjectLuaTable (item ));
56- if (filter .rightPresent ()) {
57- return MethodResult .of (null , filter .right ());
58- }
59- return addItemCommon (invDirection , filter .left ());
60- }
61-
62- private MethodResult addItemCommon (String invDirection , ItemFilter filter ) throws LuaException {
63- Direction direction = validateSide (invDirection );
64-
65- IItemHandler inventoryTo = new PlayerInvWrapper (getOwnerPlayerOrError ().getInventory ());
66- IItemHandler inventoryFrom = getLevel ().getCapability (Capabilities .ItemHandler .BLOCK , owner .getPos ().relative (direction ), direction .getOpposite ());
67- if (inventoryFrom == null ) {
68- return MethodResult .of (null , "INVENTORY_FROM_INVALID" );
69- }
70-
71- // if (invSlot >= inventoryTo.getSlots() || invSlot < 0)
72- // throw new LuaException("Inventory out of bounds " + invSlot + " (max: " + (inventoryTo.getSlots() - 1) + ")");
73-
74- return MethodResult .of (InventoryUtil .moveItem (inventoryFrom , inventoryTo , filter ));
75- }
76-
7769 @ LuaFunction (mainThread = true )
78- public final MethodResult removeItemFromPlayer (String invDirection , Map <?, ?> item ) throws LuaException {
79- Pair <ItemFilter , String > filter = ItemFilter .parse (new ObjectLuaTable (item ));
80- if (filter .rightPresent ()) {
81- return MethodResult .of (null , filter .right ());
82- }
83- return removeItemCommon (invDirection , filter .left ());
84- }
85-
86- private MethodResult removeItemCommon (String invDirection , ItemFilter filter ) throws LuaException {
87- Direction direction = validateSide (invDirection );
88-
89- IItemHandler inventoryFrom = new PlayerInvWrapper (getOwnerPlayerOrError ().getInventory ());
90- IItemHandler inventoryTo = getLevel ().getCapability (Capabilities .ItemHandler .BLOCK , owner .getPos ().relative (direction ), direction .getOpposite ());
91- if (inventoryTo == null ) {
92- return MethodResult .of (null , "INVENTORY_TO_INVALID" );
93- }
94-
95- return MethodResult .of (InventoryUtil .moveItem (inventoryFrom , inventoryTo , filter ));
70+ public final int size () throws LuaException {
71+ return getOwnerPlayerOrError ().getInventory ().getContainerSize ();
9672 }
9773
9874 @ LuaFunction (mainThread = true )
@@ -111,23 +87,47 @@ public final Map<Integer, Object> list() throws LuaException {
11187 }
11288
11389 @ LuaFunction (mainThread = true )
114- public final MethodResult listChest ( String target ) throws LuaException {
115- Direction direction = validateSide ( target );
90+ public final MethodResult pushItems ( IComputerAccess computer , String toName , Optional < Map <?, ?>> filterTable ) throws LuaException {
91+ checkAllowItemTransfers ( );
11692
117- IItemHandler inventory = getLevel ().getCapability (Capabilities .ItemHandler .BLOCK , owner .getPos ().relative (direction ), direction .getOpposite ());
118- if (inventory == null ) {
119- return MethodResult .of (null , "INVENTORY_TO_INVALID" );
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" );
120100 }
121101
122- int size = inventory .getSlots ();
123- Map <Integer , Object > items = new HashMap <>(size * 4 / 3 + 1 );
124- for (int slot = 0 ; slot < inventory .getSlots (); slot ++) {
125- ItemStack stack = inventory .getStackInSlot (slot );
126- if (!stack .isEmpty ()) {
127- items .put (slot + 1 , LuaConverter .itemStackToLua (stack ));
128- }
102+ Pair <ItemFilter , String > filter = ItemFilter .parse (EmptyLuaTable .orEmpty (filterTable .orElse (null )));
103+ if (filter .rightPresent ()) {
104+ return MethodResult .of (null , filter .right ());
105+ }
106+
107+ IItemHandler inventoryFrom = new PlayerInvWrapper (getOwnerPlayerOrError ().getInventory ());
108+ return MethodResult .of (InventoryUtil .moveItem (inventoryFrom , inventoryTo , filter .left ()));
109+ }
110+
111+ @ LuaFunction (mainThread = true )
112+ 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" );
129118 }
130- return MethodResult .of (items );
119+ IItemHandler inventoryFrom = extractItemHandler (toPeripheral );
120+ if (inventoryFrom == null ) {
121+ throw new LuaException ("Target '" + fromName + "' is not an inventory" );
122+ }
123+
124+ Pair <ItemFilter , String > filter = ItemFilter .parse (EmptyLuaTable .orEmpty (filterTable .orElse (null )));
125+ if (filter .rightPresent ()) {
126+ return MethodResult .of (null , filter .right ());
127+ }
128+
129+ IItemHandler inventoryTo = new PlayerInvWrapper (getOwnerPlayerOrError ().getInventory ());
130+ return MethodResult .of (InventoryUtil .moveItem (inventoryFrom , inventoryTo , filter .left ()));
131131 }
132132
133133 @ LuaFunction (mainThread = true )
@@ -174,11 +174,21 @@ public final Map<String, Object> getItemInOffHand() throws LuaException {
174174 return LuaConverter .itemStackToLua (getOwnerPlayerOrError ().getOffhandItem ());
175175 }
176176
177- private Player getOwnerPlayerOrError () throws LuaException {
178- Player player = owner .getOwner ();
179- if (player == null ) {
180- throw new LuaException ("The Inventory Manager doesn't have a memory card or it isn't bound to a player." );
177+ private void checkAllowItemTransfers () throws LuaException {
178+ if (!APConfig .PERIPHERALS_CONFIG .enableItemsTransfer .get ()) {
179+ throw new LuaException ("This function is disabled in the config [Inventory_Manager.enableItemsTransfer]. Activate it or ask admins if they can activate it." );
181180 }
182- return player ;
181+ }
182+
183+ private static IItemHandler extractItemHandler (IPeripheral peripheral ) {
184+ Object target = peripheral .getTarget ();
185+ if (target instanceof IItemHandler handler ) {
186+ return handler ;
187+ }
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 );
191+ }
192+ return null ;
183193 }
184194}
0 commit comments