2020-05-08 07:44:33 +08: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
2021-03-16 15:19:45 +08:00
index eb1e86e8bb0f421e3686ffa02a4015a588107863..d165e8c232c38ba2e2faf93c60c8a127bb74c9b6 100644
2020-05-08 07:44:33 +08:00
--- a/src/main/java/com/destroystokyo/paper/PaperCommand.java
+++ b/src/main/java/com/destroystokyo/paper/PaperCommand.java
2021-03-16 15:19:45 +08:00
@@ -8,6 +8,8 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
+import net.minecraft.core.BlockPosition;
+import net.minecraft.network.protocol.game.PacketPlayOutLightUpdate;
import net.minecraft.resources.MinecraftKey;
2021-01-06 05:25:45 +08:00
import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
2021-03-16 15:19:45 +08:00
@@ -15,12 +17,14 @@ import com.google.gson.stream.JsonWriter;
2021-01-06 05:25:45 +08:00
import net.minecraft.server.MCUtil;
import net.minecraft.server.MinecraftServer;
2021-03-16 15:19:45 +08:00
import net.minecraft.server.level.ChunkProviderServer;
+import net.minecraft.server.level.EntityPlayer;
+import net.minecraft.server.level.LightEngineThreaded;
import net.minecraft.server.level.PlayerChunk;
import net.minecraft.server.level.WorldServer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.level.ChunkCoordIntPair;
-import net.minecraft.server.MCUtil;
+import net.minecraft.world.level.chunk.Chunk;
2021-01-06 05:25:45 +08:00
import org.apache.commons.lang3.tuple.MutablePair;
2021-03-16 15:19:45 +08:00
import org.apache.commons.lang3.tuple.Pair;
import org.bukkit.Bukkit;
@@ -31,6 +35,7 @@ import org.bukkit.command.Command;
2020-05-08 07:44:33 +08: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;
2021-03-16 15:19:45 +08:00
@@ -39,10 +44,12 @@ import java.io.PrintStream;
2021-01-06 05:25:45 +08:00
import java.io.StringWriter;
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;
2021-03-16 15:19:45 +08:00
@@ -52,7 +59,7 @@ import java.util.stream.Collectors;
2020-05-08 07:44:33 +08:00
2021-01-06 05:25:45 +08:00
public class PaperCommand extends Command {
private static final String BASE_PERM = "bukkit.command.paper.";
- private static final ImmutableSet<String> SUBCOMMANDS = ImmutableSet.<String>builder().add("heap", "entity", "reload", "version", "debug", "chunkinfo", "dumpwaiting", "syncloadinfo").build();
+ private static final ImmutableSet<String> SUBCOMMANDS = ImmutableSet.<String>builder().add("heap", "entity", "reload", "version", "debug", "chunkinfo", "dumpwaiting", "syncloadinfo", "fixlight").build();
2020-05-08 07:44:33 +08:00
2021-01-06 05:25:45 +08:00
public PaperCommand(String name) {
super(name);
2021-03-16 15:19:45 +08:00
@@ -173,6 +180,9 @@ public class PaperCommand extends Command {
2020-05-08 07:44:33 +08:00
case "syncloadinfo":
this.doSyncLoadInfo(sender, args);
break;
+ case "fixlight":
+ this.doFixLight(sender, args);
+ break;
case "ver":
2021-01-06 05:25:45 +08:00
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)
2020-05-08 07:44:33 +08:00
case "version":
2021-03-16 15:19:45 +08:00
@@ -190,6 +200,77 @@ public class PaperCommand extends Command {
2020-05-08 07:44:33 +08:00
return true;
}
+ 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;
+ EntityPlayer handle = player.getHandle();
2021-03-16 15:19:45 +08:00
+ WorldServer world = (WorldServer) handle.world;
2020-05-08 07:44:33 +08:00
+ LightEngineThreaded lightengine = world.getChunkProvider().getLightEngine();
+
+ BlockPosition center = MCUtil.toBlockPosition(player.getLocation());
+ Deque<ChunkCoordIntPair> queue = new ArrayDeque<>(MCUtil.getSpiralOutChunks(center, radius));
+ updateLight(sender, world, lightengine, queue);
+ }
+
+ private void updateLight(CommandSender sender, WorldServer world, LightEngineThreaded lightengine, Deque<ChunkCoordIntPair> queue) {
+ ChunkCoordIntPair coord = queue.poll();
+ if (coord == null) {
+ sender.sendMessage("All Chunks Light updated");
+ return;
+ }
2020-05-10 10:30:28 +08:00
+ world.getChunkProvider().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;
+ }
+ Chunk chunk = (Chunk) either.left().orElse(null);
+ if (chunk == null) {
+ updateLight(sender, world, lightengine, queue);
+ return;
+ }
2020-05-08 07:44:33 +08:00
+ lightengine.a(world.paperConfig.lightQueueSize + 16 * 256); // ensure full chunk can fit into queue
+ sender.sendMessage("Updating Light " + coord);
2020-06-26 14:29:44 +08:00
+ int cx = chunk.getPos().x << 4;
+ int cz = chunk.getPos().z << 4;
2020-05-08 07:44:33 +08:00
+ for (int y = 0; y < world.getHeight(); y++) {
+ for (int x = 0; x < 16; x++) {
+ for (int z = 0; z < 16; z++) {
2020-06-26 14:29:44 +08:00
+ BlockPosition pos = new BlockPosition(cx + x, y, cz + z);
2020-05-08 07:44:33 +08:00
+ lightengine.a(pos);
+ }
+ }
+ }
+ lightengine.queueUpdate();
+ PlayerChunk visibleChunk = world.getChunkProvider().playerChunkMap.getVisibleChunk(chunk.coordinateKey);
+ if (visibleChunk != null) {
+ world.getChunkProvider().playerChunkMap.addLightTask(visibleChunk, () -> {
+ MinecraftServer.getServer().processQueue.add(() -> {
2020-06-26 14:29:44 +08:00
+ visibleChunk.sendPacketToTrackedPlayers(new PacketPlayOutLightUpdate(chunk.getPos(), lightengine, true), false);
2020-05-08 07:44:33 +08:00
+ updateLight(sender, world, lightengine, queue);
+ });
+ });
+ } else {
+ updateLight(sender, world, lightengine, queue);
+ }
+ lightengine.a(world.paperConfig.lightQueueSize);
2020-05-10 10:30:28 +08:00
+ }, MinecraftServer.getServer());
2020-05-08 07:44:33 +08:00
+ }
+
private void doSyncLoadInfo(CommandSender sender, String[] args) {
if (!SyncLoadFinder.ENABLED) {
sender.sendMessage(ChatColor.RED + "This command requires the server startup flag '-Dpaper.debug-sync-loads=true' to be set.");
2021-03-16 15:19:45 +08:00
diff --git a/src/main/java/net/minecraft/server/level/PlayerChunk.java b/src/main/java/net/minecraft/server/level/PlayerChunk.java
index 1f6333c2c26ad04e23d2881235ed1dcf707be038..e53054fc46e528f9c713eb4c03add61316e19396 100644
--- a/src/main/java/net/minecraft/server/level/PlayerChunk.java
+++ b/src/main/java/net/minecraft/server/level/PlayerChunk.java
@@ -350,6 +350,7 @@ public class PlayerChunk {
2020-05-08 07:44:33 +08:00
}
+ public void sendPacketToTrackedPlayers(Packet<?> packet, boolean flag) { a(packet, flag); } // Paper - OBFHELPER
private void a(Packet<?> packet, boolean flag) {
// Paper start - per player view distance
// there can be potential desync with player's last mapped section and the view distance map, so use the
2021-03-16 15:19:45 +08:00
diff --git a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java
2021-05-16 07:42:36 +08:00
index e4b5649fbc8cb9662e818581fe2891641de419b6..a7e3a979dca4dd7705a8f80366677378f0643f27 100644
2021-03-16 15:19:45 +08:00
--- a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java
2021-03-16 23:50:45 +08:00
@@ -346,11 +346,12 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2020-05-08 07:44:33 +08:00
Mailbox<Runnable> mailbox = Mailbox.a("main", iasynctaskhandler::a);
this.worldLoadListener = worldloadlistener;
- ThreadedMailbox<Runnable> threadedmailbox1 = ThreadedMailbox.a(executor, "light");
+ ThreadedMailbox<Runnable> lightthreaded; ThreadedMailbox<Runnable> threadedmailbox1 = lightthreaded = ThreadedMailbox.a(executor, "light"); // Paper
this.p = new ChunkTaskQueueSorter(ImmutableList.of(threadedmailbox, mailbox, threadedmailbox1), executor, Integer.MAX_VALUE);
this.mailboxWorldGen = this.p.a(threadedmailbox, false);
this.mailboxMain = this.p.a(mailbox, false);
+ this.mailboxLight = this.p.a(lightthreaded, false);// Paper
2020-06-26 09:58:00 +08:00
this.lightEngine = new LightEngineThreaded(ilightaccess, this, this.world.getDimensionManager().hasSkyLight(), threadedmailbox1, this.p.a(threadedmailbox1, false));
2020-05-08 07:44:33 +08:00
this.chunkDistanceManager = new PlayerChunkMap.a(executor, iasynctaskhandler); this.chunkDistanceManager.chunkMap = this; // Paper
this.l = supplier;