Example usage for net.minecraftforge.common.util FakePlayerFactory getMinecraft

List of usage examples for net.minecraftforge.common.util FakePlayerFactory getMinecraft

Introduction

In this page you can find the example usage for net.minecraftforge.common.util FakePlayerFactory getMinecraft.

Prototype

public static FakePlayer getMinecraft(ServerWorld world) 

Source Link

Usage

From source file:appeng.parts.automation.PartIdentityAnnihilationPlane.java

License:Open Source License

@Override
protected List<ItemStack> obtainBlockDrops(WorldServer w, int x, int y, int z) {
    final FakePlayer fakePlayer = FakePlayerFactory.getMinecraft(w);
    final Block block = w.getBlock(x, y, z);
    final int blockMeta = w.getBlockMetadata(x, y, z);

    if (block.canSilkHarvest(w, fakePlayer, x, y, z, blockMeta)) {
        final List<ItemStack> out = new ArrayList<ItemStack>(1);
        final Item item = Item.getItemFromBlock(block);

        if (item != null) {
            int meta = 0;
            if (item.getHasSubtypes()) {
                meta = blockMeta;//from  w w w.j av a  2 s.co  m
            }
            ItemStack itemstack = new ItemStack(item, 1, meta);
            out.add(itemstack);
        }
        return out;
    } else {
        return super.obtainBlockDrops(w, x, y, z);
    }
}

From source file:appeng.util.Platform.java

License:Open Source License

public static EntityPlayer getPlayer(WorldServer w) {
    if (w == null) {
        throw new InvalidParameterException("World is null.");
    }// www  .  jav a2  s  .  c o m

    EntityPlayer wrp = FAKE_PLAYERS.get(w);
    if (wrp != null) {
        return wrp;
    }

    EntityPlayer p = FakePlayerFactory.getMinecraft(w);
    FAKE_PLAYERS.put(w, p);
    return p;
}

From source file:com.yogpc.qp.BlockBreaker.java

License:Open Source License

@Override
public void updateTick(final World w, final int x, final int y, final int z, final Random r) {
    if (w.isRemote)
        return;// w w w . j a v a2s  .c o m
    final TileBreaker tile = (TileBreaker) w.getTileEntity(x, y, z);
    final ForgeDirection fd = ForgeDirection.getOrientation(w.getBlockMetadata(x, y, z) & 7);
    final int tx = x + fd.offsetX, ty = y + fd.offsetY, tz = z + fd.offsetZ,
            meta = w.getBlockMetadata(tx, ty, tz);
    if (ty < 1)
        return;
    final Block b = w.getBlock(tx, ty, tz);
    if (b == null || b.isAir(w, x, y, z))
        return;
    final EntityPlayer player = FakePlayerFactory.getMinecraft((WorldServer) w);
    b.onBlockHarvested(w, tx, ty, tz, meta, player);
    if (b.removedByPlayer(w, player, tx, ty, tz))
        b.onBlockDestroyedByPlayer(w, tx, ty, tz, meta);
    else
        return;
    ArrayList<ItemStack> alis;
    if (b.canSilkHarvest(w, player, tx, ty, tz, meta) && tile.silktouch) {
        alis = new ArrayList<ItemStack>();
        final ItemStack is = (ItemStack) ReflectionHelper.invoke(TileBasic.createStackedBlock, b,
                new Integer(meta));
        if (is != null)
            alis.add(is);
    } else
        alis = b.getDrops(w, tx, ty, tz, meta, tile.fortune);
    for (final ItemStack is : alis) {
        InvUtils.injectToNearTile(w, x, y, z, is);
        if (is.stackSize > 0) {
            final float f = 0.7F;
            final double d0 = w.rand.nextFloat() * f + (1.0F - f) * 0.5D;
            final double d1 = w.rand.nextFloat() * f + (1.0F - f) * 0.5D;
            final double d2 = w.rand.nextFloat() * f + (1.0F - f) * 0.5D;
            final EntityItem entityitem = new EntityItem(w, x + d0, y + d1, z + d2, is);
            entityitem.delayBeforeCanPickup = 10;
            w.spawnEntityInWorld(entityitem);
        }
    }
}

