Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build-logic/src/main/kotlin/buildlogic.adapter.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import buildlogic.getLibrary
import buildlogic.stringyLibs
import buildlogic.getVersion

Expand Down Expand Up @@ -25,6 +26,7 @@ repositories {

dependencies {
"implementation"(project(":worldedit-bukkit"))
"implementation"(stringyLibs.getLibrary("paperLib"))
constraints {
"remapper"("net.fabricmc:tiny-remapper:[${stringyLibs.getVersion("minimumTinyRemapper")},)") {
because("Need remapper to support Java 21")
Expand Down
4 changes: 2 additions & 2 deletions config/checkstyle/checkstyle-suppression.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<suppress files=".*[\\/]worldedit[\\/]bukkit[\\/]adapter[\\/]impl[\\/].*\.java" checks="PackageName"/>
<suppress files=".*[\\/]MathUtils.java" checks="MethodName"/>
<suppress files=".*[\\/]bPermissionsResolver.java" checks="TypeName"/>
<!-- This thing only works via the use of a finalizer. -->
<suppress files=".*[\\/]TracedEditSession.java" checks="NoFinalizer"/>
<!-- None of the old command stuff really matters -->
<suppress files=".*[\\/]minecraft[\\/]util[\\/]commands[\\/].*\.java" checks=".*"/>
<!-- The mixins are allowed to use weird method names -->
<suppress files=".*Mixin.*\.java" checks="MethodName"/>
</suppressions>
2 changes: 1 addition & 1 deletion config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ Checks based on Google Checks, modified for EngineHub.
<property name="allowThrowsTagsForSubclasses" value="true"/>
</module>-->
<module name="MethodName">
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9_]*$"/>
<property name="format" value="^([a-z][a-z0-9][a-zA-Z0-9_]*|[xyz])$"/>
<message key="name.invalidPattern"
value="Method name ''{0}'' must match pattern ''{1}''."/>
</module>
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
group=com.sk89q.worldedit
version=7.4.0-SNAPSHOT

org.gradle.jvmargs=-Xmx1500M
org.gradle.jvmargs=-Xmx1700M
org.gradle.parallel=true

loom_fabric_repository=https://maven.enginehub.org/artifactory/fabricmc/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
import com.sk89q.worldedit.bukkit.adapter.impl.v1_21_3.wna.PaperweightNativeBlockState;
import com.sk89q.worldedit.bukkit.adapter.impl.v1_21_3.wna.PaperweightNativeWorld;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.extension.platform.Watchdog;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.internal.Constants;
import com.sk89q.worldedit.internal.block.BlockStateIdAccess;
import com.sk89q.worldedit.internal.wna.WorldNativeAccess;
import com.sk89q.worldedit.internal.wna.NativeAdapter;
import com.sk89q.worldedit.internal.wna.NativeBlockState;
import com.sk89q.worldedit.internal.wna.NativePosition;
import com.sk89q.worldedit.internal.wna.NativeWorld;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
Expand Down Expand Up @@ -171,7 +176,6 @@
import org.spigotmc.SpigotConfig;
import org.spigotmc.WatchdogThread;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -199,9 +203,28 @@
import static com.google.common.base.Preconditions.checkState;

public final class PaperweightAdapter implements BukkitImplAdapter {
public static BlockPos adaptPos(NativePosition pos) {
return new BlockPos(pos.x(), pos.y(), pos.z());
}

private final Logger logger = Logger.getLogger(getClass().getCanonicalName());

private final NativeAdapter nativeAdapter = new NativeAdapter() {
@Override
public NativeBlockState toNative(BlockState state) {
return new PaperweightNativeBlockState(adapt(state));
}

@Override
public BlockState fromNative(NativeBlockState state) {
return adapt(((PaperweightNativeBlockState) state).delegate());
}

@Override public NativePosition newBlockPos(BlockVector3 pos) {
return pos;
}
};

private final Field serverWorldsField;
private final Method getChunkFutureMethod;
private final Field chunkProviderExecutorField;
Expand Down Expand Up @@ -259,6 +282,10 @@ public PaperweightAdapter() throws NoSuchFieldException, NoSuchMethodException {
}
}

public NativeAdapter asNativeAdapter() {
return nativeAdapter;
}

@Override
public DataFixer getDataFixer() {
return this.dataFixer;
Expand All @@ -270,7 +297,7 @@ public DataFixer getDataFixer() {
* @param tileEntity the tile entity
* @param tag the tag
*/
static void readTagIntoTileEntity(net.minecraft.nbt.CompoundTag tag, BlockEntity tileEntity) {
static void readTagIntoTileEntity(CompoundTag tag, BlockEntity tileEntity) {
tileEntity.loadWithComponents(tag, MinecraftServer.getServer().registryAccess());
tileEntity.setChanged();
}
Expand All @@ -291,7 +318,7 @@ private static String getEntityId(Entity entity) {
* @param entity the entity
* @param tag the tag
*/
private static void readEntityIntoTag(Entity entity, net.minecraft.nbt.CompoundTag tag) {
private static void readEntityIntoTag(Entity entity, CompoundTag tag) {
entity.save(tag);
}

Expand Down Expand Up @@ -375,7 +402,7 @@ public BaseBlock getFullBlock(Location location) {
// Read the NBT data
BlockEntity te = chunk.getBlockEntity(blockPos);
if (te != null) {
net.minecraft.nbt.CompoundTag tag = te.saveWithId(MinecraftServer.getServer().registryAccess());
CompoundTag tag = te.saveWithId(MinecraftServer.getServer().registryAccess());
return state.toBaseBlock(LazyReference.from(() -> (LinCompoundTag) toNative(tag)));
}

Expand Down Expand Up @@ -417,8 +444,8 @@ public void setBiome(Location location, BiomeType biome) {
}

@Override
public WorldNativeAccess<?, ?, ?> createWorldNativeAccess(World world) {
return new PaperweightWorldNativeAccess(this, new WeakReference<>(((CraftWorld) world).getHandle()));
public NativeWorld createNativeInterface(World world) {
return new PaperweightNativeWorld(this, ((CraftWorld) world).getHandle());
}

private static net.minecraft.core.Direction adapt(Direction face) {
Expand Down Expand Up @@ -479,7 +506,7 @@ public BaseEntity getEntity(org.bukkit.entity.Entity entity) {

String id = getEntityId(mcEntity);

net.minecraft.nbt.CompoundTag tag = new net.minecraft.nbt.CompoundTag();
CompoundTag tag = new CompoundTag();
readEntityIntoTag(mcEntity, tag);
return new BaseEntity(
EntityTypes.get(id),
Expand All @@ -499,12 +526,12 @@ public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state
String entityId = state.getType().id();

LinCompoundTag nativeTag = state.getNbt();
net.minecraft.nbt.CompoundTag tag;
CompoundTag tag;
if (nativeTag != null) {
tag = (net.minecraft.nbt.CompoundTag) fromNative(nativeTag);
tag = (CompoundTag) fromNative(nativeTag);
removeUnwantedEntityTagsRecursively(tag);
} else {
tag = new net.minecraft.nbt.CompoundTag();
tag = new CompoundTag();
}

tag.putString("id", entityId);
Expand All @@ -523,14 +550,14 @@ public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state
}

// This removes all unwanted tags from the main entity and all its passengers
private void removeUnwantedEntityTagsRecursively(net.minecraft.nbt.CompoundTag tag) {
private void removeUnwantedEntityTagsRecursively(CompoundTag tag) {
for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
tag.remove(name);
}

// Adapted from net.minecraft.world.entity.EntityType#loadEntityRecursive
if (tag.contains("Passengers", LinTagId.LIST.id())) {
net.minecraft.nbt.ListTag nbttaglist = tag.getList("Passengers", LinTagId.COMPOUND.id());
ListTag nbttaglist = tag.getList("Passengers", LinTagId.COMPOUND.id());

for (int i = 0; i < nbttaglist.size(); ++i) {
removeUnwantedEntityTagsRecursively(nbttaglist.getCompound(i));
Expand Down Expand Up @@ -610,7 +637,7 @@ public void sendFakeNBT(Player player, BlockVector3 pos, LinCompoundTag nbtData)
structureBlock.setLevel(((CraftPlayer) player).getHandle().level());
((CraftPlayer) player).getHandle().connection.send(ClientboundBlockEntityDataPacket.create(
structureBlock,
(blockEntity, registryAccess) -> (net.minecraft.nbt.CompoundTag) fromNative(nbtData)
(blockEntity, registryAccess) -> (CompoundTag) fromNative(nbtData)
));
}

Expand Down Expand Up @@ -834,7 +861,7 @@ private void regenForWorld(Region region, Extent extent, ServerLevel serverWorld
Objects.requireNonNull(state);
BlockEntity blockEntity = chunk.getBlockEntity(pos);
if (blockEntity != null) {
net.minecraft.nbt.CompoundTag tag = blockEntity.saveWithId(serverWorld.registryAccess());
CompoundTag tag = blockEntity.saveWithId(serverWorld.registryAccess());
state = state.toBaseBlock(LazyReference.from(() -> (LinCompoundTag) toNative(tag)));
}
extent.setBlock(vec, state.toBaseBlock());
Expand Down Expand Up @@ -997,44 +1024,44 @@ public void sendBiomeUpdates(World world, Iterable<BlockVector2> chunks) {
* @param foreign non-native NMS NBT structure
* @return native WorldEdit NBT structure
*/
LinTag<?> toNative(net.minecraft.nbt.Tag foreign) {
public LinTag<?> toNative(Tag foreign) {
if (foreign == null) {
return null;
}
if (foreign instanceof net.minecraft.nbt.CompoundTag compoundTag) {
if (foreign instanceof CompoundTag compoundTag) {
LinCompoundTag.Builder builder = LinCompoundTag.builder();
for (var entry : compoundTag.getAllKeys()) {
builder.put(entry, toNative(compoundTag.get(entry)));
}
return builder.build();
} else if (foreign instanceof net.minecraft.nbt.ByteTag byteTag) {
} else if (foreign instanceof ByteTag byteTag) {
return LinByteTag.of(byteTag.getAsByte());
} else if (foreign instanceof net.minecraft.nbt.ByteArrayTag byteArrayTag) {
} else if (foreign instanceof ByteArrayTag byteArrayTag) {
return LinByteArrayTag.of(byteArrayTag.getAsByteArray());
} else if (foreign instanceof net.minecraft.nbt.DoubleTag doubleTag) {
} else if (foreign instanceof DoubleTag doubleTag) {
return LinDoubleTag.of(doubleTag.getAsDouble());
} else if (foreign instanceof net.minecraft.nbt.FloatTag floatTag) {
} else if (foreign instanceof FloatTag floatTag) {
return LinFloatTag.of(floatTag.getAsFloat());
} else if (foreign instanceof net.minecraft.nbt.IntTag intTag) {
} else if (foreign instanceof IntTag intTag) {
return LinIntTag.of(intTag.getAsInt());
} else if (foreign instanceof net.minecraft.nbt.IntArrayTag intArrayTag) {
} else if (foreign instanceof IntArrayTag intArrayTag) {
return LinIntArrayTag.of(intArrayTag.getAsIntArray());
} else if (foreign instanceof net.minecraft.nbt.LongArrayTag longArrayTag) {
} else if (foreign instanceof LongArrayTag longArrayTag) {
return LinLongArrayTag.of(longArrayTag.getAsLongArray());
} else if (foreign instanceof net.minecraft.nbt.ListTag listTag) {
} else if (foreign instanceof ListTag listTag) {
try {
return toNativeList(listTag);
} catch (Throwable e) {
logger.log(Level.WARNING, "Failed to convert net.minecraft.nbt.ListTag", e);
return LinListTag.empty(LinTagType.endTag());
}
} else if (foreign instanceof net.minecraft.nbt.LongTag longTag) {
} else if (foreign instanceof LongTag longTag) {
return LinLongTag.of(longTag.getAsLong());
} else if (foreign instanceof net.minecraft.nbt.ShortTag shortTag) {
} else if (foreign instanceof ShortTag shortTag) {
return LinShortTag.of(shortTag.getAsShort());
} else if (foreign instanceof net.minecraft.nbt.StringTag stringTag) {
} else if (foreign instanceof StringTag stringTag) {
return LinStringTag.of(stringTag.getAsString());
} else if (foreign instanceof net.minecraft.nbt.EndTag) {
} else if (foreign instanceof EndTag) {
return LinEndTag.instance();
} else {
throw new IllegalArgumentException("Don't know how to make native " + foreign.getClass().getCanonicalName());
Expand All @@ -1049,12 +1076,12 @@ LinTag<?> toNative(net.minecraft.nbt.Tag foreign) {
* @throws SecurityException on error
* @throws IllegalArgumentException on error
*/
private LinListTag<?> toNativeList(net.minecraft.nbt.ListTag foreign) throws SecurityException, IllegalArgumentException {
private LinListTag<?> toNativeList(ListTag foreign) throws SecurityException, IllegalArgumentException {
LinListTag.Builder<LinTag<?>> builder = LinListTag.builder(
LinTagType.fromId(LinTagId.fromId(foreign.getElementType()))
);

for (net.minecraft.nbt.Tag tag : foreign) {
for (Tag tag : foreign) {
builder.add(toNative(tag));
}

Expand All @@ -1067,12 +1094,12 @@ private LinListTag<?> toNativeList(net.minecraft.nbt.ListTag foreign) throws Sec
* @param foreign structure to convert
* @return non-native structure
*/
Tag fromNative(LinTag<?> foreign) {
public Tag fromNative(LinTag<?> foreign) {
if (foreign == null) {
return null;
}
if (foreign instanceof LinCompoundTag compoundTag) {
net.minecraft.nbt.CompoundTag tag = new CompoundTag();
CompoundTag tag = new CompoundTag();
for (var entry : compoundTag.value().entrySet()) {
tag.put(entry.getKey(), fromNative(entry.getValue()));
}
Expand All @@ -1092,7 +1119,7 @@ Tag fromNative(LinTag<?> foreign) {
} else if (foreign instanceof LinLongArrayTag longArrayTag) {
return new LongArrayTag(longArrayTag.value());
} else if (foreign instanceof LinListTag<?> listTag) {
net.minecraft.nbt.ListTag tag = new ListTag();
ListTag tag = new ListTag();
for (var t : listTag.value()) {
tag.add(fromNative(t));
}
Expand Down
Loading