Search in sources :

Example 6 with LongEnergyStack

use of com.cjm721.overloaded.storage.stacks.intint.LongEnergyStack in project Overloaded by CJ-MC-Mods.

the class LongEnergyStorageTest method extractEnergy_simulate_whenFalse.

@Test
public void extractEnergy_simulate_whenFalse() {
    // Setup Base energy
    storage.give(new LongEnergyStack(1000), true);
    updateCalled = false;
    int simulatedExtract = storage.extractEnergy(1000, false);
    assertThat(simulatedExtract).isEqualTo(1000);
    assertThat(storage.status().getAmount()).isEqualTo(0);
    assertThat(updateCalled).isTrue();
}
Also used : LongEnergyStack(com.cjm721.overloaded.storage.stacks.intint.LongEnergyStack) Test(org.junit.Test)

Example 7 with LongEnergyStack

use of com.cjm721.overloaded.storage.stacks.intint.LongEnergyStack in project Overloaded by CJ-MC-Mods.

the class TileEnergyExtractor method update.

/**
     * Like the old updateEntity(), except more generic.
     */
@Override
public void update() {
    BlockPos me = this.getPos();
    TileEntity frontTE = getWorld().getTileEntity(me.add(front.getDirectionVec()));
    if (frontTE == null || !frontTE.hasCapability(HYPER_ENERGY_HANDLER, front.getOpposite()))
        return;
    IHyperHandlerEnergy storage = frontTE.getCapability(HYPER_ENERGY_HANDLER, front.getOpposite());
    LongEnergyStack energy = storage.take(new LongEnergyStack(Long.MAX_VALUE), false);
    for (EnumFacing facing : EnumFacing.values()) {
        if (energy.getAmount() == 0L)
            return;
        if (facing == front)
            continue;
        TileEntity te = world.getTileEntity(me.add(facing.getDirectionVec()));
        if (te == null || !te.hasCapability(ENERGY, facing.getOpposite()))
            continue;
        IEnergyStorage receiver = te.getCapability(ENERGY, facing.getOpposite());
        if (!receiver.canReceive())
            continue;
        int acceptedAmount = receiver.receiveEnergy((int) Math.min(energy.getAmount(), Integer.MAX_VALUE), true);
        if (acceptedAmount != 0) {
            receiver.receiveEnergy(acceptedAmount, false);
            energy = storage.take(new LongEnergyStack(acceptedAmount), true);
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) LongEnergyStack(com.cjm721.overloaded.storage.LongEnergyStack) EnumFacing(net.minecraft.util.EnumFacing) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) BlockPos(net.minecraft.util.math.BlockPos) IHyperHandlerEnergy(com.cjm721.overloaded.storage.energy.IHyperHandlerEnergy)

Example 8 with LongEnergyStack

use of com.cjm721.overloaded.storage.stacks.intint.LongEnergyStack in project Overloaded by CJ-MC-Mods.

the class BlockInfiniteCapacitor method onBlockActivated.

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
    if (!worldIn.isRemote) {
        ItemStack heldItem = playerIn.getHeldItem(hand);
        if (heldItem.isEmpty() && hand == EnumHand.MAIN_HAND) {
            LongEnergyStack stack = ((TileInfiniteCapacitor) worldIn.getTileEntity(pos)).getStorage().status();
            // TODO Make the exact number show in a tooltip so it can be easier to read at a glance
            double percent = (double) stack.getAmount() / (double) Long.MAX_VALUE;
            playerIn.sendStatusMessage(new TextComponentString(String.format("Energy Amount: %,d  %,.4f%%", stack.getAmount(), percent)), false);
            return true;
        }
    }
    return super.onBlockActivated(worldIn, pos, state, playerIn, hand, side, hitX, hitY, hitZ);
}
Also used : LongEnergyStack(com.cjm721.overloaded.storage.LongEnergyStack) ItemStack(net.minecraft.item.ItemStack) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 9 with LongEnergyStack

use of com.cjm721.overloaded.storage.stacks.intint.LongEnergyStack in project Overloaded by CJ-MC-Mods.

the class ItemEnergyShield method onItemRightClick.