From source file:com.yogpc.qp.BlockPlacer.java

License:Open Source License

@Override
public void updateTick(final World world, final int x, final int y, final int z, final Random prandom) {
    if (world.isRemote)
        return;//from ww  w .ja  v  a2s.  c  o m
    final TilePlacer tile = (TilePlacer) world.getTileEntity(x, y, z);

    if (tile != null) {
        int tx, ty, tz, sd1, sd2, sd3, sd4, sd5, sd6;
        ForgeDirection fd1, fd2, fd3, fd4, fd5, fd6;

        sd2 = world.getBlockMetadata(x, y, z) & 7;
        fd2 = ForgeDirection.getOrientation(sd2);
        fd1 = fd2.getOpposite();
        sd1 = fd1.ordinal();

        sd3 = sd1 + 2;
        if (sd3 >= 6)
            sd3 -= 6;
        fd3 = ForgeDirection.getOrientation(sd3);
        fd4 = fd3.getOpposite();
        sd4 = fd4.ordinal();

        sd5 = sd3 + 2;
        if (sd5 >= 6)
            sd5 -= 6;
        fd5 = ForgeDirection.getOrientation(sd5);
        fd6 = fd5.getOpposite();
        sd6 = fd6.ordinal();

        tx = x + fd2.offsetX;
        ty = y + fd2.offsetY;
        tz = z + fd2.offsetZ;

        final EntityPlayer player = FakePlayerFactory.getMinecraft((WorldServer) world);
        ItemStack is = null;
        int i = 0;
        for (; i < tile.getSizeInventory(); i++) {
            is = tile.getStackInSlot(i);
            if (is != null && is.getItem() != null) {
                if (is.getItem().onItemUseFirst(is, player, world, tx, ty, tz, sd1, 0.5F, 0.5F, 0.5F))
                    break;
                if (is.getItem().onItemUseFirst(is, player, world, tx + fd1.offsetX, ty + fd1.offsetY,
                        tz + fd1.offsetZ, sd2, 0.5F, 0.5F, 0.5F))
                    break;
                if (is.getItem().onItemUseFirst(is, player, world, tx + fd2.offsetX, ty + fd2.offsetY,
                        tz + fd2.offsetZ, sd1, 0.5F, 0.5F, 0.5F))
                    break;
                if (is.getItem().onItemUseFirst(is, player, world, tx + fd3.offsetX, ty + fd3.offsetY,
                        tz + fd3.offsetZ, sd4, 0.5F, 0.5F, 0.5F))
                    break;
                if (is.getItem().onItemUseFirst(is, player, world, tx + fd4.offsetX, ty + fd4.offsetY,
                        tz + fd4.offsetZ, sd3, 0.5F, 0.5F, 0.5F))
                    break;
                if (is.getItem().onItemUseFirst(is, player, world, tx + fd5.offsetX, ty + fd5.offsetY,
                        tz + fd5.offsetZ, sd6, 0.5F, 0.5F, 0.5F))
                    break;
                if (is.getItem().onItemUseFirst(is, player, world, tx + fd6.offsetX, ty + fd6.offsetY,
                        tz + fd6.offsetZ, sd5, 0.5F, 0.5F, 0.5F))
                    break;
            }
            final Block k = world.getBlock(tx, ty, tz);
            if (k != null && k.onBlockActivated(world, tx, ty, tz, player, sd1, 0.5F, 0.5F, 0.5F))
                break;
            if (is != null) {
                if (is.tryPlaceItemIntoWorld(player, world, tx, ty, tz, sd1, 0.5F, 0.5F, 0.5F))
                    break;
                if (is.tryPlaceItemIntoWorld(player, world, tx + fd1.offsetX, ty + fd1.offsetY,
                        tz + fd1.offsetZ, sd2, 0.5F, 0.5F, 0.5F))
                    break;
                if (is.tryPlaceItemIntoWorld(player, world, tx + fd2.offsetX, ty + fd2.offsetY,
                        tz + fd2.offsetZ, sd1, 0.5F, 0.5F, 0.5F))
                    break;
                if (is.tryPlaceItemIntoWorld(player, world, tx + fd3.offsetX, ty + fd3.offsetY,
                        tz + fd3.offsetZ, sd4, 0.5F, 0.5F, 0.5F))
                    break;
                if (is.tryPlaceItemIntoWorld(player, world, tx + fd4.offsetX, ty + fd4.offsetY,
                        tz + fd4.offsetZ, sd3, 0.5F, 0.5F, 0.5F))
                    break;
                if (is.tryPlaceItemIntoWorld(player, world, tx + fd5.offsetX, ty + fd5.offsetY,
                        tz + fd5.offsetZ, sd6, 0.5F, 0.5F, 0.5F))
                    break;
                if (is.tryPlaceItemIntoWorld(player, world, tx + fd6.offsetX, ty + fd6.offsetY,
                        tz + fd6.offsetZ, sd5, 0.5F, 0.5F, 0.5F))
                    break;
            }
        }
        if (is != null && is.stackSize <= 0)
            tile.setInventorySlotContents(i, null);
    }
}

