-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added fluid tank and energy storage attachments to block entities
- Loading branch information
1 parent
4612297
commit 621dd67
Showing
12 changed files
with
305 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/main/java/dev/latvian/mods/kubejs/block/entity/EnergyStorageAttachment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package dev.latvian.mods.kubejs.block.entity; | ||
|
||
import dev.latvian.mods.kubejs.bindings.DirectionWrapper; | ||
import net.minecraft.core.Direction; | ||
import net.neoforged.neoforge.capabilities.BlockCapability; | ||
import net.neoforged.neoforge.capabilities.Capabilities; | ||
import net.neoforged.neoforge.energy.EnergyStorage; | ||
import net.neoforged.neoforge.energy.IEnergyStorage; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class EnergyStorageAttachment implements BlockEntityAttachment { | ||
public static final BlockEntityAttachmentType TYPE = new BlockEntityAttachmentType("energy_storage", Factory.class); | ||
|
||
public record Factory(int capacity, Optional<Integer> maxReceive, Optional<Integer> maxExtract, Optional<Integer> autoOutput) implements BlockEntityAttachmentFactory { | ||
@Override | ||
public BlockEntityAttachment create(BlockEntityAttachmentInfo info, KubeBlockEntity entity) { | ||
int rx = Math.max(0, maxReceive.orElse(0)); | ||
int tx = Math.max(0, maxExtract.orElse(0)); | ||
int auto = Math.max(0, autoOutput.orElse(0)); | ||
return new EnergyStorageAttachment(entity, capacity, rx, tx, auto, (auto > 0 ? info.directions() : DirectionWrapper.EMPTY_SET).toArray(new Direction[0])); | ||
} | ||
|
||
@Override | ||
public boolean isTicking() { | ||
return autoOutput.isPresent() && autoOutput.get() > 0; | ||
} | ||
|
||
@Override | ||
public List<BlockCapability<?, ?>> getCapabilities() { | ||
return List.of(Capabilities.EnergyStorage.BLOCK); | ||
} | ||
} | ||
|
||
public static class Wrapped extends EnergyStorage { | ||
private final EnergyStorageAttachment attachment; | ||
|
||
public Wrapped(EnergyStorageAttachment attachment, int capacity, int maxReceive, int maxExtract) { | ||
super(capacity, maxReceive, maxExtract); | ||
this.attachment = attachment; | ||
} | ||
|
||
@Override | ||
public int extractEnergy(int toExtract, boolean simulate) { | ||
int s = super.extractEnergy(toExtract, simulate); | ||
|
||
if (s > 0 && !simulate && !attachment.entity.getLevel().isClientSide()) { | ||
attachment.entity.save(); | ||
} | ||
|
||
return s; | ||
} | ||
|
||
@Override | ||
public int receiveEnergy(int toReceive, boolean simulate) { | ||
int s = super.receiveEnergy(toReceive, simulate); | ||
|
||
if (s > 0 && !simulate && !attachment.entity.getLevel().isClientSide()) { | ||
attachment.entity.save(); | ||
} | ||
|
||
return s; | ||
} | ||
} | ||
|
||
private final KubeBlockEntity entity; | ||
public final Wrapped energyStorage; | ||
public final int autoOutput; | ||
public final Direction[] autoOutputDirections; | ||
|
||
public EnergyStorageAttachment(KubeBlockEntity entity, int capacity, int maxReceive, int maxExtract, int autoOutput, Direction[] autoOutputDirections) { | ||
this.entity = entity; | ||
this.energyStorage = new Wrapped(this, capacity, maxReceive, maxExtract); | ||
this.autoOutput = autoOutput; | ||
this.autoOutputDirections = autoOutputDirections; | ||
} | ||
|
||
@Override | ||
public Object getWrappedObject() { | ||
return energyStorage; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public <CAP, SRC> CAP getCapability(BlockCapability<CAP, SRC> capability) { | ||
if (capability == Capabilities.EnergyStorage.BLOCK) { | ||
return (CAP) energyStorage; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public void serverTick() { | ||
if (autoOutputDirections.length > 0 && autoOutput > 0) { | ||
var list = new ArrayList<IEnergyStorage>(1); | ||
|
||
for (var dir : autoOutputDirections) { | ||
var c = Capabilities.EnergyStorage.BLOCK.getCapability(entity.getLevel(), entity.getBlockPos().relative(dir), null, null, dir.getOpposite()); | ||
|
||
if (c != null) { | ||
list.add(c); | ||
} | ||
} | ||
|
||
if (!list.isEmpty()) { | ||
int draw = Math.min(autoOutput, energyStorage.getEnergyStored()) / list.size(); | ||
|
||
if (draw > 0) { | ||
for (var c : list) { | ||
int e = energyStorage.extractEnergy(draw, true); | ||
|
||
if (e > 0) { | ||
energyStorage.extractEnergy(c.receiveEnergy(e, false), false); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/dev/latvian/mods/kubejs/block/entity/FluidTankAttachment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package dev.latvian.mods.kubejs.block.entity; | ||
|
||
import net.minecraft.core.HolderLookup; | ||
import net.minecraft.nbt.Tag; | ||
import net.neoforged.neoforge.capabilities.BlockCapability; | ||
import net.neoforged.neoforge.capabilities.Capabilities; | ||
import net.neoforged.neoforge.fluids.FluidStack; | ||
import net.neoforged.neoforge.fluids.capability.templates.FluidTank; | ||
import net.neoforged.neoforge.fluids.crafting.FluidIngredient; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
|
||
public class FluidTankAttachment implements BlockEntityAttachment { | ||
public static final BlockEntityAttachmentType TYPE = new BlockEntityAttachmentType("fluid_tank", Factory.class); | ||
|
||
public record Factory(int capacity, Optional<FluidIngredient> inputFilter) implements BlockEntityAttachmentFactory { | ||
private static final Predicate<FluidStack> ALWAYS_TRUE = stack -> true; | ||
|
||
@Override | ||
public BlockEntityAttachment create(BlockEntityAttachmentInfo info, KubeBlockEntity entity) { | ||
return new FluidTankAttachment(entity, capacity, inputFilter.isEmpty() ? ALWAYS_TRUE : inputFilter.get()); | ||
} | ||
|
||
@Override | ||
public List<BlockCapability<?, ?>> getCapabilities() { | ||
return List.of(Capabilities.EnergyStorage.BLOCK); | ||
} | ||
} | ||
|
||
public static class Wrapped extends FluidTank { | ||
private final FluidTankAttachment attachment; | ||
|
||
public Wrapped(FluidTankAttachment attachment, int capacity, Predicate<FluidStack> inputFilter) { | ||
super(capacity, inputFilter); | ||
this.attachment = attachment; | ||
} | ||
|
||
@Override | ||
protected void onContentsChanged() { | ||
attachment.entity.save(); | ||
} | ||
} | ||
|
||
public final KubeBlockEntity entity; | ||
public final Wrapped fluidTank; | ||
|
||
public FluidTankAttachment(KubeBlockEntity entity, int capacity, Predicate<FluidStack> filter) { | ||
this.entity = entity; | ||
this.fluidTank = new Wrapped(this, capacity, filter); | ||
} | ||
|
||
@Override | ||
public Object getWrappedObject() { | ||
return fluidTank; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public <CAP, SRC> CAP getCapability(BlockCapability<CAP, SRC> capability) { | ||
if (capability == Capabilities.FluidHandler.BLOCK) { | ||
return (CAP) fluidTank; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public Tag serialize(HolderLookup.Provider registries) { | ||
return fluidTank.getFluid().isEmpty() ? null : fluidTank.getFluid().save(registries); | ||
} | ||
|
||
@Override | ||
public void deserialize(HolderLookup.Provider registries, @Nullable Tag tag) { | ||
fluidTank.setFluid(tag == null ? FluidStack.EMPTY : FluidStack.parse(registries, tag).orElse(FluidStack.EMPTY)); | ||
} | ||
} |
Oops, something went wrong.