Skip to content

Commit

Permalink
Swapping Buttons Implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Direwolf20-MC committed Sep 8, 2024
1 parent 0c51dae commit 0b24b8f
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.21.1 2024-09-07T23:05:26.591705 Languages: en_us for mod: justdirethings
9d7dc513a32bcfb7d38be2eec9788707e931d062 assets/justdirethings/lang/en_us.json
// 1.21.1 2024-09-07T23:32:11.7388079 Languages: en_us for mod: justdirethings
6df045e49de32b41b0c0ae7b9f14dbd59d6042d3 assets/justdirethings/lang/en_us.json
3 changes: 3 additions & 0 deletions src/generated/resources/assets/justdirethings/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@
"justdirethings.screen.paradoxentity": "Revert Entities",
"justdirethings.screen.paradoxfluidcost": "Fluid Cost: %s mb",
"justdirethings.screen.pickupdelay": "Pickup Delay (Ticks)",
"justdirethings.screen.pullitems": "Pull Items",
"justdirethings.screen.pulse": "Pulse",
"justdirethings.screen.redstone-strong": "Strong Signal",
"justdirethings.screen.redstone-weak": "Weak Signal",
Expand All @@ -409,6 +410,7 @@
"justdirethings.screen.requireequipped": "Activate if Equipped",
"justdirethings.screen.rightclicksettings": "Right Click for Settings",
"justdirethings.screen.save_close": "Save and Close",
"justdirethings.screen.senditems": "Push Items",
"justdirethings.screen.senseamount": "Sense Amount",
"justdirethings.screen.setbinding": "Set Binding",
"justdirethings.screen.showfakeplayer": "Show Fake Player",
Expand All @@ -417,6 +419,7 @@
"justdirethings.screen.snapshotarea": "Snapshot Area",
"justdirethings.screen.sneak-click": "Sneak Click",
"justdirethings.screen.stay_open": "Stay Open",
"justdirethings.screen.swapitems": "Swap Items",
"justdirethings.screen.target-adult": "Target Adult",
"justdirethings.screen.target-air": "Target Air",
"justdirethings.screen.target-block": "Target Blocks",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.InventoryHolderMoveItemsPayload;
import com.direwolf20.justdirethings.common.network.data.InventoryHolderSaveSlotPayload;
import com.direwolf20.justdirethings.common.network.data.InventoryHolderSettingsPayload;
import com.mojang.blaze3d.systems.RenderSystem;
Expand Down Expand Up @@ -58,6 +59,15 @@ public void init() {
((GrayscaleButton) b).toggleActive();
saveSettings();
}));
addRenderableWidget(ToggleButtonFactory.SEND_INV_BUTTON(getGuiLeft() + 26, topSectionTop + 132, b -> {
PacketDistributor.sendToServer(new InventoryHolderMoveItemsPayload(0));
}));
addRenderableWidget(ToggleButtonFactory.PULL_INV_BUTTON(getGuiLeft() + 134, topSectionTop + 132, b -> {
PacketDistributor.sendToServer(new InventoryHolderMoveItemsPayload(1));
}));
addRenderableWidget(ToggleButtonFactory.SWAP_INV_BUTTON(getGuiLeft() + 152, topSectionTop + 132, b -> {
PacketDistributor.sendToServer(new InventoryHolderMoveItemsPayload(2));
}));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,30 @@ public static ToggleButton SNAPSHOT_AREA_BUTTON(int x, int y, Button.OnPress onP
return new ToggleButton(x, y, STANDARD_WIDTH, STANDARD_HEIGHT, SNAPSHOT_AREA_BUTTON, 0, onPress);
}