From source file:daxum.temporalconvergence.tileentity.TileTimeChest.java

License:Open Source License

@Override
//The 55,142nd prime number is 681,047
public void update() {
    //Handle item conversion
    if (!world.isRemote) {
        if (powerRequestTimer <= 0) {
            int powerGotten = PowerHandler.requestPower(world, pos, PowerTypeList.TIME, 40);

            if (powerGotten > 0) {
                powerRequestTimer = Math.min(powerGotten, 20);
                decaySpeed = Math.max(1, powerGotten - powerRequestTimer);
                sendBlockUpdate();//w  ww.  j  a v a2s  .  co m
            } else {
                powerRequestTimer = 10;

                if (decaySpeed > 0) {
                    decaySpeed = 0;
                    sendBlockUpdate();
                }
            }
        } else {
            powerRequestTimer--;
        }

        if (decaySpeed > 0) {
            boolean changed = false;

            for (int i = 0; i < decayTimers.length; i++) {
                if (decayTimers[i] > 0) {
                    decayTimers[i] -= decaySpeed;

                    if (decayTimers[i] <= 0) {
                        convertItemInSlot(i);
                        decayTimers[i] = -1;
                    }

                    changed = true;
                }
            }

            if (changed) {
                sendBlockUpdate();
            }
        }
    }

    //Update items in inventory. I hope the fake player won't cause problems...
    if (!world.isRemote && (decaySpeed > 0 || world.getTotalWorldTime() % 10 == 0)) {
        for (int i = 0; i < inventory.getSlots(); i++) {
            if (!inventory.getStackInSlot(i).isEmpty()) {
                inventory.getStackInSlot(i).getItem().onUpdate(inventory.getStackInSlot(i), world,
                        FakePlayerFactory.getMinecraft((WorldServer) world), 0, true);
            }
        }

        markDirty();
    }

    //Update beingUsed
    if (!world.isRemote && beingUsed) {
        final boolean oldUsed = beingUsed;
        beingUsed = false;
        int x = pos.getX();
        int y = pos.getY();
        int z = pos.getZ();

        for (EntityPlayer player : WorldHelper.getPlayersWithinAABB(world,
                new AxisAlignedBB(x - 5, y - 5, z - 5, x + 6, y + 6, z + 6))) {
            if (player.openContainer instanceof ContainerTimeChest
                    && ((ContainerTimeChest) player.openContainer).getTileEntity() == this) {
                beingUsed = true;
                break;
            }
        }

        if (beingUsed != oldUsed) {
            sendBlockUpdate();
        }
    }

    if (world.isRemote) {
        EntityPlayer player = Minecraft.getMinecraft().player;
        //Store old lid angle for rendering and below
        prevLidAngle = lidAngle;

        //If just opened, play opening sound
        if (beingUsed && lidAngle == 0.0f) {
            world.playSound(player, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
                    SoundEvents.BLOCK_CHEST_OPEN, SoundCategory.BLOCKS, 0.5f,
                    world.rand.nextFloat() * 0.1f + 0.9f);
        }

        //Update lid angle based on players using
        if (!beingUsed && lidAngle > 0.0f || beingUsed && lidAngle < 1.0f) {
            if (beingUsed) {
                lidAngle += 0.1f;
            } else {
                lidAngle -= 0.1f;
            }

            if (lidAngle > 1.0f) {
                lidAngle = 1.0f;
            } else if (lidAngle < 0.0f) {
                lidAngle = 0.0f;
            }

            //If closing, play closing sound
            if (lidAngle < 0.5f && prevLidAngle >= 0.5f) {
                world.playSound(player, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
                        SoundEvents.BLOCK_CHEST_CLOSE, SoundCategory.BLOCKS, 0.5f,
                        world.rand.nextFloat() * 0.1f + 0.9f);
            }
        }
    }
}

