Search in sources :

Example 41 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project Wizardry by TeamWizardry.

the class BlockUtils method placeBlock.

/**
 * Tries placing a block safely and fires an event for it.
 *
 * @return Whether the block was successfully placed
 */
public static boolean placeBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nullable EntityPlayerMP player) {
    if (!world.isBlockLoaded(pos))
        return false;
    EntityPlayerMP playerMP;
    if (player == null) {
        playerMP = new FakePlayer((WorldServer) world, placer);
        playerMP.setPosition(pos.getX(), pos.getY(), pos.getZ());
        playerMP.setSneaking(true);
    } else
        playerMP = player;
    if (!hasEditPermission(pos, playerMP))
        return false;
    BlockEvent.PlaceEvent event = new BlockEvent.PlaceEvent(BlockSnapshot.getBlockSnapshot(world, pos), Blocks.AIR.getDefaultState(), playerMP, playerMP.getActiveHand());
    MinecraftForge.EVENT_BUS.post(event);
    if (!event.isCanceled()) {
        world.setBlockState(pos, state);
        world.notifyBlockUpdate(pos, state, state, 3);
        return true;
    }
    return false;
}
Also used : EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) WorldServer(net.minecraft.world.WorldServer) BlockEvent(net.minecraftforge.event.world.BlockEvent) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 42 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project Cavern2 by kegare.

the class MiningAssistEventHooks method onHarvestDrops.

@SubscribeEvent(priority = EventPriority.LOWEST)
public void onHarvestDrops(HarvestDropsEvent event) {
    World world = event.getWorld();
    if (world.isRemote) {
        return;
    }
    EntityPlayer player = event.getHarvester();
    if (player == null || player instanceof FakePlayer) {
        return;
    }
    BlockPos pos = event.getPos();
    MiningAssistUnit assist = MiningAssistUnit.get(player);
    if (!assist.getCaptureDrops()) {
        return;
    }
    NonNullList<ItemStack> items = NonNullList.create();
    List<ItemStack> drops = event.getDrops();
    float chance = event.getDropChance();
    for (ItemStack stack : drops) {
        if (CaveEventHooks.RANDOM.nextFloat() < chance) {
            items.add(stack);
        }
    }
    drops.clear();
    assist.addDrops(pos, items);
}
Also used : EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) FakePlayer(net.minecraftforge.common.util.FakePlayer) MiningAssistUnit(cavern.miningassist.MiningAssistUnit) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 43 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project Cavern2 by kegare.

the class CaveEventHooks method onHarvestDrops.

@SubscribeEvent
public void onHarvestDrops(HarvestDropsEvent event) {
    if (!GeneralConfig.criticalMining || event.isSilkTouching()) {
        return;
    }
    World world = event.getWorld();
    if (world.isRemote) {
        return;
    }
    EntityPlayer player = event.getHarvester();
    if (player == null || player instanceof FakePlayer || !CavernAPI.dimension.isInCaveDimensions(player)) {
        return;
    }
    IBlockState state = event.getState();
    if (MinerStats.getPointAmount(state) <= 0) {
        return;
    }
    if (state.getMaterial() != Material.ROCK) {
        return;
    }
    MinerRank rank = MinerRank.get(MinerStats.get(player).getRank());
    float f = rank.getBoost();
    if (f <= 1.0F) {
        return;
    }
    f = (f - 1.0F) * 0.3F;
    ItemStack held = player.getHeldItemMainhand();
    String tool = state.getBlock().getHarvestTool(state);
    if (held.isEmpty() || tool == null) {
        return;
    }
    int toolLevel = held.getItem().getHarvestLevel(held, tool, player, state);
    if (toolLevel <= 0) {
        return;
    }
    f *= 1.0F + toolLevel * 0.1F;
    List<ItemStack> originalDrops = event.getDrops();
    List<ItemStack> drops = Lists.newArrayList();
    for (ItemStack stack : originalDrops) {
        if (!stack.isEmpty() && !(stack.getItem() instanceof ItemBlock) && RANDOM.nextFloat() <= f) {
            drops.add(stack.copy());
        }
    }
    if (!drops.isEmpty()) {
        CriticalMiningEvent criticalEvent = new CriticalMiningEvent(world, event.getPos(), state, player, event.getFortuneLevel(), originalDrops, drops);
        if (MinecraftForge.EVENT_BUS.post(criticalEvent)) {
            return;
        }
        player.sendStatusMessage(new TextComponentTranslation("cavern.message.mining.critical"), true);
        originalDrops.addAll(criticalEvent.getBonusDrops());
    }
}
Also used : TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) IBlockState(net.minecraft.block.state.IBlockState) CriticalMiningEvent(cavern.api.event.CriticalMiningEvent) World(net.minecraft.world.World) ItemBlock(net.minecraft.item.ItemBlock) FakePlayer(net.minecraftforge.common.util.FakePlayer) MinerRank(cavern.stats.MinerRank) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 44 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project Cavern2 by kegare.

the class CaveEventHooks method onLivingUpdate.

