Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Leclowndu93150 committed Sep 4, 2024
2 parents 6b199e8 + 075f595 commit 91516c0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public final class MJCapabilities {
public static final class PowerStorage {
public static final BlockCapability<IPowerStorage, @Nullable Direction> BLOCK = BlockCapability.createSided(create("power"), IPowerStorage.class);
public static final ItemCapability<IPowerStorage, Void> ITEM = ItemCapability.createVoid(create("power"), IPowerStorage.class);
public static final ItemCapability<IPowerStorage, @Nullable Void> ITEM = ItemCapability.createVoid(create("power"), IPowerStorage.class);
public static final EntityCapability<IPowerStorage, @Nullable Direction> ENTITY = EntityCapability.createSided(create("power"), IPowerStorage.class);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DataComponentType<ComponentPowerStorage>> POWER = registerDataComponentType("power",
() -> builder -> builder.persistent(ComponentPowerStorage.CODEC).networkSynchronized(ComponentPowerStorage.STREAM_CODEC));

public static <T> Supplier<DataComponentType<T>> registerDataComponentType(
String name, Supplier<UnaryOperator<DataComponentType.Builder<T>>> builderOperator) {
return DATA_COMPONENT_TYPES.register(name, () -> builderOperator.get().apply(DataComponentType.builder()).build());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
}

}

0 comments on commit 91516c0

Please sign in to comment.