Skip to content

Instantly share code, notes, and snippets.

@TropheusJ
Created May 25, 2022 23:36
Show Gist options
  • Save TropheusJ/65a4535ff1dad6d1840a939b2ba32ff5 to your computer and use it in GitHub Desktop.
Save TropheusJ/65a4535ff1dad6d1840a939b2ba32ff5 to your computer and use it in GitHub Desktop.
public class FixLightingCommand {
public static final TranslatableComponent RESEND_RESPONSE = new TranslatableComponent("create.command.resendLightingCommand.response");
public static final TranslatableComponent RESET_WAITING = new TranslatableComponent("create.command.resetLightingCommand.waiting");
public static final TranslatableComponent RESET_SUCCESS = new TranslatableComponent("create.command.resetLightingCommand.success");
public static final TranslatableComponent RESET_UNABLE = new TranslatableComponent("create.command.resetLightingCommand.unable");
public static final TranslatableComponent RESET_FAILED = new TranslatableComponent("create.command.resetLightingCommand.failed");
static ArgumentBuilder<CommandSourceStack, ?> register() {
return literal("lighting")
.then(literal("resend")
.executes(FixLightingCommand::resendLighting))
.then(literal("reset")
.executes(ctx -> {
CommandSourceStack source = ctx.getSource();
ServerPlayer sender = source.getPlayerOrException();
BlockPos pos = sender.blockPosition();
ServerLevel level = sender.getLevel();
LevelChunk chunk = level.getChunkAt(pos);
if (level.getLightEngine() instanceof ThreadedLevelLightEngine threaded) {
source.sendSuccess(RESET_WAITING, true);
CompletableFuture<ChunkAccess> future = threaded.lightChunk(chunk, false);
future.whenCompleteAsync((c, t) -> {
if (t == null) {
source.sendSuccess(RESET_SUCCESS, true);
} else {
source.sendFailure(RESET_FAILED);
t.printStackTrace();
}
try {
resendLighting(ctx);
} catch (CommandSyntaxException e) {
throw new RuntimeException(e);
}
});
} else {
source.sendFailure(RESET_UNABLE);
}
return 0;
}));
}
public static int resendLighting(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
ServerPlayer sender = ctx.getSource().getPlayerOrException();
BlockPos pos = sender.blockPosition();
ServerLevel level = sender.getLevel();
LevelChunk chunk = level.getChunkAt(pos);
ClientboundLevelChunkWithLightPacket packet = new ClientboundLevelChunkWithLightPacket(
chunk, level.getLightEngine(), null, null, false
);
sender.connection.send(packet);
ctx.getSource().sendSuccess(RESEND_RESPONSE, true);
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment