aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0729-Allow-skipping-writing-of-comments-to-server.propert.patch
blob: 5fd98bcbe37e90f438d92ac74444c1da56707540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Professor Bloodstone <git@bloodstone.dev>
Date: Fri, 23 Jul 2021 02:32:04 +0200
Subject: [PATCH] Allow skipping writing of comments to server.properties

Makes less git noise, as it won't update the date every single time

Use -DPaper.skipServerPropertiesComments=true flag to disable writing it

diff --git a/src/main/java/net/minecraft/server/dedicated/Settings.java b/src/main/java/net/minecraft/server/dedicated/Settings.java
index 745a6735fba8343329ec31723da914fad16ae134..232c305b63df19ea923772192c97de65411531dd 100644
--- a/src/main/java/net/minecraft/server/dedicated/Settings.java
+++ b/src/main/java/net/minecraft/server/dedicated/Settings.java
@@ -1,6 +1,8 @@
 package net.minecraft.server.dedicated;
 
 import com.google.common.base.MoreObjects;
+
+import java.io.BufferedOutputStream; // Paper
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -18,11 +20,13 @@ import org.apache.logging.log4j.Logger;
 
 import joptsimple.OptionSet; // CraftBukkit
 import net.minecraft.core.RegistryAccess;
+import org.jetbrains.annotations.NotNull; // Paper
 
 public abstract class Settings<T extends Settings<T>> {
 
     private static final Logger LOGGER = LogManager.getLogger();
     public final Properties properties;
+    private static final boolean skipComments = Boolean.getBoolean("Paper.skipServerPropertiesComments"); // Paper - allow skipping server.properties comments
     // CraftBukkit start
     private OptionSet options = null;
 
@@ -79,9 +83,47 @@ public abstract class Settings<T extends Settings<T>> {
             }
             // CraftBukkit end
             OutputStream outputstream = Files.newOutputStream(path);
+            // Paper start - disable writing comments to properties file
+            BufferedOutputStream bufferedOutputStream =  !skipComments ? new BufferedOutputStream(outputstream) : new BufferedOutputStream(outputstream) {
+                private boolean isRightAfterNewline = true; // If last written char was newline
+                private boolean isComment = false; // Are we writing comment currently?
+
+                @Override
+                public void write(@NotNull byte[] b) throws IOException {
+                    this.write(b, 0, b.length);
+                }
+
+                @Override
+                public void write(@NotNull byte[] bbuf, int off, int len) throws IOException {
+                    int latest_offset = off; // The latest offset, updated when comment ends
+                    for (int index = off; index < off + len; ++index ) {
+                        byte c = bbuf[index];
+                        boolean isNewline = (c == '\n' || c == '\r');
+                        if (isNewline && isComment) {
+                            // Comment has ended
+                            isComment = false;
+                            latest_offset = index+1;
+                        }
+                        if (c == '#' && isRightAfterNewline) {
+                            isComment = true;
+                            if (index != latest_offset) {
+                                // We got some non-comment data earlier
+                                super.write(bbuf, latest_offset, index-latest_offset);
+                            }
+                        }
+                        isRightAfterNewline = isNewline; // Store for next iteration
+
+                    }
+                    if (latest_offset < off+len && !isComment) {
+                        // We have some unwritten data, that isn't part of a comment
+                        super.write(bbuf, latest_offset, (off + len) - latest_offset);
+                    }
+                }
+            };
+            // Paper end
 
             try {
-                this.properties.store(outputstream, "Minecraft server properties");
+                this.properties.store(bufferedOutputStream, "Minecraft server properties"); // Paper - use bufferedOutputStream
             } catch (Throwable throwable) {
                 if (outputstream != null) {
                     try {