Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ base {
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
vendor.set(JvmVendorSpec.JETBRAINS)
}
}

Expand Down
2 changes: 0 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
18 changes: 5 additions & 13 deletions src/main/java/com/direwolf20/justdirethings/JustDireThings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Slot> dynamicSlots = new ArrayList<>();
public ComponentItemHandler componentItemHandler;
public ItemStackHandler componentItemHandler;


public ToolSettingContainer(int windowId, Inventory playerInventory, Player player, FriendlyByteBuf extraData) {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ItemContainerContents> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -81,15 +93,15 @@ 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) {
setPotionAmount(itemStack, getPotionAmount(itemStack) + 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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.
* <p>
* 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();
Expand All @@ -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<CompoundTag> 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 */
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 <T> void setWithCodec(ItemStack stack, String key, Codec<T> codec, T value) {
DataResult<Tag> 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> T getWithCodec(ItemStack stack, String key, Codec<T> codec, T fallback) {
Optional<CompoundTag> 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);
}
}
Loading
Loading