| 1 | package net.minecraft.server.dedicated;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.collect.ImmutableMap.Builder;
|
| 6 | import com.mojang.datafixers.DataFixer;
|
| 7 | import com.mojang.logging.LogUtils;
|
| 8 | import java.io.BufferedReader;
|
| 9 | import java.io.IOException;
|
| 10 | import java.io.InputStreamReader;
|
| 11 | import java.io.Writer;
|
| 12 | import java.net.InetAddress;
|
| 13 | import java.net.Proxy;
|
| 14 | import java.net.URI;
|
| 15 | import java.nio.charset.StandardCharsets;
|
| 16 | import java.nio.file.Files;
|
| 17 | import java.nio.file.LinkOption;
|
| 18 | import java.nio.file.Path;
|
| 19 | import java.util.Collections;
|
| 20 | import java.util.List;
|
| 21 | import java.util.Locale;
|
| 22 | import java.util.Map;
|
| 23 | import java.util.Optional;
|
| 24 | import java.util.function.BooleanSupplier;
|
| 25 | import java.util.stream.Stream;
|
| 26 | import net.minecraft.DefaultUncaughtExceptionHandler;
|
| 27 | import net.minecraft.DefaultUncaughtExceptionHandlerWithName;
|
| 28 | import net.minecraft.SharedConstants;
|
| 29 | import net.minecraft.SystemReport;
|
| 30 | import net.minecraft.commands.CommandSourceStack;
|
| 31 | import net.minecraft.core.BlockPos;
|
| 32 | import net.minecraft.network.protocol.game.ClientboundLowDiskSpaceWarningPacket;
|
| 33 | import net.minecraft.server.ConsoleInput;
|
| 34 | import net.minecraft.server.MinecraftServer;
|
| 35 | import net.minecraft.server.ServerInterface;
|
| 36 | import net.minecraft.server.ServerLinks;
|
| 37 | import net.minecraft.server.Services;
|
| 38 | import net.minecraft.server.WorldStem;
|
| 39 | import net.minecraft.server.gui.MinecraftServerGui;
|
| 40 | import net.minecraft.server.jsonrpc.ManagementServer;
|
| 41 | import net.minecraft.server.level.ServerLevel;
|
| 42 | import net.minecraft.server.level.ServerPlayer;
|
| 43 | import net.minecraft.server.level.progress.LoggingLevelLoadListener;
|
| 44 | import net.minecraft.server.network.ServerTextFilter;
|
| 45 | import net.minecraft.server.network.TextFilter;
|
| 46 | import net.minecraft.server.notifications.NotificationManager;
|
| 47 | import net.minecraft.server.packs.repository.PackRepository;
|
| 48 | import net.minecraft.server.permissions.LevelBasedPermissionSet;
|
| 49 | import net.minecraft.server.permissions.Permission;
|
| 50 | import net.minecraft.server.permissions.PermissionLevel;
|
| 51 | import net.minecraft.server.permissions.PermissionSet;
|
| 52 | import net.minecraft.server.players.NameAndId;
|
| 53 | import net.minecraft.server.players.OldUsersConverter;
|
| 54 | import net.minecraft.server.rcon.RconConsoleSource;
|
| 55 | import net.minecraft.server.rcon.thread.QueryThreadGs4;
|
| 56 | import net.minecraft.server.rcon.thread.RconThread;
|
| 57 | import net.minecraft.util.Mth;
|
| 58 | import net.minecraft.util.StringUtil;
|
| 59 | import net.minecraft.util.Util;
|
| 60 | import net.minecraft.util.debug.DebugSubscriptions;
|
| 61 | import net.minecraft.util.debugchart.RemoteDebugSampleType;
|
| 62 | import net.minecraft.util.debugchart.RemoteSampleLogger;
|
| 63 | import net.minecraft.util.debugchart.SampleLogger;
|
| 64 | import net.minecraft.util.debugchart.TpsDebugDimensions;
|
| 65 | import net.minecraft.util.monitoring.jmx.MinecraftServerStatistics;
|
| 66 | import net.minecraft.world.Difficulty;
|
| 67 | import net.minecraft.world.entity.player.Player;
|
| 68 | import net.minecraft.world.level.GameType;
|
| 69 | import net.minecraft.world.level.gamerules.GameRules;
|
| 70 | import net.minecraft.world.level.storage.LevelData;
|
| 71 | import net.minecraft.world.level.storage.LevelStorageSource;
|
| 72 | import org.jspecify.annotations.Nullable;
|
| 73 | import org.slf4j.Logger;
|
| 74 |
|
| 75 | public class DedicatedServer extends MinecraftServer implements ServerInterface {
|
| 76 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 77 | private static final int CONVERSION_RETRY_DELAY_MS = 5000;
|
| 78 | private static final int CONVERSION_RETRIES = 2;
|
| 79 | private final List<ConsoleInput> consoleInput = Collections.synchronizedList(Lists.newArrayList());
|
| 80 | private @Nullable QueryThreadGs4 queryThreadGs4;
|
| 81 | private final RconConsoleSource rconConsoleSource;
|
| 82 | private @Nullable RconThread rconThread;
|
| 83 | private final DedicatedServerSettings settings;
|
| 84 | private @Nullable MinecraftServerGui gui;
|
| 85 | private final @Nullable ServerTextFilter serverTextFilter;
|
| 86 | private @Nullable RemoteSampleLogger tickTimeLogger;
|
| 87 | private boolean isTickTimeLoggingEnabled;
|
| 88 | private final ServerLinks serverLinks;
|
| 89 | private final Map<String, String> codeOfConductTexts;
|
| 90 | private final @Nullable ManagementServer jsonRpcServer;
|
| 91 |
|
| 92 | public DedicatedServer(
|
| 93 | final Thread serverThread,
|
| 94 | final LevelStorageSource.LevelStorageAccess levelStorageSource,
|
| 95 | final PackRepository packRepository,
|
| 96 | final WorldStem worldStem,
|
| 97 | final Optional<GameRules> gameRules,
|
| 98 | final DedicatedServerSettings settings,
|
| 99 | final DataFixer fixerUpper,
|
| 100 | final Services services,
|
| 101 | final @Nullable ManagementServer jsonRpcServer,
|
| 102 | final NotificationManager notificationManager
|
| 103 | ) {
|
| 104 | super(
|
| 105 | serverThread,
|
| 106 | levelStorageSource,
|
| 107 | packRepository,
|
| 108 | worldStem,
|
| 109 | gameRules,
|
| 110 | Proxy.NO_PROXY,
|
| 111 | fixerUpper,
|
| 112 | services,
|
| 113 | LoggingLevelLoadListener.forDedicatedServer(),
|
| 114 | true,
|
| 115 | notificationManager
|
| 116 | );
|
| 117 | this.settings = settings;
|
| 118 | this.rconConsoleSource = new RconConsoleSource(this);
|
| 119 | this.serverTextFilter = ServerTextFilter.createFromConfig(settings.getProperties());
|
| 120 | this.jsonRpcServer = jsonRpcServer;
|
| 121 | this.serverLinks = createServerLinks(settings);
|
| 122 | if (settings.getProperties().codeOfConduct) {
|
| 123 | this.codeOfConductTexts = readCodeOfConducts();
|
| 124 | } else {
|
| 125 | this.codeOfConductTexts = Map.of();
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | private static Map<String, String> readCodeOfConducts() {
|
| 130 | Path path = Path.of("codeofconduct");
|
| 131 | if (!Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
|
| 132 | throw new IllegalArgumentException("Code of Conduct folder does not exist: " + path);
|
| 133 | }
|
| 134 |
|
| 135 | try {
|
| 136 | Builder<String, String> builder = ImmutableMap.builder();
|
| 137 |
|
| 138 | try (Stream<Path> files = Files.list(path)) {
|
| 139 | for (Path file : files.toList()) {
|
| 140 | String filename = file.getFileName().toString();
|
| 141 | if (filename.endsWith(".txt")) {
|
| 142 | String language = filename.substring(0, filename.length() - 4).toLowerCase(Locale.ROOT);
|
| 143 | if (!file.toRealPath().getParent().equals(path.toAbsolutePath())) {
|
| 144 | throw new IllegalArgumentException("Failed to read Code of Conduct file \"" + filename + "\" because it links to a file outside the allowed directory");
|
| 145 | }
|
| 146 |
|
| 147 | try {
|
| 148 | String codeOfConduct = String.join("\n", Files.readAllLines(file, StandardCharsets.UTF_8));
|
| 149 | builder.put(language, StringUtil.stripColor(codeOfConduct));
|
| 150 | } catch (IOException e) {
|
| 151 | throw new IllegalArgumentException("Failed to read Code of Conduct file " + filename, e);
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | return builder.build();
|
| 158 | } catch (IOException e) {
|
| 159 | throw new IllegalArgumentException("Failed to read Code of Conduct folder", e);
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | protected boolean initServer() throws IOException {
|
| 165 | Thread consoleThread = new Thread("Server console handler") {
|
| 166 | @Override
|
| 167 | public void run() {
|
| 168 | BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
|
| 169 |
|
| 170 | String line;
|
| 171 | try {
|
| 172 | while (!DedicatedServer.this.isStopped() && DedicatedServer.this.isRunning() && (line = reader.readLine()) != null) {
|
| 173 | DedicatedServer.this.handleConsoleInput(line, DedicatedServer.this.createCommandSourceStack());
|
| 174 | }
|
| 175 | } catch (IOException e) {
|
| 176 | DedicatedServer.LOGGER.error("Exception handling console input", e);
|
| 177 | }
|
| 178 | }
|
| 179 | };
|
| 180 | consoleThread.setDaemon(true);
|
| 181 | consoleThread.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(LOGGER));
|
| 182 | consoleThread.start();
|
| 183 | LOGGER.info("Starting minecraft server version {}", SharedConstants.getCurrentVersion().name());
|
| 184 | if (Runtime.getRuntime().maxMemory() / 1024L / 1024L < 512L) {
|
| 185 | LOGGER.warn("To start the server with more ram, launch it as \"java -Xmx1024M -Xms1024M -jar minecraft_server.jar\"");
|
| 186 | }
|
| 187 |
|
| 188 | LOGGER.info("Loading properties");
|
| 189 | DedicatedServerProperties properties = this.settings.getProperties();
|
| 190 | if (this.isSingleplayer()) {
|
| 191 | this.setLocalIp("127.0.0.1");
|
| 192 | } else {
|
| 193 | this.setUsesAuthentication(properties.onlineMode);
|
| 194 | this.setPreventProxyConnections(properties.preventProxyConnections);
|
| 195 | this.setLocalIp(properties.serverIp);
|
| 196 | }
|
| 197 |
|
| 198 | this.worldData.setGameType(properties.gameMode.get());
|
| 199 | LOGGER.info("Default game type: {}", properties.gameMode.get());
|
| 200 | InetAddress localAddress = null;
|
| 201 | if (!this.getLocalIp().isEmpty()) {
|
| 202 | localAddress = InetAddress.getByName(this.getLocalIp());
|
| 203 | }
|
| 204 |
|
| 205 | if (this.getPort() < 0) {
|
| 206 | this.setPort(properties.serverPort);
|
| 207 | }
|
| 208 |
|
| 209 | this.initializeKeyPair();
|
| 210 | LOGGER.info("Starting Minecraft server on {}:{}", this.getLocalIp().isEmpty() ? "*" : this.getLocalIp(), this.getPort());
|
| 211 |
|
| 212 | try {
|
| 213 | this.getConnection().startTcpServerListener(localAddress, this.getPort());
|
| 214 | } catch (IOException e) {
|
| 215 | LOGGER.warn("**** FAILED TO BIND TO PORT!");
|
| 216 | LOGGER.warn("The exception was: {}", e.toString());
|
| 217 | LOGGER.warn("Perhaps a server is already running on that port?");
|
| 218 | return false;
|
| 219 | }
|
| 220 |
|
| 221 | if (!this.usesAuthentication()) {
|
| 222 | LOGGER.warn("**** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!");
|
| 223 | LOGGER.warn("The server will make no attempt to authenticate usernames. Beware.");
|
| 224 | LOGGER.warn(
|
| 225 | "While this makes the game possible to play without internet access, it also opens up the ability for hackers to connect with any username they choose."
|
| 226 | );
|
| 227 | LOGGER.warn("To change this, set \"online-mode\" to \"true\" in the server.properties file.");
|
| 228 | }
|
| 229 |
|
| 230 | if (this.convertOldUsers()) {
|
| 231 | this.services.nameToIdCache().save();
|
| 232 | }
|
| 233 |
|
| 234 | if (!OldUsersConverter.areOldUserlistsRemoved()) {
|
| 235 | return false;
|
| 236 | }
|
| 237 |
|
| 238 | this.setPlayerList(new DedicatedPlayerList(this, this.registries(), this.playerDataStorage));
|
| 239 | this.tickTimeLogger = new RemoteSampleLogger(TpsDebugDimensions.values().length, this.debugSubscribers(), RemoteDebugSampleType.TICK_TIME);
|
| 240 | long levelNanoTime = Util.getNanos();
|
| 241 | this.services.nameToIdCache().resolveOfflineUsers(!this.usesAuthentication());
|
| 242 | LOGGER.info("Preparing level \"{}\"", this.getLevelIdName());
|
| 243 | this.loadLevel();
|
| 244 | long elapsed = Util.getNanos() - levelNanoTime;
|
| 245 | String time = String.format(Locale.ROOT, "%.3fs", elapsed / 1.0E9);
|
| 246 | LOGGER.info("Done ({})! For help, type \"help\"", time);
|
| 247 | if (properties.announcePlayerAchievements != null) {
|
| 248 | this.getGameRules().set(GameRules.SHOW_ADVANCEMENT_MESSAGES, properties.announcePlayerAchievements, this);
|
| 249 | }
|
| 250 |
|
| 251 | if (properties.enableQuery) {
|
| 252 | LOGGER.info("Starting GS4 status listener");
|
| 253 | this.queryThreadGs4 = QueryThreadGs4.create(this);
|
| 254 | }
|
| 255 |
|
| 256 | if (properties.enableRcon) {
|
| 257 | LOGGER.info("Starting remote control listener");
|
| 258 | this.rconThread = RconThread.create(this);
|
| 259 | }
|
| 260 |
|
| 261 | if (this.getMaxTickLength() > 0L) {
|
| 262 | Thread watchdog = new Thread(new ServerWatchdog(this), "Server Watchdog");
|
| 263 | watchdog.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandlerWithName(LOGGER));
|
| 264 | watchdog.setDaemon(true);
|
| 265 | watchdog.start();
|
| 266 | }
|
| 267 |
|
| 268 | if (properties.enableJmxMonitoring) {
|
| 269 | MinecraftServerStatistics.registerJmxMonitoring(this);
|
| 270 | LOGGER.info("JMX monitoring enabled");
|
| 271 | }
|
| 272 |
|
| 273 | this.saveEverything(false, true, true);
|
| 274 | this.notificationManager().serverStarted();
|
| 275 | return true;
|
| 276 | }
|
| 277 |
|
| 278 | @Override
|
| 279 | public boolean isEnforceWhitelist() {
|
| 280 | return this.settings.getProperties().enforceWhitelist.get();
|
| 281 | }
|
| 282 |
|
| 283 | @Override
|
| 284 | public void setEnforceWhitelist(final boolean enforceWhitelist) {
|
| 285 | this.settings.update(p -> p.enforceWhitelist.update(this.registryAccess(), enforceWhitelist));
|
| 286 | }
|
| 287 |
|
| 288 | @Override
|
| 289 | public boolean isUsingWhitelist() {
|
| 290 | return this.settings.getProperties().whiteList.get();
|
| 291 | }
|
| 292 |
|
| 293 | @Override
|
| 294 | public void setUsingWhitelist(final boolean usingWhitelist) {
|
| 295 | this.settings.update(p -> p.whiteList.update(this.registryAccess(), usingWhitelist));
|
| 296 | }
|
| 297 |
|
| 298 | @Override
|
| 299 | protected void tickServer(final BooleanSupplier haveTime) {
|
| 300 | super.tickServer(haveTime);
|
| 301 | if (this.jsonRpcServer != null) {
|
| 302 | this.jsonRpcServer.tick();
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | @Override
|
| 307 | public boolean saveAllChunks(final boolean silent, final boolean flush, final boolean force) {
|
| 308 | this.notificationManager().serverSaveStarted();
|
| 309 | boolean savedChunks = super.saveAllChunks(silent, flush, force);
|
| 310 | this.notificationManager().serverSaveCompleted();
|
| 311 | return savedChunks;
|
| 312 | }
|
| 313 |
|
| 314 | @Override
|
| 315 | public void sendLowDiskSpaceWarning() {
|
| 316 | super.sendLowDiskSpaceWarning();
|
| 317 | Permission.HasCommandLevel adminCheck = new Permission.HasCommandLevel(PermissionLevel.ADMINS);
|
| 318 | this.getPlayerList()
|
| 319 | .getPlayers()
|
| 320 | .stream()
|
| 321 | .filter(p -> p.permissions().hasPermission(adminCheck))
|
| 322 | .forEach(p -> p.connection.send(ClientboundLowDiskSpaceWarningPacket.INSTANCE));
|
| 323 | }
|
| 324 |
|
| 325 | @Override
|
| 326 | public boolean allowFlight() {
|
| 327 | return this.settings.getProperties().allowFlight.get();
|
| 328 | }
|
| 329 |
|
| 330 | public void setAllowFlight(final boolean allowed) {
|
| 331 | this.settings.update(p -> p.allowFlight.update(this.registryAccess(), allowed));
|
| 332 | }
|
| 333 |
|
| 334 | @Override
|
| 335 | public DedicatedServerProperties getProperties() {
|
| 336 | return this.settings.getProperties();
|
| 337 | }
|
| 338 |
|
| 339 | public void setDifficulty(final Difficulty difficulty) {
|
| 340 | this.settings.update(p -> p.difficulty.update(this.registryAccess(), difficulty));
|
| 341 | this.forceDifficulty();
|
| 342 | }
|
| 343 |
|
| 344 | @Override
|
| 345 | protected void forceDifficulty() {
|
| 346 | this.setDifficulty(this.getProperties().difficulty.get(), true);
|
| 347 | }
|
| 348 |
|
| 349 | public int viewDistance() {
|
| 350 | return this.settings.getProperties().viewDistance.get();
|
| 351 | }
|
| 352 |
|
| 353 | public void setViewDistance(final int viewDistance) {
|
| 354 | this.settings.update(p -> p.viewDistance.update(this.registryAccess(), viewDistance));
|
| 355 | this.getPlayerList().setViewDistance(viewDistance);
|
| 356 | }
|
| 357 |
|
| 358 | public int simulationDistance() {
|
| 359 | return this.settings.getProperties().simulationDistance.get();
|
| 360 | }
|
| 361 |
|
| 362 | public void setSimulationDistance(final int simulationDistance) {
|
| 363 | this.settings.update(p -> p.simulationDistance.update(this.registryAccess(), simulationDistance));
|
| 364 | this.getPlayerList().setSimulationDistance(simulationDistance);
|
| 365 | }
|
| 366 |
|
| 367 | @Override
|
| 368 | public SystemReport fillServerSystemReport(final SystemReport systemReport) {
|
| 369 | systemReport.setDetail("Is Modded", () -> this.getModdedStatus().fullDescription());
|
| 370 | systemReport.setDetail("Type", () -> "Dedicated Server");
|
| 371 | return systemReport;
|
| 372 | }
|
| 373 |
|
| 374 | @Override
|
| 375 | public void dumpServerProperties(final Path path) throws IOException {
|
| 376 | DedicatedServerProperties serverProperties = this.getProperties();
|
| 377 |
|
| 378 | try (Writer output = Files.newBufferedWriter(path)) {
|
| 379 | output.write(String.format(Locale.ROOT, "sync-chunk-writes=%s%n", serverProperties.syncChunkWrites));
|
| 380 | output.write(String.format(Locale.ROOT, "gamemode=%s%n", serverProperties.gameMode.get()));
|
| 381 | output.write(String.format(Locale.ROOT, "entity-broadcast-range-percentage=%d%n", serverProperties.entityBroadcastRangePercentage.get()));
|
| 382 | output.write(String.format(Locale.ROOT, "max-world-size=%d%n", serverProperties.maxWorldSize));
|
| 383 | output.write(String.format(Locale.ROOT, "view-distance=%d%n", serverProperties.viewDistance.get()));
|
| 384 | output.write(String.format(Locale.ROOT, "simulation-distance=%d%n", serverProperties.simulationDistance.get()));
|
| 385 | output.write(String.format(Locale.ROOT, "generate-structures=%s%n", serverProperties.worldOptions.generateStructures()));
|
| 386 | output.write(String.format(Locale.ROOT, "use-native=%s%n", serverProperties.useNativeTransport));
|
| 387 | output.write(String.format(Locale.ROOT, "rate-limit=%d%n", serverProperties.rateLimitPacketsPerSecond));
|
| 388 | output.write(String.format(Locale.ROOT, "command-spam-threshold-seconds=%d%n", serverProperties.commandSpamThresholdSeconds));
|
| 389 | output.write(String.format(Locale.ROOT, "chat-spam-threshold-seconds=%d%n", serverProperties.chatSpamThresholdSeconds));
|
| 390 | }
|
| 391 | }
|
| 392 |
|
| 393 | @Override
|
| 394 | protected void onServerExit() {
|
| 395 | if (this.serverTextFilter != null) {
|
| 396 | this.serverTextFilter.close();
|
| 397 | }
|
| 398 |
|
| 399 | if (this.gui != null) {
|
| 400 | this.gui.close();
|
| 401 | }
|
| 402 |
|
| 403 | if (this.rconThread != null) {
|
| 404 | this.rconThread.stop();
|
| 405 | }
|
| 406 |
|
| 407 | if (this.queryThreadGs4 != null) {
|
| 408 | this.queryThreadGs4.stop();
|
| 409 | }
|
| 410 |
|
| 411 | if (this.jsonRpcServer != null) {
|
| 412 | try {
|
| 413 | this.jsonRpcServer.stop(true);
|
| 414 | } catch (InterruptedException e) {
|
| 415 | LOGGER.error("Interrupted while stopping the management server", e);
|
| 416 | }
|
| 417 | }
|
| 418 | }
|
| 419 |
|
| 420 | @Override
|
| 421 | protected void tickConnection() {
|
| 422 | super.tickConnection();
|
| 423 | this.handleConsoleInputs();
|
| 424 | }
|
| 425 |
|
| 426 | public void handleConsoleInput(final String msg, final CommandSourceStack source) {
|
| 427 | this.consoleInput.add(new ConsoleInput(msg, source));
|
| 428 | }
|
| 429 |
|
| 430 | public void handleConsoleInputs() {
|
| 431 | while (!this.consoleInput.isEmpty()) {
|
| 432 | ConsoleInput input = this.consoleInput.remove(0);
|
| 433 | this.getCommands().performPrefixedCommand(input.source, input.msg);
|
| 434 | }
|
| 435 | }
|
| 436 |
|
| 437 | @Override
|
| 438 | public boolean isDedicatedServer() {
|
| 439 | return true;
|
| 440 | }
|
| 441 |
|
| 442 | @Override
|
| 443 | public int getRateLimitPacketsPerSecond() {
|
| 444 | return this.getProperties().rateLimitPacketsPerSecond;
|
| 445 | }
|
| 446 |
|
| 447 | @Override
|
| 448 | public int getCommandSpamThresholdSeconds() {
|
| 449 | return this.getProperties().commandSpamThresholdSeconds;
|
| 450 | }
|
| 451 |
|
| 452 | @Override
|
| 453 | public int getChatSpamThresholdSeconds() {
|
| 454 | return this.getProperties().chatSpamThresholdSeconds;
|
| 455 | }
|
| 456 |
|
| 457 | @Override
|
| 458 | public boolean useNativeTransport() {
|
| 459 | return this.getProperties().useNativeTransport;
|
| 460 | }
|
| 461 |
|
| 462 | public DedicatedPlayerList getPlayerList() {
|
| 463 | return (DedicatedPlayerList)super.getPlayerList();
|
| 464 | }
|
| 465 |
|
| 466 | @Override
|
| 467 | public int getMaxPlayers() {
|
| 468 | return this.settings.getProperties().maxPlayers.get();
|
| 469 | }
|
| 470 |
|
| 471 | public void setMaxPlayers(final int maxPlayers) {
|
| 472 | this.settings.update(p -> p.maxPlayers.update(this.registryAccess(), maxPlayers));
|
| 473 | }
|
| 474 |
|
| 475 | @Override
|
| 476 | public boolean isPublished() {
|
| 477 | return true;
|
| 478 | }
|
| 479 |
|
| 480 | @Override
|
| 481 | public String getServerIp() {
|
| 482 | return this.getLocalIp();
|
| 483 | }
|
| 484 |
|
| 485 | @Override
|
| 486 | public int getServerPort() {
|
| 487 | return this.getPort();
|
| 488 | }
|
| 489 |
|
| 490 | @Override
|
| 491 | public String getServerName() {
|
| 492 | return this.getMotd();
|
| 493 | }
|
| 494 |
|
| 495 | public void showGui() {
|
| 496 | if (this.gui == null) {
|
| 497 | this.gui = MinecraftServerGui.showFrameFor(this);
|
| 498 | }
|
| 499 | }
|
| 500 |
|
| 501 | public int spawnProtectionRadius() {
|
| 502 | return this.getProperties().spawnProtection.get();
|
| 503 | }
|
| 504 |
|
| 505 | public void setSpawnProtectionRadius(final int spawnProtectionRadius) {
|
| 506 | this.settings.update(p -> p.spawnProtection.update(this.registryAccess(), spawnProtectionRadius));
|
| 507 | }
|
| 508 |
|
| 509 | @Override
|
| 510 | public boolean isUnderSpawnProtection(final ServerLevel level, final BlockPos pos, final Player player) {
|
| 511 | LevelData.RespawnData respawnData = level.getRespawnData();
|
| 512 | if (level.dimension() != respawnData.dimension()) {
|
| 513 | return false;
|
| 514 | }
|
| 515 |
|
| 516 | if (this.getPlayerList().getOps().isEmpty()) {
|
| 517 | return false;
|
| 518 | }
|
| 519 |
|
| 520 | if (this.getPlayerList().isOp(player.nameAndId())) {
|
| 521 | return false;
|
| 522 | }
|
| 523 |
|
| 524 | if (this.spawnProtectionRadius() <= 0) {
|
| 525 | return false;
|
| 526 | }
|
| 527 |
|
| 528 | BlockPos spawnPos = respawnData.pos();
|
| 529 | int xd = Mth.abs(pos.getX() - spawnPos.getX());
|
| 530 | int zd = Mth.abs(pos.getZ() - spawnPos.getZ());
|
| 531 | int dist = Math.max(xd, zd);
|
| 532 | return dist <= this.spawnProtectionRadius();
|
| 533 | }
|
| 534 |
|
| 535 | @Override
|
| 536 | public boolean repliesToStatus() {
|
| 537 | return this.getProperties().enableStatus.get();
|
| 538 | }
|
| 539 |
|
| 540 | public void setRepliesToStatus(final boolean enable) {
|
| 541 | this.settings.update(p -> p.enableStatus.update(this.registryAccess(), enable));
|
| 542 | }
|
| 543 |
|
| 544 | @Override
|
| 545 | public boolean hidesOnlinePlayers() {
|
| 546 | return this.getProperties().hideOnlinePlayers.get();
|
| 547 | }
|
| 548 |
|
| 549 | public void setHidesOnlinePlayers(final boolean hide) {
|
| 550 | this.settings.update(p -> p.hideOnlinePlayers.update(this.registryAccess(), hide));
|
| 551 | }
|
| 552 |
|
| 553 | @Override
|
| 554 | public LevelBasedPermissionSet operatorUserPermissions() {
|
| 555 | return this.getProperties().opPermissions.get();
|
| 556 | }
|
| 557 |
|
| 558 | public void setOperatorUserPermissions(final LevelBasedPermissionSet permissions) {
|
| 559 | this.settings.update(p -> p.opPermissions.update(this.registryAccess(), permissions));
|
| 560 | }
|
| 561 |
|
| 562 | @Override
|
| 563 | public PermissionSet getFunctionCompilationPermissions() {
|
| 564 | return this.getProperties().functionPermissions;
|
| 565 | }
|
| 566 |
|
| 567 | @Override
|
| 568 | public int playerIdleTimeout() {
|
| 569 | return this.settings.getProperties().playerIdleTimeout.get();
|
| 570 | }
|
| 571 |
|
| 572 | @Override
|
| 573 | public void setPlayerIdleTimeout(final int playerIdleTimeout) {
|
| 574 | this.settings.update(p -> p.playerIdleTimeout.update(this.registryAccess(), playerIdleTimeout));
|
| 575 | }
|
| 576 |
|
| 577 | public int statusHeartbeatInterval() {
|
| 578 | return this.settings.getProperties().statusHeartbeatInterval.get();
|
| 579 | }
|
| 580 |
|
| 581 | public boolean setStatusHeartbeatInterval(final int statusHeartbeatInterval) {
|
| 582 | if (this.jsonRpcServer != null && this.jsonRpcServer.scheduleHeartbeat(this.notificationManager(), statusHeartbeatInterval)) {
|
| 583 | this.settings.update(p -> p.statusHeartbeatInterval.update(this.registryAccess(), statusHeartbeatInterval));
|
| 584 | return true;
|
| 585 | } else {
|
| 586 | return false;
|
| 587 | }
|
| 588 | }
|
| 589 |
|
| 590 | @Override
|
| 591 | public String getMotd() {
|
| 592 | return this.settings.getProperties().motd.get();
|
| 593 | }
|
| 594 |
|
| 595 | @Override
|
| 596 | public void setMotd(final String motd) {
|
| 597 | this.settings.update(p -> p.motd.update(this.registryAccess(), motd));
|
| 598 | }
|
| 599 |
|
| 600 | @Override
|
| 601 | public boolean shouldRconBroadcast() {
|
| 602 | return this.getProperties().broadcastRconToOps;
|
| 603 | }
|
| 604 |
|
| 605 | @Override
|
| 606 | public boolean shouldInformAdmins() {
|
| 607 | return this.getProperties().broadcastConsoleToOps;
|
| 608 | }
|
| 609 |
|
| 610 | @Override
|
| 611 | public int getAbsoluteMaxWorldSize() {
|
| 612 | return this.getProperties().maxWorldSize;
|
| 613 | }
|
| 614 |
|
| 615 | @Override
|
| 616 | public int getCompressionThreshold() {
|
| 617 | return this.getProperties().networkCompressionThreshold;
|
| 618 | }
|
| 619 |
|
| 620 | @Override
|
| 621 | public boolean enforceSecureProfile() {
|
| 622 | DedicatedServerProperties properties = this.getProperties();
|
| 623 | return properties.enforceSecureProfile && properties.onlineMode && this.services.canValidateProfileKeys();
|
| 624 | }
|
| 625 |
|
| 626 | @Override
|
| 627 | public boolean logIPs() {
|
| 628 | return this.getProperties().logIPs;
|
| 629 | }
|
| 630 |
|
| 631 | protected boolean convertOldUsers() {
|
| 632 | boolean userBanlistConverted = false;
|
| 633 |
|
| 634 | for (int retries = 0; !userBanlistConverted && retries <= 2; retries++) {
|
| 635 | if (retries > 0) {
|
| 636 | LOGGER.warn("Encountered a problem while converting the user banlist, retrying in a few seconds");
|
| 637 | this.waitForRetry();
|
| 638 | }
|
| 639 |
|
| 640 | userBanlistConverted = OldUsersConverter.convertUserBanlist(this);
|
| 641 | }
|
| 642 |
|
| 643 | boolean ipBanlistConverted = false;
|
| 644 |
|
| 645 | for (int var7 = 0; !ipBanlistConverted && var7 <= 2; var7++) {
|
| 646 | if (var7 > 0) {
|
| 647 | LOGGER.warn("Encountered a problem while converting the ip banlist, retrying in a few seconds");
|
| 648 | this.waitForRetry();
|
| 649 | }
|
| 650 |
|
| 651 | ipBanlistConverted = OldUsersConverter.convertIpBanlist(this);
|
| 652 | }
|
| 653 |
|
| 654 | boolean opListConverted = false;
|
| 655 |
|
| 656 | for (int var8 = 0; !opListConverted && var8 <= 2; var8++) {
|
| 657 | if (var8 > 0) {
|
| 658 | LOGGER.warn("Encountered a problem while converting the op list, retrying in a few seconds");
|
| 659 | this.waitForRetry();
|
| 660 | }
|
| 661 |
|
| 662 | opListConverted = OldUsersConverter.convertOpsList(this);
|
| 663 | }
|
| 664 |
|
| 665 | boolean whitelistConverted = false;
|
| 666 |
|
| 667 | for (int var9 = 0; !whitelistConverted && var9 <= 2; var9++) {
|
| 668 | if (var9 > 0) {
|
| 669 | LOGGER.warn("Encountered a problem while converting the whitelist, retrying in a few seconds");
|
| 670 | this.waitForRetry();
|
| 671 | }
|
| 672 |
|
| 673 | whitelistConverted = OldUsersConverter.convertWhiteList(this);
|
| 674 | }
|
| 675 |
|
| 676 | boolean playersConverted = false;
|
| 677 |
|
| 678 | for (int var10 = 0; !playersConverted && var10 <= 2; var10++) {
|
| 679 | if (var10 > 0) {
|
| 680 | LOGGER.warn("Encountered a problem while converting the player save files, retrying in a few seconds");
|
| 681 | this.waitForRetry();
|
| 682 | }
|
| 683 |
|
| 684 | playersConverted = OldUsersConverter.convertPlayers(this);
|
| 685 | }
|
| 686 |
|
| 687 | return userBanlistConverted || ipBanlistConverted || opListConverted || whitelistConverted || playersConverted;
|
| 688 | }
|
| 689 |
|
| 690 | private void waitForRetry() {
|
| 691 | try {
|
| 692 | Thread.sleep(5000L);
|
| 693 | } catch (InterruptedException ignored) {
|
| 694 | }
|
| 695 | }
|
| 696 |
|
| 697 | public long getMaxTickLength() {
|
| 698 | return this.getProperties().maxTickTime;
|
| 699 | }
|
| 700 |
|
| 701 | @Override
|
| 702 | public int getMaxChainedNeighborUpdates() {
|
| 703 | return this.getProperties().maxChainedNeighborUpdates;
|
| 704 | }
|
| 705 |
|
| 706 | @Override
|
| 707 | public String getPluginNames() {
|
| 708 | return "";
|
| 709 | }
|
| 710 |
|
| 711 | @Override
|
| 712 | public String runCommand(final String command) {
|
| 713 | this.rconConsoleSource.prepareForCommand();
|
| 714 | this.executeBlocking(() -> this.getCommands().performPrefixedCommand(this.rconConsoleSource.createCommandSourceStack(), command));
|
| 715 | return this.rconConsoleSource.getCommandResponse();
|
| 716 | }
|
| 717 |
|
| 718 | @Override
|
| 719 | protected void stopServer() {
|
| 720 | this.notificationManager().serverShuttingDown();
|
| 721 | super.stopServer();
|
| 722 | Util.shutdownExecutors();
|
| 723 | }
|
| 724 |
|
| 725 | @Override
|
| 726 | public boolean isSingleplayerOwner(final NameAndId nameAndId) {
|
| 727 | return false;
|
| 728 | }
|
| 729 |
|
| 730 | @Override
|
| 731 | public int getScaledTrackingDistance(final int range) {
|
| 732 | return this.entityBroadcastRangePercentage() * range / 100;
|
| 733 | }
|
| 734 |
|
| 735 | public int entityBroadcastRangePercentage() {
|
| 736 | return this.getProperties().entityBroadcastRangePercentage.get();
|
| 737 | }
|
| 738 |
|
| 739 | public void setEntityBroadcastRangePercentage(final int range) {
|
| 740 | this.settings.update(p -> p.entityBroadcastRangePercentage.update(this.registryAccess(), range));
|
| 741 | }
|
| 742 |
|
| 743 | @Override
|
| 744 | public String getLevelIdName() {
|
| 745 | return this.storageSource.getLevelId();
|
| 746 | }
|
| 747 |
|
| 748 | @Override
|
| 749 | public boolean forceSynchronousWrites() {
|
| 750 | return this.settings.getProperties().syncChunkWrites;
|
| 751 | }
|
| 752 |
|
| 753 | @Override
|
| 754 | public TextFilter createTextFilterForPlayer(final ServerPlayer player) {
|
| 755 | return this.serverTextFilter != null ? this.serverTextFilter.createContext(player.getGameProfile()) : TextFilter.DUMMY;
|
| 756 | }
|
| 757 |
|
| 758 | @Override
|
| 759 | public @Nullable GameType getForcedGameType() {
|
| 760 | return this.forceGameMode() ? this.worldData.getGameType() : null;
|
| 761 | }
|
| 762 |
|
| 763 | @Override
|
| 764 | public boolean forceGameMode() {
|
| 765 | return this.settings.getProperties().forceGameMode.get();
|
| 766 | }
|
| 767 |
|
| 768 | @Override
|
| 769 | public void setForceGameMode(final boolean forceGameMode) {
|
| 770 | this.settings.update(p -> p.forceGameMode.update(this.registryAccess(), forceGameMode));
|
| 771 | this.enforceGameTypeForPlayers(this.getForcedGameType());
|
| 772 | }
|
| 773 |
|
| 774 | public GameType gameMode() {
|
| 775 | return this.getProperties().gameMode.get();
|
| 776 | }
|
| 777 |
|
| 778 | public void setGameMode(final GameType gameMode) {
|
| 779 | this.settings.update(p -> p.gameMode.update(this.registryAccess(), gameMode));
|
| 780 | this.worldData.setGameType(this.gameMode());
|
| 781 | this.enforceGameTypeForPlayers(this.getForcedGameType());
|
| 782 | }
|
| 783 |
|
| 784 | @Override
|
| 785 | public Optional<MinecraftServer.ServerResourcePackInfo> getServerResourcePack() {
|
| 786 | return this.settings.getProperties().serverResourcePackInfo;
|
| 787 | }
|
| 788 |
|
| 789 | @Override
|
| 790 | protected void endMetricsRecordingTick() {
|
| 791 | super.endMetricsRecordingTick();
|
| 792 | this.isTickTimeLoggingEnabled = this.debugSubscribers().hasAnySubscriberFor(DebugSubscriptions.DEDICATED_SERVER_TICK_TIME);
|
| 793 | }
|
| 794 |
|
| 795 | @Override
|
| 796 | protected SampleLogger getTickTimeLogger() {
|
| 797 | return this.tickTimeLogger;
|
| 798 | }
|
| 799 |
|
| 800 | @Override
|
| 801 | public boolean isTickTimeLoggingEnabled() {
|
| 802 | return this.isTickTimeLoggingEnabled;
|
| 803 | }
|
| 804 |
|
| 805 | @Override
|
| 806 | public boolean acceptsTransfers() {
|
| 807 | return this.settings.getProperties().acceptsTransfers.get();
|
| 808 | }
|
| 809 |
|
| 810 | public void setAcceptsTransfers(final boolean acceptTransfers) {
|
| 811 | this.settings.update(p -> p.acceptsTransfers.update(this.registryAccess(), acceptTransfers));
|
| 812 | }
|
| 813 |
|
| 814 | @Override
|
| 815 | public ServerLinks serverLinks() {
|
| 816 | return this.serverLinks;
|
| 817 | }
|
| 818 |
|
| 819 | @Override
|
| 820 | public int pauseWhenEmptySeconds() {
|
| 821 | return this.settings.getProperties().pauseWhenEmptySeconds.get();
|
| 822 | }
|
| 823 |
|
| 824 | public void setPauseWhenEmptySeconds(final int seconds) {
|
| 825 | this.settings.update(p -> p.pauseWhenEmptySeconds.update(this.registryAccess(), seconds));
|
| 826 | }
|
| 827 |
|
| 828 | private static ServerLinks createServerLinks(final DedicatedServerSettings settings) {
|
| 829 | Optional<URI> bugReportLink = parseBugReportLink(settings.getProperties());
|
| 830 | return bugReportLink.<ServerLinks>map(bugLink -> new ServerLinks(List.of(ServerLinks.KnownLinkType.BUG_REPORT.create(bugLink)))).orElse(ServerLinks.EMPTY);
|
| 831 | }
|
| 832 |
|
| 833 | private static Optional<URI> parseBugReportLink(final DedicatedServerProperties properties) {
|
| 834 | String bugReportLink = properties.bugReportLink;
|
| 835 | if (bugReportLink.isEmpty()) {
|
| 836 | return Optional.empty();
|
| 837 | }
|
| 838 |
|
| 839 | try {
|
| 840 | return Optional.of(Util.parseAndValidateUntrustedUri(bugReportLink));
|
| 841 | } catch (Exception e) {
|
| 842 | LOGGER.warn("Failed to parse bug link {}", bugReportLink, e);
|
| 843 | return Optional.empty();
|
| 844 | }
|
| 845 | }
|
| 846 |
|
| 847 | @Override
|
| 848 | public Map<String, String> getCodeOfConducts() {
|
| 849 | return this.codeOfConductTexts;
|
| 850 | }
|
| 851 | }
|
| 852 |
|