Skip to content

Commit 8c5c16a

Browse files
committed
add wrapStorageItem to inventory manager to operate portable storage items
1 parent 4ef5f7d commit 8c5c16a

11 files changed

Lines changed: 228 additions & 81 deletions

File tree

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ dependencies {
321321
runtimeOnly "dev.emi:emi-neoforge:${emi_version}"
322322
// Spark
323323
runtimeOnly "maven.modrinth:spark:1.10.124-neoforge-1.21.1"
324+
// Sophisticated Backpacks
325+
runtimeOnly "maven.modrinth:sophisticated-backpacks:${sophisticated_backpacks_version}"
326+
runtimeOnly "maven.modrinth:sophisticated-core:${sophisticated_core_version}"
324327
}
325328

326329

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ emi_version=1.1.22+1.21.1
6060
flywheel_version=1.0.5
6161
ponder_version=1.0.64
6262
registrate_version=MC1.21-1.3.0+62
63+
sophisticated_backpacks_version=1.21.1-3.25.37.1646
64+
sophisticated_core_version=1.21.1-1.4.19.1639

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/ChatBoxPeripheral.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private MutableComponent appendPrefix(String prefix, String brackets, String col
8282
formattablePrefix = MutableComponent.Serializer.fromJson(prefix, RegistryAccess.EMPTY);
8383
prefixComponent = formattablePrefix;
8484
} catch (JsonParseException e) {
85-
AdvancedPeripherals.debug("Not vaild json prefix, using plain text instead.");
85+
AdvancedPeripherals.debug("Not valid json prefix, using plain text instead.");
8686
prefixComponent = Component.literal(prefix);
8787
}
8888
}
@@ -108,7 +108,7 @@ private ServerPlayer getPlayer(String argument) {
108108
* @param brackets the brackets to check
109109
* @return true if brackets are not in the right format
110110
*/
111-
private boolean isInvaildBrackets(Optional<String> brackets) {
111+
private boolean isInvalidBrackets(Optional<String> brackets) {
112112
return brackets.isPresent() && brackets.get().length() != 2;
113113
}
114114

@@ -139,7 +139,7 @@ public final MethodResult sendFormattedMessage(String messageJson, Optional<Map<
139139
if (useUTF8) {
140140
brackets = brackets.map(StringUtil::byteStringToUTF8);
141141
}
142-
if (isInvaildBrackets(brackets)) {
142+
if (isInvalidBrackets(brackets)) {
143143
return Errors.INCORRECT_BRACKETS_RESULT;
144144
}
145145

@@ -195,7 +195,7 @@ public final MethodResult sendMessage(String message, Optional<Map<?, ?>> option
195195
if (useUTF8) {
196196
brackets = brackets.map(StringUtil::byteStringToUTF8);
197197
}
198-
if (isInvaildBrackets(brackets)) {
198+
if (isInvalidBrackets(brackets)) {
199199
return Errors.INCORRECT_BRACKETS_RESULT;
200200
}
201201

@@ -260,7 +260,7 @@ public final MethodResult sendFormattedMessageToPlayer(String messageJson, Strin
260260
if (useUTF8) {
261261
brackets = brackets.map(StringUtil::byteStringToUTF8);
262262
}
263-
if (isInvaildBrackets(brackets)) {
263+
if (isInvalidBrackets(brackets)) {
264264
return Errors.INCORRECT_BRACKETS_RESULT;
265265
}
266266

@@ -319,7 +319,7 @@ public final MethodResult sendMessageToPlayer(String message, String playerId, O
319319
if (useUTF8) {
320320
brackets = brackets.map(StringUtil::byteStringToUTF8);
321321
}
322-
if (isInvaildBrackets(brackets)) {
322+
if (isInvalidBrackets(brackets)) {
323323
return Errors.INCORRECT_BRACKETS_RESULT;
324324
}
325325

@@ -394,7 +394,7 @@ public final MethodResult sendFormattedToastToPlayer(Map<?, ?> options) throws L
394394
if (useUTF8) {
395395
brackets = brackets.map(StringUtil::byteStringToUTF8);
396396
}
397-
if (isInvaildBrackets(brackets)) {
397+
if (isInvalidBrackets(brackets)) {
398398
return Errors.INCORRECT_BRACKETS_RESULT;
399399
}
400400

@@ -462,7 +462,7 @@ public final MethodResult sendToastToPlayer(Map<?, ?> options) throws LuaExcepti
462462
if (useUTF8) {
463463
brackets = brackets.map(StringUtil::byteStringToUTF8);
464464
}
465-
if (isInvaildBrackets(brackets)) {
465+
if (isInvalidBrackets(brackets)) {
466466
return Errors.INCORRECT_BRACKETS_RESULT;
467467
}
468468

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/GeoScannerPeripheral.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ public final MethodResult chunkAnalyze(Optional<String> optFilter) throws LuaExc
9191
if (filter.length() > 0 && filter.charAt(0) == '#') {
9292
ResourceLocation id = ResourceLocation.tryParse(filter.substring(1));
9393
if (id == null) {
94-
throw new LuaException("argument #1 is an invaild tag ID");
94+
throw new LuaException("argument #1 is an invalid tag ID");
9595
}
9696
TagKey<Block> tag = TagKey.create(Registries.BLOCK, id);
9797
blockTester = (b) -> b.is(tag);
9898
} else {
9999
ResourceLocation id = ResourceLocation.tryParse(filter.substring(1));
100100
if (id == null) {
101-
throw new LuaException("argument #1 is an invaild block ID");
101+
throw new LuaException("argument #1 is an invalid block ID");
102102
}
103103
Block block = BuiltInRegistries.BLOCK.get(id);
104104
blockTester = block == null ? null : (b) -> b.is(block);

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/InventoryManagerPeripheral.java

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,24 @@
55
import dan200.computercraft.api.lua.MethodResult;
66
import dan200.computercraft.api.peripheral.IComputerAccess;
77
import dan200.computercraft.api.peripheral.IPeripheral;
8-
import dan200.computercraft.shared.peripheral.generic.GenericPeripheral;
98
import de.srendi.advancedperipherals.common.addons.computercraft.owner.InventoryManagerOwner;
109
import de.srendi.advancedperipherals.common.blocks.blockentities.InventoryManagerEntity;
1110
import de.srendi.advancedperipherals.common.configuration.APConfig;
1211
import de.srendi.advancedperipherals.common.util.EmptyLuaTable;
1312
import de.srendi.advancedperipherals.common.util.LuaConverter;
1413
import de.srendi.advancedperipherals.common.util.Pair;
1514
import 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;
1617
import de.srendi.advancedperipherals.common.util.inventory.ItemFilter;
1718
import 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;
2020
import net.minecraft.world.entity.player.Player;
2121
import net.minecraft.world.item.ItemStack;
22-
import net.minecraft.world.level.block.entity.BlockEntity;
23-
import net.neoforged.neoforge.capabilities.Capabilities;
2422
import net.neoforged.neoforge.items.IItemHandler;
2523
import net.neoforged.neoforge.items.wrapper.PlayerInvWrapper;
24+
import org.jetbrains.annotations.NotNull;
2625

27-
import java.util.HashMap;
2826
import java.util.List;
2927
import java.util.Map;
3028
import 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
}

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/NBTStoragePeripheral.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public final MethodResult load() {
4848
public final MethodResult save(IArguments args) throws LuaException {
4949
Object data = args.get(0);
5050
if (data == null) {
51-
throw new LuaException("argument #1 must provide a vaild SNBT string or a NBT-like table");
51+
throw new LuaException("argument #1 must provide a valid SNBT string or a NBT-like table");
5252
}
5353
CompoundTag parsedData;
5454
if (data instanceof String snbt) {

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/PlayerDetectorPeripheral.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,15 @@ private MethodResult getPlayerStat(WeakReference<ServerPlayer> playerRef, String
240240
}
241241
ResourceLocation statId = ResourceLocation.tryParse(statName);
242242
if (statId == null) {
243-
return Errors.INVAILD_STAT_ID_RESULT;
243+
return Errors.INVALID_STAT_ID_RESULT;
244244
}
245245
ResourceLocation statTypeId = ResourceLocation.tryParse(statId.getNamespace().replace('.', ':'));
246246
if (statTypeId == null) {
247-
return Errors.INVAILD_STAT_ID_RESULT;
247+
return Errors.INVALID_STAT_ID_RESULT;
248248
}
249249
ResourceLocation statValueId = ResourceLocation.tryParse(statId.getPath().replace('.', ':'));
250250
if (statValueId == null) {
251-
return Errors.INVAILD_STAT_ID_RESULT;
251+
return Errors.INVALID_STAT_ID_RESULT;
252252
}
253253

254254
@SuppressWarnings("rawtypes")
@@ -305,12 +305,12 @@ public void update() {
305305
}
306306

307307
private static final class Errors {
308-
static final String INVAILD_STAT_ID = "INVAILD_STAT_ID";
308+
static final String INVALID_STAT_ID = "INVALID_STAT_ID";
309309
static final String PLAYER_NOT_EXISTS = "PLAYER_NOT_EXISTS";
310310
static final String UNKNOWN_STAT_TYPE = "UNKNOWN_STAT_TYPE";
311311
static final String UNKNOWN_STAT_VALUE = "UNKNOWN_STAT_VALUE";
312312

313-
static final MethodResult INVAILD_STAT_ID_RESULT = MethodResult.of(null, INVAILD_STAT_ID);
313+
static final MethodResult INVALID_STAT_ID_RESULT = MethodResult.of(null, INVALID_STAT_ID);
314314
static final MethodResult PLAYER_NOT_EXISTS_RESULT = MethodResult.of(null, PLAYER_NOT_EXISTS);
315315
static final MethodResult UNKNOWN_STAT_TYPE_RESULT = MethodResult.of(null, UNKNOWN_STAT_TYPE);
316316
static final MethodResult UNKNOWN_STAT_VALUE_RESULT = MethodResult.of(null, UNKNOWN_STAT_VALUE);

src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public OverlayObject(OverlayModule module) {
5858
}
5959
@SuppressWarnings("rawtypes") PropertyType propertyType = PropertyType.of(objectProperty);
6060
if (propertyType == null) {
61-
throw new IllegalStateException("Invaild property type for field " + fieldName);
61+
throw new IllegalStateException("Invalid property type for field " + fieldName);
6262
}
6363
propertyType.init(propertyAnnotation);
6464
try {
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
package de.srendi.advancedperipherals.common.util.inventory;
22

3-
public class InventoryUtil {
3+
import dan200.computercraft.api.lua.LuaException;
4+
import de.srendi.advancedperipherals.common.util.LuaConverter;
5+
import net.minecraft.world.item.ItemStack;
6+
import net.neoforged.neoforge.items.IItemHandler;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
410

11+
public class InventoryUtil {
512
private InventoryUtil() {
613
}
714

15+
public static Map<Integer, Object> list(IItemHandler handler) throws LuaException {
16+
int size = handler.getSlots();
17+
Map<Integer, Object> items = new HashMap<>();
18+
for (int slot = 0; slot < size; slot++) {
19+
ItemStack stack = handler.getStackInSlot(slot);
20+
if (!stack.isEmpty()) {
21+
items.put(slot + 1, LuaConverter.itemStackToLua(stack));
22+
}
23+
}
24+
return items;
25+
}
826
}

0 commit comments

Comments
 (0)