Search in sources :

Example 11 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project Random-Things by lumien231.

the class TileEntityBlockBreaker method update.

@Override
public void update() {
    if (!this.world.isRemote) {
        if (firstTick) {
            firstTick = false;
            initFakePlayer();
            neighborChanged(this.world.getBlockState(pos), world, pos, null, null);
        }
        if (mining) {
            EnumFacing facing = world.getBlockState(pos).getValue(BlockBlockBreaker.FACING);
            BlockPos targetPos = pos.offset(facing);
            IBlockState targetState = world.getBlockState(targetPos);
            this.curBlockDamage += targetState.getPlayerRelativeBlockHardness(fakePlayer.get(), world, targetPos);
            if (curBlockDamage >= 1.0f) {
                mining = false;
                resetProgress();
                FakePlayer player;
                if ((player = fakePlayer.get()) != null) {
                    boolean catching = false;
                    TileEntity te;
                    if ((te = world.getTileEntity(this.pos.offset(facing.getOpposite()))) != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing)) {
                        catching = true;
                    }
                    player.dimension = this.world.provider.getDimension();
                    player.interactionManager.world = player.world = this.world;
                    player.interactionManager.tryHarvestBlock(targetPos);
                    IItemHandler itemHandler = null;
                    if (catching) {
                        itemHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing);
                    }
                    for (int i = 1; i < player.inventory.getSizeInventory(); i++) {
                        ItemStack stack = player.inventory.getStackInSlot(i);
                        player.inventory.setInventorySlotContents(i, ItemStack.EMPTY);
                        if (!stack.isEmpty()) {
                            ItemStack remainder = stack;
                            if (catching) {
                                remainder = ItemHandlerHelper.insertItemStacked(itemHandler, stack, false);
                            }
                            if (!remainder.isEmpty()) {
                                WorldUtil.spawnItemStack(world, pos.offset(facing), stack);
                            }
                        }
                    }
                }
            } else {
                world.sendBlockBreakProgress(uuid.hashCode(), targetPos, (int) (this.curBlockDamage * 10.0F) - 1);
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) IItemHandler(net.minecraftforge.items.IItemHandler) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 12 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project RFTools by McJty.

the class BuilderTileEntity method pumpBlock.

public boolean pumpBlock(int rfNeeded, BlockPos srcPos, IBlockState srcState, IBlockState pickState) {
    Block block = srcState.getBlock();
    Fluid fluid = FluidRegistry.lookupFluidForBlock(block);
    if (fluid == null) {
        return skip();
    }
    if (!isFluidBlock(block)) {
        return skip();
    }
    if (getFluidLevel(srcState) != 0) {
        return skip();
    }
    if (block.getBlockHardness(srcState, getWorld(), srcPos) >= 0) {
        FakePlayer fakePlayer = getHarvester();
        if (allowedToBreak(srcState, getWorld(), srcPos, fakePlayer)) {
            if (checkAndInsertFluids(fluid)) {
                consumeEnergy(rfNeeded);
                boolean clear = getCardType().isClearing();
                if (clear) {
                    getWorld().setBlockToAir(srcPos);
                } else {
                    // No block update!
                    getWorld().setBlockState(srcPos, getReplacementBlock(), 2);
                }
                if (!silent) {
                    SoundTools.playSound(getWorld(), block.getSoundType(srcState, getWorld(), srcPos, fakePlayer).getBreakSound(), srcPos.getX(), srcPos.getY(), srcPos.getZ(), 1.0f, 1.0f);
                }
                return skip();
            }
            // No room in tanks or not a valid tank: wait
            return waitOrSkip("No room for liquid\nor no usable tank\nabove or below!");
        }
    }
    return skip();
}
Also used : Fluid(net.minecraftforge.fluids.Fluid) BaseBlock(mcjty.lib.container.BaseBlock) Block(net.minecraft.block.Block) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 13 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project RFTools by McJty.

the class BuilderTileEntity method copyBlock.

private void copyBlock(World srcWorld, BlockPos srcPos, World destWorld, BlockPos destPos) {
    int rf = getEnergyStored();
    int rfNeeded = (int) (BuilderConfiguration.builderRfPerOperation * getDimensionCostFactor(srcWorld, destWorld) * (4.0f - getInfusedFactor()) / 4.0f);
    if (rfNeeded > rf) {
        // Not enough energy.
        return;
    }
    if (isEmptyOrReplacable(destWorld, destPos)) {
        if (srcWorld.isAirBlock(srcPos)) {
            return;
        }
        IBlockState srcState = srcWorld.getBlockState(srcPos);
        TakeableItem takeableItem = createTakeableItem(srcWorld, srcPos, srcState);
        ItemStack consumedStack = takeableItem.peek();
        if (consumedStack.isEmpty()) {
            return;
        }
        FakePlayer fakePlayer = getHarvester();
        IBlockState newState = BlockTools.placeStackAt(fakePlayer, consumedStack, destWorld, destPos, srcState);
        // placeBlockAt can reset the orientation. Restore it here
        destWorld.setBlockState(destPos, newState, 3);
        if (!ItemStack.areItemStacksEqual(consumedStack, takeableItem.peek())) {
            // Did we actually use up whatever we were holding?
            if (!consumedStack.isEmpty()) {
                // Are we holding something else that we should put back?
                // First try to put our new item where we got what we placed
                consumedStack = takeableItem.takeAndReplace(consumedStack);
                if (!consumedStack.isEmpty()) {
                    // If that didn't work, then try to put it anywhere it will fit
                    consumedStack = insertItem(consumedStack);
                    if (!consumedStack.isEmpty()) {
                        // If that still didn't work, then just drop whatever we're holding
                        getWorld().spawnEntity(new EntityItem(getWorld(), getPos().getX(), getPos().getY(), getPos().getZ(), consumedStack));
                    }
                }
            } else {
                // If we aren't holding anything, then just consume what we placed
                takeableItem.take();
            }
        }
        if (!silent) {
            SoundTools.playSound(destWorld, newState.getBlock().getSoundType(newState, destWorld, destPos, fakePlayer).getPlaceSound(), destPos.getX(), destPos.getY(), destPos.getZ(), 1.0f, 1.0f);
        }
        consumeEnergy(rfNeeded);
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) EntityItem(net.minecraft.entity.item.EntityItem) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 14 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project RFTools by McJty.

the class BuilderTileEntity method buildBlock.

public boolean buildBlock(int rfNeeded, BlockPos srcPos, IBlockState srcState, IBlockState pickState) {
    if (isEmptyOrReplacable(getWorld(), srcPos)) {
        TakeableItem item = createTakeableItem(getWorld(), srcPos, pickState);
        ItemStack stack = item.peek();
        if (stack.isEmpty()) {
            // We could not find a block. Wait
            return waitOrSkip("Cannot find block!\nor missing inventory\non top or below");
        }
        FakePlayer fakePlayer = getHarvester();
        IBlockState newState = BlockTools.placeStackAt(fakePlayer, stack, getWorld(), srcPos, pickState);
        if (!ItemStack.areItemStacksEqual(stack, item.peek())) {
            // Did we actually use up whatever we were holding?
            if (!stack.isEmpty()) {
                // Are we holding something else that we should put back?
                // First try to put our new item where we got what we placed
                stack = item.takeAndReplace(stack);
                if (!stack.isEmpty()) {
                    // If that didn't work, then try to put it anywhere it will fit
                    stack = insertItem(stack);
                    if (!stack.isEmpty()) {
                        // If that still didn't work, then just drop whatever we're holding
                        getWorld().spawnEntity(new EntityItem(getWorld(), getPos().getX(), getPos().getY(), getPos().getZ(), stack));
                    }
                }
            } else {
                // If we aren't holding anything, then just consume what we placed
                item.take();
            }
        }
        if (!silent) {
            SoundTools.playSound(getWorld(), newState.getBlock().getSoundType(newState, getWorld(), srcPos, fakePlayer).getPlaceSound(), srcPos.getX(), srcPos.getY(), srcPos.getZ(), 1.0f, 1.0f);
        }
        consumeEnergy(rfNeeded);
    }
    return skip();
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) EntityItem(net.minecraft.entity.item.EntityItem) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 15 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project RFTools by McJty.

the class BuilderTileEntity method commonQuarryBlock.

private boolean commonQuarryBlock(boolean silk, int rfNeeded, BlockPos srcPos, IBlockState srcState) {
    Block block = srcState.getBlock();
    int xCoord = getPos().getX();
    int yCoord = getPos().getY();
    int zCoord = getPos().getZ();
    int sx = srcPos.getX();
    int sy = srcPos.getY();
    int sz = srcPos.getZ();
    if (sx >= xCoord - 1 && sx <= xCoord + 1 && sy >= yCoord - 1 && sy <= yCoord + 1 && sz >= zCoord - 1 && sz <= zCoord + 1) {
        // Skip a 3x3x3 block around the builder.
        return skip();
    }
    if (isEmpty(srcState, block)) {
        return skip();
    }
    if (block.getBlockHardness(srcState, getWorld(), srcPos) >= 0) {
        boolean clear = getCardType().isClearing();
        if ((!clear) && srcState == getReplacementBlock()) {
            // We can skip dirt if we are not clearing.
            return skip();
        }
        if ((!BuilderConfiguration.quarryTileEntities) && getWorld().getTileEntity(srcPos) != null) {
            // Skip tile entities
            return skip();
        }
        FakePlayer fakePlayer = getHarvester();
        if (allowedToBreak(srcState, getWorld(), srcPos, fakePlayer)) {
            ItemStack filter = getStackInSlot(BuilderContainer.SLOT_FILTER);
            if (!filter.isEmpty()) {
                getFilterCache();
                if (filterCache != null) {
                    boolean match = filterCache.match(block.getItem(getWorld(), srcPos, srcState));
                    if (!match) {
                        consumeEnergy(Math.min(rfNeeded, BuilderConfiguration.builderRfPerSkipped));
                        // Skip this
                        return skip();
                    }
                }
            }
            if (!getCachedVoidableBlocks().contains(block)) {
                List<ItemStack> drops;
                if (silk && block.canSilkHarvest(getWorld(), srcPos, srcState, fakePlayer)) {
                    ItemStack drop;
                    try {
                        drop = (ItemStack) CommonProxy.Block_getSilkTouch.invoke(block, srcState);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        throw new RuntimeException(e);
                    }
                    drops = new ArrayList<>();
                    if (!drop.isEmpty()) {
                        drops.add(drop);
                    }
                    net.minecraftforge.event.ForgeEventFactory.fireBlockHarvesting(drops, getWorld(), srcPos, srcState, 0, 1.0f, true, fakePlayer);
                } else {
                    int fortune = getCardType().isFortune() ? 3 : 0;
                    if (block instanceof BlockShulkerBox) {
                        // Shulker boxes drop in setBlockState, rather than anywhere sensible. Work around this.
                        drops = new ArrayList<>();
                        TileEntity te = getWorld().getTileEntity(srcPos);
                        if (te instanceof TileEntityShulkerBox) {
                            TileEntityShulkerBox teShulkerBox = (TileEntityShulkerBox) te;
                            ItemStack stack = new ItemStack(Item.getItemFromBlock(block));
                            teShulkerBox.saveToNbt(stack.getOrCreateSubCompound("BlockEntityTag"));
                            if (teShulkerBox.hasCustomName()) {
                                stack.setStackDisplayName(teShulkerBox.getName());
                            }
                            drops.add(stack);
                        }
                    } else {
                        drops = block.getDrops(getWorld(), srcPos, srcState, fortune);
                    }
                    net.minecraftforge.event.ForgeEventFactory.fireBlockHarvesting(drops, getWorld(), srcPos, srcState, fortune, 1.0f, false, fakePlayer);
                }
                if (!checkAndInsertItems(block, drops)) {
                    // Not enough room. Wait
                    return waitOrSkip("Not enough room!\nor no usable storage\non top or below!");
                }
            }
            clearOrDirtBlock(rfNeeded, srcPos, srcState, clear);
        }
    }
    return silk ? skip() : false;
}
Also used : TileEntityShulkerBox(net.minecraft.tileentity.TileEntityShulkerBox) InvocationTargetException(java.lang.reflect.InvocationTargetException) FakePlayer(net.minecraftforge.common.util.FakePlayer) GenericEnergyReceiverTileEntity(mcjty.lib.entity.GenericEnergyReceiverTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) BlockShulkerBox(net.minecraft.block.BlockShulkerBox) BaseBlock(mcjty.lib.container.BaseBlock) Block(net.minecraft.block.Block)

Aggregations

FakePlayer (net.minecraftforge.common.util.FakePlayer)73 ItemStack (net.minecraft.item.ItemStack)26 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)17 EntityPlayer (net.minecraft.entity.player.EntityPlayer)16 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)15 IBlockState (net.minecraft.block.state.IBlockState)14 BlockPos (net.minecraft.util.math.BlockPos)11 EntityItem (net.minecraft.entity.item.EntityItem)10 World (net.minecraft.world.World)8 TileEntity (net.minecraft.tileentity.TileEntity)7 Block (net.minecraft.block.Block)6 BaseBlock (mcjty.lib.container.BaseBlock)4 EntityLivingBase (net.minecraft.entity.EntityLivingBase)4 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 SubscribeEvent (cpw.mods.fml.common.eventhandler.SubscribeEvent)3 IGregTechTileEntity (gregtech.api.interfaces.tileentity.IGregTechTileEntity)3 MannequinFakePlayer (riskyken.armourersWorkshop.client.render.MannequinFakePlayer)3 PlayerPointer (riskyken.armourersWorkshop.common.data.PlayerPointer)3 SacrificeKnifeUsedEvent (WayofTime.alchemicalWizardry.api.event.SacrificeKnifeUsedEvent)2 IMinerStats (cavern.api.IMinerStats)2