Basic Capability management implementation
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
package me.hypherionmc.craterlib.api.blockentities.caps;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author HypherionSA
|
||||||
|
* @date 24/09/2022
|
||||||
|
*/
|
||||||
|
public enum ForgeCapability {
|
||||||
|
ENERGY,
|
||||||
|
ITEM,
|
||||||
|
FLUID
|
||||||
|
}
|
@@ -0,0 +1,15 @@
|
|||||||
|
package me.hypherionmc.craterlib.api.blockentities.caps;
|
||||||
|
|
||||||
|
import net.minecraft.core.Direction;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author HypherionSA
|
||||||
|
* @date 24/09/2022
|
||||||
|
*/
|
||||||
|
public interface IForgeCapProvider {
|
||||||
|
|
||||||
|
<T> Optional<T> getForgeCapability(ForgeCapability capability, Direction side);
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,47 @@
|
|||||||
|
package me.hypherionmc.craterlib.common.blockentity;
|
||||||
|
|
||||||
|
import me.hypherionmc.craterlib.api.blockentities.caps.ForgeCapability;
|
||||||
|
import me.hypherionmc.craterlib.api.blockentities.caps.IForgeCapProvider;
|
||||||
|
import net.minecraft.core.BlockPos;
|
||||||
|
import net.minecraft.core.Direction;
|
||||||
|
import net.minecraft.nbt.CompoundTag;
|
||||||
|
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
||||||
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||||
|
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||||
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author HypherionSA
|
||||||
|
* @date 24/09/2022
|
||||||
|
*/
|
||||||
|
public class CraterBlockEntity extends BlockEntity implements IForgeCapProvider {
|
||||||
|
|
||||||
|
public CraterBlockEntity(BlockEntityType<?> blockEntityType, BlockPos pos, BlockState state) {
|
||||||
|
super(blockEntityType, pos, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendUpdates() {
|
||||||
|
level.blockEntityChanged(this.getBlockPos());
|
||||||
|
level.sendBlockUpdated(this.getBlockPos(), level.getBlockState(this.getBlockPos()), level.getBlockState(this.getBlockPos()), 3);
|
||||||
|
setChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ClientboundBlockEntityDataPacket getUpdatePacket() {
|
||||||
|
return ClientboundBlockEntityDataPacket.create(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompoundTag getUpdateTag() {
|
||||||
|
CompoundTag compoundTag = new CompoundTag();
|
||||||
|
saveAdditional(compoundTag);
|
||||||
|
return compoundTag;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> Optional<T> getForgeCapability(ForgeCapability capability, Direction side) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
@@ -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",
|
"package": "me.hypherionmc.craterlib.mixin",
|
||||||
"compatibilityLevel": "JAVA_17",
|
"compatibilityLevel": "JAVA_17",
|
||||||
"mixins": [
|
"mixins": [
|
||||||
|
"BlockEntityMixin"
|
||||||
],
|
],
|
||||||
"client": [
|
"client": [
|
||||||
"ConfigScreenHandlerMixin"
|
"ConfigScreenHandlerMixin"
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
# Project
|
# Project
|
||||||
version_major=0
|
version_major=0
|
||||||
version_minor=0
|
version_minor=0
|
||||||
version_patch=1d
|
version_patch=3d
|
||||||
group=me.hypherionmc.craterlib
|
group=me.hypherionmc.craterlib
|
||||||
|
|
||||||
# Common
|
# Common
|
||||||
|
Reference in New Issue
Block a user