Paper/patches/server/0370-Fix-Per-World-Difficulty-Remembering-Difficulty.patch

119 lines
7.9 KiB
Diff
Raw Permalink 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: Sun, 28 Jun 2020 03:59:10 -0400
Subject: [PATCH] Fix Per World Difficulty / Remembering Difficulty
Fixes per world difficulty with /difficulty command and also
makes it so that the server keeps the last difficulty used instead
of restoring the server.properties every single load.
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 3d2c70ab9938555c976588b0a98901d57876e51e..99b5877fc2f568929b35aa10638173bbef27503b 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -850,7 +850,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2024-04-24 08:44:48 +02:00
worldloadlistener.stop();
// CraftBukkit start
// this.updateMobSpawningFlags();
2024-10-23 16:55:24 +02:00
- worldserver.setSpawnSettings(this.isSpawningMonsters());
+ worldserver.setSpawnSettings(worldserver.serverLevelData.getDifficulty() != Difficulty.PEACEFUL && ((DedicatedServer) this).settings.getProperties().spawnMonsters); // Paper - per level difficulty (from setDifficulty(ServerLevel, Difficulty, boolean))
this.forceTicks = false;
// CraftBukkit end
2024-10-27 18:11:15 +01:00
@@ -1890,11 +1890,14 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 14:02:28 +02:00
}
}
- public void setDifficulty(Difficulty difficulty, boolean forceUpdate) {
2021-06-11 14:02:28 +02:00
- if (forceUpdate || !this.worldData.isDifficultyLocked()) {
- this.worldData.setDifficulty(this.worldData.isHardcore() ? Difficulty.HARD : difficulty);
2021-06-11 14:02:28 +02:00
- this.updateMobSpawningFlags();
- this.getPlayerList().getPlayers().forEach(this::sendDifficultyUpdate);
+ // Paper start - per level difficulty
+ public void setDifficulty(ServerLevel level, Difficulty difficulty, boolean forceUpdate) {
+ PrimaryLevelData worldData = level.serverLevelData;
+ if (forceUpdate || !worldData.isDifficultyLocked()) {
+ worldData.setDifficulty(worldData.isHardcore() ? Difficulty.HARD : difficulty);
2024-10-25 13:19:23 +02:00
+ level.setSpawnSettings(worldData.getDifficulty() != Difficulty.PEACEFUL && ((DedicatedServer) this).settings.getProperties().spawnMonsters);
+ // this.getPlayerList().getPlayers().forEach(this::sendDifficultyUpdate);
+ // Paper end - per level difficulty
2021-06-11 14:02:28 +02:00
}
}
2024-10-27 18:11:15 +01:00
@@ -1908,7 +1911,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
while (iterator.hasNext()) {
ServerLevel worldserver = (ServerLevel) iterator.next();
2024-10-23 16:55:24 +02:00
- worldserver.setSpawnSettings(this.isSpawningMonsters());
+ worldserver.setSpawnSettings(worldserver.serverLevelData.getDifficulty() != Difficulty.PEACEFUL && ((DedicatedServer) this).settings.getProperties().spawnMonsters); // Paper - per level difficulty (from setDifficulty(ServerLevel, Difficulty, boolean))
}
}
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/commands/DifficultyCommand.java b/src/main/java/net/minecraft/server/commands/DifficultyCommand.java
index 997a96a21440ae72696d68f8031ece4ba487d3ef..3d8584929cee000ae7df10c5bd94f35820e70294 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/commands/DifficultyCommand.java
+++ b/src/main/java/net/minecraft/server/commands/DifficultyCommand.java
2023-06-08 01:20:26 +02:00
@@ -49,7 +49,7 @@ public class DifficultyCommand {
2022-03-01 06:43:03 +01:00
if (worldServer.getDifficulty() == difficulty) { // CraftBukkit
throw DifficultyCommand.ERROR_ALREADY_DIFFICULT.create(difficulty.getKey());
2021-06-11 14:02:28 +02:00
} else {
2022-03-01 06:43:03 +01:00
- worldServer.serverLevelData.setDifficulty(difficulty); // CraftBukkit
+ minecraftserver.setDifficulty(worldServer, difficulty, true); // Paper - per level difficulty; don't skip other difficulty-changing logic (fix upstream's fix)
2023-06-08 01:20:26 +02:00
source.sendSuccess(() -> {
return Component.translatable("commands.difficulty.success", difficulty.getDisplayName());
}, true);
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
2024-10-27 18:11:15 +01:00
index fe1975675189c6d1a63c42b7959fa40b5ac95ef8..9a3e73a5c206b78dfcf6f41a47b614342e52acc8 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
2024-10-27 18:11:15 +01:00
@@ -337,7 +337,7 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
2021-06-11 14:02:28 +02:00
@Override
public void forceDifficulty() {
- this.setDifficulty(this.getProperties().difficulty, true);
+ // this.setDifficulty(this.getProperties().difficulty, true); // Paper - per level difficulty; Don't overwrite level.dat's difficulty, keep current
2021-06-11 14:02:28 +02:00
}
@Override
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-10-27 18:11:15 +01:00
index 0884f71d3264c2a09d2a0958d4751962e4156526..8bb1b0e7caa938170169379ce22f21c1c772db60 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-10-27 18:11:15 +01:00
@@ -3308,7 +3308,7 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
2021-06-11 14:02:28 +02:00
public void handleChangeDifficulty(ServerboundChangeDifficultyPacket packet) {
2023-06-08 01:20:26 +02:00
PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
2021-06-11 14:02:28 +02:00
if (this.player.hasPermissions(2) || this.isSingleplayerOwner()) {
- this.server.setDifficulty(packet.getDifficulty(), false);
+ // this.server.setDifficulty(packet.getDifficulty(), false); // Paper - per level difficulty; don't allow clients to change this
2021-06-11 14:02:28 +02:00
}
}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index ca8eb3216c4331a95ab44f923f6b49641662505f..9905555f249db72649bde8401835dd816ed7b428 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
2024-10-27 18:11:15 +01:00
@@ -1000,8 +1000,8 @@ public final class CraftServer implements Server {
2023-10-27 01:34:58 +02:00
org.spigotmc.SpigotConfig.init((File) this.console.options.valueOf("spigot-settings")); // Spigot
this.console.paperConfigurations.reloadConfigs(this.console);
for (ServerLevel world : this.console.getAllLevels()) {
- world.serverLevelData.setDifficulty(config.difficulty);
2024-10-23 16:55:24 +02:00
- world.setSpawnSettings(config.spawnMonsters);
+ // world.serverLevelData.setDifficulty(config.difficulty); // Paper - per level difficulty
2024-10-23 16:55:24 +02:00
+ world.setSpawnSettings(world.serverLevelData.getDifficulty() != Difficulty.PEACEFUL && config.spawnMonsters); // Paper - per level difficulty (from MinecraftServer#setDifficulty(ServerLevel, Difficulty, boolean))
for (SpawnCategory spawnCategory : SpawnCategory.values()) {
if (CraftSpawnCategory.isValidForLimits(spawnCategory)) {
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index e6a7359d120b2361669a407b92357ea1a6b9a2a8..93d1c641a6fa063f3c5a4b9b2d9a15071ae7de01 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -1169,7 +1169,7 @@ public class CraftWorld extends CraftRegionAccessor implements World {
@Override
public void setDifficulty(Difficulty difficulty) {
- this.getHandle().serverLevelData.setDifficulty(net.minecraft.world.Difficulty.byId(difficulty.getValue()));
+ this.getHandle().getServer().setDifficulty(this.getHandle(), net.minecraft.world.Difficulty.byId(difficulty.getValue()), true); // Paper - per level difficulty; don't skip other difficulty-changing logic
}
@Override