From 93df3d859c9aaae7663510da37aadc4b96e23d17 Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Wed, 10 Jan 2024 18:44:31 +1100 Subject: [PATCH] #1327: Add methods to place structures with block/entity transformers By: Jishuna --- .../craftbukkit/structure/CraftStructure.java | 30 +++++++++++++++++-- .../util/CraftStructureTransformer.java | 7 +++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/structure/CraftStructure.java b/paper-server/src/main/java/org/bukkit/craftbukkit/structure/CraftStructure.java index 0a2ca21676..d01f18dbf1 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/structure/CraftStructure.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/structure/CraftStructure.java @@ -2,6 +2,7 @@ package org.bukkit.craftbukkit.structure; import com.google.common.base.Preconditions; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Random; @@ -9,6 +10,8 @@ import java.util.stream.Collectors; import net.minecraft.core.BlockPosition; import net.minecraft.util.RandomSource; import net.minecraft.world.entity.EntityTypes; +import net.minecraft.world.level.ChunkCoordIntPair; +import net.minecraft.world.level.GeneratorAccessSeed; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.EnumBlockMirror; import net.minecraft.world.level.block.EnumBlockRotation; @@ -25,12 +28,16 @@ import org.bukkit.craftbukkit.CraftRegionAccessor; import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.util.CraftBlockVector; import org.bukkit.craftbukkit.util.CraftLocation; +import org.bukkit.craftbukkit.util.CraftStructureTransformer; import org.bukkit.craftbukkit.util.RandomSourceWrapper; +import org.bukkit.craftbukkit.util.TransformerGeneratorAccess; import org.bukkit.entity.Entity; import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.structure.Palette; import org.bukkit.structure.Structure; +import org.bukkit.util.BlockTransformer; import org.bukkit.util.BlockVector; +import org.bukkit.util.EntityTransformer; public class CraftStructure implements Structure { @@ -42,19 +49,31 @@ public class CraftStructure implements Structure { @Override public void place(Location location, boolean includeEntities, StructureRotation structureRotation, Mirror mirror, int palette, float integrity, Random random) { + place(location, includeEntities, structureRotation, mirror, palette, integrity, random, Collections.emptyList(), Collections.emptyList()); + } + + @Override + public void place(Location location, boolean includeEntities, StructureRotation structureRotation, Mirror mirror, int palette, float integrity, Random random, Collection blockTransformers, Collection entityTransformers) { Preconditions.checkArgument(location != null, "Location cannot be null"); location.checkFinite(); World world = location.getWorld(); Preconditions.checkArgument(world != null, "The World of Location cannot be null"); BlockVector blockVector = new BlockVector(location.getBlockX(), location.getBlockY(), location.getBlockZ()); - place(world, blockVector, includeEntities, structureRotation, mirror, palette, integrity, random); + place(world, blockVector, includeEntities, structureRotation, mirror, palette, integrity, random, blockTransformers, entityTransformers); } @Override public void place(RegionAccessor regionAccessor, BlockVector location, boolean includeEntities, StructureRotation structureRotation, Mirror mirror, int palette, float integrity, Random random) { + place(regionAccessor, location, includeEntities, structureRotation, mirror, palette, integrity, random, Collections.emptyList(), Collections.emptyList()); + } + + @Override + public void place(RegionAccessor regionAccessor, BlockVector location, boolean includeEntities, StructureRotation structureRotation, Mirror mirror, int palette, float integrity, Random random, Collection blockTransformers, Collection entityTransformers) { Preconditions.checkArgument(location != null, "Location cannot be null"); Preconditions.checkArgument(regionAccessor != null, "RegionAccessor cannot be null"); + Preconditions.checkArgument(blockTransformers != null, "BlockTransformers cannot be null"); + Preconditions.checkArgument(entityTransformers != null, "EntityTransformers cannot be null"); location.checkFinite(); Preconditions.checkArgument(integrity >= 0F && integrity <= 1F, "Integrity value (%S) must be between 0 and 1 inclusive", integrity); @@ -69,7 +88,14 @@ public class CraftStructure implements Structure { definedstructureinfo.palette = palette; BlockPosition blockPosition = CraftBlockVector.toBlockPosition(location); - structure.placeInWorld(((CraftRegionAccessor) regionAccessor).getHandle(), blockPosition, blockPosition, definedstructureinfo, randomSource, 2); + GeneratorAccessSeed handle = ((CraftRegionAccessor) regionAccessor).getHandle(); + + TransformerGeneratorAccess access = new TransformerGeneratorAccess(); + access.setHandle(handle); + access.setStructureTransformer(new CraftStructureTransformer(handle, new ChunkCoordIntPair(blockPosition), blockTransformers, entityTransformers)); + + structure.placeInWorld(access, blockPosition, blockPosition, definedstructureinfo, randomSource, 2); + access.getStructureTransformer().discard(); } @Override diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftStructureTransformer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftStructureTransformer.java index 7889b3c0fb..05e855bd91 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftStructureTransformer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftStructureTransformer.java @@ -1,5 +1,6 @@ package org.bukkit.craftbukkit.util; +import java.util.Collection; import java.util.Objects; import net.minecraft.core.BlockPosition; import net.minecraft.world.entity.Entity; @@ -70,6 +71,12 @@ public class CraftStructureTransformer { this.limitedRegion = new CraftLimitedRegion(generatoraccessseed, chunkcoordintpair); } + public CraftStructureTransformer(GeneratorAccessSeed generatoraccessseed, ChunkCoordIntPair chunkcoordintpair, Collection blockTransformers, Collection entityTransformers) { + this.blockTransformers = blockTransformers.toArray(BlockTransformer[]::new); + this.entityTransformers = entityTransformers.toArray(EntityTransformer[]::new); + this.limitedRegion = new CraftLimitedRegion(generatoraccessseed, chunkcoordintpair); + } + public boolean transformEntity(Entity entity) { EntityTransformer[] transformers = entityTransformers; if (transformers == null || transformers.length == 0) {