Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package loordgek.teslatest;
import net.darkhax.tesla.capability.TeslaCapabilities;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.EnergyStorage;
import javax.annotation.Nullable;
public class tile extends TileEntity {
EnergyStorage energyStorage = new EnergyStorage(20000);
Wrapper wrapper = new Wrapper(energyStorage);
@Override
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
return capability == CapabilityEnergy.ENERGY ||
capability == TeslaCapabilities.CAPABILITY_CONSUMER ||
capability == TeslaCapabilities.CAPABILITY_HOLDER ||
capability == TeslaCapabilities.CAPABILITY_PRODUCER ||
super.hasCapability(capability, facing);
}
@Nullable
@Override
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
if (capability == CapabilityEnergy.ENERGY)
return CapabilityEnergy.ENERGY.cast(energyStorage);
if (capability == TeslaCapabilities.CAPABILITY_CONSUMER)
return TeslaCapabilities.CAPABILITY_CONSUMER.cast(wrapper);
if (capability == TeslaCapabilities.CAPABILITY_HOLDER)
return TeslaCapabilities.CAPABILITY_HOLDER.cast(wrapper);
if (capability == TeslaCapabilities.CAPABILITY_PRODUCER)
return TeslaCapabilities.CAPABILITY_PRODUCER.cast(wrapper);
return super.getCapability(capability, facing);
}
}
package loordgek.teslatest;
import net.darkhax.tesla.api.ITeslaConsumer;
import net.darkhax.tesla.api.ITeslaHolder;
import net.darkhax.tesla.api.ITeslaProducer;
import net.minecraftforge.energy.IEnergyStorage;
public class Wrapper implements ITeslaConsumer, ITeslaHolder, ITeslaProducer {
private final IEnergyStorage energyStorage;
public Wrapper(IEnergyStorage energyStorage) {
this.energyStorage = energyStorage;
}
/**
* Offers power to the Tesla Consumer.
*
* @param power The amount of power to offer.
* @param simulated Whether or not this is being called as part of a simulation.
* Simulations are used to get information without affecting the Tesla Producer.
* @return The amount of power that the consumer accepts.
*/
@Override
public long givePower(long power, boolean simulated) {
return energyStorage.receiveEnergy((int) power, simulated);
}
/**
* Gets the amount of Tesla power stored being stored.
*
* @return The amount of Tesla power being stored.
*/
@Override
public long getStoredPower() {
return energyStorage.getEnergyStored();
}
/**
* Gets the maximum amount of Tesla power that can be held.
*
* @return The maximum amount of Tesla power that can be held.
*/
@Override
public long getCapacity() {
return energyStorage.getMaxEnergyStored();
}
/**
* Requests an amount of power from the Tesla Producer.
*
* @param power The amount of power to request.
* @param simulated Whether or not this is being called as part of a simulation.
* Simulations are used to get information without affecting the Tesla Producer.
* @return The amount of power that the Tesla Producer will give.
*/
@Override
public long takePower(long power, boolean simulated) {
return energyStorage.extractEnergy((int) power, simulated);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment