diff --git a/build.gradle b/build.gradle index eba10dade..409ee77ce 100644 --- a/build.gradle +++ b/build.gradle @@ -73,7 +73,6 @@ base { java { toolchain { languageVersion.set(JavaLanguageVersion.of(17)) - vendor.set(JvmVendorSpec.JETBRAINS) } } diff --git a/gradle.properties b/gradle.properties index b36c61424..bf8ef1f87 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,8 +1,6 @@ org.gradle.jvmargs=-Xmx3G org.gradle.daemon=false org.gradle.debug=false -org.gradle.java.home=C:\\Users\\zacpa\\jdk-17\\jdk-17.0.16+8 - neogradle.subsystems.parchment.minecraftVersion=1.20.1 neogradle.subsystems.parchment.mappingsVersion=2023.09.03 diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/src/main/java/com/direwolf20/justdirethings/JustDireThings.java b/src/main/java/com/direwolf20/justdirethings/JustDireThings.java index d334abcb4..cb5e1cf90 100644 --- a/src/main/java/com/direwolf20/justdirethings/JustDireThings.java +++ b/src/main/java/com/direwolf20/justdirethings/JustDireThings.java @@ -7,7 +7,6 @@ import com.direwolf20.justdirethings.common.capabilities.EnergyStorageItemStackNoReceive; import com.direwolf20.justdirethings.common.capabilities.EnergyStorageItemstack; import com.direwolf20.justdirethings.common.capabilities.ExperienceHolderFluidTank; -import com.direwolf20.justdirethings.common.containers.handlers.PotionCanisterHandler; import com.direwolf20.justdirethings.common.entities.DecoyEntity; import com.direwolf20.justdirethings.common.items.*; import com.direwolf20.justdirethings.common.items.datacomponents.JustDireDataComponents; @@ -32,7 +31,6 @@ import net.neoforged.neoforge.event.entity.EntityAttributeModificationEvent; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.capability.templates.FluidHandlerItemStack; -import net.neoforged.neoforge.items.ComponentItemHandler; import net.neoforged.neoforge.items.ItemStackHandler; import org.slf4j.Logger; @@ -76,19 +74,13 @@ private void registerCapabilities(RegisterCapabilitiesEvent event) { //Items event.registerItem(Capabilities.ItemHandler.ITEM, (itemStack, context) -> itemStack.getData(Registration.HANDLER.get()), Registration.Pocket_Generator.get()); - event.registerItem(Capabilities.ItemHandler.ITEM, (itemStack, context) -> new ComponentItemHandler(itemStack, JustDireDataComponents.TOOL_CONTENTS.get(), 1), - Registration.FerricoreBow.get() - ); - event.registerItem(Capabilities.ItemHandler.ITEM, (itemStack, context) -> new ComponentItemHandler(itemStack, JustDireDataComponents.TOOL_CONTENTS.get(), 2), - Registration.BlazegoldBow.get() - ); - event.registerItem(Capabilities.ItemHandler.ITEM, (itemStack, context) -> new ComponentItemHandler(itemStack, JustDireDataComponents.TOOL_CONTENTS.get(), 3), - Registration.CelestigemBow.get() - ); - event.registerItem(Capabilities.ItemHandler.ITEM, (itemStack, context) -> new ComponentItemHandler(itemStack, JustDireDataComponents.TOOL_CONTENTS.get(), 4), + event.registerItem(Capabilities.ItemHandler.ITEM, (itemStack, context) -> itemStack.getData(Registration.TOOL_HANDLER.get()), + Registration.FerricoreBow.get(), + Registration.BlazegoldBow.get(), + Registration.CelestigemBow.get(), Registration.EclipseAlloyBow.get() ); - event.registerItem(Capabilities.ItemHandler.ITEM, (itemStack, context) -> new PotionCanisterHandler(itemStack, JustDireDataComponents.TOOL_CONTENTS.get(), 1), + event.registerItem(Capabilities.ItemHandler.ITEM, (itemStack, context) -> itemStack.getData(Registration.POTION_CANISTER_HANDLER.get()), Registration.PotionCanister.get() ); event.registerItem(Capabilities.EnergyStorage.ITEM, (itemStack, context) -> { diff --git a/src/main/java/com/direwolf20/justdirethings/common/containers/PocketGeneratorContainer.java b/src/main/java/com/direwolf20/justdirethings/common/containers/PocketGeneratorContainer.java index 6a5612a18..228587eb9 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/containers/PocketGeneratorContainer.java +++ b/src/main/java/com/direwolf20/justdirethings/common/containers/PocketGeneratorContainer.java @@ -24,7 +24,7 @@ public PocketGeneratorContainer(int windowId, Inventory playerInventory, Player public PocketGeneratorContainer(int windowId, Inventory playerInventory, Player player, ItemStack pocketGenerator) { super(Registration.PocketGenerator_Container.get(), windowId); playerEntity = player; - handler = pocketGenerator.getData(Registration.HANDLER.get()); + handler = pocketGenerator.getData(Registration.HANDLER); this.pocketGeneratorItemStack = pocketGenerator; if (handler != null) addGeneratorSlots(handler, 0, 80, 35, 1, 18); diff --git a/src/main/java/com/direwolf20/justdirethings/common/containers/PotionCanisterContainer.java b/src/main/java/com/direwolf20/justdirethings/common/containers/PotionCanisterContainer.java index 7b55e6505..ab3cdce48 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/containers/PotionCanisterContainer.java +++ b/src/main/java/com/direwolf20/justdirethings/common/containers/PotionCanisterContainer.java @@ -2,13 +2,13 @@ import com.direwolf20.justdirethings.common.containers.basecontainers.BaseContainer; import com.direwolf20.justdirethings.common.containers.handlers.PotionCanisterHandler; -import com.direwolf20.justdirethings.common.items.datacomponents.JustDireDataComponents; import com.direwolf20.justdirethings.setup.Registration; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; import net.minecraft.world.item.PotionItem; import net.neoforged.neoforge.items.IItemHandler; import net.neoforged.neoforge.items.SlotItemHandler; @@ -26,7 +26,7 @@ public PotionCanisterContainer(int windowId, Inventory playerInventory, Player p public PotionCanisterContainer(int windowId, Inventory playerInventory, Player player, ItemStack potionCanister) { super(Registration.PotionCanister_Container.get(), windowId); playerEntity = player; - handler = new PotionCanisterHandler(potionCanister, JustDireDataComponents.TOOL_CONTENTS.get(), 1); + handler = potionCanister.getData(Registration.POTION_CANISTER_HANDLER.get()); this.potionCanister = potionCanister; if (handler != null) addItemSlots(handler, 0, 80, 35, 1, 18); @@ -44,7 +44,7 @@ protected int addItemSlots(IItemHandler handler, int index, int x, int y, int am addSlot(new SlotItemHandler(handler, i, x, y) { @Override public boolean mayPlace(ItemStack stack) { - return stack.getItem() instanceof PotionItem; + return stack.getItem() instanceof PotionItem || stack.is(Items.GLASS_BOTTLE); } }); x += dx; diff --git a/src/main/java/com/direwolf20/justdirethings/common/containers/ToolSettingContainer.java b/src/main/java/com/direwolf20/justdirethings/common/containers/ToolSettingContainer.java index 89eb1e469..09c39271e 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/containers/ToolSettingContainer.java +++ b/src/main/java/com/direwolf20/justdirethings/common/containers/ToolSettingContainer.java @@ -15,9 +15,9 @@ import net.minecraft.world.item.enchantment.EnchantmentEffectComponents; import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.neoforged.neoforge.capabilities.Capabilities; -import net.neoforged.neoforge.items.ComponentItemHandler; import net.neoforged.neoforge.items.IItemHandler; import net.neoforged.neoforge.items.SlotItemHandler; +import net.neoforged.neoforge.items.ItemStackHandler; import java.util.ArrayList; import java.util.List; @@ -34,7 +34,7 @@ public class ToolSettingContainer extends BaseContainer { }; private static final EquipmentSlot[] SLOT_IDS = new EquipmentSlot[]{EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET}; public final List dynamicSlots = new ArrayList<>(); - public ComponentItemHandler componentItemHandler; + public ItemStackHandler componentItemHandler; public ToolSettingContainer(int windowId, Inventory playerInventory, Player player, FriendlyByteBuf extraData) { @@ -126,9 +126,9 @@ public void refreshSlots(ItemStack selectedStack) { } } - public ComponentItemHandler getItemSlots(ItemStack itemStack) { + public ItemStackHandler getItemSlots(ItemStack itemStack) { IItemHandler itemHandler = itemStack.getCapability(Capabilities.ItemHandler.ITEM); - if (itemHandler instanceof ComponentItemHandler componentItemHandler) + if (itemHandler instanceof ItemStackHandler componentItemHandler) return componentItemHandler; return null; } diff --git a/src/main/java/com/direwolf20/justdirethings/common/containers/handlers/PotionCanisterHandler.java b/src/main/java/com/direwolf20/justdirethings/common/containers/handlers/PotionCanisterHandler.java index 054e28036..d03d59145 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/containers/handlers/PotionCanisterHandler.java +++ b/src/main/java/com/direwolf20/justdirethings/common/containers/handlers/PotionCanisterHandler.java @@ -1,30 +1,29 @@ package com.direwolf20.justdirethings.common.containers.handlers; import com.direwolf20.justdirethings.common.items.PotionCanister; -import net.minecraft.core.component.DataComponentType; -import net.minecraft.world.item.BottleItem; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; import net.minecraft.world.item.PotionItem; -import net.minecraft.world.item.component.ItemContainerContents; -import net.neoforged.neoforge.items.ComponentItemHandler; +import net.neoforged.neoforge.items.ItemStackHandler; -public class PotionCanisterHandler extends ComponentItemHandler { +public class PotionCanisterHandler extends ItemStackHandler { private final ItemStack potionStack; - public PotionCanisterHandler(ItemStack parent, DataComponentType component, int size) { - super(parent, component, size); - potionStack = parent; + public PotionCanisterHandler(ItemStack parent, int size) { + super(size); + this.potionStack = parent; } @Override - protected void onContentsChanged(int slot, ItemStack oldStack, ItemStack newStack) { - if (!newStack.isEmpty() && newStack.getItem() instanceof PotionItem) { + protected void onContentsChanged(int slot) { + ItemStack stackInSlot = getStackInSlot(slot); + if (!stackInSlot.isEmpty() && stackInSlot.getItem() instanceof PotionItem) { PotionCanister.attemptFill(potionStack); } } @Override public boolean isItemValid(int slot, ItemStack stack) { - return stack.getItem() instanceof PotionItem || stack.getItem() instanceof BottleItem || stack.isEmpty(); + return stack.isEmpty() || stack.getItem() instanceof PotionItem || stack.is(Items.GLASS_BOTTLE); } } diff --git a/src/main/java/com/direwolf20/justdirethings/common/items/PocketGenerator.java b/src/main/java/com/direwolf20/justdirethings/common/items/PocketGenerator.java index 4699e0820..5281102c4 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/items/PocketGenerator.java +++ b/src/main/java/com/direwolf20/justdirethings/common/items/PocketGenerator.java @@ -101,7 +101,10 @@ private void burn(EnergyStorageItemStackNoReceive energyStorage, ItemStack itemS } private boolean initBurn(ItemStack itemStack) { - ItemStackHandler handler = itemStack.getData(Registration.HANDLER.get()); + ItemStackHandler handler = itemStack.getData(Registration.HANDLER); + if (handler == null) { + return false; + } ItemStack fuelStack = handler.getStackInSlot(0); int burnTime = fuelStack.getBurnTime(RecipeType.SMELTING); @@ -115,9 +118,9 @@ private boolean initBurn(ItemStack itemStack) { } else { setFuelMultiplier(itemStack, 1); } - if (fuelStack.hasCraftingRemainingItem()) + if (fuelStack.hasCraftingRemainingItem()) { handler.setStackInSlot(0, fuelStack.getCraftingRemainingItem()); - else { + } else { fuelStack.shrink(1); handler.setStackInSlot(0, fuelStack); } diff --git a/src/main/java/com/direwolf20/justdirethings/common/items/PotionCanister.java b/src/main/java/com/direwolf20/justdirethings/common/items/PotionCanister.java index 4f03146e9..379933019 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/items/PotionCanister.java +++ b/src/main/java/com/direwolf20/justdirethings/common/items/PotionCanister.java @@ -2,8 +2,10 @@ import com.direwolf20.justdirethings.common.containers.PotionCanisterContainer; import com.direwolf20.justdirethings.common.containers.handlers.PotionCanisterHandler; -import com.direwolf20.justdirethings.common.items.datacomponents.JustDireDataComponents; +import com.direwolf20.justdirethings.common.items.data.ItemDataHelper; +import com.direwolf20.justdirethings.common.items.data.ItemDataKeys; import com.direwolf20.justdirethings.util.MagicHelpers; +import com.direwolf20.justdirethings.setup.Registration; import net.minecraft.core.component.DataComponents; import net.minecraft.network.chat.Component; import net.minecraft.world.InteractionHand; @@ -56,16 +58,26 @@ public static int getMaxMB() { } public static PotionContents getPotionContents(ItemStack itemStack) { - return itemStack.getOrDefault(JustDireDataComponents.POTION_CONTENTS, PotionContents.EMPTY); + ItemStack storedPotion = ItemDataHelper.getItemStack(itemStack, ItemDataKeys.POTION_CONTENTS); + if (storedPotion.isEmpty()) { + return PotionContents.EMPTY; + } + return storedPotion.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY); } public static void setPotionContents(ItemStack itemStack, PotionContents potionContents) { - itemStack.set(JustDireDataComponents.POTION_CONTENTS, potionContents); + if (potionContents.equals(PotionContents.EMPTY)) { + ItemDataHelper.setItemStack(itemStack, ItemDataKeys.POTION_CONTENTS, ItemStack.EMPTY); + } else { + ItemStack potionStack = new ItemStack(Items.POTION); + potionStack.set(DataComponents.POTION_CONTENTS, potionContents); + ItemDataHelper.setItemStack(itemStack, ItemDataKeys.POTION_CONTENTS, potionStack); + } } public static void attemptFill(ItemStack canister) { if (!(canister.getItem() instanceof PotionCanister)) return; - PotionCanisterHandler handler = new PotionCanisterHandler(canister, JustDireDataComponents.TOOL_CONTENTS.get(), 1); + PotionCanisterHandler handler = canister.getData(Registration.POTION_CANISTER_HANDLER.get()); ItemStack potion = handler.getStackInSlot(0); if (potion.isEmpty() || !(potion.getItem() instanceof PotionItem)) return; PotionContents currentContents = getPotionContents(canister); @@ -81,7 +93,7 @@ public static void attemptFill(ItemStack canister) { } public static int getPotionAmount(ItemStack itemStack) { - return itemStack.getOrDefault(JustDireDataComponents.POTION_AMOUNT, 0); + return ItemDataHelper.getInt(itemStack, ItemDataKeys.POTION_AMOUNT, 0); } public static void addPotionAmount(ItemStack itemStack, int amt) { @@ -89,7 +101,7 @@ public static void addPotionAmount(ItemStack itemStack, int amt) { } public static void setPotionAmount(ItemStack itemStack, int amt) { - itemStack.set(JustDireDataComponents.POTION_AMOUNT, Math.max(0, Math.min(getMaxMB(), amt))); + ItemDataHelper.setInt(itemStack, ItemDataKeys.POTION_AMOUNT, Math.max(0, Math.min(getMaxMB(), amt))); if (getPotionAmount(itemStack) == 0) setPotionContents(itemStack, PotionContents.EMPTY); } diff --git a/src/main/java/com/direwolf20/justdirethings/common/items/data/ItemDataHelper.java b/src/main/java/com/direwolf20/justdirethings/common/items/data/ItemDataHelper.java index 4bd0e3131..657b49ef7 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/items/data/ItemDataHelper.java +++ b/src/main/java/com/direwolf20/justdirethings/common/items/data/ItemDataHelper.java @@ -1,12 +1,12 @@ package com.direwolf20.justdirethings.common.items.data; -import com.direwolf20.justdirethings.setup.Registration; import com.mojang.logging.LogUtils; +import com.mojang.serialization.Codec; +import com.mojang.serialization.DataResult; import net.minecraft.core.BlockPos; import net.minecraft.core.GlobalPos; import net.minecraft.core.registries.Registries; import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.DoubleTag; import net.minecraft.nbt.ListTag; import net.minecraft.nbt.StringTag; import net.minecraft.nbt.Tag; @@ -15,6 +15,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; +import net.minecraft.nbt.NbtOps; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @@ -24,10 +25,9 @@ import java.util.UUID; /** - * Small utility for reading/writing attachment-backed item data using plain NBT. + * Small utility for reading/writing per-item data using a dedicated NBT sub-tag on the stack. *

- * Every stack receives an attachment (registered in {@link Registration}) that holds a {@link CompoundTag} - * dedicated to JustDireThings. All helper methods work inside that root tag so callers only have to supply keys. + * All helper methods work inside the {@code "justdirethings"} compound so callers only have to supply keys. */ public final class ItemDataHelper { private static final Logger LOGGER = LogUtils.getLogger(); @@ -36,24 +36,23 @@ public final class ItemDataHelper { private ItemDataHelper() { } - private static CompoundTag getAttachment(ItemStack stack) { - return stack.getData(Registration.ITEM_DATA.get()); - } - private static CompoundTag getOrCreateRoot(ItemStack stack) { - CompoundTag attachment = getAttachment(stack); - if (!attachment.contains(ROOT_TAG, Tag.TAG_COMPOUND)) { - attachment.put(ROOT_TAG, new CompoundTag()); + CompoundTag root = stack.getOrCreateTag(); + if (!root.contains(ROOT_TAG, Tag.TAG_COMPOUND)) { + root.put(ROOT_TAG, new CompoundTag()); } - return attachment.getCompound(ROOT_TAG); + return root.getCompound(ROOT_TAG); } private static Optional getRootOptional(ItemStack stack) { - CompoundTag attachment = getAttachment(stack); - if (attachment.contains(ROOT_TAG, Tag.TAG_COMPOUND)) { - return Optional.of(attachment.getCompound(ROOT_TAG)); + CompoundTag tag = stack.getTag(); + if (tag == null) { + return Optional.empty(); + } + if (!tag.contains(ROOT_TAG, Tag.TAG_COMPOUND)) { + return Optional.empty(); } - return Optional.empty(); + return Optional.of(tag.getCompound(ROOT_TAG)); } /* Basic scalar helpers */ @@ -71,7 +70,7 @@ public static void setDouble(ItemStack stack, String key, double value) { } public static double getDouble(ItemStack stack, String key, double fallback) { - return getRootOptional(stack).map(root -> root.contains(key, DoubleTag.TAG_DOUBLE) ? root.getDouble(key) : fallback).orElse(fallback); + return getRootOptional(stack).map(root -> root.contains(key, Tag.TAG_DOUBLE) ? root.getDouble(key) : fallback).orElse(fallback); } public static void setBoolean(ItemStack stack, String key, boolean value) { @@ -97,6 +96,21 @@ public static String getString(ItemStack stack, String key, String fallback) { return getRootOptional(stack).map(root -> root.contains(key, Tag.TAG_STRING) ? root.getString(key) : fallback).orElse(fallback); } + public static void setItemStack(ItemStack stack, String key, ItemStack value) { + if (value.isEmpty()) { + getOrCreateRoot(stack).remove(key); + } else { + getOrCreateRoot(stack).put(key, value.save(new CompoundTag())); + } + } + + public static ItemStack getItemStack(ItemStack stack, String key) { + return getRootOptional(stack) + .filter(root -> root.contains(key, Tag.TAG_COMPOUND)) + .map(root -> ItemStack.of(root.getCompound(key))) + .orElse(ItemStack.EMPTY); + } + public static void setUuid(ItemStack stack, String key, UUID uuid) { getOrCreateRoot(stack).putUUID(key, uuid); } @@ -227,4 +241,28 @@ public static void remove(ItemStack stack, String key) { public static boolean has(ItemStack stack, String key) { return getRootOptional(stack).map(root -> root.contains(key)).orElse(false); } + + public static void setWithCodec(ItemStack stack, String key, Codec codec, T value) { + DataResult result = codec.encodeStart(NbtOps.INSTANCE, value); + result.resultOrPartial(msg -> LOGGER.warn("Failed to encode '{}' for stack {}: {}", key, stack, msg)) + .ifPresent(tag -> getOrCreateRoot(stack).put(key, tag)); + } + + public static T getWithCodec(ItemStack stack, String key, Codec codec, T fallback) { + Optional rootOptional = getRootOptional(stack); + if (rootOptional.isEmpty()) { + return fallback; + } + CompoundTag root = rootOptional.get(); + if (!root.contains(key)) { + return fallback; + } + Tag tag = root.get(key); + if (tag == null) { + return fallback; + } + return codec.parse(NbtOps.INSTANCE, tag) + .resultOrPartial(msg -> LOGGER.warn("Failed to decode '{}' for stack {}: {}", key, stack, msg)) + .orElse(fallback); + } } diff --git a/src/main/java/com/direwolf20/justdirethings/common/items/datacomponents/JustDireDataComponents.java b/src/main/java/com/direwolf20/justdirethings/common/items/datacomponents/JustDireDataComponents.java index 470714769..e38be30d0 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/items/datacomponents/JustDireDataComponents.java +++ b/src/main/java/com/direwolf20/justdirethings/common/items/datacomponents/JustDireDataComponents.java @@ -1,6 +1,7 @@ package com.direwolf20.justdirethings.common.items.datacomponents; import com.direwolf20.justdirethings.JustDireThings; +import com.direwolf20.justdirethings.common.containers.handlers.JustDireItemContainerContents; import com.direwolf20.justdirethings.common.items.interfaces.Ability; import com.direwolf20.justdirethings.common.items.interfaces.ToolRecords; import com.direwolf20.justdirethings.util.NBTHelpers; @@ -14,7 +15,6 @@ import net.minecraft.network.codec.StreamCodec; import net.minecraft.world.item.alchemy.PotionContents; import net.minecraft.world.item.component.CustomData; -import net.minecraft.world.item.component.ItemContainerContents; import net.neoforged.neoforge.fluids.SimpleFluidContent; import net.neoforged.neoforge.registries.DeferredHolder; import net.neoforged.neoforge.registries.DeferredRegister; @@ -64,8 +64,8 @@ public class JustDireDataComponents { public static final DeferredHolder, DataComponentType>> STUPEFY_TARGETS = COMPONENTS.register("stupefy_targets", () -> DataComponentType.>builder().persistent(Codec.STRING.listOf()).networkSynchronized(ByteBufCodecs.STRING_UTF8.apply(ByteBufCodecs.list())).build()); - public static final DeferredHolder, DataComponentType> ITEMSTACK_HANDLER = COMPONENTS.register("itemstack_handler", () -> DataComponentType.builder().persistent(ItemContainerContents.CODEC).networkSynchronized(ItemContainerContents.STREAM_CODEC).cacheEncoding().build()); - public static final DeferredHolder, DataComponentType> TOOL_CONTENTS = COMPONENTS.register("tool_contents", () -> DataComponentType.builder().persistent(ItemContainerContents.CODEC).networkSynchronized(ItemContainerContents.STREAM_CODEC).cacheEncoding().build()); + public static final DeferredHolder, DataComponentType> ITEMSTACK_HANDLER = COMPONENTS.register("itemstack_handler", () -> DataComponentType.builder().persistent(JustDireItemContainerContents.CODEC).networkSynchronized(JustDireItemContainerContents.STREAM_CODEC).cacheEncoding().build()); + public static final DeferredHolder, DataComponentType> TOOL_CONTENTS = COMPONENTS.register("tool_contents", () -> DataComponentType.builder().persistent(JustDireItemContainerContents.CODEC).networkSynchronized(JustDireItemContainerContents.STREAM_CODEC).cacheEncoding().build()); public static final DeferredHolder, DataComponentType> POTION_CONTENTS = COMPONENTS.register("potion_contents", () -> DataComponentType.builder().persistent(PotionContents.CODEC).networkSynchronized(PotionContents.STREAM_CODEC).cacheEncoding().build()); public static final DeferredHolder, DataComponentType> POTION_AMOUNT = COMPONENTS.register("potion_amount", () -> DataComponentType.builder().persistent(Codec.INT).networkSynchronized(ByteBufCodecs.VAR_INT).build()); public static final DeferredHolder, DataComponentType> EPIC_ARROW = COMPONENTS.register("epic_arrow", () -> DataComponentType.builder().persistent(Codec.BOOL).networkSynchronized(ByteBufCodecs.BOOL).build()); diff --git a/src/main/java/com/direwolf20/justdirethings/common/items/interfaces/ToggleableItem.java b/src/main/java/com/direwolf20/justdirethings/common/items/interfaces/ToggleableItem.java index 4a332815f..05c9f5032 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/items/interfaces/ToggleableItem.java +++ b/src/main/java/com/direwolf20/justdirethings/common/items/interfaces/ToggleableItem.java @@ -1,7 +1,6 @@ package com.direwolf20.justdirethings.common.items.interfaces; -import com.direwolf20.justdirethings.common.items.data.ItemDataHelper; -import com.direwolf20.justdirethings.common.items.data.ItemDataKeys; +import com.direwolf20.justdirethings.common.items.datacomponents.JustDireDataComponents; import net.minecraft.network.chat.Component; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; @@ -10,11 +9,12 @@ public interface ToggleableItem { default boolean getEnabled(ItemStack stack) { - return ItemDataHelper.getBoolean(stack, ItemDataKeys.TOOL_ENABLED, true); //True by default + return stack.getOrDefault(JustDireDataComponents.TOOL_ENABLED, true); //True by default } default void toggleEnabled(ItemStack stack, Player player) { - boolean nowEnabled = ItemDataHelper.toggleBoolean(stack, ItemDataKeys.TOOL_ENABLED, true); + stack.update(JustDireDataComponents.TOOL_ENABLED, true, val -> !val); + boolean nowEnabled = stack.getOrDefault(JustDireDataComponents.TOOL_ENABLED, true); player.displayClientMessage(Component.translatable("justdirethings.toolenabled", stack.getDisplayName(), nowEnabled ? Component.translatable("justdirethings.enabled") : Component.translatable("justdirethings.disabled")), true); if (nowEnabled) player.playNotifySound(SoundEvents.END_PORTAL_FRAME_FILL, SoundSource.PLAYERS, 1.0F, 1.0F); diff --git a/src/main/java/com/direwolf20/justdirethings/common/items/tools/basetools/BaseBow.java b/src/main/java/com/direwolf20/justdirethings/common/items/tools/basetools/BaseBow.java index 1f88f288e..65e8d1783 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/items/tools/basetools/BaseBow.java +++ b/src/main/java/com/direwolf20/justdirethings/common/items/tools/basetools/BaseBow.java @@ -30,8 +30,8 @@ import net.minecraft.world.phys.Vec3; import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.energy.IEnergyStorage; -import net.neoforged.neoforge.items.ComponentItemHandler; import net.neoforged.neoforge.items.IItemHandler; +import net.neoforged.neoforge.items.ItemStackHandler; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -114,7 +114,7 @@ protected Projectile createProjectile(Level level, LivingEntity livingEntity, It return customArrow(justDireArrow, stack, itemStack); IItemHandler itemHandler = itemStack.getCapability(Capabilities.ItemHandler.ITEM); - if (itemHandler instanceof ComponentItemHandler componentItemHandler) { + if (itemHandler instanceof ItemStackHandler componentItemHandler) { PotionContents potionContents = PotionContents.EMPTY; for (int slot = 0; slot < componentItemHandler.getSlots(); slot++) { ItemStack potionCanister = componentItemHandler.getStackInSlot(slot); diff --git a/src/main/java/com/direwolf20/justdirethings/setup/Registration.java b/src/main/java/com/direwolf20/justdirethings/setup/Registration.java index 62c2be3fd..0f0cdb309 100644 --- a/src/main/java/com/direwolf20/justdirethings/setup/Registration.java +++ b/src/main/java/com/direwolf20/justdirethings/setup/Registration.java @@ -19,6 +19,7 @@ import com.direwolf20.justdirethings.common.capabilities.*; import com.direwolf20.justdirethings.common.containers.*; import com.direwolf20.justdirethings.common.containers.handlers.FilterBasicHandler; +import com.direwolf20.justdirethings.common.containers.handlers.PotionCanisterHandler; import com.direwolf20.justdirethings.common.entities.*; import com.direwolf20.justdirethings.common.fluids.basefluids.RefinedFuel; import com.direwolf20.justdirethings.common.fluids.polymorphicfluid.PolymorphicFluid; @@ -76,6 +77,7 @@ import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.BucketItem; import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.item.crafting.RecipeSerializer; import net.minecraft.world.item.crafting.RecipeType; @@ -743,10 +745,30 @@ public static void init(IEventBus eventBus) { () -> IMenuTypeExtension.create(ExperienceHolderContainer::new)); //Data Attachments - public static final Supplier> ITEM_DATA = ATTACHMENT_TYPES.register( - "item_data", () -> AttachmentType.builder(CompoundTag::new).serialize(CompoundTag.CODEC).build()); public static final Supplier> HANDLER = ATTACHMENT_TYPES.register( "handler", () -> AttachmentType.serializable(() -> new ItemStackHandler(1)).build()); + public static final Supplier> TOOL_HANDLER = ATTACHMENT_TYPES.register( + "tool_handler", () -> AttachmentType.serializable(holder -> { + if (holder instanceof ItemStack stack) { + int slots = getToolSlotCount(stack); + if (slots > 0) { + return new ItemStackHandler(slots) { + @Override + public boolean isItemValid(int slot, ItemStack stackIn) { + return stackIn.getItem() instanceof PotionCanister; + } + }; + } + } + return new ItemStackHandler(0); + }).build()); + public static final Supplier> POTION_CANISTER_HANDLER = ATTACHMENT_TYPES.register( + "potion_canister_handler", () -> AttachmentType.serializable(holder -> { + if (holder instanceof ItemStack stack) { + return new PotionCanisterHandler(stack, 1); + } + throw new IllegalStateException("Potion canister handler can only be attached to item stacks"); + }).build()); public static final Supplier> MACHINE_HANDLER = ATTACHMENT_TYPES.register( "machine_handler", () -> AttachmentType.serializable(holder -> { if (holder instanceof BaseMachineBE baseMachineBE) @@ -781,6 +803,23 @@ public static void init(IEventBus eventBus) { }).build()); + private static int getToolSlotCount(ItemStack stack) { + if (stack.is(FerricoreBow.get())) { + return 1; + } + if (stack.is(BlazegoldBow.get())) { + return 2; + } + if (stack.is(CelestigemBow.get())) { + return 3; + } + if (stack.is(EclipseAlloyBow.get())) { + return 4; + } + return 0; + } + + public static final Supplier> ENERGYSTORAGE_MACHINES = ATTACHMENT_TYPES.register( "energystorage_machines", () -> AttachmentType.serializable(holder -> { if (holder instanceof PoweredMachineBE feMachineBE) { diff --git a/src/main/java/com/direwolf20/justdirethings/util/TooltipHelpers.java b/src/main/java/com/direwolf20/justdirethings/util/TooltipHelpers.java index 8ebcdae0a..459937844 100644 --- a/src/main/java/com/direwolf20/justdirethings/util/TooltipHelpers.java +++ b/src/main/java/com/direwolf20/justdirethings/util/TooltipHelpers.java @@ -105,8 +105,8 @@ public static void appendUpgradeDetails(ItemStack stack, List tooltip } public static void appendGeneratorDetails(ItemStack stack, List tooltip) { - ItemStackHandler handler = stack.getData(Registration.HANDLER.get()); - ItemStack fuelStack = handler.getStackInSlot(0); + ItemStackHandler handler = stack.getData(Registration.HANDLER); + ItemStack fuelStack = handler != null ? handler.getStackInSlot(0) : ItemStack.EMPTY; if (Screen.hasShiftDown()) { tooltip.add(Component.translatable("justdirethings.pocketgeneratorburntime", ItemDataHelper.getInt(stack, ItemDataKeys.POCKETGEN_COUNTER, 0),