From 650f6b76b7c6e9f9a27592d3a12bab448fee9cc3 Mon Sep 17 00:00:00 2001 From: thepigcat Date: Wed, 4 Sep 2024 20:36:59 +0200 Subject: [PATCH] more power stuff --- .../modjam/api/items/IFluidItem.java | 7 +++++++ .../modjam/api/items/IPowerItem.java | 7 +++++++ .../modjam/capabilities/MJCapabilities.java | 2 +- .../capabilities/power/ItemPowerWrapper.java | 12 +++++++++++ .../modjam/data/MJDataComponents.java | 21 +++++++++++++++++++ .../modjam/events/CapabilityAttachEvent.java | 14 +++++++++++++ 6 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/portingdeadmods/modjam/api/items/IFluidItem.java create mode 100644 src/main/java/com/portingdeadmods/modjam/api/items/IPowerItem.java create mode 100644 src/main/java/com/portingdeadmods/modjam/data/MJDataComponents.java diff --git a/src/main/java/com/portingdeadmods/modjam/api/items/IFluidItem.java b/src/main/java/com/portingdeadmods/modjam/api/items/IFluidItem.java new file mode 100644 index 00000000..da31028b --- /dev/null +++ b/src/main/java/com/portingdeadmods/modjam/api/items/IFluidItem.java @@ -0,0 +1,7 @@ +package com.portingdeadmods.modjam.api.items; + +/** + * Implement this interface on your item to auto register the capability for it + */ +public interface IFluidItem { +} diff --git a/src/main/java/com/portingdeadmods/modjam/api/items/IPowerItem.java b/src/main/java/com/portingdeadmods/modjam/api/items/IPowerItem.java new file mode 100644 index 00000000..33dc681c --- /dev/null +++ b/src/main/java/com/portingdeadmods/modjam/api/items/IPowerItem.java @@ -0,0 +1,7 @@ +package com.portingdeadmods.modjam.api.items; + +/** + * Implement this interface on your item to auto register the capability for it + */ +public interface IPowerItem { +} diff --git a/src/main/java/com/portingdeadmods/modjam/capabilities/MJCapabilities.java b/src/main/java/com/portingdeadmods/modjam/capabilities/MJCapabilities.java index e053839b..62c8b3b3 100644 --- a/src/main/java/com/portingdeadmods/modjam/capabilities/MJCapabilities.java +++ b/src/main/java/com/portingdeadmods/modjam/capabilities/MJCapabilities.java @@ -12,7 +12,7 @@ public final class MJCapabilities { public static final class PowerStorage { public static final BlockCapability BLOCK = BlockCapability.createSided(create("power"), IPowerStorage.class); - public static final ItemCapability ITEM = ItemCapability.createVoid(create("power"), IPowerStorage.class); + public static final ItemCapability ITEM = ItemCapability.createVoid(create("power"), IPowerStorage.class); public static final EntityCapability ENTITY = EntityCapability.createSided(create("power"), IPowerStorage.class); } diff --git a/src/main/java/com/portingdeadmods/modjam/capabilities/power/ItemPowerWrapper.java b/src/main/java/com/portingdeadmods/modjam/capabilities/power/ItemPowerWrapper.java index bbde128b..83005d4e 100644 --- a/src/main/java/com/portingdeadmods/modjam/capabilities/power/ItemPowerWrapper.java +++ b/src/main/java/com/portingdeadmods/modjam/capabilities/power/ItemPowerWrapper.java @@ -1,9 +1,21 @@ package com.portingdeadmods.modjam.capabilities.power; +import com.portingdeadmods.modjam.data.MJDataComponents; +import com.portingdeadmods.modjam.data.components.ComponentPowerStorage; import net.minecraft.world.item.ItemStack; import org.jetbrains.annotations.Range; public record ItemPowerWrapper(ItemStack itemStack) implements IPowerStorage { + public ItemPowerWrapper { + if (!itemStack.has(MJDataComponents.POWER)) { + throw new IllegalStateException("The item: " + itemStack); + } + } + + private ComponentPowerStorage getComponent() { + return itemStack.getOrDefault(MJDataComponents.POWER, ComponentPowerStorage.EMPTY); + } + @Override public int getPowerStored() { return 0; diff --git a/src/main/java/com/portingdeadmods/modjam/data/MJDataComponents.java b/src/main/java/com/portingdeadmods/modjam/data/MJDataComponents.java new file mode 100644 index 00000000..d2aa9f47 --- /dev/null +++ b/src/main/java/com/portingdeadmods/modjam/data/MJDataComponents.java @@ -0,0 +1,21 @@ +package com.portingdeadmods.modjam.data; + +import com.portingdeadmods.modjam.ModJam; +import com.portingdeadmods.modjam.data.components.ComponentPowerStorage; +import net.minecraft.core.component.DataComponentType; +import net.neoforged.neoforge.registries.DeferredRegister; + +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +public final class MJDataComponents { + public static final DeferredRegister.DataComponents DATA_COMPONENT_TYPES = DeferredRegister.createDataComponents(ModJam.MODID); + + public static final Supplier> POWER = registerDataComponentType("power", + () -> builder -> builder.persistent(ComponentPowerStorage.CODEC).networkSynchronized(ComponentPowerStorage.STREAM_CODEC)); + + public static Supplier> registerDataComponentType( + String name, Supplier>> builderOperator) { + return DATA_COMPONENT_TYPES.register(name, () -> builderOperator.get().apply(DataComponentType.builder()).build()); + } +} diff --git a/src/main/java/com/portingdeadmods/modjam/events/CapabilityAttachEvent.java b/src/main/java/com/portingdeadmods/modjam/events/CapabilityAttachEvent.java index b5408d31..e2e6cb0e 100644 --- a/src/main/java/com/portingdeadmods/modjam/events/CapabilityAttachEvent.java +++ b/src/main/java/com/portingdeadmods/modjam/events/CapabilityAttachEvent.java @@ -1,6 +1,11 @@ package com.portingdeadmods.modjam.events; import com.portingdeadmods.modjam.ModJam; +import com.portingdeadmods.modjam.api.items.IPowerItem; +import com.portingdeadmods.modjam.capabilities.MJCapabilities; +import com.portingdeadmods.modjam.capabilities.power.ItemPowerWrapper; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.world.item.Item; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.neoforge.capabilities.Capabilities; @@ -12,6 +17,15 @@ public final class CapabilityAttachEvent { @SubscribeEvent public static void registerCapabilities(RegisterCapabilitiesEvent event) { + + } + + private static void registerItemCaps(RegisterCapabilitiesEvent event) { + for (Item item : BuiltInRegistries.ITEM) { + if (item instanceof IPowerItem) { + event.registerItem(MJCapabilities.PowerStorage.ITEM, (stack, ctx) -> new ItemPowerWrapper(stack), item); + } + } } }