Search in sources :

Example 1 with Result

use of net.minecraftforge.fml.common.eventhandler.Event.Result in project MinecraftForge by MinecraftForge.

the class ForgeEventFactory method canCreateFluidSource.

public static boolean canCreateFluidSource(World world, BlockPos pos, IBlockState state, boolean def) {
    CreateFluidSourceEvent evt = new CreateFluidSourceEvent(world, pos, state);
    MinecraftForge.EVENT_BUS.post(evt);
    Result result = evt.getResult();
    return result == Result.DEFAULT ? def : result == Result.ALLOW;
}
Also used : CreateFluidSourceEvent(net.minecraftforge.event.world.BlockEvent.CreateFluidSourceEvent) ActionResult(net.minecraft.util.ActionResult) EnumActionResult(net.minecraft.util.EnumActionResult) RayTraceResult(net.minecraft.util.math.RayTraceResult) Result(net.minecraftforge.fml.common.eventhandler.Event.Result) SleepResult(net.minecraft.entity.player.EntityPlayer.SleepResult)

Example 2 with Result

use of net.minecraftforge.fml.common.eventhandler.Event.Result in project MinecraftForge by MinecraftForge.

the class ForgeEventFactory method fireSleepingLocationCheck.

public static boolean fireSleepingLocationCheck(EntityPlayer player, BlockPos sleepingLocation) {
    SleepingLocationCheckEvent evt = new SleepingLocationCheckEvent(player, sleepingLocation);
    MinecraftForge.EVENT_BUS.post(evt);
    Result canContinueSleep = evt.getResult();
    if (canContinueSleep == Result.DEFAULT) {
        IBlockState state = player.world.getBlockState(player.bedLocation);
        return state.getBlock().isBed(state, player.world, player.bedLocation, player);
    } else
        return canContinueSleep == Result.ALLOW;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) SleepingLocationCheckEvent(net.minecraftforge.event.entity.player.SleepingLocationCheckEvent) ActionResult(net.minecraft.util.ActionResult) EnumActionResult(net.minecraft.util.EnumActionResult) RayTraceResult(net.minecraft.util.math.RayTraceResult) Result(net.minecraftforge.fml.common.eventhandler.Event.Result) SleepResult(net.minecraft.entity.player.EntityPlayer.SleepResult)

Example 3 with Result

use of net.minecraftforge.fml.common.eventhandler.Event.Result in project AppleCore by squeek502.

the class Hooks method onAppleCoreFoodStatsUpdate.

public static boolean onAppleCoreFoodStatsUpdate(FoodStats foodStats, EntityPlayer player) {
    if (!(foodStats instanceof IAppleCoreFoodStats))
        return false;
    IAppleCoreFoodStats appleCoreFoodStats = (IAppleCoreFoodStats) foodStats;
    appleCoreFoodStats.setPrevFoodLevel(foodStats.getFoodLevel());
    Result allowExhaustionResult = Hooks.fireAllowExhaustionEvent(player);
    float maxExhaustion = Hooks.fireExhaustionTickEvent(player, appleCoreFoodStats.getExhaustion());
    if (allowExhaustionResult == Result.ALLOW || (allowExhaustionResult == Result.DEFAULT && appleCoreFoodStats.getExhaustion() >= maxExhaustion)) {
        ExhaustionEvent.Exhausted exhaustedEvent = Hooks.fireExhaustionMaxEvent(player, maxExhaustion, appleCoreFoodStats.getExhaustion());
        appleCoreFoodStats.setExhaustion(appleCoreFoodStats.getExhaustion() + exhaustedEvent.deltaExhaustion);
        if (!exhaustedEvent.isCanceled()) {
            appleCoreFoodStats.setSaturation(Math.max(foodStats.getSaturationLevel() + exhaustedEvent.deltaSaturation, 0.0F));
            foodStats.setFoodLevel(Math.max(foodStats.getFoodLevel() + exhaustedEvent.deltaHunger, 0));
        }
    }
    boolean hasNaturalRegen = player.worldObj.getGameRules().getBoolean("naturalRegeneration");
    Result allowSaturatedRegenResult = Hooks.fireAllowSaturatedRegenEvent(player);
    boolean shouldDoSaturatedRegen = allowSaturatedRegenResult == Result.ALLOW || (allowSaturatedRegenResult == Result.DEFAULT && hasNaturalRegen && foodStats.getSaturationLevel() > 0.0F && player.shouldHeal() && foodStats.getFoodLevel() >= 20);
    Result allowRegenResult = shouldDoSaturatedRegen ? Result.DENY : Hooks.fireAllowRegenEvent(player);
    boolean shouldDoRegen = allowRegenResult == Result.ALLOW || (allowRegenResult == Result.DEFAULT && hasNaturalRegen && foodStats.getFoodLevel() >= 18 && player.shouldHeal());
    if (shouldDoSaturatedRegen) {
        appleCoreFoodStats.setFoodTimer(appleCoreFoodStats.getFoodTimer() + 1);
        if (appleCoreFoodStats.getFoodTimer() >= Hooks.fireSaturatedRegenTickEvent(player)) {
            HealthRegenEvent.SaturatedRegen saturatedRegenEvent = Hooks.fireSaturatedRegenEvent(player);
            if (!saturatedRegenEvent.isCanceled()) {
                player.heal(saturatedRegenEvent.deltaHealth);
                foodStats.addExhaustion(saturatedRegenEvent.deltaExhaustion);
            }
            appleCoreFoodStats.setFoodTimer(0);
        }
    } else if (shouldDoRegen) {
        appleCoreFoodStats.setFoodTimer(appleCoreFoodStats.getFoodTimer() + 1);
        if (appleCoreFoodStats.getFoodTimer() >= Hooks.fireRegenTickEvent(player)) {
            HealthRegenEvent.Regen regenEvent = Hooks.fireRegenEvent(player);
            if (!regenEvent.isCanceled()) {
                player.heal(regenEvent.deltaHealth);
                foodStats.addExhaustion(regenEvent.deltaExhaustion);
            }
            appleCoreFoodStats.setFoodTimer(0);
        }
    } else {
        appleCoreFoodStats.setFoodTimer(0);
    }
    Result allowStarvationResult = Hooks.fireAllowStarvation(player);
    if (allowStarvationResult == Result.ALLOW || (allowStarvationResult == Result.DEFAULT && foodStats.getFoodLevel() <= 0)) {
        appleCoreFoodStats.setStarveTimer(appleCoreFoodStats.getStarveTimer() + 1);
        if (appleCoreFoodStats.getStarveTimer() >= Hooks.fireStarvationTickEvent(player)) {
            StarvationEvent.Starve starveEvent = Hooks.fireStarveEvent(player);
            if (!starveEvent.isCanceled()) {
                player.attackEntityFrom(DamageSource.starve, starveEvent.starveDamage);
            }
            appleCoreFoodStats.setStarveTimer(0);
        }
    } else {
        appleCoreFoodStats.setStarveTimer(0);
    }
    return true;
}
Also used : HealthRegenEvent(squeek.applecore.api.hunger.HealthRegenEvent) StarvationEvent(squeek.applecore.api.hunger.StarvationEvent) ExhaustionEvent(squeek.applecore.api.hunger.ExhaustionEvent) IAppleCoreFoodStats(squeek.applecore.asm.util.IAppleCoreFoodStats) Result(net.minecraftforge.fml.common.eventhandler.Event.Result)