From source file:de.ellpeck.actuallyadditions.mod.items.lens.LensColor.java

@Override
public boolean invoke(IBlockState hitState, BlockPos hitBlock, IAtomicReconstructor tile) {
    if (hitBlock != null) {
        if (tile.getEnergy() >= ENERGY_USE) {
            IBlockState state = tile.getWorldObject().getBlockState(hitBlock);
            Block block = state.getBlock();
            int meta = block.getMetaFromState(state);
            ItemStack returnStack = this.tryConvert(new ItemStack(block, 1, meta), hitState, hitBlock, tile);
            if (returnStack != null && returnStack.getItem() instanceof ItemBlock) {
                Block toPlace = Block.getBlockFromItem(returnStack.getItem());
                IBlockState state2Place = toPlace.getStateForPlacement(tile.getWorldObject(), hitBlock,
                        EnumFacing.UP, 0, 0, 0, returnStack.getMetadata(),
                        FakePlayerFactory.getMinecraft((WorldServer) tile.getWorldObject()),
                        EnumHand.MAIN_HAND);
                tile.getWorldObject().setBlockState(hitBlock, state2Place, 2);
                tile.extractEnergy(ENERGY_USE);
            }//from  w  w w.j a v  a2s.  c o  m
        }

        List<EntityItem> items = tile.getWorldObject().getEntitiesWithinAABB(EntityItem.class,
                new AxisAlignedBB(hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), hitBlock.getX() + 1,
                        hitBlock.getY() + 1, hitBlock.getZ() + 1));
        for (EntityItem item : items) {
            if (!item.isDead && StackUtil.isValid(item.getItem()) && tile.getEnergy() >= ENERGY_USE) {
                ItemStack newStack = this.tryConvert(item.getItem(), hitState, hitBlock, tile);
                if (StackUtil.isValid(newStack)) {
                    item.setDead();

                    EntityItem newItem = new EntityItem(tile.getWorldObject(), item.posX, item.posY, item.posZ,
                            newStack);
                    tile.getWorldObject().spawnEntity(newItem);

                    tile.extractEnergy(ENERGY_USE);
                }
            }
        }
    }
    return false;
}

From source file:de.ellpeck.actuallyadditions.mod.items.lens.LensEvenMoarDeath.java

@Override
protected void onAttacked(EntityLivingBase entity, IAtomicReconstructor tile) {
    if (tile.getWorldObject() instanceof WorldServer) {
        try {/*  w  w w  .  j  a va  2 s.c o  m*/
            FakePlayer fake = FakePlayerFactory.getMinecraft((WorldServer) tile.getWorldObject());
            ReflectionHelper.setPrivateValue(EntityLivingBase.class, entity, fake, 37);

            ReflectionHelper.setPrivateValue(EntityLivingBase.class, entity, 100, 38);
        } catch (Exception e) {
            ModUtil.LOGGER.error("A Damage Lens at " + tile.getX() + ", " + tile.getY() + ", " + tile.getZ()
                    + " in World " + tile.getWorldObject().provider.getDimension()
                    + " threw an Exception! Don't let that happen again!", e);
        }
    }

    super.onAttacked(entity, tile);
}

