1717 * along with this program. If not, see <https://www.gnu.org/licenses/>.
1818 */
1919
20- package com .sk89q .worldedit .bukkit .adapter .impl .v1_21_5 ;
20+ package com .sk89q .worldedit .bukkit .adapter .ext . fawe .v1_21_5 ;
2121
2222import com .google .common .cache .CacheBuilder ;
2323import com .google .common .cache .CacheLoader ;
3535import com .sk89q .worldedit .blocks .BaseItemStack ;
3636import com .sk89q .worldedit .bukkit .BukkitAdapter ;
3737import com .sk89q .worldedit .bukkit .adapter .BukkitImplAdapter ;
38+ import com .sk89q .worldedit .bukkit .adapter .impl .fawe .v1_21_5 .PaperweightFaweAdapter ;
3839import com .sk89q .worldedit .entity .BaseEntity ;
3940import com .sk89q .worldedit .extension .platform .Watchdog ;
4041import com .sk89q .worldedit .extent .Extent ;
6768import com .sk89q .worldedit .world .block .BlockType ;
6869import com .sk89q .worldedit .world .block .BlockTypes ;
6970import com .sk89q .worldedit .world .entity .EntityTypes ;
70- import com .sk89q .worldedit .world .generation .ConfiguredFeatureType ;
71- import com .sk89q .worldedit .world .generation .StructureType ;
7271import com .sk89q .worldedit .world .item .ItemType ;
7372import net .minecraft .SharedConstants ;
7473import net .minecraft .Util ;
197196import static com .google .common .base .Preconditions .checkNotNull ;
198197import static com .google .common .base .Preconditions .checkState ;
199198
200- public final class PaperweightAdapter implements BukkitImplAdapter {
199+ public final class PaperweightAdapter implements BukkitImplAdapter < net . minecraft . nbt . Tag > {
201200
202201 private final Logger logger = Logger .getLogger (getClass ().getCanonicalName ());
203202
@@ -375,7 +374,7 @@ public BaseBlock getFullBlock(Location location) {
375374 BlockEntity te = chunk .getBlockEntity (blockPos );
376375 if (te != null ) {
377376 net .minecraft .nbt .CompoundTag tag = te .saveWithId (MinecraftServer .getServer ().registryAccess ());
378- return state .toBaseBlock (LazyReference .from (() -> (LinCompoundTag ) toNative (tag )));
377+ return state .toBaseBlock (LazyReference .from (() -> (LinCompoundTag ) toNativeLin (tag )));
379378 }
380379
381380 return state .toBaseBlock ();
@@ -482,7 +481,7 @@ public BaseEntity getEntity(org.bukkit.entity.Entity entity) {
482481 readEntityIntoTag (mcEntity , tag );
483482 return new BaseEntity (
484483 EntityTypes .get (id ),
485- LazyReference .from (() -> (LinCompoundTag ) toNative (tag ))
484+ LazyReference .from (() -> (LinCompoundTag ) toNativeLin (tag ))
486485 );
487486 }
488487
@@ -555,28 +554,8 @@ public Component getRichItemName(BaseItemStack itemStack) {
555554 );
556555 }
557556
558- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
559- private static final LoadingCache <net .minecraft .world .level .block .state .properties .Property , Property <?>> PROPERTY_CACHE = CacheBuilder .newBuilder ().build (new CacheLoader <>() {
560- @ Override
561- public Property <?> load (net .minecraft .world .level .block .state .properties .Property state ) {
562- return switch (state ) {
563- case net .minecraft .world .level .block .state .properties .BooleanProperty ignored ->
564- new BooleanProperty (state .getName (), ImmutableList .copyOf (state .getPossibleValues ()));
565- case net .minecraft .world .level .block .state .properties .EnumProperty ignored -> {
566- if (state .getValueClass () == net .minecraft .core .Direction .class ) {
567- yield new DirectionalProperty (state .getName (),
568- (List <Direction >) state .getPossibleValues ().stream ().map (e -> Direction .valueOf (((StringRepresentable ) e ).getSerializedName ().toUpperCase (Locale .ROOT ))).toList ());
569- }
570- yield new EnumProperty (state .getName (),
571- (List <String >) state .getPossibleValues ().stream ().map (e -> ((StringRepresentable ) e ).getSerializedName ()).toList ());
572- }
573- case net .minecraft .world .level .block .state .properties .IntegerProperty ignored ->
574- new IntegerProperty (state .getName (), ImmutableList .copyOf (state .getPossibleValues ()));
575- default ->
576- throw new IllegalArgumentException ("WorldEdit needs an update to support " + state .getClass ().getSimpleName ());
577- };
578- }
579- });
557+ private static final LoadingCache <net .minecraft .world .level .block .state .properties .Property <?>, Property <?>> PROPERTY_CACHE =
558+ CacheBuilder .newBuilder ().build (CacheLoader .from (PaperweightFaweAdapter ::adaptProperty ));
580559
581560 @ SuppressWarnings ({ "rawtypes" })
582561 @ Override
@@ -648,7 +627,8 @@ public BaseItemStack adapt(org.bukkit.inventory.ItemStack itemStack) {
648627 registryAccess .createSerializationContext (NbtOps .INSTANCE ),
649628 nmsStack .getComponentsPatch ()
650629 ).getOrThrow ();
651- return new BaseItemStack (BukkitAdapter .asItemType (itemStack .getType ()), LazyReference .from (() -> (LinCompoundTag ) toNative (tag )), itemStack .getAmount ());
630+ return new BaseItemStack (BukkitAdapter .asItemType (itemStack .getType ()),
631+ LazyReference .from (() -> (LinCompoundTag ) toNativeLin (tag )), itemStack .getAmount ());
652632 }
653633
654634 private final LoadingCache <ServerLevel , PaperweightFakePlayer > fakePlayers
@@ -826,7 +806,7 @@ private void regenForWorld(Region region, Extent extent, ServerLevel serverWorld
826806 BlockEntity blockEntity = chunk .getBlockEntity (pos );
827807 if (blockEntity != null ) {
828808 net .minecraft .nbt .CompoundTag tag = blockEntity .saveWithId (serverWorld .registryAccess ());
829- state = state .toBaseBlock (LazyReference .from (() -> (LinCompoundTag ) toNative (tag )));
809+ state = state .toBaseBlock (LazyReference .from (() -> (LinCompoundTag ) toNativeLin (tag )));
830810 }
831811 extent .setBlock (vec , state .toBaseBlock ());
832812 if (options .shouldRegenBiomes ()) {
@@ -903,20 +883,6 @@ public void initializeRegistries() {
903883 }
904884 }
905885
906- // Features
907- for (ResourceLocation name : server .registryAccess ().lookupOrThrow (Registries .CONFIGURED_FEATURE ).keySet ()) {
908- if (ConfiguredFeatureType .REGISTRY .get (name .toString ()) == null ) {
909- ConfiguredFeatureType .REGISTRY .register (name .toString (), new ConfiguredFeatureType (name .toString ()));
910- }
911- }
912-
913- // Structures
914- for (ResourceLocation name : server .registryAccess ().lookupOrThrow (Registries .STRUCTURE ).keySet ()) {
915- if (StructureType .REGISTRY .get (name .toString ()) == null ) {
916- StructureType .REGISTRY .register (name .toString (), new StructureType (name .toString ()));
917- }
918- }
919-
920886 // BiomeCategories
921887 Registry <Biome > biomeRegistry = server .registryAccess ().lookupOrThrow (Registries .BIOME );
922888 biomeRegistry .getTags ().forEach (tag -> {
@@ -935,60 +901,6 @@ public void initializeRegistries() {
935901 });
936902 }
937903
938- public boolean generateFeature (ConfiguredFeatureType type , World world , EditSession session , BlockVector3 pt ) {
939- ServerLevel originalWorld = ((CraftWorld ) world ).getHandle ();
940- ConfiguredFeature <?, ?> feature = originalWorld .registryAccess ().lookupOrThrow (Registries .CONFIGURED_FEATURE ).getValue (ResourceLocation .tryParse (type .id ()));
941- ServerChunkCache chunkManager = originalWorld .getChunkSource ();
942- try (PaperweightServerLevelDelegateProxy .LevelAndProxy proxyLevel =
943- PaperweightServerLevelDelegateProxy .newInstance (session , originalWorld , this )) {
944- return feature != null && feature .place (proxyLevel .level (), chunkManager .getGenerator (), random , new BlockPos (pt .x (), pt .y (), pt .z ()));
945- } catch (MaxChangedBlocksException e ) {
946- throw new RuntimeException (e );
947- }
948- }
949-
950- public boolean generateStructure (StructureType type , World world , EditSession session , BlockVector3 pt ) {
951- ServerLevel originalWorld = ((CraftWorld ) world ).getHandle ();
952- Registry <Structure > structureRegistry = originalWorld .registryAccess ().lookupOrThrow (Registries .STRUCTURE );
953- Structure structure = structureRegistry .getValue (ResourceLocation .tryParse (type .id ()));
954- if (structure == null ) {
955- return false ;
956- }
957-
958- ServerChunkCache chunkManager = originalWorld .getChunkSource ();
959- try (PaperweightServerLevelDelegateProxy .LevelAndProxy proxyLevel =
960- PaperweightServerLevelDelegateProxy .newInstance (session , originalWorld , this )) {
961- ChunkPos chunkPos = new ChunkPos (new BlockPos (pt .x (), pt .y (), pt .z ()));
962- StructureStart structureStart = structure .generate (
963- structureRegistry .wrapAsHolder (structure ), originalWorld .dimension (), originalWorld .registryAccess (),
964- chunkManager .getGenerator (), chunkManager .getGenerator ().getBiomeSource (), chunkManager .randomState (),
965- originalWorld .getStructureManager (), originalWorld .getSeed (), chunkPos , 0 ,
966- proxyLevel .level (), biome -> true
967- );
968-
969- if (!structureStart .isValid ()) {
970- return false ;
971- } else {
972- BoundingBox boundingBox = structureStart .getBoundingBox ();
973- ChunkPos min = new ChunkPos (SectionPos .blockToSectionCoord (boundingBox .minX ()), SectionPos .blockToSectionCoord (boundingBox .minZ ()));
974- ChunkPos max = new ChunkPos (SectionPos .blockToSectionCoord (boundingBox .maxX ()), SectionPos .blockToSectionCoord (boundingBox .maxZ ()));
975- ChunkPos .rangeClosed (min , max ).forEach ((chunkPosx ) ->
976- structureStart .placeInChunk (
977- proxyLevel .level (), originalWorld .structureManager (), chunkManager .getGenerator (),
978- originalWorld .getRandom (),
979- new BoundingBox (
980- chunkPosx .getMinBlockX (), originalWorld .getMinY (), chunkPosx .getMinBlockZ (),
981- chunkPosx .getMaxBlockX (), originalWorld .getMaxY (), chunkPosx .getMaxBlockZ ()
982- ), chunkPosx
983- )
984- );
985- return true ;
986- }
987- } catch (MaxChangedBlocksException e ) {
988- throw new RuntimeException (e );
989- }
990- }
991-
992904 @ Override
993905 public void sendBiomeUpdates (World world , Iterable <BlockVector2 > chunks ) {
994906 ServerLevel originalWorld = ((CraftWorld ) world ).getHandle ();
@@ -1011,14 +923,14 @@ public void sendBiomeUpdates(World world, Iterable<BlockVector2> chunks) {
1011923 * @param foreign non-native NMS NBT structure
1012924 * @return native WorldEdit NBT structure
1013925 */
1014- LinTag <?> toNative (net .minecraft .nbt .Tag foreign ) {
926+ public LinTag <?> toNativeLin (net .minecraft .nbt .Tag foreign ) {
1015927 if (foreign == null ) {
1016928 return null ;
1017929 }
1018930 if (foreign instanceof net .minecraft .nbt .CompoundTag compoundTag ) {
1019931 LinCompoundTag .Builder builder = LinCompoundTag .builder ();
1020932 for (var entry : compoundTag .keySet ()) {
1021- builder .put (entry , toNative (compoundTag .get (entry )));
933+ builder .put (entry , toNativeLin (compoundTag .get (entry )));
1022934 }
1023935 return builder .build ();
1024936 } else if (foreign instanceof net .minecraft .nbt .ByteTag byteTag ) {
@@ -1055,6 +967,56 @@ LinTag<?> toNative(net.minecraft.nbt.Tag foreign) {
1055967 }
1056968 }
1057969
970+ /**
971+ * Converts a WorldEdit-native NBT structure to a NMS structure.
972+ *
973+ * @param foreign structure to convert
974+ * @return non-native structure
975+ */
976+ @ Override
977+ public net .minecraft .nbt .Tag fromNativeLin (LinTag <?> foreign ) {
978+ if (foreign == null ) {
979+ return null ;
980+ }
981+ if (foreign instanceof LinCompoundTag compoundTag ) {
982+ net .minecraft .nbt .CompoundTag tag = new net .minecraft .nbt .CompoundTag ();
983+ for (var entry : compoundTag .value ().entrySet ()) {
984+ tag .put (entry .getKey (), fromNativeLin (entry .getValue ()));
985+ }
986+ return tag ;
987+ } else if (foreign instanceof LinByteTag byteTag ) {
988+ return net .minecraft .nbt .ByteTag .valueOf (byteTag .valueAsByte ());
989+ } else if (foreign instanceof LinByteArrayTag byteArrayTag ) {
990+ return new net .minecraft .nbt .ByteArrayTag (byteArrayTag .value ());
991+ } else if (foreign instanceof LinDoubleTag doubleTag ) {
992+ return net .minecraft .nbt .DoubleTag .valueOf (doubleTag .valueAsDouble ());
993+ } else if (foreign instanceof LinFloatTag floatTag ) {
994+ return net .minecraft .nbt .FloatTag .valueOf (floatTag .valueAsFloat ());
995+ } else if (foreign instanceof LinIntTag intTag ) {
996+ return net .minecraft .nbt .IntTag .valueOf (intTag .valueAsInt ());
997+ } else if (foreign instanceof LinIntArrayTag intArrayTag ) {
998+ return new net .minecraft .nbt .IntArrayTag (intArrayTag .value ());
999+ } else if (foreign instanceof LinLongArrayTag longArrayTag ) {
1000+ return new net .minecraft .nbt .LongArrayTag (longArrayTag .value ());
1001+ } else if (foreign instanceof LinListTag <?> listTag ) {
1002+ net .minecraft .nbt .ListTag tag = new net .minecraft .nbt .ListTag ();
1003+ for (var t : listTag .value ()) {
1004+ tag .add (fromNativeLin (t ));
1005+ }
1006+ return tag ;
1007+ } else if (foreign instanceof LinLongTag longTag ) {
1008+ return net .minecraft .nbt .LongTag .valueOf (longTag .valueAsLong ());
1009+ } else if (foreign instanceof LinShortTag shortTag ) {
1010+ return net .minecraft .nbt .ShortTag .valueOf (shortTag .valueAsShort ());
1011+ } else if (foreign instanceof LinStringTag stringTag ) {
1012+ return net .minecraft .nbt .StringTag .valueOf (stringTag .value ());
1013+ } else if (foreign instanceof LinEndTag ) {
1014+ return net .minecraft .nbt .EndTag .INSTANCE ;
1015+ } else {
1016+ throw new IllegalArgumentException ("Don't know how to make NMS " + foreign .getClass ().getCanonicalName ());
1017+ }
1018+ }
1019+
10581020 private static byte identifyRawElementType (net .minecraft .nbt .ListTag list ) {
10591021 byte b = 0 ;
10601022
@@ -1094,9 +1056,9 @@ private LinListTag<?> toNativeList(net.minecraft.nbt.ListTag foreign) throws Sec
10941056 ));
10951057 for (net .minecraft .nbt .Tag tag : foreign ) {
10961058 if (rawType == LinTagId .COMPOUND .id () && !(tag instanceof net .minecraft .nbt .CompoundTag )) {
1097- builder .add (toNative (wrapTag (tag )));
1059+ builder .add (toNativeLin (wrapTag (tag )));
10981060 } else {
1099- builder .add (toNative (tag ));
1061+ builder .add (toNativeLin (tag ));
11001062 }
11011063 }
11021064 return builder .build ();
0 commit comments