Example 4 with Result

use of net.minecraftforge.fml.common.eventhandler.Event.Result in project Cavern2 by kegare.

the class CaveEntitySpawner method findChunksForSpawning.

public int findChunksForSpawning(WorldServer world, boolean spawnHostileMobs, boolean spawnPeacefulMobs, boolean spawnOnSetTickRate) {
    if (!spawnHostileMobs && !spawnPeacefulMobs) {
        return 0;
    } else {
        eligibleChunksForSpawning.clear();
        int playerCount = 0;
        double playerHeight = 0.0D;
        for (EntityPlayer player : world.playerEntities) {
            if (!player.isSpectator()) {
                int j = MathHelper.floor(player.posX / 16.0D);
                int k = MathHelper.floor(player.posZ / 16.0D);
                int range = 8;
                for (int i1 = -range; i1 <= range; ++i1) {
                    for (int j1 = -range; j1 <= range; ++j1) {
                        boolean flag = i1 == -range || i1 == range || j1 == -range || j1 == range;
                        ChunkPos pos = new ChunkPos(i1 + j, j1 + k);
                        if (!eligibleChunksForSpawning.contains(pos)) {
                            ++playerCount;
                            playerHeight += player.posY;
                            if (!flag && world.getWorldBorder().contains(pos)) {
                                PlayerChunkMapEntry entry = world.getPlayerChunkMap().getEntry(pos.x, pos.z);
                                if (entry != null && entry.isSentToPlayers()) {
                                    eligibleChunksForSpawning.add(pos);
                                }
                            }
                        }
                    }
                }
            }
        }
        int playerY = playerHeight > 0.0D ? MathHelper.ceil(playerHeight / playerCount) : 50;
        int totalCount = 0;
        BlockPos spawnPos = world.getSpawnPoint();
        for (EnumCreatureType type : EnumCreatureType.values()) {
            int maxNumber = getMaxNumberOfCreature(world, spawnHostileMobs, spawnPeacefulMobs, spawnOnSetTickRate, type);
            double range = getSpawnRange(world, spawnHostileMobs, spawnPeacefulMobs, spawnOnSetTickRate, type);
            if (maxNumber > 0 && canSpawnCreature(world, spawnHostileMobs, spawnPeacefulMobs, spawnOnSetTickRate, type)) {
                int max = maxNumber * playerCount / MOB_COUNT_DIV;
                if (world.countEntities(type, true) <= max) {
                    List<ChunkPos> shuffled = Lists.newArrayList(eligibleChunksForSpawning);
                    Collections.shuffle(shuffled);
                    MutableBlockPos pos = new MutableBlockPos();
                    outside: for (ChunkPos chunkpos : shuffled) {
                        BlockPos blockpos = getRandomPosition(world, chunkpos.x, playerY, chunkpos.z);
                        int originX = blockpos.getX();
                        int originY = blockpos.getY();
                        int originZ = blockpos.getZ();
                        IBlockState state = world.getBlockState(blockpos);
                        if (!state.isNormalCube()) {
                            int mobCount = 0;
                            for (int l = 0; l < 3; ++l) {
                                int x = originX;
                                int y = originY;
                                int z = originZ;
                                int n = 6;
                                Biome.SpawnListEntry entry = null;
                                IEntityLivingData data = null;
                                int f = MathHelper.ceil(Math.random() * 4.0D);
                                for (int m = 0; m < f; ++m) {
                                    x += world.rand.nextInt(n) - world.rand.nextInt(n);
                                    y += world.rand.nextInt(1) - world.rand.nextInt(1);
                                    z += world.rand.nextInt(n) - world.rand.nextInt(n);
                                    pos.setPos(x, y, z);
                                    float posX = x + 0.5F;
                                    float posZ = z + 0.5F;
                                    if (!world.isAnyPlayerWithinRangeAt(posX, y, posZ, range) && spawnPos.distanceSq(posX, y, posZ) >= range * range) {
                                        if (entry == null) {
                                            entry = world.getSpawnListEntryForTypeAt(type, pos);
                                            if (entry == null) {
                                                break;
                                            }
                                        }
                                        if (world.canCreatureTypeSpawnHere(type, entry, pos) && WorldEntitySpawner.canCreatureTypeSpawnAtLocation(EntitySpawnPlacementRegistry.getPlacementForEntity(entry.entityClass), world, pos)) {
                                            EntityLiving entity;
                                            try {
                                                entity = entry.newInstance(world);
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                                return totalCount;
                                            }
                                            entity.setLocationAndAngles(posX, y, posZ, world.rand.nextFloat() * 360.0F, 0.0F);
                                            Result canSpawn = ForgeEventFactory.canEntitySpawn(entity, world, posX, y, posZ, null);
                                            if (canSpawn == Result.ALLOW || canSpawn == Result.DEFAULT && entity.getCanSpawnHere() && entity.isNotColliding()) {
                                                if (!ForgeEventFactory.doSpecialSpawn(entity, world, posX, y, posZ, null)) {
                                                    data = entity.onInitialSpawn(world.getDifficultyForLocation(entity.getPosition()), data);
                                                }
                                                if (entity.isNotColliding()) {
                                                    ++mobCount;
                                                    world.spawnEntity(entity);
                                                } else {
                                                    entity.setDead();
                                                }
                                                if (mobCount >= ForgeEventFactory.getMaxSpawnPackSize(entity)) {
                                                    continue outside;
                                                }
                                            }
                                            totalCount += mobCount;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return totalCount;
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) EnumCreatureType(net.minecraft.entity.EnumCreatureType) EntityLiving(net.minecraft.entity.EntityLiving) IEntityLivingData(net.minecraft.entity.IEntityLivingData) PlayerChunkMapEntry(net.minecraft.server.management.PlayerChunkMapEntry) Result(net.minecraftforge.fml.common.eventhandler.Event.Result) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ChunkPos(net.minecraft.util.math.ChunkPos) BlockPos(net.minecraft.util.math.BlockPos) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos)

Aggregations

Result (net.minecraftforge.fml.common.eventhandler.Event.Result)4 IBlockState (net.minecraft.block.state.IBlockState)2 SleepResult (net.minecraft.entity.player.EntityPlayer.SleepResult)2 ActionResult (net.minecraft.util.ActionResult)2 EnumActionResult (net.minecraft.util.EnumActionResult)2 RayTraceResult (net.minecraft.util.math.RayTraceResult)2 EntityLiving (net.minecraft.entity.EntityLiving)1 EnumCreatureType (net.minecraft.entity.EnumCreatureType)1 IEntityLivingData (net.minecraft.entity.IEntityLivingData)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 PlayerChunkMapEntry (net.minecraft.server.management.PlayerChunkMapEntry)1 BlockPos (net.minecraft.util.math.BlockPos)1 MutableBlockPos (net.minecraft.util.math.BlockPos.MutableBlockPos)1 ChunkPos (net.minecraft.util.math.ChunkPos)1 SleepingLocationCheckEvent (net.minecraftforge.event.entity.player.SleepingLocationCheckEvent)1 CreateFluidSourceEvent (net.minecraftforge.event.world.BlockEvent.CreateFluidSourceEvent)1 ExhaustionEvent (squeek.applecore.api.hunger.ExhaustionEvent)1 HealthRegenEvent (squeek.applecore.api.hunger.HealthRegenEvent)1 StarvationEvent (squeek.applecore.api.hunger.StarvationEvent)1 IAppleCoreFoodStats (squeek.applecore.asm.util.IAppleCoreFoodStats)1