Skip to content

Commit

Permalink
make GlitchSaveSlot also work with the new AdvancedContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
CD4017BE committed Jun 14, 2019
1 parent 5bc22d7 commit b86d6ad
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/java/cd4017be/lib/Gui/AdvancedContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public AdvancedContainer(IStateInteractionHandler handler, StateSynchronizer syn

@Override
public void detectAndSendChanges() {
if (player.world.isRemote) {
super.detectAndSendChanges();
return;
}
StateSyncServer sss = (StateSyncServer)sync;
sss.buffer.clear().writeInt(windowId);
sss.setHeader();
Expand Down Expand Up @@ -139,13 +143,21 @@ public void addItemSlot(Slot slot, boolean sync) {
this.addSlotToContainer(slot);
}

@Override
public void putStackInSlot(int slotID, ItemStack stack) {
if (slotsToSync.contains(slotID)) return;
Slot slot = inventorySlots.get(slotID);
if (slot instanceof ISpecialSlot)
((ISpecialSlot)slot).setStack(stack);
else slot.putStack(stack);
}

@Override
@SideOnly(Side.CLIENT)
public void setAll(List<ItemStack> items) {
int m = Math.min(items.size(), inventorySlots.size());
for (int i = 0; i < m; ++i)
if (!slotsToSync.contains(i))
inventorySlots.get(i).putStack(items.get(i));
putStackInSlot(i, items.get(i));
}

@Override
Expand Down
61 changes: 60 additions & 1 deletion src/java/cd4017be/lib/Gui/GlitchSaveSlot.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package cd4017be.lib.Gui;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.SlotItemHandler;

/**
* Special Slot type for use with inventories that have over-ranged stack sizes that don't fit in 8-bit
* and/or where vanilla inventory slot interaction would cause bad glitches (requires special hard coded handling in containers).
* @author CD4017BE
*/
public class GlitchSaveSlot extends SlotItemHandler {
public class GlitchSaveSlot extends SlotItemHandler implements ISpecialSlot {

public final int index;
public final boolean clientInteract;
Expand Down Expand Up @@ -45,4 +47,61 @@ public ItemStack decrStackSize(int amount) {
@Override
public void putStack(ItemStack stack) {}

// new container system

@Override
public int getSlot() {
return index;
}

@Override
public boolean insertHereOnly(ItemStack stack) {
return false;
}

@Override
public ItemStack onClick(int b, ClickType ct, EntityPlayer player, AdvancedContainer container) {
ItemStack item = getStack();
if (ct == ClickType.CLONE) {
ISpecialSlot.quickSelect(player, item);
return ItemStack.EMPTY;
} else if (ct != ClickType.PICKUP && ct != ClickType.QUICK_MOVE)
return ItemStack.EMPTY;
if (!clientInteract) {
if (player.world.isRemote)
return ItemStack.EMPTY;
container.hardInvUpdate();
}
boolean boost = ct == ClickType.QUICK_MOVE;
ItemStack curItem = player.inventory.getItemStack();
if (curItem.getCount() > 0 && (item.isEmpty() || ItemHandlerHelper.canItemStacksStack(item, curItem))) {
if (boost) {
ItemStack rem = insertItem(ItemHandlerHelper.copyStackWithSize(curItem, 65536), true);
int n = 65536 - rem.getCount(), n1 = 0;
if (n <= 0) return ItemStack.EMPTY;
if (b == 0) {
if (n < curItem.getCount()) curItem.shrink(n1 = n);
else {
n1 = curItem.getCount();
player.inventory.setItemStack(ItemStack.EMPTY);
}
}
if (n1 < n)
n1 += ISpecialSlot.getFromPlayerInv(ItemHandlerHelper.copyStackWithSize(curItem, n - n1), player.inventory);
insertItem(ItemHandlerHelper.copyStackWithSize(curItem, n1), false);
} else {
int n = b == 0 ? curItem.getCount() : 1;
ItemStack rem = insertItem(ItemHandlerHelper.copyStackWithSize(curItem, n), false);
curItem.shrink(n - rem.getCount());
if (curItem.getCount() <= 0) player.inventory.setItemStack(ItemStack.EMPTY);
}
} else if (item.getCount() > 0) {
int n = boost ? (b == 0 ? item.getMaxStackSize() : 65536) : (b == 0 ? 1 : 8);
if ((item = extractItem(n, true)).getCount() == 0) return ItemStack.EMPTY;
int rem = ISpecialSlot.putInPlayerInv(item.copy(), player.inventory);
extractItem(item.getCount() - rem, false);
}
return ItemStack.EMPTY;
}

}

0 comments on commit b86d6ad

Please # to comment.