Search in sources :

Example 1 with SpawnRateHandle

use of com.bergerkiller.generated.net.minecraft.world.level.biome.BiomeSettingsMobsHandle.SpawnRateHandle in project BKCommonLib by bergerhealer.

the class CreaturePreSpawnHandler_Spigot method handleMobsFor.

private static List<Object> handleMobsFor(World world, Object blockposition, List<Object> mobs) {
    try {
        // There is no use wasting CPU time when no one handles the event!
        if (LogicUtil.nullOrEmpty(mobs) || !CommonUtil.hasHandlers(CreaturePreSpawnEvent.getHandlerList())) {
            return mobs;
        }
        // Wrap the parameters and send the event along
        BlockPositionHandle pos = BlockPositionHandle.createHandle(blockposition);
        // Check all instances of SpawnRateHandle if they need to be cancelled
        // If they're cancelled, initialize a new List with those entries omitted
        CommonEventFactory eventFactory = CommonPlugin.getInstance().getEventFactory();
        List<Object> result = mobs;
        int numMobs = mobs.size();
        for (int n = 0; n < numMobs; n++) {
            SpawnRateHandle handle = SpawnRateHandle.createHandle(mobs.get(n));
            EntityType entityType = CommonEntityType.byNMSEntityClass(handle.getEntityClass()).entityType;
            if (eventFactory.handleCreaturePreSpawn(world, pos.getX(), pos.getY(), pos.getZ(), entityType)) {
                // add this entry to the list.
                if (result != mobs) {
                    result.add(handle.getRaw());
                }
            } else if (result == mobs) {
                // Create a new List of stuff, omit the entry that was cancelled
                result = new ArrayList<Object>();
                result.addAll(mobs.subList(0, n));
            }
        }
        return result;
    } catch (Throwable t) {
        Logging.LOGGER.log(Level.SEVERE, "Failed to handle mob pre-spawn event", t);
        return mobs;
    }
}
Also used : SpawnRateHandle(com.bergerkiller.generated.net.minecraft.world.level.biome.BiomeSettingsMobsHandle.SpawnRateHandle) EntityType(org.bukkit.entity.EntityType) CommonEntityType(com.bergerkiller.bukkit.common.entity.CommonEntityType) CommonEventFactory(com.bergerkiller.bukkit.common.events.CommonEventFactory) ArrayList(java.util.ArrayList) BlockPositionHandle(com.bergerkiller.generated.net.minecraft.core.BlockPositionHandle)

Aggregations

CommonEntityType (com.bergerkiller.bukkit.common.entity.CommonEntityType)1 CommonEventFactory (com.bergerkiller.bukkit.common.events.CommonEventFactory)1 BlockPositionHandle (com.bergerkiller.generated.net.minecraft.core.BlockPositionHandle)1 SpawnRateHandle (com.bergerkiller.generated.net.minecraft.world.level.biome.BiomeSettingsMobsHandle.SpawnRateHandle)1 ArrayList (java.util.ArrayList)1 EntityType (org.bukkit.entity.EntityType)1