Skip to content

Commit

Permalink
fix(network): modify which tools tools are removed from/placed into t…
Browse files Browse the repository at this point in the history
…he belt
  • Loading branch information
Jamalam360 committed Apr 3, 2024
1 parent 14ea912 commit 89ecc79
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item> ITEMS = DeferredRegister.create(MOD_ID, Registries.ITEM);
public static final RegistrySupplier<Item> UTILITY_BELT = ITEMS.register("utility_belt", UtilityBeltItem::new);
public static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(MOD_ID, Registries.MENU);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
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;
import net.fabricmc.api.EnvType;
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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
});
Expand Down

0 comments on commit 89ecc79

Please sign in to comment.