Skip to content

Commit

Permalink
Initial transition of cold fire igniter from capability to attachment…
Browse files Browse the repository at this point in the history
…. Doesn't serialize NBT by itself yet, but can be done easily now.
  • Loading branch information
Rover656 committed Feb 18, 2024
1 parent e88f79f commit 8b84b33
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.enderio.core.common.attachment;

import com.enderio.core.common.capability.StrictFluidHandlerItemStack;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.fluids.capability.templates.FluidHandlerItemStack;

import java.util.function.Predicate;
import java.util.function.Supplier;

public class AttachmentUtil {
// TODO: NEO-PORT: Would be nice for there to be a variant that doesn't act upon the item's NBT.
public static Supplier<AttachmentType<FluidHandlerItemStack>> itemFluidHandlerAttachment() {
return () -> AttachmentType.builder(holder -> {
if (holder instanceof ItemStack itemStack) {
int capacity = 1000;
if (itemStack.getItem() instanceof IItemFluidHandlerConfig attachedFluidTank) {
capacity = attachedFluidTank.getCapacity();
}

return new FluidHandlerItemStack(itemStack, capacity);
} else {
throw new IllegalStateException("Cannot attach fluid handler item to a non-item.");
}
}).build();
}

public static Supplier<AttachmentType<StrictFluidHandlerItemStack>> strictItemFluidHandlerAttachment() {
return () -> AttachmentType.builder(holder -> {
if (holder instanceof ItemStack itemStack) {
Predicate<Fluid> filter = f -> true;
int capacity = 1000;
if (itemStack.getItem() instanceof IStrictItemFluidHandlerConfig attachedFluidTank) {
capacity = attachedFluidTank.getCapacity();
filter = attachedFluidTank.getFluidFilter();
}

return new StrictFluidHandlerItemStack(itemStack, capacity, filter);
} else {
throw new IllegalStateException("Cannot attach fluid handler item to a non-item.");
}
}).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.enderio.core.common.attachment;

public interface IItemFluidHandlerConfig {
int getCapacity();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.enderio.core.common.attachment;

import net.minecraft.world.level.material.Fluid;

import java.util.function.Predicate;

public interface IStrictItemFluidHandlerConfig extends IItemFluidHandlerConfig {
Predicate<Fluid> getFluidFilter();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.enderio.base.common.capability;
package com.enderio.core.common.capability;

import net.minecraft.tags.TagKey;
import net.minecraft.world.item.ItemStack;
Expand All @@ -11,19 +11,19 @@
/**
* FluidHandler for Items which only accept specific fluids
*/
public class AcceptingFluidItemHandler extends FluidHandlerItemStack {
public class StrictFluidHandlerItemStack extends FluidHandlerItemStack {

private final Predicate<Fluid> fluidPredicate;

public AcceptingFluidItemHandler(ItemStack container, int capacity, Fluid validFluid) {
public StrictFluidHandlerItemStack(ItemStack container, int capacity, Fluid validFluid) {
this(container, capacity, fluid -> fluid == validFluid);
}

public AcceptingFluidItemHandler(ItemStack container, int capacity, TagKey<Fluid> validFluid) {
public StrictFluidHandlerItemStack(ItemStack container, int capacity, TagKey<Fluid> validFluid) {
this(container, capacity, fluid -> fluid.is(validFluid));
}

public AcceptingFluidItemHandler(ItemStack container, int capacity, Predicate<Fluid> isFluidValid) {
public StrictFluidHandlerItemStack(ItemStack container, int capacity, Predicate<Fluid> isFluidValid) {
super(container, capacity);
fluidPredicate = isFluidValid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import com.enderio.api.attachment.StoredEntityData;
import com.enderio.api.attachment.CoordinateSelection;
import com.enderio.base.common.capacitor.LootCapacitorData;
import com.enderio.core.common.attachment.AttachmentUtil;
import com.enderio.core.common.capability.StrictFluidHandlerItemStack;
import com.mojang.serialization.Codec;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.fluids.capability.templates.FluidHandlerItemStack;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
Expand All @@ -28,6 +31,12 @@ public class EIOAttachments {
public static final Supplier<AttachmentType<LootCapacitorData>> LOOT_CAPACITOR_DATA
= ATTACHMENT_TYPES.register("loot_capacitor_data", () -> AttachmentType.serializable(LootCapacitorData::new).build());

public static final Supplier<AttachmentType<FluidHandlerItemStack>> ITEM_SIMPLE_FLUID
= ATTACHMENT_TYPES.register("item_simple_fluid", AttachmentUtil.itemFluidHandlerAttachment());

public static final Supplier<AttachmentType<StrictFluidHandlerItemStack>> ITEM_STRICT_FLUID
= ATTACHMENT_TYPES.register("item_strict_fluid", AttachmentUtil.strictItemFluidHandlerAttachment());

public static void register(IEventBus bus) {
ATTACHMENT_TYPES.register(bus);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.enderio.base.common.item.tool;

import com.enderio.base.common.capability.AcceptingFluidItemHandler;
import com.enderio.base.common.init.EIOAttachments;
import com.enderio.core.common.attachment.IStrictItemFluidHandlerConfig;
import com.enderio.core.common.capability.StrictFluidHandlerItemStack;
import com.enderio.base.common.init.EIOBlocks;
import com.enderio.base.common.init.EIOFluids;
import com.enderio.base.common.tag.EIOTags;
Expand All @@ -23,6 +25,7 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.capabilities.ICapabilityProvider;
import net.neoforged.neoforge.fluids.FluidStack;
Expand All @@ -32,11 +35,12 @@
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.function.Predicate;

public class ColdFireIgniter extends Item implements ITabVariants {
public class ColdFireIgniter extends Item implements ITabVariants, IStrictItemFluidHandlerConfig {

public static ICapabilityProvider<ItemStack, Void, IFluidHandlerItem> FLUID_HANDLER_PROVIDER =
(stack, v) -> new AcceptingFluidItemHandler(stack, 1000, EIOTags.Fluids.COLD_FIRE_IGNITER_FUEL);
(stack, v) -> stack.getData(EIOAttachments.ITEM_STRICT_FLUID);

public ColdFireIgniter(Properties properties) {
super(properties);
Expand Down Expand Up @@ -118,10 +122,21 @@ public void addAllVariants(CreativeModeTab.Output modifier) {

var fluidHandler = is.getCapability(Capabilities.FluidHandler.ITEM);
if (fluidHandler != null) {
if (fluidHandler instanceof AcceptingFluidItemHandler acceptingFluidItemHandler) {
acceptingFluidItemHandler.setFluid(new FluidStack(EIOFluids.VAPOR_OF_LEVITY.getSource(), fluidHandler.getTankCapacity(0)));
if (fluidHandler instanceof StrictFluidHandlerItemStack strictFluidHandlerItemStack) {
strictFluidHandlerItemStack.setFluid(new FluidStack(EIOFluids.VAPOR_OF_LEVITY.getSource(), fluidHandler.getTankCapacity(0)));
modifier.accept(is);
}
}
}

@Override
public int getCapacity() {
// TODO: Config
return 1000;
}

@Override
public Predicate<Fluid> getFluidFilter() {
return f -> f.is(EIOTags.Fluids.COLD_FIRE_IGNITER_FUEL);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.enderio.base.common.item.tool;

import com.enderio.base.common.capability.AcceptingFluidItemHandler;
import com.enderio.core.common.capability.StrictFluidHandlerItemStack;
import com.enderio.base.common.config.BaseConfig;
import com.enderio.base.common.init.EIOFluids;
import com.enderio.base.common.tag.EIOTags;
Expand All @@ -19,7 +19,7 @@
public class LevitationStaffItem extends PoweredToggledItem {

public static final ICapabilityProvider<ItemStack, Void, IFluidHandlerItem> FLUID_HANDLER_PROVIDER
= (stack, v) -> new AcceptingFluidItemHandler(stack, 1000, EIOTags.Fluids.STAFF_OF_LEVITY_FUEL);
= (stack, v) -> new StrictFluidHandlerItemStack(stack, 1000, EIOTags.Fluids.STAFF_OF_LEVITY_FUEL);

public LevitationStaffItem(Properties pProperties) {
super(pProperties);
Expand Down Expand Up @@ -67,8 +67,8 @@ protected void setFullCharge(ItemStack pStack) {

var fluidHandler = pStack.getCapability(Capabilities.FluidHandler.ITEM);
if (fluidHandler != null) {
if (fluidHandler instanceof AcceptingFluidItemHandler acceptingFluidItemHandler) {
acceptingFluidItemHandler.setFluid(new FluidStack(EIOFluids.VAPOR_OF_LEVITY.getSource(), fluidHandler.getTankCapacity(0)));
if (fluidHandler instanceof StrictFluidHandlerItemStack strictFluidHandlerItemStack) {
strictFluidHandlerItemStack.setFluid(new FluidStack(EIOFluids.VAPOR_OF_LEVITY.getSource(), fluidHandler.getTankCapacity(0)));
}
}
}
Expand Down

0 comments on commit 8b84b33

Please sign in to comment.