Added Tesla support.

Closes #124
This commit is contained in:
Ellpeck 2016-06-17 14:00:52 +02:00
parent eaed253629
commit 138a1af26c
8 changed files with 198 additions and 5 deletions

View file

@ -37,6 +37,9 @@ repositories {
maven { maven {
url "http://dvs1.progwml6.com/files/maven" url "http://dvs1.progwml6.com/files/maven"
} }
maven {
url "http://maven.epoxide.xyz"
}
} }
dependencies { dependencies {
@ -44,6 +47,7 @@ dependencies {
//compile "codechicken:CodeChickenCore:1.8-1.0.5.36:dev" //compile "codechicken:CodeChickenCore:1.8-1.0.5.36:dev"
//compile "codechicken:NotEnoughItems:1.8-1.0.5.104:dev" //compile "codechicken:NotEnoughItems:1.8-1.0.5.104:dev"
compile "net.darkhax.tesla:Tesla:1.9.4-1.1.0.24"
deobfCompile "mezz.jei:jei_1.9.4:3.4.0.202" deobfCompile "mezz.jei:jei_1.9.4:3.4.0.202"
compile files("lib/Rarmor.jar") compile files("lib/Rarmor.jar")
} }

View file

@ -13,7 +13,7 @@ package de.ellpeck.actuallyadditions.mod.blocks;
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockPlant; import de.ellpeck.actuallyadditions.mod.blocks.base.BlockPlant;
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockStair; import de.ellpeck.actuallyadditions.mod.blocks.base.BlockStair;
import de.ellpeck.actuallyadditions.mod.blocks.metalists.TheMiscBlocks; import de.ellpeck.actuallyadditions.mod.blocks.metalists.TheMiscBlocks;
import de.ellpeck.actuallyadditions.mod.util.CompatUtil; import de.ellpeck.actuallyadditions.mod.util.compat.CompatUtil;
import de.ellpeck.actuallyadditions.mod.util.ModUtil; import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import net.minecraft.block.Block; import net.minecraft.block.Block;

View file

@ -22,7 +22,7 @@ import de.ellpeck.actuallyadditions.mod.items.metalists.TheFoods;
import de.ellpeck.actuallyadditions.mod.items.metalists.TheMiscItems; import de.ellpeck.actuallyadditions.mod.items.metalists.TheMiscItems;
import de.ellpeck.actuallyadditions.mod.material.InitArmorMaterials; import de.ellpeck.actuallyadditions.mod.material.InitArmorMaterials;
import de.ellpeck.actuallyadditions.mod.material.InitToolMaterials; import de.ellpeck.actuallyadditions.mod.material.InitToolMaterials;
import de.ellpeck.actuallyadditions.mod.util.CompatUtil; import de.ellpeck.actuallyadditions.mod.util.compat.CompatUtil;
import de.ellpeck.actuallyadditions.mod.util.ModUtil; import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.Util; import de.ellpeck.actuallyadditions.mod.util.Util;
import net.minecraft.init.Blocks; import net.minecraft.init.Blocks;

View file

@ -17,6 +17,7 @@ import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketServerToClient; import de.ellpeck.actuallyadditions.mod.network.PacketServerToClient;
import de.ellpeck.actuallyadditions.mod.util.ModUtil; import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import de.ellpeck.actuallyadditions.mod.util.compat.TeslaUtil;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
@ -30,6 +31,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fml.common.ModAPIManager;
import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.registry.GameRegistry; import net.minecraftforge.fml.common.registry.GameRegistry;
@ -39,6 +41,8 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
public boolean isRedstonePowered; public boolean isRedstonePowered;
protected int ticksElapsed; protected int ticksElapsed;
public static boolean teslaLoaded;
public TileEntityBase(String name){ public TileEntityBase(String name){
this.name = "container."+ModUtil.MOD_ID+"."+name; this.name = "container."+ModUtil.MOD_ID+"."+name;
} }
@ -95,6 +99,14 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
GameRegistry.registerTileEntity(TileEntityItemViewer.class, ModUtil.MOD_ID+":tileItemViewer"); GameRegistry.registerTileEntity(TileEntityItemViewer.class, ModUtil.MOD_ID+":tileItemViewer");
GameRegistry.registerTileEntity(TileEntityBookletStand.class, ModUtil.MOD_ID+":tileEntityBookletStand"); GameRegistry.registerTileEntity(TileEntityBookletStand.class, ModUtil.MOD_ID+":tileEntityBookletStand");
GameRegistry.registerTileEntity(TileEntityDisplayStand.class, ModUtil.MOD_ID+":tileEntityDisplayStand"); GameRegistry.registerTileEntity(TileEntityDisplayStand.class, ModUtil.MOD_ID+":tileEntityDisplayStand");
if(ModAPIManager.INSTANCE.hasAPI("Tesla|API")){
ModUtil.LOGGER.info("Tesla API loaded... Activating Tesla Power System integration...");
teslaLoaded = true;
}
else{
ModUtil.LOGGER.info("Tesla API not found! Skipping Tesla Power System integration.");
}
} }
@Override @Override
@ -233,6 +245,12 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
return (T)tank; return (T)tank;
} }
} }
else if(teslaLoaded){
T cap = TeslaUtil.getTeslaCapability(this, capability, facing);
if(cap != null){
return cap;
}
}
return super.getCapability(capability, facing); return super.getCapability(capability, facing);
} }

