Skip to content

Commit 627f6fc

Browse files
committed
this actually fixes #67 again ( i hope )
1 parent 41c2242 commit 627f6fc

File tree

2 files changed

+99
-34
lines changed

2 files changed

+99
-34
lines changed

common/src/main/java/com/periut/chisel/gui/ChiselScreenHandler.java

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,20 @@ public ItemStack quickMove(PlayerEntity player, int invSlot) {
139139
public void onClosed(PlayerEntity player) {
140140
super.onClosed(player);
141141
ItemStack hand = player.getHandItems().iterator().next();
142-
if (!hand.isOf(Chisel.chiselSupplier.get()))
143-
{
144-
ItemStack chiselStack = ItemStack.EMPTY;
145-
int chiselSlot = -1;
146-
147-
// Find the chisel in inventory
148-
for (int i = 0; i < player.getInventory().size(); i++)
149-
{
150-
if(player.getInventory().getStack(i).isOf(Chisel.chiselSupplier.get()))
151-
{
152-
chiselStack = player.getInventory().getStack(i);
153-
chiselSlot = i;
154-
break; // Stop at the first chisel found
155-
}
156-
}
157142

158-
// Only modify inventory if we found a chisel
159-
if (chiselSlot != -1 && !chiselStack.isEmpty() && chiselStack.getCount() > 0) {
160-
player.getInventory().removeStack(chiselSlot);
143+
// Sanitize inventory before any modifications
144+
sanitizeInventory(player.getInventory());
145+
146+
if (!hand.isOf(Chisel.chiselSupplier.get())) {
147+
ItemStack chiselStack = findChiselInInventory(player);
148+
149+
// Only modify inventory if we found a valid chisel stack
150+
if (chiselStack != null && !chiselStack.isEmpty() && chiselStack.getCount() > 0) {
151+
// Ensure we're not creating an invalid stack
152+
chiselStack = chiselStack.copyWithCount(Math.max(1, Math.min(chiselStack.getCount(), chiselStack.getMaxCount())));
153+
154+
// Remove from original slot and set to first slot
155+
player.getInventory().removeStack(player.getInventory().getSlotWithStack(chiselStack));
161156
player.getInventory().setStack(0, chiselStack);
162157
}
163158
}
@@ -171,7 +166,35 @@ public void onClosed(PlayerEntity player) {
171166
var changes = ComponentChanges.builder().remove(DataComponentTypes.BUNDLE_CONTENTS).add(DataComponentTypes.BUNDLE_CONTENTS, c).build();
172167
hand.applyChanges(changes);
173168
}
169+
}
174170

171+
// Helper method to find a chisel in the player's inventory
172+
private ItemStack findChiselInInventory(PlayerEntity player) {
173+
for (int i = 0; i < player.getInventory().size(); i++) {
174+
ItemStack stack = player.getInventory().getStack(i);
175+
if (stack.isOf(Chisel.chiselSupplier.get()) && stack.getCount() > 0) {
176+
return stack;
177+
}
178+
}
179+
return null;
180+
}
181+
182+
// Sanitization method for inventory
183+
private void sanitizeInventory(PlayerInventory inventory) {
184+
for (int i = 0; i < inventory.size(); i++) {
185+
ItemStack stack = inventory.getStack(i);
186+
187+
// Sanitize stack
188+
if (stack == null || stack.isEmpty() || stack.getCount() <= 0) {
189+
inventory.setStack(i, ItemStack.EMPTY);
190+
} else {
191+
// Ensure count is within valid range
192+
int sanitizedCount = Math.max(1, Math.min(stack.getCount(), stack.getMaxCount()));
193+
if (sanitizedCount != stack.getCount()) {
194+
inventory.setStack(i, stack.copyWithCount(sanitizedCount));
195+
}
196+
}
197+
}
175198
}
176199

177200
@Override

common/src/main/java/com/periut/chisel/inventory/ChiselInventory.java

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
public class ChiselInventory implements IInventory {
1313
private final DefaultedList<ItemStack> inventory;
1414

15-
public ChiselInventory()
16-
{
15+
public ChiselInventory() {
1716
this.inventory = DefaultedList.ofSize(61, ItemStack.EMPTY);
1817
}
1918

@@ -24,46 +23,81 @@ public DefaultedList<ItemStack> getItems() {
2423

2524
@Override
2625
public void setStack(int slot, ItemStack stack) {
27-
if (stack.getCount() <= 0 || stack.getCount() > 99) {
28-
stack = ItemStack.EMPTY; // Ensure invalid stacks are cleared
26+
validateAndSetStack(slot, stack);
27+
}
28+
29+
private void validateAndSetStack(int slot, ItemStack stack) {
30+
// Ensure stack is either empty or has a valid count
31+
if (stack == null) {
32+
getItems().set(slot, ItemStack.EMPTY);
33+
return;
2934
}
30-
getItems().set(slot, stack);
35+
36+
// If stack is empty or count is invalid, set to empty
37+
if (stack.isEmpty() || stack.getCount() <= 0) {
38+
getItems().set(slot, ItemStack.EMPTY);
39+
} else {
40+
// Clamp count between 1 and 99
41+
ItemStack sanitizedStack = stack.copyWithCount(Math.max(1, Math.min(stack.getCount(), 99)));
42+
getItems().set(slot, sanitizedStack);
43+
}
44+
45+
// Refresh if it's the input slot
3146
if (slot == 0) {
32-
refresh(stack.getItem());
47+
refresh(getStack(slot).getItem());
3348
}
3449
}
3550

36-
3751
@Override
3852
public ItemStack removeStack(int slot, int count) {
3953
ItemStack result = Inventories.splitStack(getItems(), slot, count);
54+
55+
// Ensure the result has a valid count
4056
if (!result.isEmpty()) {
41-
markDirty();
57+
// If the input slot, respect the current input stack count
4258
if (slot != 0 && !inventory.get(0).isEmpty()) {
43-
int c = inventory.get(0).getCount();
44-
result.setCount(Math.max(1, Math.min(c, 99))); // Clamp between 1 and 99
59+
int inputCount = inventory.get(0).getCount();
60+
result.setCount(Math.max(1, Math.min(result.getCount(), inputCount)));
4561
}
4662

63+
// Ensure the result always has at least 1 item
64+
if (result.getCount() <= 0) {
65+
result = ItemStack.EMPTY;
66+
}
67+
68+
markDirty();
4769
}
70+
4871
return result;
4972
}
5073

51-
public void refresh(Item item)
52-
{
53-
if (!(item instanceof BlockItem))
54-
{
74+
@Override
75+
public ItemStack removeStack(int slot) {
76+
ItemStack stack = getItems().get(slot);
77+
78+
// If the stack is empty or invalid, return empty stack
79+
if (stack.isEmpty() || stack.getCount() <= 0) {
80+
return ItemStack.EMPTY;
81+
}
82+
83+
getItems().set(slot, ItemStack.EMPTY);
84+
return stack;
85+
}
86+
87+
public void refresh(Item item) {
88+
if (!(item instanceof BlockItem)) {
5589
clearInv();
5690
return;
5791
}
5892
List<Item> chiselBlocks = ChiselGroupLookup.getBlocksInGroup(item);
5993
populate(chiselBlocks);
6094
}
6195

62-
public void populate(List<Item> chiselBlocks)
63-
{
96+
public void populate(List<Item> chiselBlocks) {
6497
clearInv();
6598
int baseCount = getStack(0).getCount();
6699
baseCount = Math.max(1, Math.min(baseCount, 99)); // Clamp stack size
100+
67101
for (int i = 0; i < 60 && i < chiselBlocks.size(); i++) {
68102
this.setStack(i + 1, new ItemStack(chiselBlocks.get(i), baseCount));
69103
}
@@ -74,4 +108,12 @@ public void clearInv() {
74108
this.setStack(i, ItemStack.EMPTY);
75109
}
76110
}
111+
112+
// Additional method to ensure all stacks are valid before saving
113+
public void sanitizeInventory() {
114+
for (int i = 0; i < inventory.size(); i++) {
115+
ItemStack stack = inventory.get(i);
116+
validateAndSetStack(i, stack);
117+
}
118+
}
77119
}

0 commit comments

Comments
 (0)