Skip to content

Commit

Permalink
Right click slots to save their positions
Browse files Browse the repository at this point in the history
  • Loading branch information
Direwolf20-MC committed Sep 7, 2024
1 parent 4bb43a4 commit ef8cf94
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
import com.direwolf20.justdirethings.client.screens.basescreens.BaseMachineScreen;
import com.direwolf20.justdirethings.common.blockentities.InventoryHolderBE;
import com.direwolf20.justdirethings.common.containers.InventoryHolderContainer;
import com.direwolf20.justdirethings.common.containers.slots.InventoryHolderSlot;
import com.direwolf20.justdirethings.common.network.data.InventoryHolderSaveSlotPayload;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.network.PacketDistributor;

public class InventoryHolderScreen extends BaseMachineScreen<InventoryHolderContainer> {
InventoryHolderBE inventoryHolderBE;
Expand Down Expand Up @@ -38,6 +47,28 @@ protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) {
super.renderLabels(guiGraphics, mouseX, mouseY);
}

@Override
protected void renderSlot(GuiGraphics guiGraphics, Slot slot) {
if (slot instanceof InventoryHolderSlot && slot.getItem().isEmpty() && inventoryHolderBE.savedItemStacks.containsKey(slot.getSlotIndex())) {
ItemStack showStack = inventoryHolderBE.savedItemStacks.get(slot.getSlotIndex());
guiGraphics.renderFakeItem(showStack, slot.x, slot.y, 0);
guiGraphics.fill(RenderType.guiGhostRecipeOverlay(), slot.x, slot.y, slot.x + 16, slot.y + 16, 0x80888888);
} else {
super.renderSlot(guiGraphics, slot);
}
}

@Override
protected void renderTooltip(GuiGraphics guiGraphics, int x, int y) {
Slot slot = this.hoveredSlot;
if (slot instanceof InventoryHolderSlot && slot.getItem().isEmpty() && inventoryHolderBE.savedItemStacks.containsKey(slot.getSlotIndex())) {
ItemStack itemstack = inventoryHolderBE.savedItemStacks.get(slot.getSlotIndex());
guiGraphics.renderTooltip(this.font, this.getTooltipFromContainerItem(itemstack), itemstack.getTooltipImage(), itemstack, x, y);
} else {
super.renderTooltip(guiGraphics, x, y);
}
}

