Fix Item Slot not registering changes properly

This commit is contained in:
Kam committed 2024-03-16 16:21:44 -04:00
1 parent 6f3b8f0a77
commit a872bc7035
3 files changed
+68 -22

No files matched your search

@@ -8,7 +8,11 @@ package noppes.npcs.client.gui.custom.components;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import noppes.npcs.EventHooks;
import noppes.npcs.api.entity.IPlayer;
import noppes.npcs.api.gui.IItemSlot;
import noppes.npcs.containers.ContainerCustomGui;
import noppes.npcs.scripted.NpcAPI;
public class CustomGuiSlot extends Slot {
public boolean clientSide;
@@ -23,4 +27,26 @@ public class CustomGuiSlot extends Slot {
this.slot = slot;
this.player = player;
}
@Override
public void onSlotChanged() {
if(!player.worldObj.isRemote){
boolean changed;
if (getStack() != null && slot.getStack() != null) {
changed = !getStack().equals(slot.getStack().getMCItemStack());
} else {
// Handle the case when either getStack() or slot.getStack() is null (indicating an empty slot)
changed = getStack() != null || slot.getStack() != null;
}
if(changed) {
slot.setStack(NpcAPI.Instance().getIItemStack(getStack()));
if (player.openContainer instanceof ContainerCustomGui) {
EventHooks.onCustomGuiSlot((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui,
getSlotIndex(), getStack(), slot);
}
}
}
super.onSlotChanged();
}
}
@@ -38,7 +38,7 @@ public class ContainerCustomGui extends Container {
}
slotCount=0;
inventorySlots.clear();
// inventorySlots.clear();
for (IItemSlot slot : this.customGui.getSlots()) {
if (slot.hasStack()) {
this.addSlot(player, slot.getPosX(), slot.getPosY(), slot, slot.getStack().getMCItemStack(), player.worldObj.isRemote);
@@ -106,40 +106,59 @@ public class ContainerCustomGui extends Container {
if(slotId < 0)
return super.slotClick(slotId, dragType, clickTypeIn, player);
if (slotId >= this.inventorySlots.size()) {
return null;
}
Slot mcSlot = (Slot)this.inventorySlots.get(slotId);
if(!player.worldObj.isRemote && mcSlot != null) {
if(!player.worldObj.isRemote) {
IItemSlot slot = null;
if (this.getSlot(slotId) instanceof CustomGuiSlot) {
slot = ((CustomGuiSlot)this.getSlot(slotId)).slot;
}
if(!EventHooks.onCustomGuiSlotClicked((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui, slotId, slot, dragType, clickTypeIn)) {
ItemStack prevStack = mcSlot.getStack();
if (slot != null) {
prevStack = slot.getStack() == null ? null : slot.getStack().getMCItemStack();
}
ItemStack item = super.slotClick(slotId, dragType, clickTypeIn, player);
if (!ItemStack.areItemStacksEqual(prevStack,item)) {
if (slot != null) {
slot.setStack(NpcAPI.Instance().getIItemStack(mcSlot.getStack()));
}
if (player.openContainer instanceof ContainerCustomGui) {
EventHooks.onCustomGuiSlot((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui,
mcSlot.slotNumber, prevStack, slot);
}
}
EntityPlayerMP p = (EntityPlayerMP) player;
p.sendContainerToPlayer(this);
return item;
}
}
return null;
// if(slotId < 0)
// return super.slotClick(slotId, dragType, clickTypeIn, player);
//
// if (slotId >= this.inventorySlots.size()) {
// return null;
// }
//
// Slot mcSlot = (Slot)this.inventorySlots.get(slotId);
// if(!player.worldObj.isRemote && mcSlot != null) {
// IItemSlot slot = null;
// if (this.getSlot(slotId) instanceof CustomGuiSlot) {
// slot = ((CustomGuiSlot)this.getSlot(slotId)).slot;
// }
// if(!EventHooks.onCustomGuiSlotClicked((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui, slotId, slot, dragType, clickTypeIn)) {
// ItemStack prevStack = mcSlot.getStack();
// if (slot != null) {
// prevStack = slot.getStack() == null ? null : slot.getStack().getMCItemStack();
// }
// ItemStack item = super.slotClick(slotId, dragType, clickTypeIn, player);
//
// if (!ItemStack.areItemStacksEqual(prevStack,item)) {
// if (slot != null) {
// slot.setStack(NpcAPI.Instance().getIItemStack(mcSlot.getStack()));
// }
// if (player.openContainer instanceof ContainerCustomGui) {
// EventHooks.onCustomGuiSlot((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui,
// mcSlot.slotNumber, prevStack, slot);
// }
// }
//
// EntityPlayerMP p = (EntityPlayerMP) player;
// p.sendContainerToPlayer(this);
// return item;
// }
// }
//
// return null;
}
public boolean canDragIntoSlot(Slot p_94531_1_) { return true; }
@@ -256,7 +256,7 @@ public class ScriptGui implements ICustomGui {
for(int i = 0; i < list.tagCount(); i++){
NBTBase b = list.getCompoundTagAt(i);
ScriptGuiComponent component = ScriptGuiComponent.createFromNBT((NBTTagCompound)b);
if (component != null) {
if (component != null && !(component instanceof ScriptGuiItemSlot)) {
components.put(component.getID(), component);
}
}
@@ -267,6 +267,7 @@ public class ScriptGui implements ICustomGui {
for(int i = 0; i < list.tagCount(); i++){
NBTBase b = list.getCompoundTagAt(i);
ScriptGuiItemSlot component = (ScriptGuiItemSlot)ScriptGuiComponent.createFromNBT((NBTTagCompound)b);
components.put(component.getID(), component);
slots.add(component);
}