From source file:de.ellpeck.actuallyadditions.mod.items.lens.LensKiller.java

@Override
protected void onAttacked(EntityLivingBase entity, IAtomicReconstructor tile) {
    if (!tile.getWorldObject().isRemote) {
        entity.attackEntityFrom(DamageSource
                .causePlayerDamage(FakePlayerFactory.getMinecraft((WorldServer) tile.getWorldObject())), 20);
    }//from w  w w.  ja  v a  2s  .  c o m
}

From source file:de.ellpeck.actuallyadditions.mod.misc.apiimpl.MethodHandler.java

@Override
public boolean invokeConversionLens(IBlockState hitState, BlockPos hitBlock, IAtomicReconstructor tile) {
    if (hitBlock != null) {
        int range = 1;
        int rangeX = 0;
        int rangeY = 0;
        int rangeZ = 0;

        EnumFacing facing = tile.getOrientation();
        if (facing != EnumFacing.UP && facing != EnumFacing.DOWN) {
            rangeY = range;//w  ww.j ava 2 s.  c  o m

            if (facing == EnumFacing.NORTH || facing == EnumFacing.SOUTH) {
                rangeX = range;
            } else {
                rangeZ = range;
            }
        } else {
            rangeX = range;
            rangeZ = range;
        }

        //Converting the Blocks
        for (int reachX = -rangeX; reachX <= rangeX; reachX++) {
            for (int reachZ = -rangeZ; reachZ <= rangeZ; reachZ++) {
                for (int reachY = -rangeY; reachY <= rangeY; reachY++) {
                    BlockPos pos = new BlockPos(hitBlock.getX() + reachX, hitBlock.getY() + reachY,
                            hitBlock.getZ() + reachZ);
                    if (!tile.getWorldObject().isAirBlock(pos)) {
                        IBlockState state = tile.getWorldObject().getBlockState(pos);
                        if (state.getBlock() instanceof BlockLaserRelay)
                            continue;
                        LensConversionRecipe recipe = LensRecipeHandler.findMatchingRecipe(
                                new ItemStack(state.getBlock(), 1, state.getBlock().getMetaFromState(state)),
                                tile.getLens());
                        if (recipe != null && tile.getEnergy() >= recipe.getEnergyUsed()) {
                            ItemStack output = recipe.getOutput();
                            if (StackUtil.isValid(output)) {
                                tile.getWorldObject().playEvent(2001, pos, Block.getStateId(state));
                                recipe.transformHook(ItemStack.EMPTY, state, pos, tile);
                                if (output.getItem() instanceof ItemBlock) {
                                    Block toPlace = Block.getBlockFromItem(output.getItem());
                                    IBlockState state2Place = toPlace.getStateForPlacement(
                                            tile.getWorldObject(), pos, facing, 0, 0, 0, output.getMetadata(),
                                            FakePlayerFactory.getMinecraft((WorldServer) tile.getWorldObject()),
                                            EnumHand.MAIN_HAND);
                                    tile.getWorldObject().setBlockState(pos, state2Place, 2);
                                } else {
                                    EntityItem item = new EntityItem(tile.getWorldObject(), pos.getX() + 0.5,
                                            pos.getY() + 0.5, pos.getZ() + 0.5, output.copy());
                                    tile.getWorldObject().spawnEntity(item);
                                    tile.getWorldObject().setBlockToAir(pos);
                                }

                                tile.extractEnergy(recipe.getEnergyUsed());
                                break;
                            }
                        }
                    }
                }
            }
        }

        //Converting the Items
        AxisAlignedBB aabb = new AxisAlignedBB(tile.getPosition().getX(), tile.getPosition().getY(),
                tile.getPosition().getZ(), hitBlock.getX() + 1, hitBlock.getY() + 1, hitBlock.getZ() + 1);
        Vec3i dir = tile.getOrientation().getDirectionVec();
        aabb = aabb.grow(0.02, 0.02, 0.02).expand(dir.getX(), dir.getY(), dir.getZ());
        List<EntityItem> items = tile.getWorldObject().getEntitiesWithinAABB(EntityItem.class, aabb);
        for (EntityItem item : items) {
            ItemStack stack = item.getItem();
            if (!item.isDead && StackUtil.isValid(stack)) {
                LensConversionRecipe recipe = LensRecipeHandler.findMatchingRecipe(stack, tile.getLens());
                if (recipe != null) {
                    int itemsPossible = Math.min(tile.getEnergy() / recipe.getEnergyUsed(), stack.getCount());

                    if (itemsPossible > 0) {
                        recipe.transformHook(item.getItem(), null, item.getPosition(), tile);
                        item.setDead();

                        if (stack.getCount() - itemsPossible > 0) {
                            ItemStack stackCopy = stack.copy();
                            stackCopy.shrink(itemsPossible);

                            EntityItem inputLeft = new EntityItem(tile.getWorldObject(), item.posX, item.posY,
                                    item.posZ, stackCopy);
                            tile.getWorldObject().spawnEntity(inputLeft);
                        }

                        ItemStack outputCopy = recipe.getOutput().copy();
                        outputCopy.setCount(itemsPossible);

                        EntityItem newItem = new EntityItem(tile.getWorldObject(), item.posX, item.posY,
                                item.posZ, outputCopy);
                        tile.getWorldObject().spawnEntity(newItem);

                        tile.extractEnergy(recipe.getEnergyUsed() * itemsPossible);
                        break;
                    }
                }
            }
        }
        return !hitState.getBlock().isAir(hitState, tile.getWorldObject(), hitBlock);
    }
    return false;
}

