Paper/patches/server/0355-Fix-Light-Command.patch

167 lines
8.8 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Thu, 7 May 2020 19:17:36 -0400
Subject: [PATCH] Fix Light Command
This lets you run /paper fixlight <chunkRadius> (max 5) to automatically
fix all light data in the chunks.
diff --git a/src/main/java/com/destroystokyo/paper/PaperCommand.java b/src/main/java/com/destroystokyo/paper/PaperCommand.java
2022-06-03 06:26:56 +02:00
index 68f3f5ebd2188bf8280fc028f98ef8bb51622c26..b5756d067b013ee78a55b97ad00375d15ecd483c 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/com/destroystokyo/paper/PaperCommand.java
+++ b/src/main/java/com/destroystokyo/paper/PaperCommand.java
2021-06-13 21:29:58 +02:00
@@ -10,7 +10,8 @@ import net.minecraft.server.MinecraftServer;
2021-06-11 14:02:28 +02:00
import net.minecraft.server.level.ChunkHolder;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ServerLevel;
2021-06-13 21:29:58 +02:00
-import net.minecraft.world.entity.Entity;
2021-06-11 14:02:28 +02:00
+import net.minecraft.server.level.ServerPlayer;
+import net.minecraft.server.level.ThreadedLevelLightEngine;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.ChunkPos;
2021-06-13 21:29:58 +02:00
import net.minecraft.resources.ResourceLocation;
2022-06-03 06:26:56 +02:00
@@ -24,15 +25,18 @@ import org.bukkit.command.Command;
2021-06-11 14:02:28 +02:00
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
+import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
+import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
2022-06-03 06:26:56 +02:00
@@ -49,7 +53,7 @@ import static net.kyori.adventure.text.format.NamedTextColor.YELLOW;
2021-06-11 14:02:28 +02:00
public class PaperCommand extends Command {
private static final String BASE_PERM = "bukkit.command.paper.";
2021-06-13 21:29:58 +02:00
- private static final ImmutableSet<String> SUBCOMMANDS = ImmutableSet.<String>builder().add("heap", "entity", "reload", "version", "debug", "chunkinfo").build();
+ private static final ImmutableSet<String> SUBCOMMANDS = ImmutableSet.<String>builder().add("heap", "entity", "reload", "version", "debug", "chunkinfo", "fixlight").build();
2021-06-11 14:02:28 +02:00
public PaperCommand(String name) {
super(name);
2022-06-03 06:26:56 +02:00
@@ -164,6 +168,9 @@ public class PaperCommand extends Command {
2021-06-13 21:29:58 +02:00
case "chunkinfo":
doChunkInfo(sender, args);
2021-06-11 14:02:28 +02:00
break;
+ case "fixlight":
+ this.doFixLight(sender, args);
+ break;
case "ver":
if (!testPermission(sender, "version")) break; // "ver" needs a special check because it's an alias. All other commands are checked up before the switch statement (because they are present in the SUBCOMMANDS set)
case "version":
2022-06-03 06:26:56 +02:00
@@ -427,4 +434,74 @@ public class PaperCommand extends Command {
2021-06-11 14:02:28 +02:00
2022-06-03 06:26:56 +02:00
Command.broadcastCommandMessage(sender, text("Paper config reload complete.", GREEN));
2021-06-13 21:29:58 +02:00
}
2021-06-11 14:02:28 +02:00
+ private void doFixLight(CommandSender sender, String[] args) {
+ if (!(sender instanceof Player)) {
+ sender.sendMessage("Only players can use this command");
+ return;
+ }
+ int radius = 2;
+ if (args.length > 1) {
+ try {
+ radius = Math.min(5, Integer.parseInt(args[1]));
+ } catch (Exception e) {
+ sender.sendMessage("Not a number");
+ return;
+ }
+
+ }
+
+ CraftPlayer player = (CraftPlayer) sender;
+ ServerPlayer handle = player.getHandle();
+ ServerLevel world = (ServerLevel) handle.level;
+ ThreadedLevelLightEngine lightengine = world.getChunkSource().getLightEngine();
+
2021-06-13 21:29:58 +02:00
+ net.minecraft.core.BlockPos center = MCUtil.toBlockPosition(player.getLocation());
2021-06-11 14:02:28 +02:00
+ Deque<ChunkPos> queue = new ArrayDeque<>(MCUtil.getSpiralOutChunks(center, radius));
+ updateLight(sender, world, lightengine, queue);
+ }
+
+ private void updateLight(CommandSender sender, ServerLevel world, ThreadedLevelLightEngine lightengine, Deque<ChunkPos> queue) {
+ ChunkPos coord = queue.poll();
+ if (coord == null) {
+ sender.sendMessage("All Chunks Light updated");
+ return;
+ }
+ world.getChunkSource().getChunkAtAsynchronously(coord.x, coord.z, false, false).whenCompleteAsync((either, ex) -> {
+ if (ex != null) {
+ sender.sendMessage("Error loading chunk " + coord);
+ updateLight(sender, world, lightengine, queue);
+ return;
+ }
2021-06-13 21:29:58 +02:00
+ net.minecraft.world.level.chunk.LevelChunk chunk = (net.minecraft.world.level.chunk.LevelChunk) either.left().orElse(null);
2021-06-11 14:02:28 +02:00
+ if (chunk == null) {
+ updateLight(sender, world, lightengine, queue);
+ return;
+ }
+ lightengine.setTaskPerBatch(world.paperConfig.lightQueueSize + 16 * 256); // ensure full chunk can fit into queue
+ sender.sendMessage("Updating Light " + coord);
+ int cx = chunk.getPos().x << 4;
+ int cz = chunk.getPos().z << 4;
+ for (int y = 0; y < world.getHeight(); y++) {
+ for (int x = 0; x < 16; x++) {
+ for (int z = 0; z < 16; z++) {
2021-06-13 21:29:58 +02:00
+ net.minecraft.core.BlockPos pos = new net.minecraft.core.BlockPos(cx + x, y, cz + z);
2021-06-11 14:02:28 +02:00
+ lightengine.checkBlock(pos);
+ }
+ }
+ }
+ lightengine.tryScheduleUpdate();
+ ChunkHolder visibleChunk = world.getChunkSource().chunkMap.getVisibleChunkIfPresent(chunk.coordinateKey);
+ if (visibleChunk != null) {
+ world.getChunkSource().chunkMap.addLightTask(visibleChunk, () -> {
+ MinecraftServer.getServer().processQueue.add(() -> {
2021-06-13 21:29:58 +02:00
+ visibleChunk.broadcast(new net.minecraft.network.protocol.game.ClientboundLightUpdatePacket(chunk.getPos(), lightengine, null, null, true), false);
2021-06-11 14:02:28 +02:00
+ updateLight(sender, world, lightengine, queue);
+ });
+ });
+ } else {
+ updateLight(sender, world, lightengine, queue);
+ }
+ lightengine.setTaskPerBatch(world.paperConfig.lightQueueSize);
+ }, MinecraftServer.getServer());
+ }
2021-06-13 21:29:58 +02:00
}
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
2022-03-30 22:28:38 +02:00
index 5685ffe47fd68230cf30ca9f0e49e471b86eab00..071d821094ce1c13ad5755a1ba76d9a8f27bba97 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
2022-03-01 06:43:03 +01:00
@@ -137,6 +137,12 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
2021-06-13 21:29:58 +02:00
private final ChunkTaskPriorityQueueSorter queueSorter;
private final ProcessorHandle<ChunkTaskPriorityQueueSorter.Message<Runnable>> worldgenMailbox;
2021-10-03 03:42:30 +02:00
public final ProcessorHandle<ChunkTaskPriorityQueueSorter.Message<Runnable>> mainThreadMailbox;
2021-06-13 21:29:58 +02:00
+ // Paper start
+ final ProcessorHandle<ChunkTaskPriorityQueueSorter.Message<Runnable>> mailboxLight;
+ public void addLightTask(ChunkHolder playerchunk, Runnable run) {
+ this.mailboxLight.tell(ChunkTaskPriorityQueueSorter.message(playerchunk, run));
+ }
+ // Paper end
public final ChunkProgressListener progressListener;
private final ChunkStatusUpdateListener chunkStatusListener;
public final ChunkMap.ChunkDistanceManager distanceManager;
2022-03-01 06:43:03 +01:00
@@ -247,11 +253,12 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
2021-06-11 14:02:28 +02:00
this.progressListener = worldGenerationProgressListener;
2021-06-13 21:29:58 +02:00
this.chunkStatusListener = chunkStatusChangeListener;
- ProcessorMailbox<Runnable> threadedmailbox1 = ProcessorMailbox.create(executor, "light");
+ ProcessorMailbox<Runnable> lightthreaded; ProcessorMailbox<Runnable> threadedmailbox1 = lightthreaded = ProcessorMailbox.create(executor, "light"); // Paper
2021-06-11 14:02:28 +02:00
2021-06-13 21:29:58 +02:00
this.queueSorter = new ChunkTaskPriorityQueueSorter(ImmutableList.of(threadedmailbox, mailbox, threadedmailbox1), executor, Integer.MAX_VALUE);
2021-06-11 14:02:28 +02:00
this.worldgenMailbox = this.queueSorter.getProcessor(threadedmailbox, false);
this.mainThreadMailbox = this.queueSorter.getProcessor(mailbox, false);
+ this.mailboxLight = this.queueSorter.getProcessor(lightthreaded, false);// Paper
this.lightEngine = new ThreadedLevelLightEngine(chunkProvider, this, this.level.dimensionType().hasSkyLight(), threadedmailbox1, this.queueSorter.getProcessor(threadedmailbox1, false));
2021-06-13 21:29:58 +02:00
this.distanceManager = new ChunkMap.ChunkDistanceManager(executor, mainThreadExecutor);
this.overworldDataStorage = persistentStateManagerFactory;