private static final List<TextureLocalization> SEND_INV_BUTTON = List.of(
new TextureLocalization(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/senditems.png"), Component.translatable("justdirethings.screen.senditems"))
);

public static ToggleButton SEND_INV_BUTTON(int x, int y, Button.OnPress onPress) {
return new ToggleButton(x, y, STANDARD_WIDTH, STANDARD_HEIGHT, SEND_INV_BUTTON, 0, onPress);
}

private static final List<TextureLocalization> PULL_INV_BUTTON = List.of(
new TextureLocalization(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/pullitems.png"), Component.translatable("justdirethings.screen.pullitems"))
);

public static ToggleButton PULL_INV_BUTTON(int x, int y, Button.OnPress onPress) {
return new ToggleButton(x, y, STANDARD_WIDTH, STANDARD_HEIGHT, PULL_INV_BUTTON, 0, onPress);
}

private static final List<TextureLocalization> SWAP_INV_BUTTON = List.of(
new TextureLocalization(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/swapitems.png"), Component.translatable("justdirethings.screen.swapitems"))
);

public static ToggleButton SWAP_INV_BUTTON(int x, int y, Button.OnPress onPress) {
return new ToggleButton(x, y, STANDARD_WIDTH, STANDARD_HEIGHT, SWAP_INV_BUTTON, 0, onPress);
}

private static final ResourceLocation COPY_AREA_BUTTON = ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/area.png");
private static final Component COPY_AREA_LOCALIZATION = Component.translatable("justdirethings.screen.copy_area");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,57 @@ public boolean stillValid(Player playerIn) {
return stillValid(ContainerLevelAccess.create(player.level(), pos), player, Registration.InventoryHolder.get());
}

public void sendAllItemsToMachine() {
for (int i = MACHINE_SLOTS; i < MACHINE_SLOTS + Inventory.INVENTORY_SIZE + 5; i++) {
quickMoveStack(this.player, i); // Move each stack from player's inventory to the machine
}
}

public void sendAllItemsToPlayer() {
// Loop over all the machine slots
for (int i = 0; i < MACHINE_SLOTS; i++) {
// Try to move each item from the machine slots to the player's inventory
quickMoveStack(this.player, i); // Move the item from machine slot to player's inventory
}
}

public void swapItems() {
// Loop through the player's inventory slots
for (int playerSlot = MACHINE_SLOTS; playerSlot < MACHINE_SLOTS + Inventory.INVENTORY_SIZE + 5; playerSlot++) {
Slot playerInventorySlot = this.slots.get(playerSlot);
ItemStack playerStack = playerInventorySlot.getItem(); // Get the item from the player's inventory slot

// Calculate the corresponding machine slot index
int machineSlot = playerSlot - MACHINE_SLOTS;
if (machineSlot >= MACHINE_SLOTS) {
// Out of bounds, skip
continue;
}

Slot machineInventorySlot = this.slots.get(machineSlot);
ItemStack machineStack = machineInventorySlot.getItem(); // Copy the Machine's Item Stack
if (playerStack.isEmpty() && machineStack.isEmpty()) continue;
ItemStack machineStackCopy = machineStack.copy();
ItemStack playerStackCopy = playerStack.copy();

// Try to insert the player's item into the machine
if (!playerStack.isEmpty()) {
machineInventorySlot.set(ItemStack.EMPTY); //Temporarily clear the Machine's Item Stack
if (moveItemStackTo(playerStack, machineSlot, machineSlot + 1, false) && playerStack.isEmpty()) { //Try to insert into the exact slot, ensure the whole thing fits
// If the insertion was successful, swap the items
playerInventorySlot.set(machineStackCopy); // Place the machine item in the player's slot
machineInventorySlot.setChanged();
playerInventorySlot.setChanged();
} else {
machineInventorySlot.set(machineStackCopy); //Set the Machine Slot back
playerInventorySlot.set(playerStackCopy); //Do this incase there was a partial insert above
}
} else if (!machineStack.isEmpty()) {
moveItemStackTo(machineStack, playerSlot, playerSlot + 1, false);
}
}
}

@Override
public ItemStack quickMoveStack(Player playerIn, int index) {
ItemStack itemstack = ItemStack.EMPTY;
Expand All @@ -66,7 +117,7 @@ public ItemStack quickMoveStack(Player playerIn, int index) {
if (index < MACHINE_SLOTS) { //Machine Slots to Player Inventory
if (this.moveItemStackTo(currentStack, index + MACHINE_SLOTS, index + MACHINE_SLOTS + 1, false)) {
//No-Op
} else if (!this.moveItemStackTo(currentStack, MACHINE_SLOTS, MACHINE_SLOTS + Inventory.INVENTORY_SIZE, true)) {
} else if (!this.moveItemStackTo(currentStack, MACHINE_SLOTS, MACHINE_SLOTS + Inventory.INVENTORY_SIZE + 5, true)) {
return ItemStack.EMPTY;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static void registerNetworking(final RegisterPayloadHandlersEvent event)
registrar.playToServer(ParadoxRenderPayload.TYPE, ParadoxRenderPayload.STREAM_CODEC, ParadoxRenderPacket.get()::handle);
registrar.playToServer(InventoryHolderSaveSlotPayload.TYPE, InventoryHolderSaveSlotPayload.STREAM_CODEC, InventoryHolderSaveSlotPacket.get()::handle);
registrar.playToServer(InventoryHolderSettingsPayload.TYPE, InventoryHolderSettingsPayload.STREAM_CODEC, InventoryHolderSettingsPacket.get()::handle);
registrar.playToServer(InventoryHolderMoveItemsPayload.TYPE, InventoryHolderMoveItemsPayload.STREAM_CODEC, InventoryHolderMoveItemsPacket.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 InventoryHolderMoveItemsPayload(
int moveType
) implements CustomPacketPayload {
public static final Type<InventoryHolderMoveItemsPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "inventory_holder_move_items"));

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

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

import com.direwolf20.justdirethings.common.containers.InventoryHolderContainer;
import com.direwolf20.justdirethings.common.network.data.InventoryHolderMoveItemsPayload;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.neoforged.neoforge.network.handling.IPayloadContext;

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

public static InventoryHolderMoveItemsPacket get() {
return INSTANCE;
}

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

if (container instanceof InventoryHolderContainer inventoryHolderContainer) {
int moveType = payload.moveType();
if (moveType == 0)
inventoryHolderContainer.sendAllItemsToMachine();
else if (moveType == 1)
inventoryHolderContainer.sendAllItemsToPlayer();
else if (moveType == 2)
inventoryHolderContainer.swapItems();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ protected void addTranslations() {
add("justdirethings.screen.paradoxall", "Revert Blocks and Entities");
add("justdirethings.screen.paradoxenergycost", "Energy Cost: %s FE");
add("justdirethings.screen.paradoxfluidcost", "Fluid Cost: %s mb");
add("justdirethings.screen.senditems", "Push Items");
add("justdirethings.screen.pullitems", "Pull Items");
add("justdirethings.screen.swapitems", "Swap Items");

//Buttons
//add("justdirethings.buttons.save", "Save");
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0b24b8f

Please sign in to comment.