@Override
public void saveSettings() {
super.saveSettings();
Expand All @@ -46,4 +77,14 @@ public void saveSettings() {
public void renderInventorySection(GuiGraphics guiGraphics, int relX, int relY) {
guiGraphics.blitSprite(SOCIALBACKGROUND, relX, relY + 83 - 8, this.imageWidth, this.imageHeight - 55); //Inventory Section
}

@Override
public boolean mouseClicked(double x, double y, int btn) {
if (btn == 1 && hoveredSlot != null && hoveredSlot instanceof InventoryHolderSlot) {
PacketDistributor.sendToServer(new InventoryHolderSaveSlotPayload(hoveredSlot.getSlotIndex()));
Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F));
return true;
}
return super.mouseClicked(x, y, btn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import com.direwolf20.justdirethings.common.blockentities.basebe.BaseMachineBE;
import com.direwolf20.justdirethings.setup.Registration;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

import java.util.Map;

public class InventoryHolderBE extends BaseMachineBE {
public final Map<Integer, ItemStack> savedItemStacks = new Int2ObjectOpenHashMap<>();
public InventoryHolderBE(BlockEntityType<?> pType, BlockPos pPos, BlockState pBlockState) {
super(pType, pPos, pBlockState);
MACHINE_SLOTS = 41; //Hotbar, Inventory, Armor, and Offhand
Expand All @@ -18,6 +23,15 @@ public InventoryHolderBE(BlockPos pPos, BlockState pBlockState) {
this(Registration.InventoryHolderBE.get(), pPos, pBlockState);
}

public void addSavedItem(int slot) {
ItemStack itemStack = getMachineHandler().getStackInSlot(slot).copy();
if (itemStack.isEmpty())
savedItemStacks.remove(slot);
else
savedItemStacks.put(slot, itemStack);
markDirtyClient();
}

@Override
public boolean isDefaultSettings() {
return true;
Expand All @@ -26,11 +40,38 @@ public boolean isDefaultSettings() {
@Override
public void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) {
super.saveAdditional(tag, provider);
// Create a new CompoundTag to hold all saved items
CompoundTag savedItemsTag = new CompoundTag();

// Loop through savedItemStacks and save each item
for (Map.Entry<Integer, ItemStack> entry : savedItemStacks.entrySet()) {
if (entry.getValue().isEmpty()) continue;
CompoundTag itemTag = new CompoundTag();
savedItemsTag.put(String.valueOf(entry.getKey()), entry.getValue().save(provider, itemTag)); // Save it under the slot index as the key
}

// Add this CompoundTag to the main tag
tag.put("SavedItemStacks", savedItemsTag);
}

@Override
public void loadAdditional(CompoundTag tag, HolderLookup.Provider provider) {

super.loadAdditional(tag, provider);
// Clear the savedItemStacks map to avoid duplicates
savedItemStacks.clear();

// Check if the tag contains saved items
if (tag.contains("SavedItemStacks", 10)) { // 10 is the ID for CompoundTag
CompoundTag savedItemsTag = tag.getCompound("SavedItemStacks");

// Loop through each key (slot index) in the saved items tag
for (String key : savedItemsTag.getAllKeys()) {
int slotIndex = Integer.parseInt(key);
CompoundTag itemTag = savedItemsTag.getCompound(key);
ItemStack stack = ItemStack.parse(provider, itemTag).orElse(ItemStack.EMPTY); // Load the ItemStack from the tag
if (!stack.isEmpty())
savedItemStacks.put(slotIndex, stack); // Put it back into the map
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.direwolf20.justdirethings.common.containers;

import com.direwolf20.justdirethings.common.containers.basecontainers.BaseMachineContainer;
import com.direwolf20.justdirethings.common.containers.slots.InventoryHolderSlot;
import com.direwolf20.justdirethings.setup.Registration;
import com.mojang.datafixers.util.Pair;
import net.minecraft.core.BlockPos;
Expand All @@ -16,7 +17,6 @@
import net.minecraft.world.item.enchantment.EnchantmentEffectComponents;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.SlotItemHandler;

public class InventoryHolderContainer extends BaseMachineContainer {
public static final ResourceLocation EMPTY_ARMOR_SLOT_HELMET = ResourceLocation.parse("item/empty_armor_slot_helmet");
Expand All @@ -41,9 +41,9 @@ public InventoryHolderContainer(int windowId, Inventory playerInventory, BlockPo

public void addMachineSlots() {
machineHandler = baseMachineBE.getMachineHandler();
addSlotRange(machineHandler, 0, 8, 50, 9, 18); //Hotbar
addSlotBox(machineHandler, 9, 8, -8, 9, 18, 3, 18);
addArmorSlots(machineHandler, player, 36, 44, -28);
addMachineSlotRange(machineHandler, 0, 8, 50, 9, 18); //Hotbar
addMachineSlotBox(machineHandler, 9, 8, -8, 9, 18, 3, 18);
addMachineArmorSlots(machineHandler, player, 36, 44, -28);
}

@Override
Expand All @@ -68,10 +68,28 @@ protected void addPlayerSlots(Inventory playerInventory) {
addPlayerSlots(playerInventory, 8, 102);
}

public void addArmorSlots(IItemHandler itemHandler, Player playerEntity, int index, int x, int y) {
//Overrides for custom slot
protected int addMachineSlotRange(IItemHandler handler, int index, int x, int y, int amount, int dx) {
for (int i = 0; i < amount; i++) {
addSlot(new InventoryHolderSlot(handler, index, x, y));
x += dx;
index++;
}
return index;
}

protected int addMachineSlotBox(IItemHandler handler, int index, int x, int y, int horAmount, int dx, int verAmount, int dy) {
for (int j = 0; j < verAmount; j++) {
index = addMachineSlotRange(handler, index, x, y, horAmount, dx);
y += dy;
}
return index;
}

public void addMachineArmorSlots(IItemHandler itemHandler, Player playerEntity, int index, int x, int y) {
for (int k = 0; k < 4; ++k) {
final EquipmentSlot equipmentslot = SLOT_IDS[k];
this.addSlot(new SlotItemHandler(itemHandler, index + k, x + k * 18, y) {
this.addSlot(new InventoryHolderSlot(itemHandler, index + k, x + k * 18, y) {
@Override
public int getMaxStackSize() {
return 1;
Expand All @@ -89,7 +107,7 @@ public Pair<ResourceLocation, ResourceLocation> getNoItemIcon() {
});
}

this.addSlot(new SlotItemHandler(itemHandler, index + 4, x + 4 * 18, y) {
this.addSlot(new InventoryHolderSlot(itemHandler, index + 4, x + 4 * 18, y) {
@Override
public Pair<ResourceLocation, ResourceLocation> getNoItemIcon() {
return Pair.of(InventoryMenu.BLOCK_ATLAS, EMPTY_ARMOR_SLOT_SHIELD);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.direwolf20.justdirethings.common.containers.slots;

import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.SlotItemHandler;

public class InventoryHolderSlot extends SlotItemHandler {
public InventoryHolderSlot(IItemHandler itemHandler, int index, int xPosition, int yPosition) {
super(itemHandler, index, xPosition, yPosition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static void registerNetworking(final RegisterPayloadHandlersEvent event)
registrar.playToServer(ToggleToolRefreshSlots.TYPE, ToggleToolRefreshSlots.STREAM_CODEC, ToggleToolRefreshSlotsPacket.get()::handle);
registrar.playToServer(ParadoxMachineSnapshotPayload.TYPE, ParadoxMachineSnapshotPayload.STREAM_CODEC, ParadoxSnapshotPacket.get()::handle);
registrar.playToServer(ParadoxRenderPayload.TYPE, ParadoxRenderPayload.STREAM_CODEC, ParadoxRenderPacket.get()::handle);
registrar.playToServer(InventoryHolderSaveSlotPayload.TYPE, InventoryHolderSaveSlotPayload.STREAM_CODEC, InventoryHolderSaveSlotPacket.get()::handle);

//Going to Client
registrar.playToClient(ClientSoundPayload.TYPE, ClientSoundPayload.STREAM_CODEC, ClientSoundPacket.get()::handle);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.direwolf20.justdirethings.common.network.data;

import com.direwolf20.justdirethings.JustDireThings;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;

public record InventoryHolderSaveSlotPayload(
int slot
) implements CustomPacketPayload {
public static final Type<InventoryHolderSaveSlotPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "inventory_holder_save_slot"));

@Override
public Type<InventoryHolderSaveSlotPayload> type() {
return TYPE;
}

public static final StreamCodec<FriendlyByteBuf, InventoryHolderSaveSlotPayload> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT, InventoryHolderSaveSlotPayload::slot,
InventoryHolderSaveSlotPayload::new
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.direwolf20.justdirethings.common.network.handler;

import com.direwolf20.justdirethings.common.blockentities.InventoryHolderBE;
import com.direwolf20.justdirethings.common.containers.basecontainers.BaseMachineContainer;
import com.direwolf20.justdirethings.common.network.data.InventoryHolderSaveSlotPayload;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.neoforged.neoforge.network.handling.IPayloadContext;

public class InventoryHolderSaveSlotPacket {
public static final InventoryHolderSaveSlotPacket INSTANCE = new InventoryHolderSaveSlotPacket();

public static InventoryHolderSaveSlotPacket get() {
return INSTANCE;
}

public void handle(final InventoryHolderSaveSlotPayload payload, final IPayloadContext context) {
context.enqueueWork(() -> {
Player sender = context.player();
AbstractContainerMenu container = sender.containerMenu;

if (container instanceof BaseMachineContainer baseMachineContainer && baseMachineContainer.baseMachineBE instanceof InventoryHolderBE inventoryHolderBE) {
inventoryHolderBE.addSavedItem(payload.slot());
}
});
}
}

0 comments on commit ef8cf94

Please sign in to comment.