Finalize IC2 electric item support (#1644)

* Item charging no longer uses CapabilityEnergy, now fully uses ExternalPowerSystems

* Remove accidental update from build.gradle

* Remove some unused imports

* Use requestEnergyFromArmor in all powered tools
This commit is contained in:
coderbot16 2018-12-28 01:25:40 -08:00 committed by Modmuss50
parent 5724fc3d8c
commit fbec0c89c2
13 changed files with 69 additions and 64 deletions

View file

@ -41,9 +41,9 @@ import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.energy.IEnergyStorage;
import prospector.shootingstar.ShootingStar;
import prospector.shootingstar.model.ModelCompound;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import reborncore.common.util.WorldUtils;
import techreborn.events.TRRecipeHandler;
@ -170,7 +170,7 @@ public class BlockRubberLog extends Block {
if (stack.isEmpty()) {
return false;
}
IEnergyStorage capEnergy = null;
ForgePowerItemManager capEnergy = null;
if (stack.getItem() instanceof ItemElectricTreetap) {
capEnergy = new ForgePowerItemManager(stack);
}
@ -183,6 +183,8 @@ public class BlockRubberLog extends Block {
if (!worldIn.isRemote) {
if (capEnergy != null) {
capEnergy.extractEnergy(20, false);
ExternalPowerSystems.requestEnergyFromArmor(capEnergy, playerIn);
} else {
playerIn.getHeldItem(EnumHand.MAIN_HAND).damageItem(1, playerIn);
}

View file

@ -34,11 +34,10 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
@ -66,26 +65,11 @@ public class ItemLithiumBatpack extends ItemArmor implements IEnergyItemInfo {
return;
}
// TODO: Find out how to become compatible with IC2 items
// IC2 handles battery pack charging in a very peculiar way. Battery packs do not actually distribute power on their own,
// rather items explicitly request charging from armor slots. The default ElectricItemManager implementation will only
// draw power from an IElectricItem, so items utilizing ISpecialElectricItem or IBackupElectricItemManager will not work.
// This means that the only way for battery armor to be 100% compatible with IC2 is to be completely managed by the
// ElectricItemManager. This will obviously not work. Instead, we should automatically distribute power to IC2 electric items.
// However, this brings up a somewhat unrelated problem: TechReborn items do not know how to explicitly pull power from IC2
// battery packs. Perhaps we need to add a function to ExternalPowerManager to hint that an ItemStack has discharged,
// which will appropriately ask IC2 battery packs for energy.
IEnergyStorage capEnergy = new ForgePowerItemManager(itemStack);
ForgePowerItemManager capEnergy = new ForgePowerItemManager(itemStack);
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
if (!player.inventory.getStackInSlot(i).isEmpty()) {
ItemStack item = player.inventory.getStackInSlot(i);
if (!item.hasCapability(CapabilityEnergy.ENERGY, null)) {
continue;
}
IEnergyStorage itemPower = item.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.extractEnergy(itemPower.receiveEnergy(Math.min(capEnergy.getEnergyStored(), maxSend), false), false);
ExternalPowerSystems.chargeItem(capEnergy, player.inventory.getStackInSlot(i));
}
}
}

View file

@ -33,9 +33,9 @@ import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import techreborn.config.ConfigTechReborn;
import techreborn.init.ModItems;
@ -85,10 +85,12 @@ public class ItemAdvancedChainsaw extends ItemChainsaw {
if (oldPos == pos) {
return;
}
IEnergyStorage capEnergy = new ForgePowerItemManager(stack);
ForgePowerItemManager capEnergy = new ForgePowerItemManager(stack);
if (capEnergy.getEnergyStored() < cost) {
return;
}
IBlockState blockState = world.getBlockState(pos);
if (blockState.getBlockHardness(world, pos) == -1.0F) {
return;
@ -96,7 +98,10 @@ public class ItemAdvancedChainsaw extends ItemChainsaw {
if(!(entityLiving instanceof EntityPlayer)){
return;
}
capEnergy.extractEnergy(cost, false);
ExternalPowerSystems.requestEnergyFromArmor(capEnergy, entityLiving);
blockState.getBlock().harvestBlock(world, (EntityPlayer) entityLiving, pos, blockState, world.getTileEntity(pos), stack);
world.setBlockToAir(pos);
world.removeTileEntity(pos);

View file

@ -38,6 +38,7 @@ import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import techreborn.config.ConfigTechReborn;
import techreborn.init.ModItems;
@ -98,7 +99,12 @@ public class ItemAdvancedDrill extends ItemDrill {
public void breakBlock(BlockPos pos, World world, EntityPlayer playerIn, ItemStack drill) {
IBlockState blockState = world.getBlockState(pos);
new ForgePowerItemManager(drill).extractEnergy(cost, false);
ForgePowerItemManager capEnergy = new ForgePowerItemManager(drill);
capEnergy.extractEnergy(cost, false);
ExternalPowerSystems.requestEnergyFromArmor(capEnergy, playerIn);
blockState.getBlock().harvestBlock(world, playerIn, pos, blockState, world.getTileEntity(pos), drill);
world.setBlockToAir(pos);
world.removeTileEntity(pos);

View file

@ -41,6 +41,7 @@ import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
@ -96,7 +97,10 @@ public class ItemChainsaw extends ItemAxe implements IEnergyItemInfo {
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) {
Random rand = new Random();
if (rand.nextInt(EnchantmentHelper.getEnchantmentLevel(Enchantments.UNBREAKING, stack) + 1) == 0) {
new ForgePowerItemManager(stack).extractEnergy(cost, false);
ForgePowerItemManager capEnergy = new ForgePowerItemManager(stack);
capEnergy.extractEnergy(cost, false);
ExternalPowerSystems.requestEnergyFromArmor(capEnergy, entityLiving);
}
return true;
}

View file

@ -36,6 +36,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
@ -81,7 +82,10 @@ public class ItemDrill extends ItemPickaxe implements IEnergyItemInfo {
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) {
Random rand = new Random();
if (rand.nextInt(EnchantmentHelper.getEnchantmentLevel(Enchantments.UNBREAKING, stack) + 1) == 0) {
new ForgePowerItemManager(stack).extractEnergy(cost, false);
ForgePowerItemManager capEnergy = new ForgePowerItemManager(stack);
capEnergy.extractEnergy(cost, false);
ExternalPowerSystems.requestEnergyFromArmor(capEnergy, entityLiving);
}
return true;
}

View file

@ -36,10 +36,10 @@ import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
@ -68,10 +68,12 @@ public class ItemElectricTreetap extends ItemTR implements IEnergyItemInfo {
@Override
public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
IBlockState state = worldIn.getBlockState(pos);
IEnergyStorage capEnergy = new ForgePowerItemManager(playerIn.getHeldItem(hand));
ForgePowerItemManager capEnergy = new ForgePowerItemManager(playerIn.getHeldItem(hand));
if(TechRebornAPI.ic2Helper != null && capEnergy.getEnergyStored() >= cost){
if(TechRebornAPI.ic2Helper.extractSap(playerIn, worldIn, pos, side, state, null) && !worldIn.isRemote){
capEnergy.extractEnergy(cost, false);
ExternalPowerSystems.requestEnergyFromArmor(capEnergy, playerIn);
return EnumActionResult.SUCCESS;
}
}

View file

@ -37,6 +37,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
@ -79,7 +80,10 @@ public class ItemJackhammer extends ItemPickaxe implements IEnergyItemInfo {
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) {
Random rand = new Random();
if (rand.nextInt(EnchantmentHelper.getEnchantmentLevel(Enchantments.UNBREAKING, stack) + 1) == 0) {
new ForgePowerItemManager(stack).extractEnergy(cost, false);
ForgePowerItemManager capEnergy = new ForgePowerItemManager(stack);
capEnergy.extractEnergy(cost, false);
ExternalPowerSystems.requestEnergyFromArmor(capEnergy, entityLiving);
}
return true;
}

View file

@ -44,10 +44,10 @@ import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
@ -89,10 +89,12 @@ public class ItemNanosaber extends ItemSword implements IEnergyItemInfo {
// ItemSword
@Override
public boolean hitEntity(ItemStack stack, EntityLivingBase entityliving, EntityLivingBase entityliving1) {
IEnergyStorage capEnergy = new ForgePowerItemManager(stack);
public boolean hitEntity(ItemStack stack, EntityLivingBase entityHit, EntityLivingBase entityHitter) {
ForgePowerItemManager capEnergy = new ForgePowerItemManager(stack);
if (capEnergy.getEnergyStored() >= cost) {
capEnergy.extractEnergy(cost, false);
ExternalPowerSystems.requestEnergyFromArmor(capEnergy, entityHitter);
return true;
} else {
return false;

View file

@ -38,10 +38,10 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
@ -80,7 +80,11 @@ public class ItemOmniTool extends ItemPickaxe implements IEnergyItemInfo {
// ItemTool
@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) {
new ForgePowerItemManager(stack).extractEnergy(cost, false);
ForgePowerItemManager capEnergy = new ForgePowerItemManager(stack);
capEnergy.extractEnergy(cost, false);
ExternalPowerSystems.requestEnergyFromArmor(capEnergy, entityLiving);
return true;
}
@ -107,9 +111,11 @@ public class ItemOmniTool extends ItemPickaxe implements IEnergyItemInfo {
@Override
public boolean hitEntity(ItemStack stack, EntityLivingBase entityliving, EntityLivingBase attacker) {
IEnergyStorage capEnergy = new ForgePowerItemManager(stack);
ForgePowerItemManager capEnergy = new ForgePowerItemManager(stack);
if (capEnergy.getEnergyStored() >= hitCost) {
capEnergy.extractEnergy(hitCost, false);
ExternalPowerSystems.requestEnergyFromArmor(capEnergy, entityliving);
entityliving.attackEntityFrom(DamageSource.causePlayerDamage((EntityPlayer) attacker), 8F);
}
return false;

View file

@ -41,6 +41,7 @@ import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
@ -88,7 +89,10 @@ public class ItemRockCutter extends ItemPickaxe implements IEnergyItemInfo {
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) {
Random rand = new Random();
if (rand.nextInt(EnchantmentHelper.getEnchantmentLevel(Enchantments.UNBREAKING, stack) + 1) == 0) {
new ForgePowerItemManager(stack).extractEnergy(cost, false);
ForgePowerItemManager capEnergy = new ForgePowerItemManager(stack);
capEnergy.extractEnergy(cost, false);
ExternalPowerSystems.requestEnergyFromArmor(capEnergy, entityLiving);
}
return true;
}

View file

@ -27,11 +27,8 @@ package techreborn.tiles;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import reborncore.api.IToolDrop;
import reborncore.api.tile.IInventoryProvider;
import reborncore.common.RebornCoreConfig;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.TilePowerAcceptor;
import reborncore.common.registration.RebornRegistry;
@ -47,6 +44,8 @@ import techreborn.lib.ModInfo;
public class TileChargeOMat extends TilePowerAcceptor
implements IToolDrop, IInventoryProvider, IContainerProvider {
@ConfigRegistry(config = "machines", category = "charge_bench", key = "ChargeBenchMaxOutput", comment = "Charge Bench Max Output (Value in EU)")
public static int maxOutput = 512;
@ConfigRegistry(config = "machines", category = "charge_bench", key = "ChargeBenchMaxInput", comment = "Charge Bench Max Input (Value in EU)")
public static int maxInput = 512;
@ConfigRegistry(config = "machines", category = "charge_bench", key = "ChargeBenchMaxEnergy", comment = "Charge Bench Max Energy (Value in EU)")
@ -67,22 +66,14 @@ public class TileChargeOMat extends TilePowerAcceptor
return;
}
for (int i = 0; i < 6; i++) {
if (!inventory.getStackInSlot(i).isEmpty()) {
final ItemStack stack = inventory.getStackInSlot(i);
if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) {
IEnergyStorage powerItem = stack.getCapability(CapabilityEnergy.ENERGY, null);
int maxReceive = powerItem.receiveEnergy((int)getMaxInput() * RebornCoreConfig.euPerFU, true);
double maxUse = Math.min((double) (maxReceive / RebornCoreConfig.euPerFU), getMaxInput());
if (getEnergy() >= 0.0 && maxReceive > 0) {
powerItem.receiveEnergy((int) useEnergy(maxUse) * RebornCoreConfig.euPerFU, false);
}
} else if (ExternalPowerSystems.isPoweredItem(stack)) {
ExternalPowerSystems.chargeItem(this, stack);
}
ItemStack stack = inventory.getStackInSlot(i);
if (!stack.isEmpty()) {
ExternalPowerSystems.chargeItem(this, stack);
}
}
}
@Override
public double getBaseMaxPower() {
return maxEnergy;
@ -100,7 +91,7 @@ public class TileChargeOMat extends TilePowerAcceptor
@Override
public double getBaseMaxOutput() {
return 0;
return maxOutput;
}
@Override

View file

@ -28,12 +28,9 @@ import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import reborncore.api.IToolDrop;
import reborncore.api.power.EnumPowerTier;
import reborncore.api.tile.IInventoryProvider;
import reborncore.common.RebornCoreConfig;
import reborncore.common.powerSystem.ExternalPowerSystems;
import reborncore.common.powerSystem.TilePowerAcceptor;
import reborncore.common.util.Inventory;
@ -70,14 +67,8 @@ public class TileEnergyStorage extends TilePowerAcceptor
super.update();
if (!inventory.getStackInSlot(0).isEmpty()) {
ItemStack stack = inventory.getStackInSlot(0);
if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) {
IEnergyStorage powerItem = stack.getCapability(CapabilityEnergy.ENERGY, null);
int maxReceive = powerItem.receiveEnergy((int)getMaxOutput() * RebornCoreConfig.euPerFU, true);
double maxUse = Math.min((double) (maxReceive / RebornCoreConfig.euPerFU), getMaxOutput());
if (getEnergy() >= 0.0 && maxReceive > 0) {
powerItem.receiveEnergy((int) useEnergy(maxUse) * RebornCoreConfig.euPerFU, false);
}
} else if (ExternalPowerSystems.isPoweredItem(stack)) {
if (ExternalPowerSystems.isPoweredItem(stack)) {
ExternalPowerSystems.chargeItem(this, stack);
}
}