Started by
Swyde
on
Topic category: Troubleshooting, bugs, and solutions
Hello !
My mod crash when I open my market GUI (with an input slot) and tell me Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0.
Chat GPT said me that the problem is the variable i but I can't change the code. Do you know how it's possible ?
Here is my GUI code if you have an idea to patch it
package net.mcreator.blockeffect.world.inventory;
/* imports omitted */
public class AdditemmarketMenu extends AbstractContainerMenu implements Supplier<Map<Integer, Slot>> {
public final static HashMap<String, Object> guistate = new HashMap<>();
public final Level world;
public final Player entity;
public int x, y, z;
private ContainerLevelAccess access = ContainerLevelAccess.NULL;
private IItemHandler internal;
private final Map<Integer, Slot> customSlots = new HashMap<>();
private boolean bound = false;
private Supplier<Boolean> boundItemMatcher = null;
private Entity boundEntity = null;
private BlockEntity boundBlockEntity = null;
public AdditemmarketMenu(int id, Inventory inv, FriendlyByteBuf extraData) {
super(BlockEffectModMenus.ADDITEMMARKET.get(), id);
this.entity = inv.player;
this.world = inv.player.level();
this.internal = new ItemStackHandler(1);
BlockPos pos = null;
if (extraData != null) {
pos = extraData.readBlockPos();
this.x = pos.getX();
this.y = pos.getY();
this.z = pos.getZ();
access = ContainerLevelAccess.create(world, pos);
}
if (pos != null) {
if (extraData.readableBytes() == 1) { // bound to item
byte hand = extraData.readByte();
ItemStack itemstack = hand == 0 ? this.entity.getMainHandItem() : this.entity.getOffhandItem();
this.boundItemMatcher = () -> itemstack == (hand == 0 ? this.entity.getMainHandItem() : this.entity.getOffhandItem());
itemstack.getCapability(ForgeCapabilities.ITEM_HANDLER, null).ifPresent(capability -> {
this.internal = capability;
this.bound = true;
});
} else if (extraData.readableBytes() > 1) { // bound to entity
extraData.readByte(); // drop padding
boundEntity = world.getEntity(extraData.readVarInt());
if (boundEntity != null)
boundEntity.getCapability(ForgeCapabilities.ITEM_HANDLER, null).ifPresent(capability -> {
this.internal = capability;
this.bound = true;
});
} else { // might be bound to block
boundBlockEntity = this.world.getBlockEntity(pos);
if (boundBlockEntity != null)
boundBlockEntity.getCapability(ForgeCapabilities.ITEM_HANDLER, null).ifPresent(capability -> {
this.internal = capability;
this.bound = true;
});
}
}
this.customSlots.put(0, this.addSlot(new SlotItemHandler(internal, 0, 144, 22) {
private final int slot = 0;
}));
for (int si = 0; si < 3; ++si)
for (int sj = 0; sj < 9; ++sj)
this.addSlot(new Slot(inv, sj + (si + 1) * 9, 2 + 8 + sj * 18, 56 + 84 + si * 18));
for (int si = 0; si < 9; ++si)
this.addSlot(new Slot(inv, si, 2 + 8 + si * 18, 56 + 142));
}
@Override
public boolean stillValid(Player player) {
if (this.bound) {
if (this.boundItemMatcher != null)
return this.boundItemMatcher.get();
else if (this.boundBlockEntity != null)
return AbstractContainerMenu.stillValid(this.access, player, this.boundBlockEntity.getBlockState().getBlock());
else if (this.boundEntity != null)
return this.boundEntity.isAlive();
}
return true;
}
@Override
public ItemStack quickMoveStack(Player playerIn, int index) {
ItemStack itemstack = ItemStack.EMPTY;
Slot slot = (Slot) this.slots.get(index);
if (slot != null && slot.hasItem()) {
ItemStack itemstack1 = slot.getItem();
itemstack = itemstack1.copy();
if (index < 1) {
if (!this.moveItemStackTo(itemstack1, 1, this.slots.size(), true))
return ItemStack.EMPTY;
slot.onQuickCraft(itemstack1, itemstack);
} else if (!this.moveItemStackTo(itemstack1, 0, 1, false)) {
if (index < 1 + 27) {
if (!this.moveItemStackTo(itemstack1, 1 + 27, this.slots.size(), true))
return ItemStack.EMPTY;
} else {
if (!this.moveItemStackTo(itemstack1, 1, 1 + 27, false))
return ItemStack.EMPTY;
}
return ItemStack.EMPTY;
}
if (itemstack1.getCount() == 0)
slot.set(ItemStack.EMPTY);
else
slot.setChanged();
if (itemstack1.getCount() == itemstack.getCount())
return ItemStack.EMPTY;
slot.onTake(playerIn, itemstack1);
}
return itemstack;
}
@Override
protected boolean moveItemStackTo(ItemStack p_38904_, int p_38905_, int p_38906_, boolean p_38907_) {
boolean flag = false;
int i = p_38905_;
if (p_38907_) {
i = p_38906_ - 1;
}
if (p_38904_.isStackable()) {
while (!p_38904_.isEmpty()) {
if (p_38907_) {
if (i < p_38905_) {
break;
}
} else if (i >= p_38906_) {
break;
}
Slot slot = this.slots.get(i);
ItemStack itemstack = slot.getItem();
if (slot.mayPlace(itemstack) && !itemstack.isEmpty() && ItemStack.isSameItemSameTags(p_38904_, itemstack)) {
int j = itemstack.getCount() + p_38904_.getCount();
int maxSize = Math.min(slot.getMaxStackSize(), p_38904_.getMaxStackSize());
if (j <= maxSize) {
p_38904_.setCount(0);
itemstack.setCount(j);
slot.set(itemstack);
flag = true;
} else if (itemstack.getCount() < maxSize) {
p_38904_.shrink(maxSize - itemstack.getCount());
itemstack.setCount(maxSize);
slot.set(itemstack);
flag = true;
}
}
if (p_38907_) {
--i;
} else {
++i;
}
}
}
if (!p_38904_.isEmpty()) {
if (p_38907_) {
i = p_38906_ - 1;
} else {
i = p_38905_;
}
while (true) {
if (p_38907_) {
if (i < p_38905_) {
break;
}
} else if (i >= p_38906_) {
break;
}
Slot slot1 = this.slots.get(i);
ItemStack itemstack1 = slot1.getItem();
if (itemstack1.isEmpty() && slot1.mayPlace(p_38904_)) {
if (p_38904_.getCount() > slot1.getMaxStackSize()) {
slot1.setByPlayer(p_38904_.split(slot1.getMaxStackSize()));
} else {
slot1.setByPlayer(p_38904_.split(p_38904_.getCount()));
}
slot1.setChanged();
flag = true;
break;
}
if (p_38907_) {
--i;
} else {
++i;
}
}
}
return flag;
}
@Override
public void removed(Player playerIn) {
super.removed(playerIn);
if (!bound && playerIn instanceof ServerPlayer serverPlayer) {
if (!serverPlayer.isAlive() || serverPlayer.hasDisconnected()) {
for (int j = 0; j < internal.getSlots(); ++j) {
playerIn.drop(internal.extractItem(j, internal.getStackInSlot(j).getCount(), false), false);
}
} else {
for (int i = 0; i < internal.getSlots(); ++i) {
playerIn.getInventory().placeItemBackInInventory(internal.extractItem(i, internal.getStackInSlot(i).getCount(), false));
}
}
}
}
public Map<Integer, Slot> get() {
return customSlots;
}
}
The solution to this problem and many other problems can be found in our knowledge base: https://mcreator.net/support/knowledgebase Please check the knowledge base before opening a new topic on the issue which has a known fix.
Thanks You