Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
chore: give proper thought to trash can inventory logic
Browse files Browse the repository at this point in the history
I rewrote the majority of the damn thing!
  • Loading branch information
Giggitybyte committed Feb 8, 2021
1 parent e6b13c6 commit 107c302
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 22 deletions.
3 changes: 1 addition & 2 deletions src/main/java/me/thegiggitybyte/ttc/block/TrashCanBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
player.openHandledScreen(screenHandlerFactory);
world.playSound(null, pos,
SoundEvents.BLOCK_IRON_TRAPDOOR_OPEN,
SoundCategory.BLOCKS,
0.9f, 1.3f);
SoundCategory.BLOCKS, 0.9f, 1.3f);
}
}

Expand Down
103 changes: 83 additions & 20 deletions src/main/java/me/thegiggitybyte/ttc/gui/TrashCanGuiDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import me.thegiggitybyte.ttc.TinyTrashCan;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandlerContext;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
Expand Down Expand Up @@ -37,30 +39,12 @@ public TrashCanGuiDescription(int syncId, PlayerInventory playerInventory, Scree
@Override
public ItemStack onSlotClick(int slotIndex, int button, SlotActionType action, PlayerEntity player) {
Slot trashSlot = this.slots.get(0);
ItemStack trashSlotStack = trashSlot.getStack();
boolean wasItemDeleted = false;

if ((slotIndex == 0) && ((action == SlotActionType.PICKUP) & (button == 0))) {
ItemStack cursorStack = player.inventory.getCursorStack();

if (!cursorStack.isEmpty() && !trashSlotStack.isEmpty()) {
trashSlot.setStack(cursorStack);
player.inventory.setCursorStack(ItemStack.EMPTY);

wasItemDeleted = true;
}

wasItemDeleted = tryCursorDelete(trashSlot, player);
} else if ((action == SlotActionType.QUICK_MOVE) && (slotIndex != 0)) { // Shift-click.
Slot selectedSlot = this.slots.get(slotIndex);
ItemStack selectedStack = selectedSlot.getStack();

if (!trashSlotStack.isEmpty() && !selectedStack.isEmpty()) {
trashSlot.setStack(selectedStack);
selectedSlot.setStack(ItemStack.EMPTY);

selectedSlot.markDirty();
wasItemDeleted = true;
}
wasItemDeleted = tryQuickDelete(trashSlot, this.slots.get(slotIndex));
}

if (wasItemDeleted) {
Expand All @@ -84,4 +68,83 @@ public void close(PlayerEntity player) {
SoundEvents.BLOCK_IRON_TRAPDOOR_CLOSE,
SoundCategory.BLOCKS, 0.9f, 1.3f);
}

private boolean tryCursorDelete(Slot trashSlot, PlayerEntity player) {
ItemStack cursorStack = player.inventory.getCursorStack();
ItemStack trashStack = trashSlot.getStack();

if (cursorStack.isEmpty() | trashStack.isEmpty() |
trashStack.getItem() == Items.AIR) {
return false;
}

if (canStacksCombine(cursorStack, trashStack)) {
if (trashStack.getCount() >= trashStack.getMaxCount()) {
trashSlot.setStack(cursorStack);
player.inventory.setCursorStack(ItemStack.EMPTY);

return true;
}

Item stackItem = trashStack.getItem();
int maxStackCount = stackItem.getMaxCount();

int combinedStackCount = cursorStack.getCount() + trashStack.getCount();
if (combinedStackCount > maxStackCount) {
int remainder = combinedStackCount - maxStackCount;

trashSlot.setStack(new ItemStack(stackItem, maxStackCount));
player.inventory.setCursorStack(new ItemStack(stackItem, remainder));
}

return false;

} else {
trashSlot.setStack(cursorStack);
player.inventory.setCursorStack(ItemStack.EMPTY);

return true;
}
}

private boolean tryQuickDelete(Slot trashSlot, Slot originSlot) {
ItemStack originStack = originSlot.getStack();
ItemStack trashStack = trashSlot.getStack();

if (originStack.isEmpty() | trashStack.isEmpty() |
trashStack.getItem() == Items.AIR) {
return false;
}

if (canStacksCombine(originStack, trashStack)) {
if (trashStack.getCount() >= trashStack.getMaxCount()) {
trashSlot.setStack(originStack);
originSlot.setStack(ItemStack.EMPTY);
originSlot.markDirty();

return true;
}

Item stackItem = trashStack.getItem();
int maxStackCount = stackItem.getMaxCount();

int combinedStackCount = originStack.getCount() + trashStack.getCount();
if (combinedStackCount > maxStackCount) {
int remainder = combinedStackCount - maxStackCount;

trashSlot.setStack(new ItemStack(stackItem, maxStackCount));
originSlot.setStack(new ItemStack(stackItem, remainder));
originSlot.markDirty();
}

return false;

} else {
trashSlot.setStack(originStack);
originSlot.setStack(ItemStack.EMPTY);
originSlot.markDirty();

return true;
}
}
}

0 comments on commit 107c302

Please sign in to comment.