mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-12 16:27:47 +00:00
Introducing a new standardized energy capability system.
Heavily inspired by the old RedstoneFlux API developed by King Lemming and Team CoFH.
This commit is contained in:
1 parent
5964bf8cb2
commit
45097fed0c
4 files changed
+183
No files matched your search
@@ -54,6 +54,7 @@ import net.minecraftforge.common.config.Configuration;
|
||||
import net.minecraftforge.common.config.Property;
|
||||
import net.minecraftforge.common.model.animation.CapabilityAnimation;
|
||||
import net.minecraftforge.common.network.ForgeNetworkHandler;
|
||||
import net.minecraftforge.energy.CapabilityEnergy;
|
||||
import net.minecraftforge.fluids.FluidRegistry;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
@@ -401,6 +402,7 @@ public class ForgeModContainer extends DummyModContainer implements WorldAccessC
|
||||
CapabilityItemHandler.register();
|
||||
CapabilityFluidHandler.register();
|
||||
CapabilityAnimation.register();
|
||||
CapabilityEnergy.register();
|
||||
MinecraftForge.EVENT_BUS.register(MinecraftForge.INTERNAL_HANDLER);
|
||||
ForgeChunkManager.captureConfig(evt.getModConfigurationDirectory());
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package net.minecraftforge.energy;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import net.minecraft.nbt.NBTBase;
|
||||
import net.minecraft.nbt.NBTTagInt;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.capabilities.*;
|
||||
import net.minecraftforge.common.capabilities.Capability.IStorage;
|
||||
|
||||
public class CapabilityEnergy
|
||||
{
|
||||
@CapabilityInject(IEnergyStorage.class)
|
||||
public static Capability<IEnergyStorage> ENERGY = null;
|
||||
|
||||
public static void register()
|
||||
{
|
||||
CapabilityManager.INSTANCE.register(IEnergyStorage.class, new IStorage<IEnergyStorage>()
|
||||
{
|
||||
@Override
|
||||
public NBTBase writeNBT(Capability<IEnergyStorage> capability, IEnergyStorage instance, EnumFacing side)
|
||||
{
|
||||
return new NBTTagInt(instance.getEnergyStored());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readNBT(Capability<IEnergyStorage> capability, IEnergyStorage instance, EnumFacing side, NBTBase nbt)
|
||||
{
|
||||
if (!(instance instanceof EnergyStorage))
|
||||
throw new IllegalArgumentException("Can not deserialize to an instance that isn't the default implementation");
|
||||
((EnergyStorage)instance).energy = ((NBTTagInt)nbt).getInt();
|
||||
}
|
||||
},
|
||||
new Callable<IEnergyStorage>()
|
||||
{
|
||||
@Override
|
||||
public IEnergyStorage call() throws Exception
|
||||
{
|
||||
return new EnergyStorage(1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package net.minecraftforge.energy;
|
||||
|
||||
/**
|
||||
* Reference implementation of {@link IEnergyStorage}. Use/extend this or implement your own.
|
||||
*
|
||||
* Derived from the Redstone Flux power system designed by King Lemming and originally utilized in Thermal Expansion and related mods.
|
||||
* Created with consent and permission of King Lemming and Team CoFH. Released with permission under LGPL 2.1 when bundled with Forge.
|
||||
*/
|
||||
public class EnergyStorage implements IEnergyStorage
|
||||
{
|
||||
protected int energy;
|
||||
protected int capacity;
|
||||
protected int maxReceive;
|
||||
protected int maxExtract;
|
||||
|
||||
public EnergyStorage(int capacity)
|
||||
{
|
||||
this(capacity, capacity, capacity);
|
||||
}
|
||||
|
||||
public EnergyStorage(int capacity, int maxTransfer)
|
||||
{
|
||||
this(capacity, maxTransfer, maxTransfer);
|
||||
}
|
||||
|
||||
public EnergyStorage(int capacity, int maxReceive, int maxExtract)
|
||||
{
|
||||
this.capacity = capacity;
|
||||
this.maxReceive = maxReceive;
|
||||
this.maxExtract = maxExtract;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int receiveEnergy(int maxReceive, boolean simulate)
|
||||
{
|
||||
if (!canReceive())
|
||||
return 0;
|
||||
|
||||
int energyReceived = Math.min(capacity - energy, Math.min(this.maxReceive, maxReceive));
|
||||
if (!simulate)
|
||||
energy += energyReceived;
|
||||
return energyReceived;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int extractEnergy(int maxExtract, boolean simulate)
|
||||
{
|
||||
if (!canExtract())
|
||||
return 0;
|
||||
|
||||
int energyExtracted = Math.min(energy, Math.min(this.maxExtract, maxExtract));
|
||||
if (!simulate)
|
||||
energy -= energyExtracted;
|
||||
return energyExtracted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEnergyStored()
|
||||
{
|
||||
return energy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxEnergyStored()
|
||||
{
|
||||
return capacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExtract()
|
||||
{
|
||||
return this.maxExtract > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canReceive()
|
||||
{
|
||||
return this.maxReceive > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package net.minecraftforge.energy;
|
||||
|
||||
/**
|
||||
* An energy storage is the unit of interaction with Energy inventories.
|
||||
* <p>
|
||||
* A reference implementation can be found at {@link EnergyStorage}.
|
||||
*
|
||||
* Derived from the Redstone Flux power system designed by King Lemming and originally utilized in Thermal Expansion and related mods.
|
||||
* Created with consent and permission of King Lemming and Team CoFH. Released with permission under LGPL 2.1 when bundled with Forge.
|
||||
*
|
||||
*/
|
||||
public interface IEnergyStorage
|
||||
{
|
||||
/**
|
||||
* Adds energy to the storage. Returns quantity of energy that was accepted.
|
||||
*
|
||||
* @param maxReceive
|
||||
* Maximum amount of energy to be inserted.
|
||||
* @param simulate
|
||||
* If TRUE, the insertion will only be simulated.
|
||||
* @return Amount of energy that was (or would have been, if simulated) accepted by the storage.
|
||||
*/
|
||||
int receiveEnergy(int maxReceive, boolean simulate);
|
||||
|
||||
/**
|
||||
* Removes energy from the storage. Returns quantity of energy that was removed.
|
||||
*
|
||||
* @param maxExtract
|
||||
* Maximum amount of energy to be extracted.
|
||||
* @param simulate
|
||||
* If TRUE, the extraction will only be simulated.
|
||||
* @return Amount of energy that was (or would have been, if simulated) extracted from the storage.
|
||||
*/
|
||||
int extractEnergy(int maxExtract, boolean simulate);
|
||||
|
||||
/**
|
||||
* Returns the amount of energy currently stored.
|
||||
*/
|
||||
int getEnergyStored();
|
||||
|
||||
/**
|
||||
* Returns the maximum amount of energy that can be stored.
|
||||
*/
|
||||
int getMaxEnergyStored();
|
||||
|
||||
/**
|
||||
* Returns if this storage can have energy extracted.
|
||||
* If this is false, then any calls to extractEnergy will return 0.
|
||||
*/
|
||||
boolean canExtract();
|
||||
|
||||
/**
|
||||
* Used to determine if this storage can receive energy.
|
||||
* If this is false, then any calls to receiveEnergy will return 0.
|
||||
*/
|
||||
boolean canReceive();
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user