diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f61730..febef1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,11 @@ -- Fix a dupe exploit (#44) +This release brings about a significant internal rework with should make Utility Belt significantly +less buggy. I have conducted a long play testing session before this release to try to verify +everything works as intended. + +## Closed Issues + +- #47: fishing rod flickering +- #46: dupe bug +- #45: dupe bug + +Alongside the issues that were reported, many others have been fixed. diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/StateManager.java b/common/src/main/java/io/github/jamalam360/utility_belt/StateManager.java index 8462dfa..a009f47 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/StateManager.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/StateManager.java @@ -28,9 +28,6 @@ public boolean hasBelt(Player player) { return belt != null && belt.is(UtilityBelt.UTILITY_BELT_ITEM.get()); } - public void onStartTick(Player player) { - } - public abstract boolean isInBelt(Player player); public abstract void setInBelt(Player player, boolean inBelt); @@ -40,4 +37,10 @@ public void onStartTick(Player player) { public abstract void setSelectedBeltSlot(Player player, int slot); public abstract UtilityBeltInventory getInventory(Player player); + + public UtilityBeltInventory.Mutable getMutableInventory(Player player) { + return new UtilityBeltInventory.Mutable(this.getInventory(player)); + } + + public abstract void setInventory(Player player, UtilityBeltInventory.Mutable inventory); } 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 e49bf83..aff63e4 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 @@ -42,7 +42,7 @@ public class UtilityBelt { public static final RegistrySupplier UTILITY_BELT_ITEM = ITEMS.register("utility_belt", UtilityBeltItem::new); public static final RegistrySupplier> MENU_TYPE = MENUS.register("utility_belt", () -> new MenuType<>(UtilityBeltMenu::new, FeatureFlagSet.of())); public static final RegistrySupplier> UTILITY_BELT_INVENTORY_COMPONENT_TYPE = COMPONENT_TYPES.register("utility_belt_inventory", () -> - DataComponentType.builder().persistent(UtilityBeltInventory.CODEC).networkSynchronized(UtilityBeltInventory.STREAM_CODEC).build() + DataComponentType.builder().persistent(UtilityBeltInventory.CODEC).cacheEncoding().build() ); public static void init() { diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltInventory.java b/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltInventory.java index 76d0af6..7bc53cb 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltInventory.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltInventory.java @@ -1,73 +1,110 @@ package io.github.jamalam360.utility_belt; import com.mojang.serialization.Codec; +import java.util.ArrayList; import java.util.List; +import net.minecraft.core.NonNullList; +import net.minecraft.core.component.DataComponents; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.world.SimpleContainer; import net.minecraft.world.item.ItemStack; -public class UtilityBeltInventory extends SimpleContainer { +public record UtilityBeltInventory(List items) { + public static final UtilityBeltInventory EMPTY = new UtilityBeltInventory(NonNullList.withSize(4, ItemStack.EMPTY)); public static final Codec CODEC = Codec - .list(ItemStack.CODEC) - .xmap(UtilityBeltInventory::new, UtilityBeltInventory::getItems); + .list(ItemStack.OPTIONAL_CODEC) + .xmap(UtilityBeltInventory::new, UtilityBeltInventory::items); public static final StreamCodec STREAM_CODEC = StreamCodec .composite( - ItemStack.STREAM_CODEC.apply(ByteBufCodecs.list(4)), - UtilityBeltInventory::getItems, + ItemStack.OPTIONAL_STREAM_CODEC.apply(ByteBufCodecs.list(4)), + UtilityBeltInventory::items, UtilityBeltInventory::new ); + public UtilityBeltInventory { + if (items.size() != 4) { + throw new IllegalArgumentException("Utility belt inventory must have exactly 4 items"); + } + } - public UtilityBeltInventory() { - super(4); + public ItemStack getItem(int index) { + return items.get(index); } - private UtilityBeltInventory(List stacks) { - super(4); + public int getContainerSize() { + return 4; + } - for (int i = 0; i < stacks.size(); i++) { - this.setItem(i, stacks.get(i)); - } + public UtilityBeltInventory clone() { + return new UtilityBeltInventory(new ArrayList<>(items)); } @Override public boolean equals(Object obj) { if (obj instanceof UtilityBeltInventory other) { - if (other.getContainerSize() == this.getContainerSize()) { - for (int i = 0; i < this.getContainerSize(); i++) { - if (!ItemStack.matches(this.getItem(i), other.getItem(i))) { - return false; - } - } - - return true; - } + return ItemStack.listMatches(items, other.items); } return false; } + @SuppressWarnings("deprecation") @Override public int hashCode() { - int hash = 0; - for (int i = 0; i < this.getContainerSize(); i++) { - hash += this.getItem(i).hashCode(); + return ItemStack.hashStackList(items); + } + + @Override + public String toString() { + return invToString("UtilityBeltInventory[", items); + } + + public static class Mutable extends SimpleContainer { + + public Mutable(UtilityBeltInventory inv) { + super(4); + + for (int i = 0; i < inv.getContainerSize(); i++) { + this.setItem(i, inv.getItem(i)); + } + } + + public UtilityBeltInventory toImmutable() { + return new UtilityBeltInventory(new ArrayList<>(this.getItems())); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof UtilityBeltInventory other) { + return ItemStack.listMatches(this.getItems(), other.items); + } + + return false; } - return hash; + @Override + public String toString() { + return invToString("UtilityBeltInventory$Mutable[", this.getItems()); + } } - @Override - public UtilityBeltInventory clone() { - UtilityBeltInventory inv = new UtilityBeltInventory(); - for (int i = 0; i < this.getContainerSize(); i++) { - inv.setItem(i, this.getItem(i).copy()); + private static String invToString(String prefix, List items) { + StringBuilder string = new StringBuilder(prefix); + + for (int i = 0; i < items.size(); i++) { + string.append(items.get(i)); + string.append(" {"); + string.append(items.get(i).getComponents().get(DataComponents.DAMAGE)); + string.append("}"); + if (i < items.size() - 1) { + string.append(", "); + } } - return inv; + return string.toString(); } } diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltItem.java b/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltItem.java index 1e33f29..90cbd86 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltItem.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltItem.java @@ -1,5 +1,6 @@ package io.github.jamalam360.utility_belt; +import io.github.jamalam360.utility_belt.UtilityBeltInventory.Mutable; import io.github.jamalam360.utility_belt.client.ClientNetworking; import java.util.List; import java.util.function.Consumer; @@ -7,6 +8,7 @@ import net.minecraft.sounds.SoundEvents; import net.minecraft.util.Mth; import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.SlotAccess; import net.minecraft.world.entity.player.Player; @@ -30,11 +32,11 @@ public class UtilityBeltItem extends Item { private static final int BAR_COLOR = Mth.color(0.4F, 0.4F, 1.0F); public UtilityBeltItem() { - super(new Item.Properties().stacksTo(1).component(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get(), new UtilityBeltInventory())); + super(new Item.Properties().stacksTo(1).component(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get(), UtilityBeltInventory.EMPTY)); } @SuppressWarnings("BooleanMethodIsAlwaysInverted") - private static boolean handleStack(ItemStack stack, UtilityBeltInventory inv, Consumer slotAccess) { + private static boolean handleStack(ItemStack stack, UtilityBeltInventory.Mutable inv, Consumer slotAccess) { if (stack.isEmpty()) { for (int i = 0; i < inv.getContainerSize(); i++) { if (!inv.getItem(i).isEmpty()) { @@ -67,12 +69,13 @@ public static boolean isValidItem(ItemStack stack) { // Going to keep this name as is until 1.20.4 support is dropped, to keep the diffs smaller public static UtilityBeltInventory getInventoryFromTag(ItemStack stack) { if (!stack.has(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get())) { - stack.set(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get(), new UtilityBeltInventory()); + stack.set(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get(), UtilityBeltInventory.EMPTY); } return stack.get(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get()); } + // This should only be called by the state managers, or when the belt is NOT equipped public static void setInventory(ItemStack stack, UtilityBeltInventory inv) { stack.set(UtilityBelt.UTILITY_BELT_INVENTORY_COMPONENT_TYPE.get(), inv); } @@ -90,12 +93,12 @@ public static ItemStack getBelt(Player player) { @Override public boolean isBarVisible(ItemStack itemStack) { - return getInventoryFromTag(itemStack).hasAnyMatching(s -> !s.isEmpty()); + return getInventoryFromTag(itemStack).items().stream().anyMatch(s -> !s.isEmpty()); } @Override public int getBarWidth(ItemStack itemStack) { - long size = getInventoryFromTag(itemStack).getItems().stream().filter((s) -> !s.isEmpty()).count(); + long size = getInventoryFromTag(itemStack).items().stream().filter((s) -> !s.isEmpty()).count(); return size == 4L ? 13 : (int) (size * 3); } @@ -123,14 +126,13 @@ public boolean overrideStackedOnOther(ItemStack belt, Slot slot, ClickAction cli } ItemStack slotStack = slot.getItem(); - UtilityBeltInventory inv = getInventoryFromTag(belt); + UtilityBeltInventory.Mutable inv = new Mutable(getInventoryFromTag(belt)); if (!handleStack(slotStack, inv, slot::set)) { return false; } - playInsertSound(player); - setInventory(belt, inv); + setInventory(belt, inv.toImmutable()); return true; } @@ -140,17 +142,23 @@ public boolean overrideOtherStackedOnMe(ItemStack belt, ItemStack otherStack, Sl return false; } - UtilityBeltInventory inv = getInventoryFromTag(belt); + UtilityBeltInventory.Mutable inv = new Mutable(getInventoryFromTag(belt)); if (!handleStack(otherStack, inv, slotAccess::set)) { return false; } playInsertSound(player); - setInventory(belt, inv); + setInventory(belt, inv.toImmutable()); return true; } + public void onEquip(LivingEntity wearer, ItemStack belt) { + if (wearer instanceof Player player && !player.level().isClientSide) { + StateManager.getServerInstance().setInventory(player, new Mutable(getInventoryFromTag(belt))); + } + } + public void onUnequip(LivingEntity wearer) { if (wearer instanceof Player player && player.level().isClientSide) { StateManager.getClientInstance().setInBelt(player, false); diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltPackets.java b/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltPackets.java index 21015d3..dd2190c 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltPackets.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/UtilityBeltPackets.java @@ -10,6 +10,7 @@ public class UtilityBeltPackets { public static final CustomPacketPayload.Type C2S_OPEN_SCREEN = new CustomPacketPayload.Type<>(UtilityBelt.id("open_screen")); public static final CustomPacketPayload.Type S2C_SET_BELT_SLOT = new CustomPacketPayload.Type<>(UtilityBelt.id("set_belt_slot")); public static final CustomPacketPayload.Type S2C_SET_HOTBAR_SLOT = new CustomPacketPayload.Type<>(UtilityBelt.id("set_hotbar_slot")); + public static final CustomPacketPayload.Type S2C_UPDATE_BELT_INVENTORY = new CustomPacketPayload.Type<>(UtilityBelt.id("update_belt_inventory")); public record C2SUpdateState(boolean inBelt, int slot, boolean swapItems) implements CustomPacketPayload { @@ -67,4 +68,14 @@ public Type type() { return S2C_SET_HOTBAR_SLOT; } } + + public record S2CUpdateBeltInventory(UtilityBeltInventory inventory) implements CustomPacketPayload { + + public static final StreamCodec STREAM_CODEC = UtilityBeltInventory.STREAM_CODEC.map(S2CUpdateBeltInventory::new, S2CUpdateBeltInventory::inventory); + + @Override + public Type type() { + return new CustomPacketPayload.Type<>(UtilityBelt.id("update_belt_inventory")); + } + } } diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/client/BeltHotbarRenderer.java b/common/src/main/java/io/github/jamalam360/utility_belt/client/BeltHotbarRenderer.java index 1987332..03d2120 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/client/BeltHotbarRenderer.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/client/BeltHotbarRenderer.java @@ -1,7 +1,6 @@ package io.github.jamalam360.utility_belt.client; import com.mojang.blaze3d.systems.RenderSystem; -import io.github.jamalam360.utility_belt.Config; import io.github.jamalam360.utility_belt.StateManager; import io.github.jamalam360.utility_belt.UtilityBelt; import io.github.jamalam360.utility_belt.UtilityBeltInventory; @@ -16,64 +15,65 @@ @Environment(EnvType.CLIENT) public class BeltHotbarRenderer { - private static final ResourceLocation UTILITY_BELT_WIDGET_TEXTURE = UtilityBelt - .id("textures/gui/utility_belt_widget.png"); - private static final ResourceLocation HOTBAR_SELECTION_SPRITE = new ResourceLocation("hud/hotbar_selection"); - public static void render(GuiGraphics graphics, float tickDelta) { - Player player = Minecraft.getInstance().player; - StateManager stateManager = StateManager.getClientInstance(); + private static final ResourceLocation UTILITY_BELT_WIDGET_TEXTURE = UtilityBelt + .id("textures/gui/utility_belt_widget.png"); + private static final ResourceLocation HOTBAR_SELECTION_SPRITE = new ResourceLocation("hud/hotbar_selection"); - if (player != null && stateManager.hasBelt(player) && (stateManager.isInBelt(player) - || UtilityBelt.CONFIG.get().displayUtilityBeltWhenNotSelected)) { - int scaledHeight = Minecraft.getInstance().getWindow().getGuiScaledHeight(); - int x = switch (UtilityBelt.CONFIG.get().hotbarPosition) { - case TOP_LEFT, MIDDLE_LEFT, BOTTOM_LEFT -> 2; - case TOP_RIGHT, MIDDLE_RIGHT, BOTTOM_RIGHT -> Minecraft.getInstance().getWindow().getGuiScaledWidth() - 24; - }; - int y = switch (UtilityBelt.CONFIG.get().hotbarPosition) { - case TOP_LEFT, TOP_RIGHT -> 2; - case MIDDLE_LEFT, MIDDLE_RIGHT -> scaledHeight / 2 - 44; - case BOTTOM_LEFT, BOTTOM_RIGHT -> scaledHeight - 84; - }; + public static void render(GuiGraphics graphics, float tickDelta) { + Player player = Minecraft.getInstance().player; + StateManager stateManager = StateManager.getClientInstance(); - RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); - RenderSystem.setShader(GameRenderer::getPositionTexShader); + if (player != null && stateManager.hasBelt(player) && (stateManager.isInBelt(player) + || UtilityBelt.CONFIG.get().displayUtilityBeltWhenNotSelected)) { + int scaledHeight = Minecraft.getInstance().getWindow().getGuiScaledHeight(); + int x = switch (UtilityBelt.CONFIG.get().hotbarPosition) { + case TOP_LEFT, MIDDLE_LEFT, BOTTOM_LEFT -> 2; + case TOP_RIGHT, MIDDLE_RIGHT, BOTTOM_RIGHT -> Minecraft.getInstance().getWindow().getGuiScaledWidth() - 24; + }; + int y = switch (UtilityBelt.CONFIG.get().hotbarPosition) { + case TOP_LEFT, TOP_RIGHT -> 2; + case MIDDLE_LEFT, MIDDLE_RIGHT -> scaledHeight / 2 - 44; + case BOTTOM_LEFT, BOTTOM_RIGHT -> scaledHeight - 84; + }; - graphics.blit(UTILITY_BELT_WIDGET_TEXTURE, x, y, 0, 0, 22, 88); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); + RenderSystem.setShader(GameRenderer::getPositionTexShader); - if (stateManager.isInBelt(player)) { - graphics.blitSprite(HOTBAR_SELECTION_SPRITE, x - 1, y - 1 + stateManager.getSelectedBeltSlot(player) * 20, 0, 24, 23); - } + graphics.blit(UTILITY_BELT_WIDGET_TEXTURE, x, y, 0, 0, 22, 88); - UtilityBeltInventory inv = stateManager.getInventory(player); - RenderSystem.enableBlend(); - RenderSystem.defaultBlendFunc(); - int m = 1; + if (stateManager.isInBelt(player)) { + graphics.blitSprite(HOTBAR_SELECTION_SPRITE, x - 1, y - 1 + stateManager.getSelectedBeltSlot(player) * 20, 0, 24, 23); + } - for (int n = 0; n < inv.getContainerSize(); ++n) { - renderHotbarItem(graphics, x, y + n * 20 + 3, tickDelta, player, inv.getItem(n), m++); - } - } - } + UtilityBeltInventory inv = stateManager.getInventory(player); + RenderSystem.enableBlend(); + RenderSystem.defaultBlendFunc(); + int m = 1; - private static void renderHotbarItem(GuiGraphics graphics, int x, int y, float tickDelta, Player player, ItemStack stack, int seed) { - if (!stack.isEmpty()) { - float f = (float) stack.getPopTime() - tickDelta; - if (f > 0.0F) { - float g = 1.0F + f / 5.0F; - graphics.pose().pushPose(); - graphics.pose().translate(12, y + 12, 0); - graphics.pose().scale(1.0F / g, (g + 1.0F) / 2.0F, 1); - graphics.pose().translate(-12, -(y + 12), 0); - } + for (int n = 0; n < inv.getContainerSize(); ++n) { + renderHotbarItem(graphics, x, y + n * 20 + 3, tickDelta, player, inv.getItem(n), m++); + } + } + } - graphics.renderItem(player, stack, x + 3, y, seed); - if (f > 0.0F) { - graphics.pose().popPose(); - } + private static void renderHotbarItem(GuiGraphics graphics, int x, int y, float tickDelta, Player player, ItemStack stack, int seed) { + if (!stack.isEmpty()) { + float f = (float) stack.getPopTime() - tickDelta; + if (f > 0.0F) { + float g = 1.0F + f / 5.0F; + graphics.pose().pushPose(); + graphics.pose().translate(12, y + 12, 0); + graphics.pose().scale(1.0F / g, (g + 1.0F) / 2.0F, 1); + graphics.pose().translate(-12, -(y + 12), 0); + } - graphics.renderItemDecorations(Minecraft.getInstance().font, stack, x + 3, y); - } - } + graphics.renderItem(player, stack, x + 3, y, seed); + if (f > 0.0F) { + graphics.pose().popPose(); + } + + graphics.renderItemDecorations(Minecraft.getInstance().font, stack, x + 3, y); + } + } } 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 ccc62b7..5619bed 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 @@ -3,23 +3,23 @@ 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.UtilityBeltInventory.Mutable; import io.github.jamalam360.utility_belt.UtilityBeltPackets; import io.github.jamalam360.utility_belt.UtilityBeltPackets.C2SOpenScreen; import io.github.jamalam360.utility_belt.UtilityBeltPackets.C2SUpdateState; import io.github.jamalam360.utility_belt.UtilityBeltPackets.S2CSetBeltSlot; import io.github.jamalam360.utility_belt.UtilityBeltPackets.S2CSetHotbarSlot; -import io.netty.buffer.Unpooled; +import io.github.jamalam360.utility_belt.UtilityBeltPackets.S2CUpdateBeltInventory; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.network.FriendlyByteBuf; @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, UtilityBeltPackets.S2C_SET_BELT_SLOT, S2CSetBeltSlot.STREAM_CODEC, ClientNetworking::handleSetBeltSlot); NetworkManager.registerReceiver(NetworkManager.Side.S2C, UtilityBeltPackets.S2C_SET_HOTBAR_SLOT, S2CSetHotbarSlot.STREAM_CODEC, ClientNetworking::handleSetHotbarSlot); + NetworkManager.registerReceiver(NetworkManager.Side.S2C, UtilityBeltPackets.S2C_UPDATE_BELT_INVENTORY, S2CUpdateBeltInventory.STREAM_CODEC, ClientNetworking::handleUpdateBeltInventory); } public static void sendNewStateToServer(boolean inBelt, int slot, boolean swapItems) { @@ -35,12 +35,6 @@ public static void sendOpenScreenToServer() { NetworkManager.sendToServer(new C2SOpenScreen()); } - private static void handleUpdateInventory(FriendlyByteBuf buf, NetworkManager.PacketContext ctx) { -// CompoundTag tag = buf.readNbt(); -// ItemStack belt = UtilityBeltItem.getBelt(ctx.getPlayer()); -// belt.getTag().put("Inventory", tag.getList("Inventory", CompoundTag.TAG_COMPOUND)); - } - private static void handleSetBeltSlot(S2CSetBeltSlot payload, NetworkManager.PacketContext ctx) { StateManager.getClientInstance().setSelectedBeltSlot(ctx.getPlayer(), payload.slot()); } @@ -48,4 +42,8 @@ private static void handleSetBeltSlot(S2CSetBeltSlot payload, NetworkManager.Pac private static void handleSetHotbarSlot(S2CSetHotbarSlot payload, NetworkManager.PacketContext ctx) { ctx.getPlayer().getInventory().selected = payload.slot(); } + + private static void handleUpdateBeltInventory(S2CUpdateBeltInventory payload, NetworkManager.PacketContext ctx) { + StateManager.getClientInstance().setInventory(ctx.getPlayer(), new Mutable(payload.inventory())); + } } diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/client/ClientStateManager.java b/common/src/main/java/io/github/jamalam360/utility_belt/client/ClientStateManager.java index 522bd81..0c5ae5e 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/client/ClientStateManager.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/client/ClientStateManager.java @@ -16,32 +16,41 @@ public class ClientStateManager extends StateManager { @Override public boolean isInBelt(Player player) { - assert player == Minecraft.getInstance().player; return this.isInUtilityBelt; } @Override public void setInBelt(Player player, boolean inBelt) { - assert player == Minecraft.getInstance().player; this.isInUtilityBelt = inBelt; } @Override public int getSelectedBeltSlot(Player player) { - assert player == Minecraft.getInstance().player; return this.selectedSlot; } @Override public void setSelectedBeltSlot(Player player, int slot) { - assert player == Minecraft.getInstance().player; this.selectedSlot = slot; } @Override public UtilityBeltInventory getInventory(Player player) { ItemStack belt = UtilityBeltItem.getBelt(player); - assert belt != null; - return UtilityBeltItem.getInventoryFromTag(belt); + + if (belt == null) { + return UtilityBeltInventory.EMPTY; + } else { + return UtilityBeltItem.getInventoryFromTag(belt); + } + } + + @Override + public void setInventory(Player player, UtilityBeltInventory.Mutable inventory) { + ItemStack belt = UtilityBeltItem.getBelt(player); + + if (belt != null) { + UtilityBeltItem.setInventory(belt, inventory.toImmutable()); + } } } diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/client/UtilityBeltClient.java b/common/src/main/java/io/github/jamalam360/utility_belt/client/UtilityBeltClient.java index a9b326f..9b7e0d4 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/client/UtilityBeltClient.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/client/UtilityBeltClient.java @@ -112,7 +112,11 @@ private static EventResult onMouseScrolled(Minecraft client, double scrollX, dou if (scrollY != 0 && stateManager.isInBelt(client.player)) { int slot = stateManager.getSelectedBeltSlot(client.player); ItemStack belt = UtilityBeltItem.getBelt(client.player); - assert belt != null; + + if (belt == null) { + return EventResult.pass(); + } + int beltSize = stateManager.getInventory(client.player).getContainerSize(); if (UtilityBelt.CONFIG.get().invertScrolling) { @@ -168,9 +172,7 @@ public static void onClientDisconnect() { } private static void toggleInBelt(Minecraft client) { - assert client.player != null; - - if (UtilityBeltItem.getBelt(client.player) == null) { + if (client.player == null || UtilityBeltItem.getBelt(client.player) == null) { return; } @@ -181,7 +183,6 @@ private static void toggleInBelt(Minecraft client) { } private static void playSwapSound(Minecraft client) { - assert client.level != null; client.getSoundManager().play(SimpleSoundInstance.forUI( SoundEvents.ARMOR_EQUIP_LEATHER, client.level.random.nextFloat() + 0.50f)); } diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/mixin/InventoryMixin.java b/common/src/main/java/io/github/jamalam360/utility_belt/mixin/InventoryMixin.java index a27ae78..322774d 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/mixin/InventoryMixin.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/mixin/InventoryMixin.java @@ -34,7 +34,11 @@ public abstract class InventoryMixin { StateManager stateManager = StateManager.getServerInstance(); if (stateManager.isInBelt(this.player)) { ItemStack belt = UtilityBeltItem.getBelt(this.player); - assert belt != null; + + if (belt == null) { + return; + } + UtilityBeltInventory inv = stateManager.getInventory(this.player); cir.setReturnValue(inv.getItem(stateManager.getSelectedBeltSlot(this.player))); } @@ -52,7 +56,11 @@ public abstract class InventoryMixin { StateManager stateManager = StateManager.getServerInstance(); if (stateManager.isInBelt(this.player)) { ItemStack belt = UtilityBeltItem.getBelt(this.player); - assert belt != null; + + if (belt == null) { + return; + } + UtilityBeltInventory inv = stateManager.getInventory(this.player); cir.setReturnValue(inv.getItem(stateManager.getSelectedBeltSlot(this.player)).getDestroySpeed(blockState)); } @@ -63,10 +71,14 @@ public abstract class InventoryMixin { at = @At("RETURN") ) private void utilitybelt$tick(CallbackInfo ci) { - ItemStack belt = UtilityBeltItem.getBelt(this.player); StateManager stateManager = StateManager.getServerInstance(); if (stateManager.isInBelt(this.player)) { - assert belt != null; + ItemStack belt = UtilityBeltItem.getBelt(this.player); + + if (belt == null) { + return; + } + UtilityBeltInventory inv = stateManager.getInventory(this.player); int selectedSlot = stateManager.getSelectedBeltSlot(this.player); @@ -83,7 +95,7 @@ public abstract class InventoryMixin { private void utilitybelt$patchRemoveOneForHeldItems(ItemStack stack, CallbackInfo ci) { ItemStack belt = UtilityBeltItem.getBelt(this.player); if (belt != null) { - UtilityBeltInventory inv = StateManager.getServerInstance().getInventory(this.player); + UtilityBeltInventory.Mutable inv = StateManager.getServerInstance().getMutableInventory(this.player); int found = -1; for (int i = 0; i < inv.getContainerSize(); i++) { @@ -95,6 +107,7 @@ public abstract class InventoryMixin { if (found != -1) { inv.setItem(found, ItemStack.EMPTY); + StateManager.getServerInstance().setInventory(this.player, inv); ci.cancel(); } } @@ -117,16 +130,19 @@ public abstract class InventoryMixin { StateManager stateManager = StateManager.getServerInstance(); if (stateManager.isInBelt(this.player)) { int slot = stateManager.getSelectedBeltSlot(this.player); - UtilityBeltInventory inv = stateManager.getInventory(this.player); + UtilityBeltInventory.Mutable inv = stateManager.getMutableInventory(this.player); ItemStack selected = inv.getItem(slot); if (this.player.isLocalPlayer()) { + //TODO: is this needed? // Because of how the inventory is synced, we fake the return value on the clientside (not actually removing it) ItemStack fakeReturn = selected.copy(); fakeReturn.setCount(entireStack ? selected.getCount() : 1); cir.setReturnValue(selected.isEmpty() ? ItemStack.EMPTY : fakeReturn); } else { - cir.setReturnValue(selected.isEmpty() ? ItemStack.EMPTY : inv.removeItem(slot, entireStack ? selected.getCount() : 1)); + ItemStack item = selected.isEmpty() ? ItemStack.EMPTY : inv.removeItem(slot, entireStack ? selected.getCount() : 1); + stateManager.setInventory(this.player, inv); + cir.setReturnValue(item); } } } @@ -135,8 +151,9 @@ public abstract class InventoryMixin { private void utilitybelt$clearUtilityBelt(CallbackInfo ci) { ItemStack belt = UtilityBeltItem.getBelt(this.player); if (belt != null) { - UtilityBeltInventory inv = StateManager.getServerInstance().getInventory(this.player); + UtilityBeltInventory.Mutable inv = StateManager.getServerInstance().getMutableInventory(this.player); inv.clearContent(); + StateManager.getServerInstance().setInventory(this.player, inv); } } @@ -172,7 +189,7 @@ public abstract class InventoryMixin { private void utilitybelt$dropAllFromUtilityBelt(CallbackInfo ci) { ItemStack belt = UtilityBeltItem.getBelt(this.player); if (belt != null) { - UtilityBeltInventory inv = StateManager.getServerInstance().getInventory(this.player); + UtilityBeltInventory.Mutable inv = StateManager.getServerInstance().getMutableInventory(this.player); for (int i = 0; i < inv.getContainerSize(); i++) { ItemStack itemStack = inv.getItem(i); @@ -181,6 +198,8 @@ public abstract class InventoryMixin { inv.setItem(i, ItemStack.EMPTY); } } + + StateManager.getServerInstance().setInventory(this.player, inv); } } diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/mixin/LivingEntityMixin.java b/common/src/main/java/io/github/jamalam360/utility_belt/mixin/LivingEntityMixin.java index 0d4a9fb..57c5973 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/mixin/LivingEntityMixin.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/mixin/LivingEntityMixin.java @@ -31,9 +31,14 @@ public abstract class LivingEntityMixin implements Duck.LivingEntity { StateManager stateManager = StateManager.getServerInstance(); if (stateManager.isInBelt(player)) { ItemStack belt = UtilityBeltItem.getBelt(player); - assert belt != null; - UtilityBeltInventory inv = stateManager.getInventory(player); + + if (belt == null) { + return; + } + + UtilityBeltInventory.Mutable inv = stateManager.getMutableInventory(player); inv.setItem(stateManager.getSelectedBeltSlot(player), ItemStack.EMPTY); + stateManager.setInventory(player, inv); } } } diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/mixin/PlayerMixin.java b/common/src/main/java/io/github/jamalam360/utility_belt/mixin/PlayerMixin.java index 9b3ef52..9f12cdf 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/mixin/PlayerMixin.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/mixin/PlayerMixin.java @@ -24,8 +24,9 @@ public class PlayerMixin { if (equipmentSlot == EquipmentSlot.MAINHAND && (Object) this instanceof ServerPlayer player) { StateManager stateManager = StateManager.getServerInstance(); if (stateManager.isInBelt(player)) { - UtilityBeltInventory inv = stateManager.getInventory(player); + UtilityBeltInventory.Mutable inv = stateManager.getMutableInventory(player); inv.setItem(stateManager.getSelectedBeltSlot(player), itemStack); + stateManager.setInventory(player, inv); } } } diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/mixin/ServerPlayerMixin.java b/common/src/main/java/io/github/jamalam360/utility_belt/mixin/ServerPlayerMixin.java index 575ae40..cba0df9 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/mixin/ServerPlayerMixin.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/mixin/ServerPlayerMixin.java @@ -8,31 +8,34 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ServerPlayer.class) public class ServerPlayerMixin { - @SuppressWarnings("UnreachableCode") - @Inject( - method = "tick", - at = @At("HEAD") - ) - private void utilitybelt$startPlayerTick(CallbackInfo ci) { - StateManager stateManager = StateManager.getServerInstance(); - ItemStack belt = UtilityBeltItem.getBelt((Player) (Object) this); - if (belt != null) { - UtilityBeltInventory inv = stateManager.getInventory((Player) (Object) this); - UtilityBeltInventory nbtInv = UtilityBeltItem.getInventoryFromTag(belt); + @Unique + private int utilitybelt$lastSyncedInventoryHash = 0; + + @SuppressWarnings("UnreachableCode") + @Inject( + method = "tick", + at = @At("HEAD") + ) + private void utilitybelt$startPlayerTick(CallbackInfo ci) { + StateManager stateManager = StateManager.getServerInstance(); + ItemStack belt = UtilityBeltItem.getBelt((Player) (Object) this); - if (!inv.equals(nbtInv)) { - UtilityBeltItem.setInventory(belt, inv); - ServerNetworking.sendNewInventoryToClient((ServerPlayer) (Object) this); - } - } + if (belt != null) { + UtilityBeltInventory inv = stateManager.getInventory((Player) (Object) this); - stateManager.onStartTick((Player) (Object) this); - } + if (this.utilitybelt$lastSyncedInventoryHash != inv.hashCode()) { + this.utilitybelt$lastSyncedInventoryHash = inv.hashCode(); + ServerNetworking.sendInventoryToClient((ServerPlayer) (Object) this, inv); + UtilityBeltItem.setInventory(belt, inv); + } + } + } } diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/mixin/client/MinecraftMixin.java b/common/src/main/java/io/github/jamalam360/utility_belt/mixin/client/MinecraftMixin.java index 8768719..64bc9e3 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/mixin/client/MinecraftMixin.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/mixin/client/MinecraftMixin.java @@ -55,7 +55,11 @@ public class MinecraftMixin { return true; case SWITCH_BELT_SLOT: ItemStack belt = UtilityBeltItem.getBelt(this.player); - assert belt != null; + + if (belt == null) { + return false; + } + int beltSize = stateManager.getInventory(this.player).getContainerSize(); if (i >= 0 && i < beltSize) { diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/screen/UtilityBeltMenu.java b/common/src/main/java/io/github/jamalam360/utility_belt/screen/UtilityBeltMenu.java index 5a4a294..1cff805 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/screen/UtilityBeltMenu.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/screen/UtilityBeltMenu.java @@ -3,6 +3,7 @@ import io.github.jamalam360.utility_belt.StateManager; import io.github.jamalam360.utility_belt.UtilityBelt; import io.github.jamalam360.utility_belt.UtilityBeltInventory; +import io.github.jamalam360.utility_belt.UtilityBeltInventory.Mutable; import io.github.jamalam360.utility_belt.UtilityBeltItem; import io.github.jamalam360.utility_belt.server.ServerStateManager; import net.minecraft.network.chat.Component; @@ -18,124 +19,121 @@ import org.jetbrains.annotations.Nullable; public class UtilityBeltMenu extends AbstractContainerMenu { - private final UtilityBeltInventory inventory; - - public UtilityBeltMenu(int syncId, Inventory playerInventory) { - this(syncId, playerInventory, new UtilityBeltInventory()); - } - - public UtilityBeltMenu(int syncId, Inventory playerInventory, UtilityBeltInventory inventory) { - super(UtilityBelt.MENU_TYPE.get(), syncId); - this.inventory = inventory; - - int m; - int l; - - for (l = 0; l < this.inventory.getContainerSize(); ++l) { - this.addSlot(new Slot(inventory, l, 53 + l * 18, 17) { - @Override - public boolean mayPlace(ItemStack stack) { - return UtilityBeltItem.isValidItem(stack); - } - }); - } - - for (m = 0; m < 3; ++m) { - for (l = 0; l < 9; ++l) { - this.addSlot(new Slot(playerInventory, l + m * 9 + 9, 8 + l * 18, 48 + m * 18)); - } - } - - for (m = 0; m < 9; ++m) { - this.addSlot(new Slot(playerInventory, m, 8 + m * 18, 106)); - } - } - - @Override - public boolean canTakeItemForPickAll(ItemStack stack, Slot slot) { - if (slot.getContainerSlot() < this.inventory.getContainerSize()) { - return UtilityBeltItem.isValidItem(stack); - } - - return super.canTakeItemForPickAll(stack, slot); - } - - @Override - public @NotNull ItemStack quickMoveStack(Player player, int index) { - ItemStack newStack = ItemStack.EMPTY; - Slot slot = this.slots.get(index); - - if (slot.hasItem()) { - ItemStack originalStack = slot.getItem(); - newStack = originalStack.copy(); - if (index < this.inventory.getContainerSize()) { - if (!this.moveItemStackTo(originalStack, this.inventory.getContainerSize(), this.slots.size(), true)) { - return ItemStack.EMPTY; - } - } else if (!this.moveItemStackTo(originalStack, 0, this.inventory.getContainerSize(), false)) { - return ItemStack.EMPTY; - } - - if (originalStack.isEmpty()) { - slot.set(ItemStack.EMPTY); - } else { - slot.setChanged(); - } - } - - this.markDirty(player); - return newStack; - } - - @Override - public void clicked(int slotIndex, int button, ClickType clickType, Player player) { - super.clicked(slotIndex, button, clickType, player); - - if (slotIndex < this.inventory.getContainerSize()) { - this.markDirty(player); - } - } - - @Override - public boolean stillValid(Player player) { - return UtilityBeltItem.getBelt(player) != null; - } - - @Override - public void removed(Player player) { - this.markDirty(player); - super.removed(player); - } - - private void markDirty(Player player) { - ItemStack belt = UtilityBeltItem.getBelt(player); - - if (belt == null) { - // uh oh - // anyways - return; - } - - UtilityBeltItem.setInventory(belt, this.inventory); - - if (player instanceof ServerPlayer) { - ((ServerStateManager) StateManager.getServerInstance()).getInventoryFromTag(player); - } - } - - public static class Factory implements MenuProvider { - - public static final Factory INSTANCE = new Factory(); - - @Override - public @NotNull Component getDisplayName() { - return Component.translatable("container.utility_belt.utility_belt"); - } - - @Nullable - @Override - public AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) { - return new UtilityBeltMenu(i, inventory, UtilityBeltItem.getInventoryFromTag(UtilityBeltItem.getBelt(player))); - } - } + + private final UtilityBeltInventory.Mutable inventory; + + public UtilityBeltMenu(int syncId, Inventory playerInventory) { + this(syncId, playerInventory, new Mutable(UtilityBeltInventory.EMPTY)); + } + + public UtilityBeltMenu(int syncId, Inventory playerInventory, UtilityBeltInventory.Mutable inventory) { + super(UtilityBelt.MENU_TYPE.get(), syncId); + this.inventory = inventory; + + int m; + int l; + + for (l = 0; l < this.inventory.getContainerSize(); ++l) { + this.addSlot(new Slot(inventory, l, 53 + l * 18, 17) { + @Override + public boolean mayPlace(ItemStack stack) { + return UtilityBeltItem.isValidItem(stack); + } + }); + } + + for (m = 0; m < 3; ++m) { + for (l = 0; l < 9; ++l) { + this.addSlot(new Slot(playerInventory, l + m * 9 + 9, 8 + l * 18, 48 + m * 18)); + } + } + + for (m = 0; m < 9; ++m) { + this.addSlot(new Slot(playerInventory, m, 8 + m * 18, 106)); + } + } + + @Override + public boolean canTakeItemForPickAll(ItemStack stack, Slot slot) { + if (slot.getContainerSlot() < this.inventory.getContainerSize()) { + return UtilityBeltItem.isValidItem(stack); + } + + return super.canTakeItemForPickAll(stack, slot); + } + + @Override + public @NotNull ItemStack quickMoveStack(Player player, int index) { + ItemStack newStack = ItemStack.EMPTY; + Slot slot = this.slots.get(index); + + if (slot.hasItem()) { + ItemStack originalStack = slot.getItem(); + newStack = originalStack.copy(); + if (index < this.inventory.getContainerSize()) { + if (!this.moveItemStackTo(originalStack, this.inventory.getContainerSize(), this.slots.size(), true)) { + return ItemStack.EMPTY; + } + } else if (!this.moveItemStackTo(originalStack, 0, this.inventory.getContainerSize(), false)) { + return ItemStack.EMPTY; + } + + if (originalStack.isEmpty()) { + slot.set(ItemStack.EMPTY); + } else { + slot.setChanged(); + } + } + + this.markDirty(player); + return newStack; + } + + @Override + public void clicked(int slotIndex, int button, ClickType clickType, Player player) { + super.clicked(slotIndex, button, clickType, player); + + if (slotIndex < this.inventory.getContainerSize()) { + this.markDirty(player); + } + } + + @Override + public boolean stillValid(Player player) { + return UtilityBeltItem.getBelt(player) != null; + } + + @Override + public void removed(Player player) { + this.markDirty(player); + super.removed(player); + } + + private void markDirty(Player player) { + ItemStack belt = UtilityBeltItem.getBelt(player); + + if (belt == null) { + // uh oh + // anyways + return; + } + + StateManager.getServerInstance().setInventory(player, this.inventory); + } + + public static class Factory implements MenuProvider { + + public static final Factory INSTANCE = new Factory(); + + @Override + public @NotNull Component getDisplayName() { + return Component.translatable("container.utility_belt.utility_belt"); + } + + @Nullable + @Override + public AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) { + return new UtilityBeltMenu(i, inventory, new Mutable(UtilityBeltItem.getInventoryFromTag(UtilityBeltItem.getBelt(player)))); + } + } } 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 1acc12a..143296e 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 @@ -23,13 +23,8 @@ public static void init() { NetworkManager.registerReceiver(NetworkManager.Side.C2S, UtilityBeltPackets.C2S_OPEN_SCREEN, C2SOpenScreen.STREAM_CODEC, ServerNetworking::handleOpenScreen); } - public static void sendNewInventoryToClient(ServerPlayer player) { -// FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); -// UtilityBeltInventory inv = StateManager.getServerInstance().getInventory(player); -// CompoundTag tag = new CompoundTag(); -// tag.put("Inventory", inv.createTag()); -// buf.writeNbt(tag); -// NetworkManager.sendToPlayer(player, UtilityBelt.S2C_UPDATE_INV, buf); + public static void sendInventoryToClient(ServerPlayer player, UtilityBeltInventory inventory) { + NetworkManager.sendToPlayer(player, new UtilityBeltPackets.S2CUpdateBeltInventory(inventory)); } private static void handleUpdateState(C2SUpdateState payload, NetworkManager.PacketContext ctx) { @@ -52,7 +47,7 @@ private static void handleUpdateState(C2SUpdateState payload, NetworkManager.Pac return; } - UtilityBeltInventory inv = stateManager.getInventory(ctx.getPlayer()); + UtilityBeltInventory.Mutable inv = stateManager.getMutableInventory(ctx.getPlayer()); ItemStack stackInHand = ctx.getPlayer().getInventory().getItem(ctx.getPlayer().getInventory().selected); int hotbarSlot = ctx.getPlayer().getInventory().selected; ItemStack stackInBelt = inv.getItem(beltSlot); @@ -78,6 +73,7 @@ private static void handleUpdateState(C2SUpdateState payload, NetworkManager.Pac if (UtilityBeltItem.isValidItem(stackInHand)) { ctx.getPlayer().getInventory().setItem(hotbarSlot, stackInBelt); inv.setItem(beltSlot, stackInHand); + stateManager.setInventory(ctx.getPlayer(), inv); ((Duck.LivingEntity) ctx.getPlayer()).utilitybelt$detectEquipmentUpdates(); if (beltSlot != slot) { diff --git a/common/src/main/java/io/github/jamalam360/utility_belt/server/ServerStateManager.java b/common/src/main/java/io/github/jamalam360/utility_belt/server/ServerStateManager.java index faa32c5..08a9883 100644 --- a/common/src/main/java/io/github/jamalam360/utility_belt/server/ServerStateManager.java +++ b/common/src/main/java/io/github/jamalam360/utility_belt/server/ServerStateManager.java @@ -19,19 +19,6 @@ private PlayerState getState(Player player) { return playerStates.computeIfAbsent(player.getUUID(), uuid -> new PlayerState(false, 0)); } - @Override - public void onStartTick(Player player) { - if (this.hasBelt(player)) { - getState(player).inventory = UtilityBeltItem.getInventoryFromTag(UtilityBeltItem.getBelt(player)); - } else { - getState(player).inventory = null; - } - } - - public void getInventoryFromTag(Player player) { - getState(player).inventory = UtilityBeltItem.getInventoryFromTag(UtilityBeltItem.getBelt(player)); - } - @Override public boolean isInBelt(Player player) { return getState(player).inBelt; @@ -58,13 +45,22 @@ public UtilityBeltInventory getInventory(Player player) { if (state.inventory == null) { ItemStack belt = UtilityBeltItem.getBelt(player); - assert belt != null; + + if (belt == null) { + return UtilityBeltInventory.EMPTY; + } + state.inventory = UtilityBeltItem.getInventoryFromTag(belt); } return state.inventory; } + @Override + public void setInventory(Player player, UtilityBeltInventory.Mutable inventory) { + getState(player).inventory = inventory.toImmutable(); + } + private static class PlayerState { boolean inBelt; int selectedBeltSlot; diff --git a/gradle.properties b/gradle.properties index a196e12..b92ee5c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx3G org.gradle.daemon=false org.gradle.parallel=true -version=2.1.1+1.20.6 +version=2.1.2+1.20.6 minecraft_version=1.20.6 branch=main group=io.github.jamalam360 diff --git a/neoforge/src/main/java/io/github/jamalam360/utility_belt/neoforge/UtilityBeltCurio.java b/neoforge/src/main/java/io/github/jamalam360/utility_belt/neoforge/UtilityBeltCurio.java index d9474d8..10adec4 100644 --- a/neoforge/src/main/java/io/github/jamalam360/utility_belt/neoforge/UtilityBeltCurio.java +++ b/neoforge/src/main/java/io/github/jamalam360/utility_belt/neoforge/UtilityBeltCurio.java @@ -10,6 +10,11 @@ public class UtilityBeltCurio implements ICurioItem { + @Override + public void onEquip(SlotContext slotContext, ItemStack prevStack, ItemStack stack) { + ((UtilityBeltItem) stack.getItem()).onEquip(slotContext.entity(), stack); + } + @Override public void onUnequip(SlotContext slotContext, ItemStack newStack, ItemStack stack) { ((UtilityBeltItem) stack.getItem()).onUnequip(slotContext.entity());