3434import com .sk89q .worldedit .blocks .BaseItemStack ;
3535import com .sk89q .worldedit .bukkit .BukkitAdapter ;
3636import com .sk89q .worldedit .bukkit .adapter .BukkitImplAdapter ;
37+ import com .sk89q .worldedit .bukkit .adapter .impl .v1_21_3 .wna .PaperweightNativeBlockState ;
38+ import com .sk89q .worldedit .bukkit .adapter .impl .v1_21_3 .wna .PaperweightNativeWorld ;
3739import com .sk89q .worldedit .entity .BaseEntity ;
3840import com .sk89q .worldedit .extension .platform .Watchdog ;
3941import com .sk89q .worldedit .extent .Extent ;
4042import com .sk89q .worldedit .internal .Constants ;
4143import com .sk89q .worldedit .internal .block .BlockStateIdAccess ;
42- import com .sk89q .worldedit .internal .wna .WorldNativeAccess ;
44+ import com .sk89q .worldedit .internal .wna .NativeAdapter ;
45+ import com .sk89q .worldedit .internal .wna .NativeBlockState ;
46+ import com .sk89q .worldedit .internal .wna .NativePosition ;
47+ import com .sk89q .worldedit .internal .wna .NativeWorld ;
4348import com .sk89q .worldedit .math .BlockVector2 ;
4449import com .sk89q .worldedit .math .BlockVector3 ;
4550import com .sk89q .worldedit .regions .Region ;
171176import org .spigotmc .SpigotConfig ;
172177import org .spigotmc .WatchdogThread ;
173178
174- import java .lang .ref .WeakReference ;
175179import java .lang .reflect .Field ;
176180import java .lang .reflect .InvocationTargetException ;
177181import java .lang .reflect .Method ;
199203import static com .google .common .base .Preconditions .checkState ;
200204
201205public final class PaperweightAdapter implements BukkitImplAdapter {
206+ public static BlockPos adaptPos (NativePosition pos ) {
207+ return new BlockPos (pos .x (), pos .y (), pos .z ());
208+ }
202209
203210 private final Logger logger = Logger .getLogger (getClass ().getCanonicalName ());
204211
212+ private final NativeAdapter nativeAdapter = new NativeAdapter () {
213+ @ Override
214+ public NativeBlockState toNative (BlockState state ) {
215+ return new PaperweightNativeBlockState (adapt (state ));
216+ }
217+
218+ @ Override
219+ public BlockState fromNative (NativeBlockState state ) {
220+ return adapt (((PaperweightNativeBlockState ) state ).delegate ());
221+ }
222+
223+ @ Override public NativePosition newBlockPos (BlockVector3 pos ) {
224+ return pos ;
225+ }
226+ };
227+
205228 private final Field serverWorldsField ;
206229 private final Method getChunkFutureMethod ;
207230 private final Field chunkProviderExecutorField ;
@@ -259,6 +282,10 @@ public PaperweightAdapter() throws NoSuchFieldException, NoSuchMethodException {
259282 }
260283 }
261284
285+ public NativeAdapter asNativeAdapter () {
286+ return nativeAdapter ;
287+ }
288+
262289 @ Override
263290 public DataFixer getDataFixer () {
264291 return this .dataFixer ;
@@ -270,7 +297,7 @@ public DataFixer getDataFixer() {
270297 * @param tileEntity the tile entity
271298 * @param tag the tag
272299 */
273- static void readTagIntoTileEntity (net . minecraft . nbt . CompoundTag tag , BlockEntity tileEntity ) {
300+ static void readTagIntoTileEntity (CompoundTag tag , BlockEntity tileEntity ) {
274301 tileEntity .loadWithComponents (tag , MinecraftServer .getServer ().registryAccess ());
275302 tileEntity .setChanged ();
276303 }
@@ -291,7 +318,7 @@ private static String getEntityId(Entity entity) {
291318 * @param entity the entity
292319 * @param tag the tag
293320 */
294- private static void readEntityIntoTag (Entity entity , net . minecraft . nbt . CompoundTag tag ) {
321+ private static void readEntityIntoTag (Entity entity , CompoundTag tag ) {
295322 entity .save (tag );
296323 }
297324
@@ -375,7 +402,7 @@ public BaseBlock getFullBlock(Location location) {
375402 // Read the NBT data
376403 BlockEntity te = chunk .getBlockEntity (blockPos );
377404 if (te != null ) {
378- net . minecraft . nbt . CompoundTag tag = te .saveWithId (MinecraftServer .getServer ().registryAccess ());
405+ CompoundTag tag = te .saveWithId (MinecraftServer .getServer ().registryAccess ());
379406 return state .toBaseBlock (LazyReference .from (() -> (LinCompoundTag ) toNative (tag )));
380407 }
381408
@@ -417,8 +444,8 @@ public void setBiome(Location location, BiomeType biome) {
417444 }
418445
419446 @ Override
420- public WorldNativeAccess <?, ?, ?> createWorldNativeAccess (World world ) {
421- return new PaperweightWorldNativeAccess (this , new WeakReference <>((( CraftWorld ) world ).getHandle () ));
447+ public NativeWorld createNativeInterface (World world ) {
448+ return new PaperweightNativeWorld (this , (( CraftWorld ) world ).getHandle ());
422449 }
423450
424451 private static net .minecraft .core .Direction adapt (Direction face ) {
@@ -479,7 +506,7 @@ public BaseEntity getEntity(org.bukkit.entity.Entity entity) {
479506
480507 String id = getEntityId (mcEntity );
481508
482- net . minecraft . nbt . CompoundTag tag = new net . minecraft . nbt . CompoundTag ();
509+ CompoundTag tag = new CompoundTag ();
483510 readEntityIntoTag (mcEntity , tag );
484511 return new BaseEntity (
485512 EntityTypes .get (id ),
@@ -499,12 +526,12 @@ public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state
499526 String entityId = state .getType ().id ();
500527
501528 LinCompoundTag nativeTag = state .getNbt ();
502- net . minecraft . nbt . CompoundTag tag ;
529+ CompoundTag tag ;
503530 if (nativeTag != null ) {
504- tag = (net . minecraft . nbt . CompoundTag ) fromNative (nativeTag );
531+ tag = (CompoundTag ) fromNative (nativeTag );
505532 removeUnwantedEntityTagsRecursively (tag );
506533 } else {
507- tag = new net . minecraft . nbt . CompoundTag ();
534+ tag = new CompoundTag ();
508535 }
509536
510537 tag .putString ("id" , entityId );
@@ -523,14 +550,14 @@ public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state
523550 }
524551
525552 // This removes all unwanted tags from the main entity and all its passengers
526- private void removeUnwantedEntityTagsRecursively (net . minecraft . nbt . CompoundTag tag ) {
553+ private void removeUnwantedEntityTagsRecursively (CompoundTag tag ) {
527554 for (String name : Constants .NO_COPY_ENTITY_NBT_FIELDS ) {
528555 tag .remove (name );
529556 }
530557
531558 // Adapted from net.minecraft.world.entity.EntityType#loadEntityRecursive
532559 if (tag .contains ("Passengers" , LinTagId .LIST .id ())) {
533- net . minecraft . nbt . ListTag nbttaglist = tag .getList ("Passengers" , LinTagId .COMPOUND .id ());
560+ ListTag nbttaglist = tag .getList ("Passengers" , LinTagId .COMPOUND .id ());
534561
535562 for (int i = 0 ; i < nbttaglist .size (); ++i ) {
536563 removeUnwantedEntityTagsRecursively (nbttaglist .getCompound (i ));
@@ -610,7 +637,7 @@ public void sendFakeNBT(Player player, BlockVector3 pos, LinCompoundTag nbtData)
610637 structureBlock .setLevel (((CraftPlayer ) player ).getHandle ().level ());
611638 ((CraftPlayer ) player ).getHandle ().connection .send (ClientboundBlockEntityDataPacket .create (
612639 structureBlock ,
613- (blockEntity , registryAccess ) -> (net . minecraft . nbt . CompoundTag ) fromNative (nbtData )
640+ (blockEntity , registryAccess ) -> (CompoundTag ) fromNative (nbtData )
614641 ));
615642 }
616643
@@ -834,7 +861,7 @@ private void regenForWorld(Region region, Extent extent, ServerLevel serverWorld
834861 Objects .requireNonNull (state );
835862 BlockEntity blockEntity = chunk .getBlockEntity (pos );
836863 if (blockEntity != null ) {
837- net . minecraft . nbt . CompoundTag tag = blockEntity .saveWithId (serverWorld .registryAccess ());
864+ CompoundTag tag = blockEntity .saveWithId (serverWorld .registryAccess ());
838865 state = state .toBaseBlock (LazyReference .from (() -> (LinCompoundTag ) toNative (tag )));
839866 }
840867 extent .setBlock (vec , state .toBaseBlock ());
@@ -997,44 +1024,44 @@ public void sendBiomeUpdates(World world, Iterable<BlockVector2> chunks) {
9971024 * @param foreign non-native NMS NBT structure
9981025 * @return native WorldEdit NBT structure
9991026 */
1000- LinTag <?> toNative (net . minecraft . nbt . Tag foreign ) {
1027+ public LinTag <?> toNative (Tag foreign ) {
10011028 if (foreign == null ) {
10021029 return null ;
10031030 }
1004- if (foreign instanceof net . minecraft . nbt . CompoundTag compoundTag ) {
1031+ if (foreign instanceof CompoundTag compoundTag ) {
10051032 LinCompoundTag .Builder builder = LinCompoundTag .builder ();
10061033 for (var entry : compoundTag .getAllKeys ()) {
10071034 builder .put (entry , toNative (compoundTag .get (entry )));
10081035 }
10091036 return builder .build ();
1010- } else if (foreign instanceof net . minecraft . nbt . ByteTag byteTag ) {
1037+ } else if (foreign instanceof ByteTag byteTag ) {
10111038 return LinByteTag .of (byteTag .getAsByte ());
1012- } else if (foreign instanceof net . minecraft . nbt . ByteArrayTag byteArrayTag ) {
1039+ } else if (foreign instanceof ByteArrayTag byteArrayTag ) {
10131040 return LinByteArrayTag .of (byteArrayTag .getAsByteArray ());
1014- } else if (foreign instanceof net . minecraft . nbt . DoubleTag doubleTag ) {
1041+ } else if (foreign instanceof DoubleTag doubleTag ) {
10151042 return LinDoubleTag .of (doubleTag .getAsDouble ());
1016- } else if (foreign instanceof net . minecraft . nbt . FloatTag floatTag ) {
1043+ } else if (foreign instanceof FloatTag floatTag ) {
10171044 return LinFloatTag .of (floatTag .getAsFloat ());
1018- } else if (foreign instanceof net . minecraft . nbt . IntTag intTag ) {
1045+ } else if (foreign instanceof IntTag intTag ) {
10191046 return LinIntTag .of (intTag .getAsInt ());
1020- } else if (foreign instanceof net . minecraft . nbt . IntArrayTag intArrayTag ) {
1047+ } else if (foreign instanceof IntArrayTag intArrayTag ) {
10211048 return LinIntArrayTag .of (intArrayTag .getAsIntArray ());
1022- } else if (foreign instanceof net . minecraft . nbt . LongArrayTag longArrayTag ) {
1049+ } else if (foreign instanceof LongArrayTag longArrayTag ) {
10231050 return LinLongArrayTag .of (longArrayTag .getAsLongArray ());
1024- } else if (foreign instanceof net . minecraft . nbt . ListTag listTag ) {
1051+ } else if (foreign instanceof ListTag listTag ) {
10251052 try {
10261053 return toNativeList (listTag );
10271054 } catch (Throwable e ) {
10281055 logger .log (Level .WARNING , "Failed to convert net.minecraft.nbt.ListTag" , e );
10291056 return LinListTag .empty (LinTagType .endTag ());
10301057 }
1031- } else if (foreign instanceof net . minecraft . nbt . LongTag longTag ) {
1058+ } else if (foreign instanceof LongTag longTag ) {
10321059 return LinLongTag .of (longTag .getAsLong ());
1033- } else if (foreign instanceof net . minecraft . nbt . ShortTag shortTag ) {
1060+ } else if (foreign instanceof ShortTag shortTag ) {
10341061 return LinShortTag .of (shortTag .getAsShort ());
1035- } else if (foreign instanceof net . minecraft . nbt . StringTag stringTag ) {
1062+ } else if (foreign instanceof StringTag stringTag ) {
10361063 return LinStringTag .of (stringTag .getAsString ());
1037- } else if (foreign instanceof net . minecraft . nbt . EndTag ) {
1064+ } else if (foreign instanceof EndTag ) {
10381065 return LinEndTag .instance ();
10391066 } else {
10401067 throw new IllegalArgumentException ("Don't know how to make native " + foreign .getClass ().getCanonicalName ());
@@ -1049,12 +1076,12 @@ LinTag<?> toNative(net.minecraft.nbt.Tag foreign) {
10491076 * @throws SecurityException on error
10501077 * @throws IllegalArgumentException on error
10511078 */
1052- private LinListTag <?> toNativeList (net . minecraft . nbt . ListTag foreign ) throws SecurityException , IllegalArgumentException {
1079+ private LinListTag <?> toNativeList (ListTag foreign ) throws SecurityException , IllegalArgumentException {
10531080 LinListTag .Builder <LinTag <?>> builder = LinListTag .builder (
10541081 LinTagType .fromId (LinTagId .fromId (foreign .getElementType ()))
10551082 );
10561083
1057- for (net . minecraft . nbt . Tag tag : foreign ) {
1084+ for (Tag tag : foreign ) {
10581085 builder .add (toNative (tag ));
10591086 }
10601087
@@ -1067,12 +1094,12 @@ private LinListTag<?> toNativeList(net.minecraft.nbt.ListTag foreign) throws Sec
10671094 * @param foreign structure to convert
10681095 * @return non-native structure
10691096 */
1070- Tag fromNative (LinTag <?> foreign ) {
1097+ public Tag fromNative (LinTag <?> foreign ) {
10711098 if (foreign == null ) {
10721099 return null ;
10731100 }
10741101 if (foreign instanceof LinCompoundTag compoundTag ) {
1075- net . minecraft . nbt . CompoundTag tag = new CompoundTag ();
1102+ CompoundTag tag = new CompoundTag ();
10761103 for (var entry : compoundTag .value ().entrySet ()) {
10771104 tag .put (entry .getKey (), fromNative (entry .getValue ()));
10781105 }
@@ -1092,7 +1119,7 @@ Tag fromNative(LinTag<?> foreign) {
10921119 } else if (foreign instanceof LinLongArrayTag longArrayTag ) {
10931120 return new LongArrayTag (longArrayTag .value ());
10941121 } else if (foreign instanceof LinListTag <?> listTag ) {
1095- net . minecraft . nbt . ListTag tag = new ListTag ();
1122+ ListTag tag = new ListTag ();
10961123 for (var t : listTag .value ()) {
10971124 tag .add (fromNative (t ));
10981125 }
0 commit comments