From 1356a91e778f0646da666faf942667cc9894f0f9 Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 8 Jun 2022 16:41:44 +0200 Subject: [PATCH] [Fix] Reduced Radius for Eternal Portal Search --- .../java/org/betterx/betterend/BetterEnd.java | 3 +- .../betterend/commands/CommandRegistry.java | 72 +++++++++++++++++++ .../complexmaterials/StoneMaterial.java | 6 +- .../betterend/registry/EndPoiTypes.java | 28 -------- .../betterend/rituals/EternalRitual.java | 47 ++++++------ 5 files changed, 102 insertions(+), 54 deletions(-) create mode 100644 src/main/java/org/betterx/betterend/commands/CommandRegistry.java delete mode 100644 src/main/java/org/betterx/betterend/registry/EndPoiTypes.java diff --git a/src/main/java/org/betterx/betterend/BetterEnd.java b/src/main/java/org/betterx/betterend/BetterEnd.java index 5c06618f..97a4d1c8 100644 --- a/src/main/java/org/betterx/betterend/BetterEnd.java +++ b/src/main/java/org/betterx/betterend/BetterEnd.java @@ -10,6 +10,7 @@ import org.betterx.bclib.api.v2.WorldDataAPI; import org.betterx.bclib.api.v2.levelgen.biomes.BiomeAPI; import org.betterx.bclib.util.Logger; import org.betterx.betterend.api.BetterEndPlugin; +import org.betterx.betterend.commands.CommandRegistry; import org.betterx.betterend.config.Configs; import org.betterx.betterend.effects.EndPotions; import org.betterx.betterend.integration.Integrations; @@ -37,7 +38,6 @@ public class BetterEnd implements ModInitializer { EndEntities.register(); EndBiomes.register(); EndTags.register(); - EndPoiTypes.register(); EndEnchantments.register(); EndPotions.register(); CraftingRecipes.register(); @@ -50,6 +50,7 @@ public class BetterEnd implements ModInitializer { BonemealPlants.init(); GeneratorOptions.init(); LootTableUtil.init(); + CommandRegistry.register(); FabricLoader.getInstance() .getEntrypoints("betterend", BetterEndPlugin.class) .forEach(BetterEndPlugin::register); diff --git a/src/main/java/org/betterx/betterend/commands/CommandRegistry.java b/src/main/java/org/betterx/betterend/commands/CommandRegistry.java new file mode 100644 index 00000000..a873cfe3 --- /dev/null +++ b/src/main/java/org/betterx/betterend/commands/CommandRegistry.java @@ -0,0 +1,72 @@ +package org.betterx.betterend.commands; + +import net.minecraft.commands.CommandBuildContext; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.core.Holder; +import net.minecraft.world.level.biome.Biome; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockState; + +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; + +import java.util.HashMap; +import java.util.Map; + +public class CommandRegistry { + public static void register() { + CommandRegistrationCallback.EVENT.register(CommandRegistry::register); + } + + private static void register(CommandDispatcher dispatcher, + CommandBuildContext commandBuildContext, + Commands.CommandSelection commandSelection) { +// dispatcher.register( +// Commands.literal("be") +// .requires(source -> source.hasPermission(Commands.LEVEL_OWNERS)) +// .then(Commands.literal("locate_portal") +// .requires(source -> source.hasPermission(Commands.LEVEL_OWNERS)) +// .executes(ctx -> find_poi(ctx)) +// ) +// ); + } + + private static final Map, BlockState> biomeMap = new HashMap<>(); + private static int biomeMapIdx = 0; + private static final BlockState[] states = { + Blocks.RED_STAINED_GLASS.defaultBlockState(), + Blocks.BLUE_STAINED_GLASS.defaultBlockState(), + Blocks.YELLOW_STAINED_GLASS.defaultBlockState(), + Blocks.LIME_STAINED_GLASS.defaultBlockState(), + Blocks.PINK_STAINED_GLASS.defaultBlockState(), + Blocks.GREEN_STAINED_GLASS.defaultBlockState(), + Blocks.WHITE_STAINED_GLASS.defaultBlockState(), + Blocks.BLACK_STAINED_GLASS.defaultBlockState(), + Blocks.ORANGE_STAINED_GLASS.defaultBlockState(), + Blocks.LIGHT_BLUE_STAINED_GLASS.defaultBlockState() + }; + private static final BlockState[] states2 = { + Blocks.RED_CONCRETE.defaultBlockState(), + Blocks.BLUE_CONCRETE.defaultBlockState(), + Blocks.YELLOW_CONCRETE.defaultBlockState(), + Blocks.LIME_CONCRETE.defaultBlockState(), + Blocks.PINK_CONCRETE.defaultBlockState(), + Blocks.GREEN_CONCRETE.defaultBlockState(), + Blocks.WHITE_CONCRETE.defaultBlockState(), + Blocks.BLACK_CONCRETE.defaultBlockState(), + Blocks.ORANGE_CONCRETE.defaultBlockState(), + Blocks.LIGHT_BLUE_CONCRETE.defaultBlockState() + }; + + + private static int find_poi(CommandContext ctx) throws CommandSyntaxException { + + return Command.SINGLE_SUCCESS; + } +} + diff --git a/src/main/java/org/betterx/betterend/complexmaterials/StoneMaterial.java b/src/main/java/org/betterx/betterend/complexmaterials/StoneMaterial.java index 3857a63f..88e62b38 100644 --- a/src/main/java/org/betterx/betterend/complexmaterials/StoneMaterial.java +++ b/src/main/java/org/betterx/betterend/complexmaterials/StoneMaterial.java @@ -39,8 +39,12 @@ public class StoneMaterial { public final Block furnace; public final Block flowerPot; + public static FabricBlockSettings createMaterial(MaterialColor color) { + return FabricBlockSettings.copyOf(Blocks.END_STONE).mapColor(color); + } + public StoneMaterial(String name, MaterialColor color) { - FabricBlockSettings material = FabricBlockSettings.copyOf(Blocks.END_STONE).mapColor(color); + FabricBlockSettings material = createMaterial(color); stone = EndBlocks.registerBlock(name, new BaseBlock(material)); polished = EndBlocks.registerBlock(name + "_polished", new BaseBlock(material)); diff --git a/src/main/java/org/betterx/betterend/registry/EndPoiTypes.java b/src/main/java/org/betterx/betterend/registry/EndPoiTypes.java deleted file mode 100644 index d79f185e..00000000 --- a/src/main/java/org/betterx/betterend/registry/EndPoiTypes.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.betterx.betterend.registry; - -import org.betterx.bclib.api.v2.poi.BCLPoiType; -import org.betterx.bclib.api.v2.poi.PoiRegistry; -import org.betterx.bclib.blocks.BlockProperties; -import org.betterx.betterend.BetterEnd; - -import java.util.stream.Collectors; - -public class EndPoiTypes { - public static final BCLPoiType ETERNAL_PORTAL = PoiRegistry - .register( - BetterEnd.makeID("eternal_portal"), - () -> BCLPoiType - .getBlockStates(EndBlocks.FLAVOLITE_RUNED_ETERNAL) - .stream().filter(state -> !state.getValue(BlockProperties.ACTIVE)) - .collect(Collectors.toSet()), - 0, 1 - ); - - private static void bootstrap() { - //This basically ensures that our static structures are loaded and registered - } - - public static void register() { - PoiRegistry.registerForBootstrap(EndPoiTypes::bootstrap); - } -} diff --git a/src/main/java/org/betterx/betterend/rituals/EternalRitual.java b/src/main/java/org/betterx/betterend/rituals/EternalRitual.java index 642aa58a..f24cfc43 100644 --- a/src/main/java/org/betterx/betterend/rituals/EternalRitual.java +++ b/src/main/java/org/betterx/betterend/rituals/EternalRitual.java @@ -13,6 +13,7 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; +import net.minecraft.world.entity.ai.village.poi.PoiManager; import net.minecraft.world.item.Item; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; @@ -35,13 +36,11 @@ import org.betterx.betterend.blocks.RunedFlavolite; import org.betterx.betterend.blocks.entities.EternalPedestalEntity; import org.betterx.betterend.registry.EndBlocks; import org.betterx.betterend.registry.EndFeatures; -import org.betterx.betterend.registry.EndPoiTypes; import org.betterx.betterend.registry.EndPortals; import java.awt.*; import java.util.List; import java.util.Objects; -import java.util.Optional; import java.util.Set; import java.util.function.Predicate; import org.jetbrains.annotations.Nullable; @@ -97,7 +96,7 @@ public class EternalRitual { private final static Block PORTAL = EndBlocks.END_PORTAL_BLOCK; private final static BooleanProperty ACTIVE = BlockProperties.ACTIVE; - public final static int SEARCH_RADIUS = calculateSearchSteps(48); + public final static int SEARCH_RADIUS = calculateSearchSteps(16); private Level world; private Direction.Axis axis; @@ -366,28 +365,20 @@ public class EternalRitual { @Nullable private BlockPos findFrame(ServerLevel level, BlockPos.MutableBlockPos startPos) { - Optional foundPos = EndPoiTypes - .ETERNAL_PORTAL.findPoiAround(level, startPos, false, level.getWorldBorder()); - if (foundPos.isPresent()) { - if (checkFrame(world, foundPos.get())) { - return foundPos.get(); + List foundPos = findAllBlockPos( + level, + startPos, + (SEARCH_RADIUS >> 4) + 1, + FRAME, + blockState -> blockState.is(FRAME) && !blockState.getValue(ACTIVE) + ); + for (BlockPos.MutableBlockPos testPos : foundPos) { + if (checkFrame(level, testPos)) { + return testPos; } } return null; -// List foundPos = findAllBlockPos( -// world, -// startPos, -// (SEARCH_RADIUS >> 4) + 1, -// FRAME, -// blockState -> blockState.is(FRAME) && !blockState.getValue(ACTIVE) -// ); -// for (BlockPos.MutableBlockPos testPos : foundPos) { -// if (checkFrame(world, testPos)) { -// return testPos; -// } -// } -// return null; } private BlockPos findPortalPos(int portalId) { @@ -408,6 +399,7 @@ public class EternalRitual { } Direction.Axis portalAxis = (Direction.Axis.X == axis) ? Direction.Axis.Z : Direction.Axis.X; int worldCeil = targetWorld.getHeight() - 1; + if (checkIsAreaValid(targetWorld, basePos, portalAxis)) { generatePortal(targetWorld, basePos, portalAxis, portalId); return basePos.immutable(); @@ -415,6 +407,10 @@ public class EternalRitual { Direction direction = Direction.EAST; BlockPos.MutableBlockPos checkPos = basePos.mutable(); int radius = (int) ((SEARCH_RADIUS / multiplier) + 1); + //make sure chunks are properly loaded for faster searches + PoiManager poiManager = targetWorld.getPoiManager(); + poiManager.ensureLoadedAndValid(world, checkPos, radius >> 4); + for (int step = 1; step < radius; step++) { for (int i = 0; i < (step >> 1); i++) { ChunkAccess chunk = targetWorld.getChunk(checkPos); @@ -743,11 +739,14 @@ public class EternalRitual { * @param condition Predicate for test block states in the chunk section * @return List of positions of the all found blocks or empty list. */ - public static List findAllBlockPos(Level world, + public static List findAllBlockPos(ServerLevel world, BlockPos.MutableBlockPos checkPos, int radius, Block searchBlock, Predicate condition) { + //make sure chunks are properly loaded for faster searches + PoiManager poiManager = world.getPoiManager(); + poiManager.ensureLoadedAndValid(world, checkPos, radius >> 4); List posFound = Lists.newArrayList(); Direction moveDirection = Direction.EAST; @@ -779,7 +778,7 @@ public class EternalRitual { return posFound; } - public static int calculateSearchSteps(int radius) { - return radius * 4 - 1; + public static int calculateSearchSteps(int chunkRadius) { + return chunkRadius * 4 - 1; } }