34
34
import com .sk89q .worldedit .blocks .BaseItemStack ;
35
35
import com .sk89q .worldedit .bukkit .BukkitAdapter ;
36
36
import 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 ;
37
39
import com .sk89q .worldedit .entity .BaseEntity ;
38
40
import com .sk89q .worldedit .extension .platform .Watchdog ;
39
41
import com .sk89q .worldedit .extent .Extent ;
40
42
import com .sk89q .worldedit .internal .Constants ;
41
43
import 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 ;
43
48
import com .sk89q .worldedit .math .BlockVector2 ;
44
49
import com .sk89q .worldedit .math .BlockVector3 ;
45
50
import com .sk89q .worldedit .regions .Region ;
171
176
import org .spigotmc .SpigotConfig ;
172
177
import org .spigotmc .WatchdogThread ;
173
178
174
- import java .lang .ref .WeakReference ;
175
179
import java .lang .reflect .Field ;
176
180
import java .lang .reflect .InvocationTargetException ;
177
181
import java .lang .reflect .Method ;
199
203
import static com .google .common .base .Preconditions .checkState ;
200
204
201
205
public final class PaperweightAdapter implements BukkitImplAdapter {
206
+ public static BlockPos adaptPos (NativePosition pos ) {
207
+ return new BlockPos (pos .x (), pos .y (), pos .z ());
208
+ }
202
209
203
210
private final Logger logger = Logger .getLogger (getClass ().getCanonicalName ());
204
211
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
+
205
228
private final Field serverWorldsField ;
206
229
private final Method getChunkFutureMethod ;
207
230
private final Field chunkProviderExecutorField ;
@@ -259,6 +282,10 @@ public PaperweightAdapter() throws NoSuchFieldException, NoSuchMethodException {
259
282
}
260
283
}
261
284
285
+ public NativeAdapter asNativeAdapter () {
286
+ return nativeAdapter ;
287
+ }
288
+
262
289
@ Override
263
290
public DataFixer getDataFixer () {
264
291
return this .dataFixer ;
@@ -270,7 +297,7 @@ public DataFixer getDataFixer() {
270
297
* @param tileEntity the tile entity
271
298
* @param tag the tag
272
299
*/
273
- static void readTagIntoTileEntity (net . minecraft . nbt . CompoundTag tag , BlockEntity tileEntity ) {
300
+ static void readTagIntoTileEntity (CompoundTag tag , BlockEntity tileEntity ) {
274
301
tileEntity .loadWithComponents (tag , MinecraftServer .getServer ().registryAccess ());
275
302
tileEntity .setChanged ();
276
303
}
@@ -291,7 +318,7 @@ private static String getEntityId(Entity entity) {
291
318
* @param entity the entity
292
319
* @param tag the tag
293
320
*/
294
- private static void readEntityIntoTag (Entity entity , net . minecraft . nbt . CompoundTag tag ) {
321
+ private static void readEntityIntoTag (Entity entity , CompoundTag tag ) {
295
322
entity .save (tag );
296
323
}
297
324
@@ -375,7 +402,7 @@ public BaseBlock getFullBlock(Location location) {
375
402
// Read the NBT data
376
403
BlockEntity te = chunk .getBlockEntity (blockPos );
377
404
if (te != null ) {
378
- net . minecraft . nbt . CompoundTag tag = te .saveWithId (MinecraftServer .getServer ().registryAccess ());
405
+ CompoundTag tag = te .saveWithId (MinecraftServer .getServer ().registryAccess ());
379
406
return state .toBaseBlock (LazyReference .from (() -> (LinCompoundTag ) toNative (tag )));
380
407
}
381
408
@@ -417,8 +444,8 @@ public void setBiome(Location location, BiomeType biome) {
417
444
}
418
445
419
446
@ 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 ());
422
449
}
423
450
424
451
private static net .minecraft .core .Direction adapt (Direction face ) {
@@ -479,7 +506,7 @@ public BaseEntity getEntity(org.bukkit.entity.Entity entity) {
479
506
480
507
String id = getEntityId (mcEntity );
481
508
482
- net . minecraft . nbt . CompoundTag tag = new net . minecraft . nbt . CompoundTag ();
509
+ CompoundTag tag = new CompoundTag ();
483
510
readEntityIntoTag (mcEntity , tag );
484
511
return new BaseEntity (
485
512
EntityTypes .get (id ),
@@ -499,12 +526,12 @@ public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state
499
526
String entityId = state .getType ().id ();
500
527
501
528
LinCompoundTag nativeTag = state .getNbt ();
502
- net . minecraft . nbt . CompoundTag tag ;
529
+ CompoundTag tag ;
503
530
if (nativeTag != null ) {
504
- tag = (net . minecraft . nbt . CompoundTag ) fromNative (nativeTag );
531
+ tag = (CompoundTag ) fromNative (nativeTag );
505
532
removeUnwantedEntityTagsRecursively (tag );
506
533
} else {
507
- tag = new net . minecraft . nbt . CompoundTag ();
534
+ tag = new CompoundTag ();
508
535
}
509
536
510
537
tag .putString ("id" , entityId );
@@ -523,14 +550,14 @@ public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state
523
550
}
524
551
525
552
// 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 ) {
527
554
for (String name : Constants .NO_COPY_ENTITY_NBT_FIELDS ) {
528
555
tag .remove (name );
529
556
}
530
557
531
558
// Adapted from net.minecraft.world.entity.EntityType#loadEntityRecursive
532
559
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 ());
534
561
535
562
for (int i = 0 ; i < nbttaglist .size (); ++i ) {
536
563
removeUnwantedEntityTagsRecursively (nbttaglist .getCompound (i ));
@@ -610,7 +637,7 @@ public void sendFakeNBT(Player player, BlockVector3 pos, LinCompoundTag nbtData)
610
637
structureBlock .setLevel (((CraftPlayer ) player ).getHandle ().level ());
611
638
((CraftPlayer ) player ).getHandle ().connection .send (ClientboundBlockEntityDataPacket .create (
612
639
structureBlock ,
613
- (blockEntity , registryAccess ) -> (net . minecraft . nbt . CompoundTag ) fromNative (nbtData )
640
+ (blockEntity , registryAccess ) -> (CompoundTag ) fromNative (nbtData )
614
641
));
615
642
}
616
643
@@ -834,7 +861,7 @@ private void regenForWorld(Region region, Extent extent, ServerLevel serverWorld
834
861
Objects .requireNonNull (state );
835
862
BlockEntity blockEntity = chunk .getBlockEntity (pos );
836
863
if (blockEntity != null ) {
837
- net . minecraft . nbt . CompoundTag tag = blockEntity .saveWithId (serverWorld .registryAccess ());
864
+ CompoundTag tag = blockEntity .saveWithId (serverWorld .registryAccess ());
838
865
state = state .toBaseBlock (LazyReference .from (() -> (LinCompoundTag ) toNative (tag )));
839
866
}
840
867
extent .setBlock (vec , state .toBaseBlock ());
@@ -997,44 +1024,44 @@ public void sendBiomeUpdates(World world, Iterable<BlockVector2> chunks) {
997
1024
* @param foreign non-native NMS NBT structure
998
1025
* @return native WorldEdit NBT structure
999
1026
*/
1000
- LinTag <?> toNative (net . minecraft . nbt . Tag foreign ) {
1027
+ public LinTag <?> toNative (Tag foreign ) {
1001
1028
if (foreign == null ) {
1002
1029
return null ;
1003
1030
}
1004
- if (foreign instanceof net . minecraft . nbt . CompoundTag compoundTag ) {
1031
+ if (foreign instanceof CompoundTag compoundTag ) {
1005
1032
LinCompoundTag .Builder builder = LinCompoundTag .builder ();
1006
1033
for (var entry : compoundTag .getAllKeys ()) {
1007
1034
builder .put (entry , toNative (compoundTag .get (entry )));
1008
1035
}
1009
1036
return builder .build ();
1010
- } else if (foreign instanceof net . minecraft . nbt . ByteTag byteTag ) {
1037
+ } else if (foreign instanceof ByteTag byteTag ) {
1011
1038
return LinByteTag .of (byteTag .getAsByte ());
1012
- } else if (foreign instanceof net . minecraft . nbt . ByteArrayTag byteArrayTag ) {
1039
+ } else if (foreign instanceof ByteArrayTag byteArrayTag ) {
1013
1040
return LinByteArrayTag .of (byteArrayTag .getAsByteArray ());
1014
- } else if (foreign instanceof net . minecraft . nbt . DoubleTag doubleTag ) {
1041
+ } else if (foreign instanceof DoubleTag doubleTag ) {
1015
1042
return LinDoubleTag .of (doubleTag .getAsDouble ());
1016
- } else if (foreign instanceof net . minecraft . nbt . FloatTag floatTag ) {
1043
+ } else if (foreign instanceof FloatTag floatTag ) {
1017
1044
return LinFloatTag .of (floatTag .getAsFloat ());
1018
- } else if (foreign instanceof net . minecraft . nbt . IntTag intTag ) {
1045
+ } else if (foreign instanceof IntTag intTag ) {
1019
1046
return LinIntTag .of (intTag .getAsInt ());
1020
- } else if (foreign instanceof net . minecraft . nbt . IntArrayTag intArrayTag ) {
1047
+ } else if (foreign instanceof IntArrayTag intArrayTag ) {
1021
1048
return LinIntArrayTag .of (intArrayTag .getAsIntArray ());
1022
- } else if (foreign instanceof net . minecraft . nbt . LongArrayTag longArrayTag ) {
1049
+ } else if (foreign instanceof LongArrayTag longArrayTag ) {
1023
1050
return LinLongArrayTag .of (longArrayTag .getAsLongArray ());
1024
- } else if (foreign instanceof net . minecraft . nbt . ListTag listTag ) {
1051
+ } else if (foreign instanceof ListTag listTag ) {
1025
1052
try {
1026
1053
return toNativeList (listTag );
1027
1054
} catch (Throwable e ) {
1028
1055
logger .log (Level .WARNING , "Failed to convert net.minecraft.nbt.ListTag" , e );
1029
1056
return LinListTag .empty (LinTagType .endTag ());
1030
1057
}
1031
- } else if (foreign instanceof net . minecraft . nbt . LongTag longTag ) {
1058
+ } else if (foreign instanceof LongTag longTag ) {
1032
1059
return LinLongTag .of (longTag .getAsLong ());
1033
- } else if (foreign instanceof net . minecraft . nbt . ShortTag shortTag ) {
1060
+ } else if (foreign instanceof ShortTag shortTag ) {
1034
1061
return LinShortTag .of (shortTag .getAsShort ());
1035
- } else if (foreign instanceof net . minecraft . nbt . StringTag stringTag ) {
1062
+ } else if (foreign instanceof StringTag stringTag ) {
1036
1063
return LinStringTag .of (stringTag .getAsString ());
1037
- } else if (foreign instanceof net . minecraft . nbt . EndTag ) {
1064
+ } else if (foreign instanceof EndTag ) {
1038
1065
return LinEndTag .instance ();
1039
1066
} else {
1040
1067
throw new IllegalArgumentException ("Don't know how to make native " + foreign .getClass ().getCanonicalName ());
@@ -1049,12 +1076,12 @@ LinTag<?> toNative(net.minecraft.nbt.Tag foreign) {
1049
1076
* @throws SecurityException on error
1050
1077
* @throws IllegalArgumentException on error
1051
1078
*/
1052
- private LinListTag <?> toNativeList (net . minecraft . nbt . ListTag foreign ) throws SecurityException , IllegalArgumentException {
1079
+ private LinListTag <?> toNativeList (ListTag foreign ) throws SecurityException , IllegalArgumentException {
1053
1080
LinListTag .Builder <LinTag <?>> builder = LinListTag .builder (
1054
1081
LinTagType .fromId (LinTagId .fromId (foreign .getElementType ()))
1055
1082
);
1056
1083
1057
- for (net . minecraft . nbt . Tag tag : foreign ) {
1084
+ for (Tag tag : foreign ) {
1058
1085
builder .add (toNative (tag ));
1059
1086
}
1060
1087
@@ -1067,12 +1094,12 @@ private LinListTag<?> toNativeList(net.minecraft.nbt.ListTag foreign) throws Sec
1067
1094
* @param foreign structure to convert
1068
1095
* @return non-native structure
1069
1096
*/
1070
- Tag fromNative (LinTag <?> foreign ) {
1097
+ public Tag fromNative (LinTag <?> foreign ) {
1071
1098
if (foreign == null ) {
1072
1099
return null ;
1073
1100
}
1074
1101
if (foreign instanceof LinCompoundTag compoundTag ) {
1075
- net . minecraft . nbt . CompoundTag tag = new CompoundTag ();
1102
+ CompoundTag tag = new CompoundTag ();
1076
1103
for (var entry : compoundTag .value ().entrySet ()) {
1077
1104
tag .put (entry .getKey (), fromNative (entry .getValue ()));
1078
1105
}
@@ -1092,7 +1119,7 @@ Tag fromNative(LinTag<?> foreign) {
1092
1119
} else if (foreign instanceof LinLongArrayTag longArrayTag ) {
1093
1120
return new LongArrayTag (longArrayTag .value ());
1094
1121
} else if (foreign instanceof LinListTag <?> listTag ) {
1095
- net . minecraft . nbt . ListTag tag = new ListTag ();
1122
+ ListTag tag = new ListTag ();
1096
1123
for (var t : listTag .value ()) {
1097
1124
tag .add (fromNative (t ));
1098
1125
}
0 commit comments