Skip to content

Commit

Permalink
Clean up use of DistExecutor
Browse files Browse the repository at this point in the history
- Use DistExecutor.unsafe* methods where using a method reference isn't possible, e.g. when passing arguments to the client-only method
- Don't implement DistExecutor.SafeReferent with anonymous classes
- Add doc comment to ClientUtil.getClientPlayer explaining how it crashes if called on the dedicated server
  • Loading branch information
Choonster committed Jan 24, 2021
1 parent 777db69 commit fa5c6a5
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package choonster.testmod3.block;

import choonster.testmod3.client.block.ClientOnlyBlockMethods;
import choonster.testmod3.client.util.ClientUtil;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
Expand Down Expand Up @@ -63,7 +62,7 @@ public void onEntityCollision(final BlockState state, final World world, final B
LOGGER.info("Switching pitch direction! Now pitching {}.", isPitchingUp ? "up" : "down");
}

DistExecutor.runWhenOn(Dist.CLIENT, () -> ClientOnlyBlockMethods.rotateEntityTowards(entity, ROTATION_YAW, isPitchingUp ? ROTATION_PITCH : -ROTATION_PITCH));
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> entity.rotateTowards(ROTATION_YAW, isPitchingUp ? ROTATION_PITCH : -ROTATION_PITCH));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.Entity;
import net.minecraftforge.fml.DistExecutor;

/**
* Client-only methods used by various blocks.
Expand All @@ -14,8 +12,4 @@ public class ClientOnlyBlockMethods {
public static void pressUseItemKeyBinding() {
KeyBinding.onTick(Minecraft.getInstance().gameSettings.keyBindUseItem.getKey());
}

public static DistExecutor.SafeRunnable rotateEntityTowards(final Entity entity, final float rotationYaw, final float rotationPitch) {
return () -> entity.rotateTowards(rotationYaw, rotationPitch);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.Mod;

/**
Expand Down Expand Up @@ -78,12 +77,10 @@ public static boolean shouldHeldItemRevealHiddenBlocksClient() {
* @param pos The position of the hidden block to update
* @return A SafeRunnable that updates the chunk when run
*/
public static DistExecutor.SafeRunnable refresh(final World world, final BlockPos pos) {
return () -> {
if (toggled) {
final BlockState state = world.getBlockState(pos);
world.notifyBlockUpdate(pos, state, state, 3);
}
};
public static void refresh(final World world, final BlockPos pos) {
if (toggled) {
final BlockState state = world.getBlockState(pos);
world.notifyBlockUpdate(pos, state, state, 3);
}
}
}

This file was deleted.

9 changes: 9 additions & 0 deletions src/main/java/choonster/testmod3/client/util/ClientUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
* @author Choonster
*/
public class ClientUtil {
/**
* Gets the client player.
* <p>
* NOTE: Although this method can be safely referenced in common code, it will throw a classloading exception
* if called on the dedicated server. Due to this, callers must check the logical side/dist before calling this
* method.
*
* @return The client player
*/
@Nullable
public static PlayerEntity getClientPlayer() {
return DistExecutor.safeCallWhenOn(Dist.CLIENT, () -> ClientOnlyMethods::getClientPlayer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package choonster.testmod3.network;

import choonster.testmod3.client.network.ClientOnlyNetworkMethods;
import choonster.testmod3.client.gui.ClientScreenManager;
import io.netty.buffer.Unpooled;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.inventory.container.Container;
import net.minecraft.network.PacketBuffer;
Expand Down Expand Up @@ -43,7 +44,7 @@ public static void encode(final OpenClientScreenMessage message, final PacketBuf

public static void handle(final OpenClientScreenMessage message, final Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() ->
DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> ClientOnlyNetworkMethods.openClientScreen(message))
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientScreenManager.openScreen(message.getId(), message.getAdditionalData(), Minecraft.getInstance()))
);
ctx.get().setPacketHandled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public HiddenTileEntity() {
@Override
public void tick() {
if (world.isRemote) {
DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> HiddenBlockManager.refresh(world, pos));
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> HiddenBlockManager.refresh(world, pos));
}
}
}

0 comments on commit fa5c6a5

Please sign in to comment.