Basic Capability management implementation
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package me.hypherionmc.craterlib.mixin;
|
||||
|
||||
import me.hypherionmc.craterlib.api.blockentities.caps.ForgeCapability;
|
||||
import me.hypherionmc.craterlib.api.blockentities.caps.IForgeCapProvider;
|
||||
import me.hypherionmc.craterlib.common.blockentity.CraterBlockEntity;
|
||||
import me.hypherionmc.craterlib.systems.energy.CustomEnergyStorage;
|
||||
import me.hypherionmc.craterlib.systems.energy.ForgeEnergyWrapper;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ForgeCapabilities;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* @date 24/09/2022
|
||||
*/
|
||||
@Mixin(CraterBlockEntity.class)
|
||||
public class BlockEntityMixin implements ICapabilityProvider {
|
||||
|
||||
@Override
|
||||
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
|
||||
IForgeCapProvider capProvider = (IForgeCapProvider) this;
|
||||
|
||||
if (cap == ForgeCapabilities.ENERGY) {
|
||||
Optional<CustomEnergyStorage> forgeCap = capProvider.getForgeCapability(ForgeCapability.ENERGY, side);
|
||||
if (forgeCap.isPresent()) {
|
||||
return LazyOptional.of(() -> new ForgeEnergyWrapper(forgeCap.get())).cast();
|
||||
}
|
||||
}
|
||||
|
||||
return LazyOptional.empty();
|
||||
}
|
||||
}
|
@@ -1,48 +0,0 @@
|
||||
package me.hypherionmc.craterlib.systems.energy;
|
||||
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* @date 03/07/2022
|
||||
* Forge Energy Support on top of the custom system
|
||||
*/
|
||||
public class ForgeCustomEnergyStorage extends CustomEnergyStorage implements IEnergyStorage {
|
||||
|
||||
|
||||
public ForgeCustomEnergyStorage(int capacity) {
|
||||
super(capacity);
|
||||
}
|
||||
|
||||
public ForgeCustomEnergyStorage(int powerCapacity, int maxTransfer) {
|
||||
super(powerCapacity, maxTransfer);
|
||||
}
|
||||
|
||||
public ForgeCustomEnergyStorage(int powerCapacity, int maxInput, int maxOutput) {
|
||||
super(powerCapacity, maxInput, maxOutput);
|
||||
}
|
||||
|
||||
public ForgeCustomEnergyStorage(int capacity, int maxInput, int maxOutput, int initialPower) {
|
||||
super(capacity, maxInput, maxOutput, initialPower);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEnergyStored() {
|
||||
return this.getPowerLevel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxEnergyStored() {
|
||||
return this.getPowerCapacity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExtract() {
|
||||
return this.maxOutput > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canReceive() {
|
||||
return this.maxInput > 0;
|
||||
}
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
package me.hypherionmc.craterlib.systems.energy;
|
||||
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* @date 03/07/2022
|
||||
* Forge Energy Support on top of the custom system
|
||||
*/
|
||||
public record ForgeEnergyWrapper(CustomEnergyStorage storage) implements IEnergyStorage {
|
||||
|
||||
@Override
|
||||
public int receiveEnergy(int maxReceive, boolean simulate) {
|
||||
return storage.receiveEnergy(maxReceive, simulate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int extractEnergy(int maxExtract, boolean simulate) {
|
||||
return storage.extractEnergy(maxExtract, simulate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEnergyStored() {
|
||||
return storage.getPowerLevel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxEnergyStored() {
|
||||
return storage.getPowerCapacity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExtract() {
|
||||
return storage.getMaxOutput() > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canReceive() {
|
||||
return storage.getMaxInput() > 0;
|
||||
}
|
||||
}
|
@@ -4,6 +4,7 @@
|
||||
"package": "me.hypherionmc.craterlib.mixin",
|
||||
"compatibilityLevel": "JAVA_17",
|
||||
"mixins": [
|
||||
"BlockEntityMixin"
|
||||
],
|
||||
"client": [
|
||||
"ConfigScreenHandlerMixin"
|
||||
|
Reference in New Issue
Block a user