@Nonnull
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, @Nonnull EnumHand handIn) {
    ItemStack itemstack = playerIn.getHeldItem(handIn);
    playerIn.setActiveHand(handIn);
    IHyperHandlerEnergy handler = itemstack.getCapability(HYPER_ENERGY_HANDLER, null);
    LongEnergyStack energy = handler.take(new LongEnergyStack(initialUseCost), true);
    if (energy.amount == initialUseCost) {
        System.out.println("Right click Success");
        return new ActionResult<>(EnumActionResult.SUCCESS, itemstack);
    } else {
        System.out.println("Right click FAIL");
        return new ActionResult<>(EnumActionResult.FAIL, itemstack);
    }
}
Also used : LongEnergyStack(com.cjm721.overloaded.storage.LongEnergyStack) ItemStack(net.minecraft.item.ItemStack) IHyperHandlerEnergy(com.cjm721.overloaded.storage.energy.IHyperHandlerEnergy) Nonnull(javax.annotation.Nonnull)

Example 10 with LongEnergyStack

use of com.cjm721.overloaded.storage.stacks.intint.LongEnergyStack in project Overloaded by CJ-MC-Mods.

the class TileEnergyExtractor method tick.

@Override
public void tick() {
    if (getLevel().isClientSide) {
        return;
    }
    BlockPos me = this.getBlockPos();
    TileEntity frontTE = getLevel().getBlockEntity(me.offset(getFacing().getNormal()));
    if (frontTE == null) {
        return;
    }
    LazyOptional<IHyperHandlerEnergy> optionalStorage = frontTE.getCapability(HYPER_ENERGY_HANDLER, getFacing().getOpposite());
    if (!optionalStorage.isPresent()) {
        return;
    }
    IHyperHandlerEnergy storage = optionalStorage.orElse(null);
    for (Direction facing : Direction.values()) {
        if (facing == getFacing())
            continue;
        TileEntity te = level.getBlockEntity(me.offset(facing.getNormal()));
        if (te == null)
            continue;
        LazyOptional<IEnergyStorage> optionalReceiver = te.getCapability(ENERGY, facing.getOpposite());
        LongEnergyStack energy = storage.take(new LongEnergyStack(Long.MAX_VALUE), false);
        if (energy.getAmount() == 0L)
            return;
        optionalReceiver.ifPresent(receiver -> {
            if (!receiver.canReceive())
                return;
            int acceptedAmount = receiver.receiveEnergy((int) Math.min(energy.getAmount(), Integer.MAX_VALUE), true);
            if (acceptedAmount != 0) {
                LongEnergyStack actualTaken = storage.take(new LongEnergyStack(acceptedAmount), true);
                receiver.receiveEnergy((int) Math.min(actualTaken.getAmount(), Integer.MAX_VALUE), false);
            }
        });
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ITickableTileEntity(net.minecraft.tileentity.ITickableTileEntity) LongEnergyStack(com.cjm721.overloaded.storage.stacks.intint.LongEnergyStack) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) BlockPos(net.minecraft.util.math.BlockPos) IHyperHandlerEnergy(com.cjm721.overloaded.storage.energy.IHyperHandlerEnergy) Direction(net.minecraft.util.Direction)

Aggregations

LongEnergyStack (com.cjm721.overloaded.storage.stacks.intint.LongEnergyStack)12 IHyperHandlerEnergy (com.cjm721.overloaded.storage.energy.IHyperHandlerEnergy)6 Nonnull (javax.annotation.Nonnull)6 Test (org.junit.Test)6 LongEnergyStack (com.cjm721.overloaded.storage.LongEnergyStack)4 ItemStack (net.minecraft.item.ItemStack)3 TileEntity (net.minecraft.tileentity.TileEntity)2 BlockPos (net.minecraft.util.math.BlockPos)2 IEnergyStorage (net.minecraftforge.energy.IEnergyStorage)2 BigInteger (java.math.BigInteger)1 ITickableTileEntity (net.minecraft.tileentity.ITickableTileEntity)1 ActionResult (net.minecraft.util.ActionResult)1 Direction (net.minecraft.util.Direction)1 EnumFacing (net.minecraft.util.EnumFacing)1 StringTextComponent (net.minecraft.util.text.StringTextComponent)1 TextComponentString (net.minecraft.util.text.TextComponentString)1