View file

@ -12,6 +12,8 @@ package de.ellpeck.actuallyadditions.mod.util;
import cofh.api.energy.IEnergyProvider; import cofh.api.energy.IEnergyProvider;
import cofh.api.energy.IEnergyReceiver; import cofh.api.energy.IEnergyReceiver;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.util.compat.TeslaUtil;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid; import net.minecraft.block.BlockLiquid;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
@ -30,6 +32,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.network.play.client.CPacketPlayerDigging; import net.minecraft.network.play.client.CPacketPlayerDigging;
import net.minecraft.network.play.server.SPacketBlockChange; import net.minecraft.network.play.server.SPacketBlockChange;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityBanner;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
@ -90,6 +93,9 @@ public final class WorldUtil{
} }
} }
} }
else if(TileEntityBase.teslaLoaded){
TeslaUtil.doTeslaInteraction(tile, otherTile, side);
}
} }
} }
} }
@ -114,8 +120,8 @@ public final class WorldUtil{
} }
//Push and pull with new fluid system //Push and pull with new fluid system
else{ else{
IFluidHandler handlerFrom = tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, i == 0 ? side.getOpposite() : side); IFluidHandler handlerFrom = (i == 0 ? tile : otherTile).getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, i == 0 ? side : side.getOpposite());
IFluidHandler handlerTo = otherTile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, i == 0 ? side : side.getOpposite()); IFluidHandler handlerTo = (i == 0 ? otherTile : tile).getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, i == 0 ? side.getOpposite() : side);
if(handlerFrom != null && handlerTo != null){ if(handlerFrom != null && handlerTo != null){
FluidStack drain = handlerFrom.drain(Integer.MAX_VALUE, false); FluidStack drain = handlerFrom.drain(Integer.MAX_VALUE, false);
if(drain != null){ if(drain != null){

View file

@ -8,7 +8,7 @@
* © 2015-2016 Ellpeck * © 2015-2016 Ellpeck
*/ */
package de.ellpeck.actuallyadditions.mod.util; package de.ellpeck.actuallyadditions.mod.util.compat;
import de.ellpeck.actuallyadditions.mod.items.base.ItemSeed; import de.ellpeck.actuallyadditions.mod.items.base.ItemSeed;
import net.minecraft.block.Block; import net.minecraft.block.Block;

View file

@ -0,0 +1,71 @@
/*
* This file ("TeslaHandler.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015-2016 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.util.compat;
import cofh.api.energy.IEnergyHandler;
import cofh.api.energy.IEnergyProvider;
import cofh.api.energy.IEnergyReceiver;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import net.darkhax.tesla.api.ITeslaConsumer;
import net.darkhax.tesla.api.ITeslaHolder;
import net.darkhax.tesla.api.ITeslaProducer;
import net.minecraft.util.EnumFacing;
public class TeslaHandler implements ITeslaProducer, ITeslaHolder, ITeslaConsumer{
private final EnumFacing side;
private final TileEntityBase tile;
public TeslaHandler(TileEntityBase tile, EnumFacing side){
this.tile = tile;
this.side = side;
}
@Override
public long givePower(long power, boolean simulated){
if(this.tile instanceof IEnergyReceiver){
return ((IEnergyReceiver)this.tile).receiveEnergy(this.side, (int)power, simulated);
}
else{
return 0;
}
}
@Override
public long getStoredPower(){
if(this.tile instanceof IEnergyHandler){
return ((IEnergyHandler)this.tile).getEnergyStored(this.side);
}
else{
return 0;
}
}
@Override
public long getCapacity(){
if(this.tile instanceof IEnergyHandler){
return ((IEnergyHandler)this.tile).getMaxEnergyStored(this.side);
}
else{
return 0;
}
}
@Override
public long takePower(long power, boolean simulated){
if(this.tile instanceof IEnergyProvider){
return ((IEnergyProvider)this.tile).extractEnergy(this.side, (int)power, simulated);
}
else{
return 0;
}
}
}

View file

@ -0,0 +1,94 @@
/*
* This file ("TeslaUtil.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015-2016 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.util.compat;
import cofh.api.energy.IEnergyHandler;
import cofh.api.energy.IEnergyProvider;
import cofh.api.energy.IEnergyReceiver;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import net.darkhax.tesla.api.ITeslaConsumer;
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.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import java.util.HashMap;
import java.util.Map;
public final class TeslaUtil{
@CapabilityInject(ITeslaConsumer.class)
public static Capability<ITeslaConsumer> teslaConsumer = null;
@CapabilityInject(ITeslaProducer.class)
public static Capability<ITeslaProducer> teslaProducer = null;
@CapabilityInject(ITeslaHolder.class)
public static Capability<ITeslaHolder> teslaHolder = null;
private static final Map<TileEntityBase, TeslaHandler[]> TESLA_MAP = new HashMap<TileEntityBase, TeslaHandler[]>();
public static <T> T getTeslaCapability(TileEntityBase tile, Capability<T> capability, EnumFacing facing){
boolean receive = tile instanceof IEnergyReceiver && capability == teslaConsumer;
boolean provide = tile instanceof IEnergyProvider && capability == teslaProducer;
boolean hold = tile instanceof IEnergyHandler && capability == teslaHolder;
if(receive || provide || hold){
return (T)getHandler(tile, facing);
}
else{
return null;
}
}
public static boolean doTeslaInteraction(TileEntity tile, TileEntity otherTile, EnumFacing side){
ITeslaConsumer handlerTo = null;
ITeslaProducer handlerFrom = null;
for(int i = 0; i < 2; i++){
if(handlerFrom == null){
handlerFrom = (i == 0 ? tile : otherTile).getCapability(teslaProducer, i == 0 ? side : side.getOpposite());
}
if(handlerTo == null){
handlerTo = (i == 0 ? otherTile : tile).getCapability(teslaConsumer, i == 0 ? side.getOpposite() : side);
}
}
if(handlerFrom != null && handlerTo != null){
long drain = handlerFrom.takePower(Integer.MAX_VALUE, true);
if(drain > 0){
long filled = handlerTo.givePower(drain, false);
handlerFrom.takePower(filled, false);
return true;
}
}
return false;
}
private static TeslaHandler getHandler(TileEntityBase tile, EnumFacing facing){
TeslaHandler[] handlers = TESLA_MAP.get(tile);
if(handlers == null || handlers.length != 6){
handlers = new TeslaHandler[6];
TESLA_MAP.put(tile, handlers);
}
int side = facing.ordinal();
if(handlers[side] == null){
handlers[side] = new TeslaHandler(tile, facing);
}
return handlers[side];
}
}