Advertisement
Scouter456

Untitled

Jun 8th, 2023
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.71 KB | None | 0 0
  1. public class NetherFortressPiece extends Structure {
  2.     private static final Logger LOGGER = LogUtils.getLogger();
  3.     static Random rand = new Random();
  4.     public static final Codec<NetherFortressPiece> CODEC =  RecordCodecBuilder.<NetherFortressPiece>mapCodec(instance ->
  5.             instance.group(NetherFortressPiece.settingsCodec(instance),
  6.                     StructureTemplatePool.CODEC.fieldOf("start_pool").forGetter(structure -> structure.startPool),
  7.                     ResourceLocation.CODEC.optionalFieldOf("start_jigsaw_name").forGetter(structure -> structure.startJigsawName),
  8.                     Codec.intRange(0, 30).fieldOf("size").forGetter(structure -> structure.size),
  9.                     HeightProvider.CODEC.fieldOf("start_height").forGetter(structure -> structure.startHeight),
  10.                     Heightmap.Types.CODEC.optionalFieldOf("project_start_to_heightmap").forGetter(structure -> structure.projectStartToHeightmap),
  11.                     Codec.intRange(1, 128).fieldOf("max_distance_from_center").forGetter(structure -> structure.maxDistanceFromCenter)
  12.             ).apply(instance, NetherFortressPiece::new)).codec();
  13.  
  14.     private final Holder<StructureTemplatePool> startPool;
  15.     private final Optional<ResourceLocation> startJigsawName;
  16.     private final int size;
  17.     private final HeightProvider startHeight;
  18.     private final Optional<Heightmap.Types> projectStartToHeightmap;
  19.     private final int maxDistanceFromCenter;
  20.  
  21.  
  22.     public NetherFortressPiece(StructureSettings config,
  23.                                Holder<StructureTemplatePool> startPool,
  24.                                Optional<ResourceLocation> startJigsawName,
  25.                                int size,
  26.                                HeightProvider startHeight,
  27.                                Optional<Heightmap.Types> projectStartToHeightmap,
  28.                                int maxDistanceFromCenter)
  29.     {
  30.         super(config);
  31.         this.startPool = startPool;
  32.         this.startJigsawName = startJigsawName;
  33.         this.size = size;
  34.         this.startHeight = startHeight;
  35.         this.projectStartToHeightmap = projectStartToHeightmap;
  36.         this.maxDistanceFromCenter = maxDistanceFromCenter;
  37.     }
  38.  
  39.     @Override
  40.     public GenerationStep.Decoration step() {
  41.         return GenerationStep.Decoration.SURFACE_STRUCTURES;
  42.     }
  43.  
  44.     //private static boolean isFeatureChunk(Structure.GenerationContext context, BlockPos pos) {
  45.     //    // Grabs the chunk position we are at
  46.     //    ChunkPos chunkpos = context.chunkPos();
  47. //
  48.   //      // Checks to make sure our structure does not spawn within 10 chunks of an Ocean Monument
  49.         // to demonstrate how this method is good for checking extra conditions for spawning
  50.    //     return !context.chunkGenerator().findNearestMapStructure( BuiltinStructureSets.NETHER_COMPLEXES, pos, 10);
  51.     //}
  52.     @Override
  53.     public Optional<GenerationStub> findGenerationPoint(GenerationContext context) {
  54.         // Check if the spot is valid for our structure. This is just as another method for cleanness.
  55.         // Returning an empty optional tells the game to skip this spot as it will not generate the structure.
  56.         int y = rand.nextInt(15,30);
  57.         BlockPos centerPos = new BlockPos(context.chunkPos().getMinBlockX(), y, context.chunkPos().getMinBlockZ());
  58.  
  59.         // Turns the chunk coordinates into actual coordinates we can use. (Gets center of that chunk)
  60.         BlockPos blockpos = context.chunkPos().getMiddleBlockPosition(0);
  61.         blockpos = blockpos.offset(0,y,0);
  62.  
  63.         BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos();
  64.         ChunkGenerator chunkGenerator = context.chunkGenerator();
  65.         mutable = mutable.set(centerPos);
  66.         NoiseColumn columnOfBlocks = chunkGenerator.getBaseColumn(blockpos.getX(), blockpos.getZ(), context.heightAccessor(), context.randomState());
  67.         BlockState state = columnOfBlocks.getBlock(blockpos.getY());
  68.         if(!state.getFluidState().is(Fluids.LAVA)){
  69.             return Optional.empty();
  70.         }
  71.  
  72.         while(state.getFluidState().is(Fluids.LAVA) || blockpos.getY() > 0){
  73.             blockpos = blockpos.below();
  74.             state = columnOfBlocks.getBlock(blockpos.getY());
  75.             if(!state.getFluidState().is(Fluids.LAVA)){
  76.                 break;
  77.             }
  78.         }
  79.         // Set's our spawning blockpos's y offset to be 60 blocks up.
  80.         // Since we are going to have heightmap/terrain height spawning set to true further down, this will make it so we spawn 60 blocks above terrain.
  81.         // If we wanted to spawn on ocean floor, we would set heightmap/terrain height spawning to false and the grab the y value of the terrain with OCEAN_FLOOR_WG heightmap.
  82.  
  83.         Optional<GenerationStub> structurePiecesGenerator =
  84.                 JigsawPlacement.addPieces(
  85.                         context, // Used for JigsawPlacement to get all the proper behaviors done.
  86.                         this.startPool, // The starting pool to use to create the structure layout from
  87.                         this.startJigsawName, // Can be used to only spawn from one Jigsaw block. But we don't need to worry about this.
  88.                         this.size, // How deep a branch of pieces can go away from center piece. (5 means branches cannot be longer than 5 pieces from center piece)
  89.                         blockpos, // Where to spawn the structure.
  90.                         false, // "useExpansionHack" This is for legacy villages to generate properly. You should keep this false always.
  91.                         this.projectStartToHeightmap, // Adds the terrain height's y value to the passed in blockpos's y value. (This uses WORLD_SURFACE_WG heightmap which stops at top water too)
  92.                         // Here, blockpos's y value is 60 which means the structure spawn 60 blocks above terrain height.
  93.                         // Set this to false for structure to be place only at the passed in blockpos's Y value instead.
  94.                         // Definitely keep this false when placing structures in the nether as otherwise, heightmap placing will put the structure on the Bedrock roof.
  95.                         this.maxDistanceFromCenter); // Maximum limit for how far pieces can spawn from center. You cannot set this bigger than 128 or else pieces gets cutoff.
  96.  
  97.  
  98.         /*
  99.          * Note, you are always free to make your own JigsawPlacement class and implementation of how the structure
  100.          * should generate. It is tricky but extremely powerful if you are doing something that vanilla's jigsaw system cannot do.
  101.          * Such as for example, forcing 3 pieces to always spawn every time, limiting how often a piece spawns, or remove the intersection limitation of pieces.
  102.          *
  103.          * An example of a custom JigsawPlacement.addPieces in action can be found here (warning, it is using Mojmap mappings):
  104.          * https://github.com/TelepathicGrunt/RepurposedStructures/blob/1.18.2/src/main/java/com/telepathicgrunt/repurposedstructures/world/structures/pieces/PieceLimitedJigsawManager.java
  105.          */
  106.  
  107.         if(structurePiecesGenerator.isPresent()) {
  108.             // I use to debug and quickly find out if the structure is spawning or not and where it is.
  109.             // This is returning the coordinates of the center starting piece.
  110.             //LOGGER.info("Fortress piece at {}", blockpos);
  111.         }
  112.         // Return the pieces generator that is now set up so that the game runs it when it needs to create the layout of structure pieces.
  113.         return structurePiecesGenerator;
  114.     }
  115.  
  116.     @Override
  117.     public StructureType<?> type() {
  118.         return NDUStructures.NETHER_FORTRESS_PIECE; // Helps the game know how to turn this structure back to json to save to chunks
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement