From 94055c54543ca7d977c64765095de0e9f2d8056b Mon Sep 17 00:00:00 2001 From: Frank Date: Sun, 28 Nov 2021 14:27:02 +0100 Subject: [PATCH] Corrected Spelling and some Cache IDs --- src/main/java/ru/bclib/api/SpawnAPI.java | 126 ------------------ ...nRuleBulder.java => SpawnRuleBuilder.java} | 74 +++++----- 2 files changed, 39 insertions(+), 161 deletions(-) delete mode 100644 src/main/java/ru/bclib/api/SpawnAPI.java rename src/main/java/ru/bclib/api/spawning/{SpawnRuleBulder.java => SpawnRuleBuilder.java} (80%) diff --git a/src/main/java/ru/bclib/api/SpawnAPI.java b/src/main/java/ru/bclib/api/SpawnAPI.java deleted file mode 100644 index 1493ca99..00000000 --- a/src/main/java/ru/bclib/api/SpawnAPI.java +++ /dev/null @@ -1,126 +0,0 @@ -package ru.bclib.api; - -import net.minecraft.core.BlockPos; -import net.minecraft.world.Difficulty; -import net.minecraft.world.entity.Entity; -import net.minecraft.world.entity.EntityType; -import net.minecraft.world.entity.MobSpawnType; -import net.minecraft.world.level.LevelAccessor; -import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.phys.AABB; -import ru.bclib.util.BlocksHelper; - -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -@Deprecated(forRemoval = true) -public class SpawnAPI { - @FunctionalInterface - public interface SpawnRule { - boolean test(EntityType type, LevelAccessor world, MobSpawnType spawnReason, BlockPos pos, Random random); - } - - public interface IsBlock{ - boolean is(BlockState state); - } - - private Class entityClass; - public SpawnAPI(Class entityClass){ - this.entityClass = entityClass; - } - - ArrayList> rules = new ArrayList<>(); - - public SpawnAPI notPeaceful() { - rules.add((type, world, spawnReason, pos, random) -> world.getDifficulty() != Difficulty.PEACEFUL); - return this; - } - - public SpawnAPI notPeacefulBelowBrightness() { - return notPeacefulBelowBrightness(7); - } - - public SpawnAPI notPeacefulBelowBrightness(int bright) { - rules.add((type, world, spawnReason, pos, random) -> world.getDifficulty() != Difficulty.PEACEFUL && world.getMaxLocalRawBrightness(pos) <= bright); - return this; - } - - public SpawnAPI belowBrightness() { - return belowBrightness(7); - } - - public SpawnAPI belowBrightness(int bright) { - rules.add((type, world, spawnReason, pos, random) -> world.getMaxLocalRawBrightness(pos) <= bright); - return this; - } - - public SpawnAPI belowMaxHeight() { - rules.add((type, world, spawnReason, pos, random) -> pos.getY() >= world.dimensionType().logicalHeight()); - return this; - } - - public SpawnAPI maxAlive(){ - return maxAlive(4, 256); - } - - public SpawnAPI maxAlive(int count){ - return maxAlive(count, 256); - } - - public SpawnAPI maxAlive(int count, int size){ - rules.add((type, world, spawnReason, pos, random) -> { - try { - final AABB box = new AABB(pos).inflate(size, 256, size); - final List list = world.getEntitiesOfClass(entityClass, box, (entity) -> true); - return list.size() < count; - } - catch (Exception e) { - return true; - } - }); - return this; - } - - public SpawnAPI maxHeight(int height) { - rules.add((type, world, spawnReason, pos, random) -> { - int h = BlocksHelper.downRay(world, pos, height+1); - return h<=height; - }); - return this; - } - - public SpawnAPI notAboveBlock(IsBlock blockTest, int height) { - rules.add((type, world, spawnReason, pos, random) -> { - int h = BlocksHelper.downRay(world, pos, height+1); - if (h>height) return false; - - for (int i = 1; i <= h; i++) - if (blockTest.is(world.getBlockState(pos.below(i)))) - return false; - - return true; - }); - return this; - } - - public SpawnAPI aboveBlock(IsBlock blockTest, int height) { - rules.add((type, world, spawnReason, pos, random) -> { - int h = BlocksHelper.downRay(world, pos, height+1); - if (h>height) return false; - - for (int i = 1; i <= h; i++) - if (blockTest.is(world.getBlockState(pos.below(i)))) - return true; - - return false; - }); - return this; - } - - public boolean canSpawn(EntityType type, LevelAccessor world, MobSpawnType spawnReason, BlockPos pos, Random random) { - return rules.stream() - .map(r -> r.test(type, world, spawnReason, pos, random)) - .reduce(true, (p, c) -> p && c); - } -} diff --git a/src/main/java/ru/bclib/api/spawning/SpawnRuleBulder.java b/src/main/java/ru/bclib/api/spawning/SpawnRuleBuilder.java similarity index 80% rename from src/main/java/ru/bclib/api/spawning/SpawnRuleBulder.java rename to src/main/java/ru/bclib/api/spawning/SpawnRuleBuilder.java index 1a328d29..ac9395d8 100644 --- a/src/main/java/ru/bclib/api/spawning/SpawnRuleBulder.java +++ b/src/main/java/ru/bclib/api/spawning/SpawnRuleBuilder.java @@ -20,20 +20,20 @@ import java.util.List; import java.util.Map; import java.util.function.Supplier; -public class SpawnRuleBulder { +public class SpawnRuleBuilder { private static final Map RULES_CACHE = Maps.newHashMap(); - private static final SpawnRuleBulder INSTANCE = new SpawnRuleBulder(); + private static final SpawnRuleBuilder INSTANCE = new SpawnRuleBuilder(); private List rules = Lists.newArrayList(); private SpawnRuleEntry entryInstance; private EntityType entityType; - private SpawnRuleBulder() {} + private SpawnRuleBuilder() {} /** * Starts new rule building process. - * @return prepared {@link SpawnRuleBulder} instance. + * @return prepared {@link SpawnRuleBuilder} instance. */ - public static SpawnRuleBulder start(EntityType entityType) { + public static SpawnRuleBuilder start(EntityType entityType) { INSTANCE.entityType = entityType; INSTANCE.rules.clear(); return INSTANCE; @@ -41,9 +41,9 @@ public class SpawnRuleBulder { /** * Stop entity spawn on peaceful {@link Difficulty} - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder notPeaceful() { + public SpawnRuleBuilder notPeaceful() { entryInstance = getFromCache("not_peaceful", () -> { return new SpawnRuleEntry(0, (type, world, spawnReason, pos, random) -> world.getDifficulty() != Difficulty.PEACEFUL); }); @@ -54,9 +54,9 @@ public class SpawnRuleBulder { /** * Restricts entity spawn above world surface (flying mobs). * @param minHeight minimal spawn height. - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder aboveGround(int minHeight) { + public SpawnRuleBuilder aboveGround(int minHeight) { entryInstance = getFromCache("above_ground", () -> { return new SpawnRuleEntry(0, (type, world, spawnReason, pos, random) -> { if (pos.getY() < world.getMinBuildHeight() + 2) { @@ -71,9 +71,9 @@ public class SpawnRuleBulder { /** * Restricts entity spawn below world logical height (useful for Nether mobs). - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder belowMaxHeight() { + public SpawnRuleBuilder belowMaxHeight() { entryInstance = getFromCache("below_max_height", () -> { return new SpawnRuleEntry(0, (type, world, spawnReason, pos, random) -> pos.getY() < world.dimensionType().logicalHeight()); }); @@ -83,9 +83,9 @@ public class SpawnRuleBulder { /** * Restricts spawning only to vanilla valid blocks. - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder onlyOnValidBlocks() { + public SpawnRuleBuilder onlyOnValidBlocks() { entryInstance = getFromCache("below_max_height", () -> { return new SpawnRuleEntry(0, (type, world, spawnReason, pos, random) -> { BlockPos below = pos.below(); @@ -98,11 +98,15 @@ public class SpawnRuleBulder { /** * Restricts spawning only to specified blocks. - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder onlyOnBlocks(Block... blocks) { + public SpawnRuleBuilder onlyOnBlocks(Block... blocks) { + String id = "" + blocks.length; + for(Block bl : blocks){ + id += "_" + bl.getDescriptionId(); + } final Block[] floorBlocks = blocks; - entryInstance = getFromCache("below_max_height", () -> { + entryInstance = getFromCache("below_max_height_" + id, () -> { return new SpawnRuleEntry(0, (type, world, spawnReason, pos, random) -> { Block below = world.getBlockState(pos.below()).getBlock(); for (Block floor: floorBlocks) { @@ -120,9 +124,9 @@ public class SpawnRuleBulder { /** * Will spawn entity with 1 / chance probability (randomly). * @param chance probability limit. - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder withChance(int chance) { + public SpawnRuleBuilder withChance(int chance) { entryInstance = getFromCache("with_chance_" + chance, () -> { return new SpawnRuleEntry(1, (type, world, spawnReason, pos, random) -> random.nextInt(chance) == 0); }); @@ -133,9 +137,9 @@ public class SpawnRuleBulder { /** * Will spawn entity only below specified brightness value. * @param lightLevel light level upper limit. - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder belowBrightness(int lightLevel) { + public SpawnRuleBuilder belowBrightness(int lightLevel) { entryInstance = getFromCache("below_brightness_" + lightLevel, () -> { return new SpawnRuleEntry(2, (type, world, spawnReason, pos, random) -> world.getMaxLocalRawBrightness(pos) <= lightLevel); }); @@ -146,9 +150,9 @@ public class SpawnRuleBulder { /** * Will spawn entity only above specified brightness value. * @param lightLevel light level lower limit. - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder aboveBrightness(int lightLevel) { + public SpawnRuleBuilder aboveBrightness(int lightLevel) { entryInstance = getFromCache("above_brightness_" + lightLevel, () -> { return new SpawnRuleEntry(2, (type, world, spawnReason, pos, random) -> world.getMaxLocalRawBrightness(pos) >= lightLevel); }); @@ -159,17 +163,17 @@ public class SpawnRuleBulder { /** * Entity spawn will follow common vanilla spawn rules - spawn in darkness and not on peaceful level. * @param lightLevel light level upper limit. - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder hostile(int lightLevel) { + public SpawnRuleBuilder hostile(int lightLevel) { return notPeaceful().belowBrightness(lightLevel); } /** * Entity spawn will follow common vanilla spawn rules - spawn in darkness (below light level 7) and not on peaceful level. - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder vanillaHostile() { + public SpawnRuleBuilder vanillaHostile() { return hostile(7); } @@ -178,11 +182,11 @@ public class SpawnRuleBulder { * @param selectorType selector {@link EntityType} to search. * @param count max entity count. * @param side side of box to search in. - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder maxNearby(EntityType selectorType, int count, int side) { + public SpawnRuleBuilder maxNearby(EntityType selectorType, int count, int side) { final Class baseClass = selectorType.getBaseClass(); - entryInstance = getFromCache("max_nearby_" + selectorType.getDescriptionId(), () -> { + entryInstance = getFromCache("max_nearby_" + selectorType.getDescriptionId()+"_"+count+"_"+side, () -> { return new SpawnRuleEntry(3, (type, world, spawnReason, pos, random) -> { try { final AABB box = new AABB(pos).inflate(side, world.getHeight(), side); @@ -202,27 +206,27 @@ public class SpawnRuleBulder { * Will spawn entity only if count of nearby entities with same type will be lower than specified. * @param count max entity count. * @param side side of box to search in. - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder maxNearby(int count, int side) { + public SpawnRuleBuilder maxNearby(int count, int side) { return maxNearby(entityType, count, side); } /** * Will spawn entity only if count of nearby entities with same type will be lower than specified. * @param count max entity count. - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder maxNearby(int count) { + public SpawnRuleBuilder maxNearby(int count) { return maxNearby(entityType, count, 256); } /** * Allows to add custom spawning rule for specific entities. * @param rule {@link SpawnRule} rule, can be a lambda expression. - * @return same {@link SpawnRuleBulder} instance. + * @return same {@link SpawnRuleBuilder} instance. */ - public SpawnRuleBulder customRule(SpawnRule rule) { + public SpawnRuleBuilder customRule(SpawnRule rule) { rules.add(new SpawnRuleEntry(7, rule)); return this; }