Search in sources :

Example 26 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project ImmersiveEngineering by BluSunrize.

the class TileEntityExcavator method digBlock.

ItemStack digBlock(BlockPos pos) {
    if (!(world instanceof WorldServer))
        return ItemStack.EMPTY;
    FakePlayer fakePlayer = FakePlayerUtil.getFakePlayer(world);
    IBlockState blockstate = world.getBlockState(pos);
    Block block = blockstate.getBlock();
    if (block != null && !world.isAirBlock(pos) && blockstate.getPlayerRelativeBlockHardness(fakePlayer, world, pos) != 0) {
        if (!block.canHarvestBlock(world, pos, fakePlayer))
            return ItemStack.EMPTY;
        block.onBlockHarvested(world, pos, blockstate, fakePlayer);
        if (block.removedByPlayer(blockstate, world, pos, fakePlayer, true)) {
            block.onPlayerDestroy(world, pos, blockstate);
            if (block.canSilkHarvest(world, pos, blockstate, fakePlayer)) {
                ArrayList<ItemStack> items = new ArrayList<ItemStack>();
                Item bitem = Item.getItemFromBlock(block);
                if (bitem == Items.AIR)
                    return ItemStack.EMPTY;
                ItemStack itemstack = new ItemStack(bitem, 1, block.getMetaFromState(blockstate));
                if (!itemstack.isEmpty())
                    items.add(itemstack);
                ForgeEventFactory.fireBlockHarvesting(items, world, pos, blockstate, 0, 1.0f, true, fakePlayer);
                for (int i = 0; i < items.size(); i++) if (i != 0) {
                    EntityItem ei = new EntityItem(world, pos.getX() + .5, pos.getY() + .5, pos.getZ() + .5, items.get(i).copy());
                    this.world.spawnEntity(ei);
                }
                world.playEvent(2001, pos, Block.getStateId(blockstate));
                if (items.size() > 0)
                    return items.get(0);
            } else {
                block.harvestBlock(world, fakePlayer, pos, blockstate, world.getTileEntity(pos), ItemStack.EMPTY);
                world.playEvent(2001, pos, Block.getStateId(blockstate));
            }
        }
    }
    return ItemStack.EMPTY;
}
Also used : Item(net.minecraft.item.Item) EntityItem(net.minecraft.entity.item.EntityItem) IBlockState(net.minecraft.block.state.IBlockState) ArrayList(java.util.ArrayList) Block(net.minecraft.block.Block) WorldServer(net.minecraft.world.WorldServer) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 27 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project Overloaded by CJ-MC-Mods.

the class TileItemManipulator method getPlayer.

private FakePlayer getPlayer() {
    if (this.player == null || this.player.get() == null) {
        FakePlayer fakePlayer = FakePlayerFactory.get((ServerWorld) this.getLevel(), FAKEPLAYER);
        this.player = new WeakReference<>(fakePlayer);
        fakePlayer.moveTo(this.getBlockPos().getX(), this.getBlockPos().getY(), this.getBlockPos().getZ(), 0f, 0f);
        fakePlayer.inventory.clearContent();
    }
    return this.player.get();
}
Also used : FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 28 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project Overloaded by CJ-MC-Mods.

the class TileItemManipulator method tick.

@Override
public void tick() {
    if (this.getLevel().isClientSide) {
        return;
    }
    ItemStack currentItem = itemStack.getStackInSlot(0);
    if (currentItem.isEmpty())
        return;
    FakePlayer player = getPlayer();
    BlockPos.Mutable blockPos = this.getBlockPos().mutable();
// for (int i = 0; i < player.interactionManager.getBlockReachDistance(); i++) {
// if (!this.getWorld().isAirBlock(blockPos.move(this.facing))) {
// EnumActionResult result = currentItem.getItem().onItemUse(player, getWorld(),
// blockPos, Hand.MAIN_HAND, facing.getOpposite(), 0.5f, 0.5f, 0.5f);
// System.out.println(result);
// currentItem.onItemUse(player,this.getWorld(),blockPos,Hand.MAIN_HAND,
// this.facing.getOpposite(),0.5f,0.5f,0.5f);
// 
// player.interactionManager.processRightClickBlock(player,this.getWorld(),currentItem,Hand.MAIN_HAND,
// blockPos,this.facing.getOpposite(),0.5f,0.5f,0.5f);
// break;
// }
// }
}
Also used : BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 29 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project RecurrentComplex by Ivorforce.

the class IvGuiRegistry method openGui.

// From FMLNetworkHandler.openGui
public void openGui(EntityPlayer entityPlayer, String modid, int modGuiId, ByteBuf data) {
    IvGuiHandler handler = handlers.get(modid);
    if (handler == null)
        return;
    if (entityPlayer instanceof EntityPlayerMP && !(entityPlayer instanceof FakePlayer)) {
        EntityPlayerMP entityPlayerMP = (EntityPlayerMP) entityPlayer;
        ByteBuf dataCopy = data.copy();
        Container remoteGuiContainer = handler.getServerGuiElement(modGuiId, entityPlayerMP, data);
        entityPlayerMP.getNextWindowId();
        entityPlayerMP.closeContainer();
        int windowId = entityPlayerMP.currentWindowId;
        RecurrentComplex.network.sendTo(new PacketOpenGui(windowId, modid, modGuiId, dataCopy), entityPlayerMP);
        if (// If null, it's client only
        remoteGuiContainer != null) {
            entityPlayerMP.openContainer = remoteGuiContainer;
            entityPlayerMP.openContainer.windowId = windowId;
            entityPlayerMP.openContainer.addListener(entityPlayerMP);
        }
    } else if (entityPlayer instanceof FakePlayer) {
    // NO OP - I won't even log a message!
    } else if (FMLCommonHandler.instance().getSide().equals(Side.CLIENT)) {
        RecurrentComplex.network.sendToServer(new PacketOpenGui(0, modid, modGuiId, data));
    } else {
        FMLLog.fine("Invalid attempt to open a local GUI on a dedicated server. This is likely a bug. GUI ID: %s,%d", modid, modGuiId);
    }
}
Also used : Container(net.minecraft.inventory.Container) PacketOpenGui(ivorius.reccomplex.network.PacketOpenGui) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) ByteBuf(io.netty.buffer.ByteBuf) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 30 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project GregTech by GregTechCE.

the class GregFakePlayer method get.

public static FakePlayer get(WorldServer world) {
    FakePlayer ret = GREGTECH_PLAYER != null ? GREGTECH_PLAYER.get() : null;
    if (ret == null) {
        ret = FakePlayerFactory.get(world, GREGTECH);
        GREGTECH_PLAYER = new WeakReference<>(ret);
    }
    return ret;
}
Also used : FakePlayer(net.minecraftforge.common.util.FakePlayer)

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