From source file:de.ellpeck.actuallyadditions.mod.util.WorldUtil.java

public static ItemStack useItemAtSide(EnumFacing side, World world, BlockPos pos, ItemStack stack) {
    if (world instanceof WorldServer && StackUtil.isValid(stack) && pos != null) {
        BlockPos offsetPos = pos.offset(side);
        IBlockState state = world.getBlockState(offsetPos);
        Block block = state.getBlock();//from w ww.java 2  s  .c  om
        boolean replaceable = block.isReplaceable(world, offsetPos);

        //Redstone
        if (replaceable && stack.getItem() == Items.REDSTONE) {
            world.setBlockState(offsetPos, Blocks.REDSTONE_WIRE.getDefaultState(), 2);
            return StackUtil.addStackSize(stack, -1);
        }

        //Plants
        if (replaceable && stack.getItem() instanceof IPlantable) {
            if (((IPlantable) stack.getItem()).getPlant(world, offsetPos).getBlock().canPlaceBlockAt(world,
                    offsetPos)) {
                if (world.setBlockState(offsetPos, ((IPlantable) stack.getItem()).getPlant(world, offsetPos),
                        2)) {
                    return StackUtil.addStackSize(stack, -1);
                }
            }
        }

        //Everything else
        try {
            if (world instanceof WorldServer) {
                FakePlayer fake = FakePlayerFactory.getMinecraft((WorldServer) world);
                ItemStack heldBefore = fake.getHeldItemMainhand();
                fake.setHeldItem(EnumHand.MAIN_HAND, stack.copy());

                fake.getHeldItemMainhand().onItemUse(fake, world, offsetPos, fake.getActiveHand(),
                        side.getOpposite(), 0.5F, 0.5F, 0.5F);

                ItemStack result = fake.getHeldItem(EnumHand.MAIN_HAND);
                fake.setHeldItem(EnumHand.MAIN_HAND, heldBefore);
                return result;
            }
        } catch (Exception e) {
            ModUtil.LOGGER.error("Something that places Blocks at " + offsetPos.getX() + ", " + offsetPos.getY()
                    + ", " + offsetPos.getZ() + " in World " + world.provider.getDimension()
                    + " threw an Exception! Don't let that happen again!", e);
        }
    }
    return stack;
}