Added Forge Units support to everything

This commit is contained in:
Ellpeck 2016-11-26 19:00:02 +01:00
parent 3176bafd2b
commit b9a84481d1
13 changed files with 176 additions and 35 deletions

View file

@ -24,6 +24,7 @@ import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@ -94,7 +95,7 @@ public class ContainerEnergizer extends Container{
//Other Slots in Inventory excluded
else if(slot >= inventoryStart){
//Shift from Inventory
if(newStack.getItem() instanceof IEnergyContainerItem || (ActuallyAdditions.teslaLoaded && newStack.hasCapability(TeslaUtil.teslaConsumer, null))){
if(newStack.getItem() instanceof IEnergyContainerItem || (ActuallyAdditions.teslaLoaded && newStack.hasCapability(TeslaUtil.teslaConsumer, null)) || newStack.hasCapability(CapabilityEnergy.ENERGY, null)){
if(!this.mergeItemStack(newStack, 0, 1, false)){
return StackUtil.getNull();
}

View file

@ -24,6 +24,7 @@ import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@ -93,7 +94,7 @@ public class ContainerEnervator extends Container{
//Other Slots in Inventory excluded
else if(slot >= inventoryStart){
//Shift from Inventory
if(newStack.getItem() instanceof IEnergyContainerItem || (ActuallyAdditions.teslaLoaded && newStack.hasCapability(TeslaUtil.teslaProducer, null))){
if(newStack.getItem() instanceof IEnergyContainerItem || (ActuallyAdditions.teslaLoaded && newStack.hasCapability(TeslaUtil.teslaProducer, null)) || newStack.hasCapability(CapabilityEnergy.ENERGY, null)){
if(!this.mergeItemStack(newStack, 0, 1, false)){
return StackUtil.getNull();
}

View file

@ -28,6 +28,8 @@ import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import java.util.List;
@ -55,16 +57,22 @@ public class ItemBattery extends ItemEnergy{
for(int i = 0; i < player.inventory.getSizeInventory(); i++){
ItemStack slot = player.inventory.getStackInSlot(i);
if(StackUtil.isValid(slot)){
int extractable = this.extractEnergy(stack, Integer.MAX_VALUE, true);
int received = 0;
Item item = slot.getItem();
if(item instanceof IEnergyContainerItem){
received = ((IEnergyContainerItem)item).receiveEnergy(slot, this.extractEnergy(stack, Integer.MAX_VALUE, true), false);
if(slot.getItem() instanceof IEnergyContainerItem){
received = ((IEnergyContainerItem)slot.getItem()).receiveEnergy(slot, extractable, false);
}
else if(slot.hasCapability(CapabilityEnergy.ENERGY, null)){
IEnergyStorage cap = slot.getCapability(CapabilityEnergy.ENERGY, null);
if(cap != null){
received = cap.receiveEnergy(extractable, false);
}
}
else if(ActuallyAdditions.teslaLoaded && slot.hasCapability(TeslaUtil.teslaConsumer, null)){
ITeslaConsumer cap = slot.getCapability(TeslaUtil.teslaConsumer, null);
if(cap != null){
received = (int)cap.givePower(this.extractEnergy(stack, Integer.MAX_VALUE, true), false);
received = (int)cap.givePower(extractable, false);
}
}

View file

@ -10,6 +10,8 @@
package de.ellpeck.actuallyadditions.mod.items.base;
import cofh.api.energy.IEnergyContainerItem;
import cofh.api.energy.IEnergyStorage;
import cofh.api.energy.ItemEnergyContainer;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
@ -20,12 +22,17 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.text.NumberFormat;
import java.util.List;
@ -119,7 +126,7 @@ public abstract class ItemEnergy extends ItemEnergyContainer{
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt){
return ActuallyAdditions.teslaLoaded ? new ItemTeslaWrapper(stack, this) : null;
return new EnergyCapabilityProvider(stack, this);
}
public int extractEnergyInternal(ItemStack stack, int maxExtract, boolean simulate){
@ -141,4 +148,60 @@ public abstract class ItemEnergy extends ItemEnergyContainer{
this.setMaxReceive(before);
return toReturn;
}
private static class EnergyCapabilityProvider implements ICapabilityProvider{
private final Object forgeUnitsWrapper;
private Object teslaWrapper;
private final IEnergyContainerItem item;
private final ItemStack stack;
public EnergyCapabilityProvider(final ItemStack stack, final IEnergyContainerItem item){
this.stack = stack;
this.item = item;
this.forgeUnitsWrapper = new IEnergyStorage(){
@Override
public int receiveEnergy(int maxReceive, boolean simulate){
return item.receiveEnergy(stack, maxReceive, simulate);
}
@Override
public int extractEnergy(int maxExtract, boolean simulate){
return item.extractEnergy(stack, maxExtract, simulate);
}
@Override
public int getEnergyStored(){
return item.getEnergyStored(stack);
}
@Override
public int getMaxEnergyStored(){
return item.getMaxEnergyStored(stack);
}
};
}
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing){
return this.getCapability(capability, facing) != null;
}
@Nullable
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing){
if(capability == CapabilityEnergy.ENERGY){
return (T)this.forgeUnitsWrapper;
}
else if(ActuallyAdditions.teslaLoaded){
if(this.teslaWrapper == null){
this.teslaWrapper = new ItemTeslaWrapper(this.stack, this.item);
}
return (T)this.teslaWrapper;
}
return null;
}
}
}

View file

@ -353,6 +353,15 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
}
@Override
public void invalidate(){
super.invalidate();
if(ActuallyAdditions.teslaLoaded){
TeslaUtil.removeTile(this);
}
}
public enum NBTType{
SAVE_TILE,
SYNC,

View file

@ -20,6 +20,7 @@ import net.darkhax.tesla.api.ITeslaHolder;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@ -59,6 +60,13 @@ public class TileEntityEnergizer extends TileEntityInventoryBase implements ICus
received = (item.receiveEnergy(this.slots.get(0), this.storage.getEnergyStored(), false));
canTakeUp = item.getEnergyStored(this.slots.get(0)) >= item.getMaxEnergyStored(this.slots.get(0));
}
else if(this.slots.get(0).hasCapability(CapabilityEnergy.ENERGY, null)){
IEnergyStorage cap = this.slots.get(0).getCapability(CapabilityEnergy.ENERGY, null);
if(cap != null){
received = cap.receiveEnergy(this.storage.getEnergyStored(), false);
canTakeUp = cap.getEnergyStored() >= cap.getMaxEnergyStored();
}
}
else if(ActuallyAdditions.teslaLoaded){
if(this.slots.get(0).hasCapability(TeslaUtil.teslaConsumer, null)){
ITeslaConsumer cap = this.slots.get(0).getCapability(TeslaUtil.teslaConsumer, null);
@ -92,7 +100,7 @@ public class TileEntityEnergizer extends TileEntityInventoryBase implements ICus
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
return i == 0 && (stack.getItem() instanceof IEnergyContainerItem || (ActuallyAdditions.teslaLoaded && stack.hasCapability(TeslaUtil.teslaConsumer, null)));
return i == 0 && (stack.getItem() instanceof IEnergyContainerItem || (ActuallyAdditions.teslaLoaded && stack.hasCapability(TeslaUtil.teslaConsumer, null)) || stack.hasCapability(CapabilityEnergy.ENERGY, null));
}
@Override

View file

@ -10,7 +10,6 @@
package de.ellpeck.actuallyadditions.mod.tile;
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyContainerItem;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
@ -20,6 +19,7 @@ import net.darkhax.tesla.api.ITeslaProducer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@ -60,6 +60,13 @@ public class TileEntityEnervator extends TileEntityInventoryBase implements ISha
extracted = item.extractEnergy(this.slots.get(0), maxExtract, false);
canTakeUp = item.getEnergyStored(this.slots.get(0)) <= 0;
}
else if(this.slots.get(0).hasCapability(CapabilityEnergy.ENERGY, null)){
IEnergyStorage cap = this.slots.get(0).getCapability(CapabilityEnergy.ENERGY, null);
if(cap != null){
extracted = cap.extractEnergy(maxExtract, false);
canTakeUp = cap.getEnergyStored() <= 0;
}
}
else if(ActuallyAdditions.teslaLoaded){
if(this.slots.get(0).hasCapability(TeslaUtil.teslaProducer, null)){
ITeslaProducer cap = this.slots.get(0).getCapability(TeslaUtil.teslaProducer, null);
@ -93,7 +100,7 @@ public class TileEntityEnervator extends TileEntityInventoryBase implements ISha
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
return i == 0 && (stack.getItem() instanceof IEnergyContainerItem || (ActuallyAdditions.teslaLoaded && stack.hasCapability(TeslaUtil.teslaProducer, null)));
return i == 0 && (stack.getItem() instanceof IEnergyContainerItem || (ActuallyAdditions.teslaLoaded && stack.hasCapability(TeslaUtil.teslaProducer, null)) || stack.hasCapability(CapabilityEnergy.ENERGY, null));
}
@Override

View file

@ -23,6 +23,8 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import java.util.ArrayList;
import java.util.HashMap;
@ -89,7 +91,7 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
BlockPos pos = this.getPos().offset(side);
TileEntity tile = this.worldObj.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityLaserRelay)){
if(tile instanceof IEnergyReceiver || (ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, side.getOpposite()))){
if(tile instanceof IEnergyReceiver || (ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, side.getOpposite())) || tile.hasCapability(CapabilityEnergy.ENERGY, side.getOpposite())){
this.receiversAround.put(side, tile);
TileEntity oldTile = old.get(side);
@ -165,6 +167,21 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
}
}
}
else if(tile.hasCapability(CapabilityEnergy.ENERGY, opp)){
IEnergyStorage cap = tile.getCapability(CapabilityEnergy.ENERGY, opp);
if(cap != null){
int theoreticalReceived = cap.receiveEnergy(Math.min(amountPer, lowestCap), true);
if(theoreticalReceived > 0){
int deduct = this.calcDeduction(theoreticalReceived, highestLoss);
if(deduct >= theoreticalReceived){ //Happens with small numbers
deduct = 0;
}
transmitted += cap.receiveEnergy(theoreticalReceived-deduct, simulate);
transmitted += deduct;
}
}
}
else if(ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, opp)){
ITeslaConsumer cap = tile.getCapability(TeslaUtil.teslaConsumer, opp);
if(cap != null){

View file

@ -22,6 +22,8 @@ import net.darkhax.tesla.api.ITeslaHolder;
import net.darkhax.tesla.api.ITeslaProducer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
public class TileEntityPhantomEnergyface extends TileEntityPhantomface implements ICustomEnergyReceiver, ISharingEnergyProvider{
@ -38,6 +40,12 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
if(tile instanceof IEnergyReceiver){
return ((IEnergyReceiver)tile).receiveEnergy(from, maxReceive, simulate);
}
else if(tile.hasCapability(CapabilityEnergy.ENERGY, from)){
IEnergyStorage cap = tile.getCapability(CapabilityEnergy.ENERGY, from);
if(cap != null){
return cap.receiveEnergy(maxReceive, simulate);
}
}
else if(ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, from)){
ITeslaConsumer cap = tile.getCapability(TeslaUtil.teslaConsumer, from);
if(cap != null){
@ -57,6 +65,12 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
if(tile instanceof IEnergyProvider){
return ((IEnergyProvider)tile).extractEnergy(from, maxExtract, simulate);
}
else if(tile.hasCapability(CapabilityEnergy.ENERGY, from)){
IEnergyStorage cap = tile.getCapability(CapabilityEnergy.ENERGY, from);
if(cap != null){
return cap.extractEnergy(maxExtract, simulate);
}
}
else if(ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaProducer, from)){
ITeslaProducer cap = tile.getCapability(TeslaUtil.teslaProducer, from);
if(cap != null){
@ -76,6 +90,12 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
if(tile instanceof IEnergyHandler){
return ((IEnergyHandler)tile).getEnergyStored(from);
}
else if(tile.hasCapability(CapabilityEnergy.ENERGY, from)){
IEnergyStorage cap = tile.getCapability(CapabilityEnergy.ENERGY, from);
if(cap != null){
return cap.getEnergyStored();
}
}
else if(ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaHolder, from)){
ITeslaHolder cap = tile.getCapability(TeslaUtil.teslaHolder, from);
if(cap != null){
@ -95,6 +115,12 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
if(tile instanceof IEnergyHandler){
return ((IEnergyHandler)tile).getMaxEnergyStored(from);
}
else if(tile.hasCapability(CapabilityEnergy.ENERGY, from)){
IEnergyStorage cap = tile.getCapability(CapabilityEnergy.ENERGY, from);
if(cap != null){
return cap.getMaxEnergyStored();
}
}
else if(ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaHolder, from)){
ITeslaHolder cap = tile.getCapability(TeslaUtil.teslaHolder, from);
if(cap != null){
@ -114,8 +140,12 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
if(tile instanceof IEnergyHandler){
return true;
}
else if(ActuallyAdditions.teslaLoaded){
else{
for(EnumFacing facing : EnumFacing.values()){
if(tile.hasCapability(CapabilityEnergy.ENERGY, facing)){
return true;
}
else if(ActuallyAdditions.teslaLoaded){
if(tile.hasCapability(TeslaUtil.teslaHolder, facing)){
return true;
}
@ -123,6 +153,7 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
}
}
}
}
return false;
}
@ -135,7 +166,7 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
return ((IEnergyConnection)tile).canConnectEnergy(from);
}
else{
return ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaHolder, from);
return tile.hasCapability(CapabilityEnergy.ENERGY, from) || (ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaHolder, from));
}
}
}

View file

@ -21,6 +21,7 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import java.util.UUID;
@ -70,6 +71,12 @@ public class TileEntityPlayerInterface extends TileEntityInventoryBase implement
if(item instanceof IEnergyContainerItem){
received = ((IEnergyContainerItem)item).receiveEnergy(slot, this.storage.getEnergyStored(), false);
}
else if(slot.hasCapability(CapabilityEnergy.ENERGY, null)){
IEnergyStorage cap = slot.getCapability(CapabilityEnergy.ENERGY, null);
if(cap != null){
received = cap.receiveEnergy(this.storage.getEnergyStored(), false);
}
}
else if(ActuallyAdditions.teslaLoaded && slot.hasCapability(TeslaUtil.teslaConsumer, null)){
ITeslaConsumer cap = slot.getCapability(TeslaUtil.teslaConsumer, null);
if(cap != null){

View file

@ -91,12 +91,6 @@ public final class WorldUtil{
}
}
if(ActuallyAdditions.teslaLoaded){
if(TeslaUtil.doWrappedTeslaRFInteraction(tileFrom, tileTo, sideTo, maxTransfer)){
return;
}
}
if(tileFrom.hasCapability(CapabilityEnergy.ENERGY, sideTo) && tileTo.hasCapability(CapabilityEnergy.ENERGY, sideTo.getOpposite())){
IEnergyStorage handlerFrom = tileFrom.getCapability(CapabilityEnergy.ENERGY, sideTo);
IEnergyStorage handlerTo = tileTo.getCapability(CapabilityEnergy.ENERGY, sideTo.getOpposite());
@ -106,9 +100,14 @@ public final class WorldUtil{
if(drain > 0){
int filled = handlerTo.receiveEnergy(drain, false);
handlerFrom.extractEnergy(filled, false);
return;
}
}
}
if(ActuallyAdditions.teslaLoaded){
TeslaUtil.doWrappedTeslaRFInteraction(tileFrom, tileTo, sideTo, maxTransfer);
}
}
}

View file

@ -15,11 +15,8 @@ import net.darkhax.tesla.api.ITeslaConsumer;
import net.darkhax.tesla.api.ITeslaHolder;
import net.darkhax.tesla.api.ITeslaProducer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
public class ItemTeslaWrapper implements ITeslaProducer, ITeslaHolder, ITeslaConsumer, ICapabilityProvider{
public class ItemTeslaWrapper implements ITeslaProducer, ITeslaHolder, ITeslaConsumer{
private final ItemStack stack;
private final IEnergyContainerItem item;
@ -48,14 +45,4 @@ public class ItemTeslaWrapper implements ITeslaProducer, ITeslaHolder, ITeslaCon
public long takePower(long power, boolean simulated){
return this.item.extractEnergy(this.stack, (int)power, simulated);
}
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing){
return capability == TeslaUtil.teslaProducer || capability == TeslaUtil.teslaHolder || capability == TeslaUtil.teslaConsumer;
}
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing){
return this.hasCapability(capability, facing) ? (T)this : null;
}
}

View file

@ -47,6 +47,10 @@ public final class TeslaUtil{
}
}
public static void removeTile(TileEntityBase tile){
TESLA_MAP.remove(tile);
}
public static boolean doWrappedTeslaRFInteraction(TileEntity tileFrom, TileEntity tileTo, EnumFacing side, int maxTransfer){
if(tileTo.hasCapability(teslaConsumer, side.getOpposite()) && tileFrom.hasCapability(teslaProducer, side)){
ITeslaConsumer handlerTo = tileTo.getCapability(teslaConsumer, side.getOpposite());
@ -77,5 +81,4 @@ public final class TeslaUtil{
}
return handlers[side];
}
}