From 89ecc79d22938d05df0d4df39dd58281539d3826 Mon Sep 17 00:00:00 2001 From: "James (Jamalam)" Date: Wed, 3 Apr 2024 15:25:08 +0100 Subject: [PATCH] fix(network): modify which tools tools are removed from/placed into the belt --- CHANGELOG.md | 5 ++--- .../github/jamalam360/utility_belt/UtilityBelt.java | 2 ++ .../utility_belt/client/ClientNetworking.java | 13 ++++++++++++- .../utility_belt/server/ServerNetworking.java | 12 ++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3fc937..143439a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,4 @@ -- Initial multiloader port. -- Feature parity with original Utility Belt, more features planned once the port is stable. -- Hopefully more stable than the original Utility Belt due to the complete rewrite, but bugs may still occur while we are in beta. +- Fix item rendering in hotbar being 1 pixel to the left +- When moving to or from the belt where there is an empty slot to place the item you are moving, you will now move to that empty slot. ## **WARNING**: Utility Belt 2.0.0 is currently in a **BETA STATE**: backup your worlds and tread with caution! Please report any issues. diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBelt.java b/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBelt.java index 95c5d73..5b93bc9 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBelt.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBelt.java @@ -35,6 +35,8 @@ public class UtilityBelt { public static final ResourceLocation C2S_UPDATE_STATE = id("update_state"); public static final ResourceLocation C2S_OPEN_SCREEN = id("open_screen"); public static final ResourceLocation S2C_UPDATE_INV = id("update_inv"); + public static final ResourceLocation S2C_SET_BELT_SLOT = id("set_belt_slot"); + public static final ResourceLocation S2C_SET_HOTBAR_SLOT = id("set_hotbar_slot"); private static final DeferredRegister ITEMS = DeferredRegister.create(MOD_ID, Registries.ITEM); public static final RegistrySupplier UTILITY_BELT = ITEMS.register("utility_belt", UtilityBeltItem::new); public static final DeferredRegister> MENUS = DeferredRegister.create(MOD_ID, Registries.MENU); diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/client/ClientNetworking.java b/common/src/main/java/io/github/jamalam360/utility_belt/client/ClientNetworking.java index 7817703..3063a2f 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/client/ClientNetworking.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/client/ClientNetworking.java @@ -1,6 +1,7 @@ package io.github.jamalam360.utility_belt.client; import dev.architectury.networking.NetworkManager; +import io.github.jamalam360.utility_belt.StateManager; import io.github.jamalam360.utility_belt.UtilityBelt; import io.github.jamalam360.utility_belt.UtilityBeltItem; import io.netty.buffer.Unpooled; @@ -8,13 +9,14 @@ import net.fabricmc.api.Environment; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; @Environment(EnvType.CLIENT) public class ClientNetworking { public static void init() { NetworkManager.registerReceiver(NetworkManager.Side.S2C, UtilityBelt.S2C_UPDATE_INV, ClientNetworking::handleUpdateInventory); + NetworkManager.registerReceiver(NetworkManager.Side.S2C, UtilityBelt.S2C_SET_BELT_SLOT, ClientNetworking::handleSetBeltSlot); + NetworkManager.registerReceiver(NetworkManager.Side.S2C, UtilityBelt.S2C_SET_HOTBAR_SLOT, ClientNetworking::handleSetHotbarSlot); } public static void sendNewStateToServer(boolean inBelt, int slot, boolean swap) { @@ -34,4 +36,13 @@ private static void handleUpdateInventory(FriendlyByteBuf buf, NetworkManager.Pa ItemStack belt = UtilityBeltItem.getBelt(ctx.getPlayer()); belt.getTag().put("Inventory", tag.getList("Inventory", CompoundTag.TAG_COMPOUND)); } + + private static void handleSetBeltSlot(FriendlyByteBuf buf, NetworkManager.PacketContext ctx) { + int slot = buf.readInt(); + StateManager.getClientInstance().setSelectedBeltSlot(ctx.getPlayer(), slot); + } + + private static void handleSetHotbarSlot(FriendlyByteBuf buf, NetworkManager.PacketContext ctx) { + ctx.getPlayer().getInventory().selected = buf.readInt(); + } } diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/server/ServerNetworking.java b/common/src/main/java/io/github/jamalam360/utility_belt/server/ServerNetworking.java index 146749c..e9218da 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/server/ServerNetworking.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/server/ServerNetworking.java @@ -73,6 +73,18 @@ private static void handleUpdateState(FriendlyByteBuf buf, NetworkManager.Packet ctx.getPlayer().getInventory().setItem(hotbarSlot, stackInBelt); inv.setItem(beltSlot, stackInHand); ((Duck.LivingEntity) ctx.getPlayer()).utilitybelt$detectEquipmentUpdates(); + + if (beltSlot != slot) { + stateManager.setSelectedBeltSlot(ctx.getPlayer(), beltSlot); + FriendlyByteBuf buf2 = new FriendlyByteBuf(Unpooled.buffer()); + buf2.writeInt(beltSlot); + NetworkManager.sendToPlayer((ServerPlayer) ctx.getPlayer(), UtilityBelt.S2C_SET_BELT_SLOT, buf2); + } else if (hotbarSlot != ctx.getPlayer().getInventory().selected) { + ctx.getPlayer().getInventory().selected = hotbarSlot; + FriendlyByteBuf buf2 = new FriendlyByteBuf(Unpooled.buffer()); + buf2.writeInt(hotbarSlot); + NetworkManager.sendToPlayer((ServerPlayer) ctx.getPlayer(), UtilityBelt.S2C_SET_HOTBAR_SLOT, buf2); + } } } });