Skip to content

Commit

Permalink
fix world serverThread handling (for sign_contents tag)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 27, 2019
1 parent 91d6768 commit 2825268
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Expand Up @@ -413,6 +413,10 @@ public Block getBlockForTag(Attribute attribute) {
}

public static BlockState getBlockStateFor(Block block) {
return block.getState();
}

public static BlockState getBlockStateSafe(Block block) {
NMSHandler.getChunkHelper().changeChunkServerThread(block.getWorld());
try {
return block.getState();
Expand All @@ -431,7 +435,7 @@ public BlockState getBlockStateForTag(Attribute attribute) {
if (block == null) {
return null;
}
return getBlockStateFor(block);
return getBlockStateSafe(block);
}

public LocationTag getBlockLocation() {
Expand Down
Expand Up @@ -16,23 +16,29 @@ public class ChunkHelperImpl implements ChunkHelper {

public final static Field chunkProviderServerThreadField;
public final static MethodHandle chunkProviderServerThreadFieldSetter;
public final static Field worldThreadField;
public final static MethodHandle worldThreadFieldSetter;

static {
chunkProviderServerThreadField = ReflectionHelper.getFields(ChunkProviderServer.class).get("serverThread");
chunkProviderServerThreadFieldSetter = ReflectionHelper.getFinalSetter(ChunkProviderServer.class, "serverThread");
worldThreadField = ReflectionHelper.getFields(net.minecraft.server.v1_14_R1.World.class).get("serverThread");
worldThreadFieldSetter = ReflectionHelper.getFinalSetter(net.minecraft.server.v1_14_R1.World.class, "serverThread");
}

Thread resetServerThread;
public Thread resetServerThread;

@Override
public void changeChunkServerThread(World world) {
if (resetServerThread != null) {
return;
}
ChunkProviderServer provider = ((CraftWorld) world).getHandle().getChunkProvider();
WorldServer nmsWorld = ((CraftWorld) world).getHandle();
ChunkProviderServer provider = nmsWorld.getChunkProvider();
try {
resetServerThread = (Thread) chunkProviderServerThreadField.get(provider);
chunkProviderServerThreadFieldSetter.invoke(provider, Thread.currentThread());
worldThreadFieldSetter.invoke(nmsWorld, Thread.currentThread());
}
catch (Throwable ex) {
Debug.echoError(ex);
Expand All @@ -44,9 +50,11 @@ public void restoreServerThread(World world) {
if (resetServerThread == null) {
return;
}
ChunkProviderServer provider = ((CraftWorld) world).getHandle().getChunkProvider();
WorldServer nmsWorld = ((CraftWorld) world).getHandle();
ChunkProviderServer provider = nmsWorld.getChunkProvider();
try {
chunkProviderServerThreadFieldSetter.invoke(provider, resetServerThread);
worldThreadFieldSetter.invoke(nmsWorld, resetServerThread);
resetServerThread = null;
}
catch (Throwable ex) {
Expand Down

0 comments on commit 2825268

Please sign in to comment.