@SubscribeEvent
public void onLivingUpdate(LivingUpdateEvent event) {
    EntityLivingBase entity = event.getEntityLiving();
    if (!(entity instanceof EntityPlayer) || entity instanceof FakePlayer) {
        return;
    }
    EntityPlayer player = (EntityPlayer) entity;
    MiningData.get(player).onUpdate();
    if (!player.isInWater() || !CavernAPI.dimension.isInCaveDimensions(player)) {
        return;
    }
    IMinerStats stats = MinerStats.get(player);
    if (stats.getRank() < MinerRank.AQUA_MINER.getRank()) {
        return;
    }
    if (!player.canBreatheUnderwater() && !player.isPotionActive(MobEffects.WATER_BREATHING) && player.ticksExisted % 20 == 0) {
        player.setAir(300);
    }
    if (player.capabilities.isFlying || EnchantmentHelper.getDepthStriderModifier(player) > 0) {
        return;
    }
    double prevY = player.posY;
    float vec1 = 0.6F;
    float vec2 = 0.01F;
    float vec3 = 0.4F;
    if (!player.onGround) {
        vec3 *= 0.5F;
    }
    if (player.getArmorVisibility() >= 0.75F) {
        vec3 *= 0.5F;
    }
    if (vec3 > 0.0F) {
        vec1 += (0.54600006F - vec1) * vec3 / 3.0F;
        vec2 += (player.getAIMoveSpeed() - vec2) * vec3 / 3.0F;
    }
    player.moveRelative(player.moveStrafing, player.moveVertical, player.moveForward, vec2);
    player.move(MoverType.SELF, player.motionX, player.motionY, player.motionZ);
    player.motionX *= vec1;
    player.motionY *= 0.800000011920929D;
    player.motionZ *= vec1;
    if (player.collidedHorizontally && player.isOffsetPositionInLiquid(player.motionX, player.motionY + 0.6000000238418579D - player.posY + prevY, player.motionZ)) {
        player.motionY = 0.30000001192092896D;
    }
    if (player.isSwingInProgress || player.isSneaking()) {
        player.motionY *= 0.3D;
    }
}
Also used : EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) FakePlayer(net.minecraftforge.common.util.FakePlayer) IMinerStats(cavern.api.IMinerStats) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 45 with FakePlayer

use of net.minecraftforge.common.util.FakePlayer in project Cavern2 by kegare.

the class CaveEventHooks method onBlockBreak.

@SubscribeEvent
public void onBlockBreak(BreakEvent event) {
    EntityPlayer entityPlayer = event.getPlayer();
    if (entityPlayer == null || entityPlayer instanceof FakePlayer || !(entityPlayer instanceof EntityPlayerMP)) {
        return;
    }
    EntityPlayerMP player = (EntityPlayerMP) entityPlayer;
    if (!CavernAPI.dimension.isInCaveDimensions(player)) {
        return;
    }
    World world = event.getWorld();
    IBlockState state = event.getState();
    ItemStack stack = player.getHeldItemMainhand();
    if (GeneralConfig.isMiningPointItem(stack)) {
        int point = MinerStats.getPointAmount(state);
        if (point != 0) {
            IMinerStats stats = MinerStats.get(player);
            IMiningData data = MiningData.get(player);
            if (player.inventory.hasItemStack(ItemCave.EnumType.MINER_ORB.getItemStack())) {
                if (RANDOM.nextDouble() < 0.15D) {
                    point += Math.max(point / 2, 1);
                }
            }
            stats.addPoint(point);
            stats.addMiningRecord(new BlockMeta(state));
            data.notifyMining(state, point);
            int combo = data.getMiningCombo();
            if (combo > 0 && combo % 10 == 0) {
                world.playSound(null, player.posX, player.posY + 0.25D, player.posZ, SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 0.1F, 0.5F * ((RANDOM.nextFloat() - RANDOM.nextFloat()) * 0.7F + 1.8F));
                player.addExperience(combo / 10);
            }
            if (combo >= 50) {
                PlayerHelper.grantAdvancement(player, "good_mine");
            }
            CaveNetworkRegistry.sendTo(new MiningMessage(state, point), player);
        }
    }
    if (CavernAPI.dimension.isInFrostMountains(player)) {
        if (player.capabilities.isCreativeMode) {
            return;
        }
        if (state.getBlock() != Blocks.PACKED_ICE || EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, stack) > 0) {
            return;
        }
        BlockPos pos = event.getPos();
        Biome biome = world.getBiome(pos);
        if (!biome.isSnowyBiome()) {
            return;
        }
        if (RANDOM.nextDouble() < 0.05D) {
            Block.spawnAsEntity(world, pos, new ItemStack(Blocks.ICE));
        } else if (RANDOM.nextDouble() < 0.005D) {
            Block.spawnAsEntity(world, pos, RandomiteHelper.getDropItem());
        } else if (stack.getItem() instanceof IIceEquipment && RANDOM.nextDouble() < 0.03D || RANDOM.nextDouble() < 0.01D) {
            Block.spawnAsEntity(world, pos, new ItemStack(Blocks.PACKED_ICE));
        }
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) IIceEquipment(cavern.api.IIceEquipment) World(net.minecraft.world.World) FakePlayer(net.minecraftforge.common.util.FakePlayer) IMinerStats(cavern.api.IMinerStats) IMiningData(cavern.api.IMiningData) Biome(net.minecraft.world.biome.Biome) EntityPlayer(net.minecraft.entity.player.EntityPlayer) MiningMessage(cavern.network.client.MiningMessage) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) BlockMeta(cavern.util.BlockMeta) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

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