Skip to content

Commit 055d08c

Browse files
committed
Add support for comments. Bug fixes.
1 parent 3cf1626 commit 055d08c

7 files changed

Lines changed: 137 additions & 25 deletions

File tree

.run/clean package.run.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="clean package" type="MavenRunConfiguration" factoryName="Maven">
3+
<MavenSettings>
4+
<option name="myGeneralSettings" />
5+
<option name="myRunnerSettings" />
6+
<option name="myRunnerParameters">
7+
<MavenRunnerParameters>
8+
<option name="profiles">
9+
<set />
10+
</option>
11+
<option name="goals">
12+
<list>
13+
<option value="clean" />
14+
<option value="package" />
15+
</list>
16+
</option>
17+
<option name="pomFileName" />
18+
<option name="profilesMap">
19+
<map />
20+
</option>
21+
<option name="resolveToWorkspace" value="false" />
22+
<option name="workingDirPath" value="$PROJECT_DIR$" />
23+
</MavenRunnerParameters>
24+
</option>
25+
</MavenSettings>
26+
<method v="2" />
27+
</configuration>
28+
</component>

config-values-bukkit/src/main/java/community/leaf/configvalues/bukkit/YamlValue.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ static Builder<Instant> ofInstant(String key)
9393

9494
List<Migration> migrations();
9595

96+
List<String> comments();
97+
9698
interface Builder<V>
9799
{
98100
Builder<V> migrates(Migration ... migrations);
99101

102+
Builder<V> comments(String ... lines);
103+
100104
YamlValue<V> maybe();
101105

102106
DefaultYamlValue<V> defaults(V value);

config-values-bukkit/src/main/java/community/leaf/configvalues/bukkit/YamlValues.java

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static <V> YamlValue.Builder<V> builder(String key, YamlAccessor<V> accessor)
4444
static class BuilderImpl<V> implements YamlValue.Builder<V>
4545
{
4646
@NullOr List<Migration> migrations = null;
47+
@NullOr List<String> comments = null;
4748

4849
final String key;
4950
final YamlAccessor<V> accessor;
@@ -64,23 +65,34 @@ public YamlValue.Builder<V> migrates(Migration ... policies)
6465

6566
return this;
6667
}
67-
68+
69+
@Override
70+
public YamlValue.Builder<V> comments(String ... lines)
71+
{
72+
Objects.requireNonNull(lines, "lines");
73+
74+
if (comments == null) { comments = new ArrayList<>(); }
75+
Collections.addAll(comments, lines);
76+
77+
return this;
78+
}
79+
6880
@Override
6981
public YamlValue<V> maybe()
7082
{
71-
return new MaybeImpl<>(key, accessor, migrations);
83+
return new MaybeImpl<>(key, accessor, migrations, comments);
7284
}
7385

7486
@Override
7587
public DefaultYamlValue<V> defaults(V def)
7688
{
77-
return new DefaultImpl<>(key, accessor, migrations, def);
89+
return new DefaultImpl<>(key, accessor, migrations, comments, def);
7890
}
79-
91+
8092
@Override
8193
public ExampleYamlValue<V> example(V example)
8294
{
83-
return new ExampleImpl<>(key, accessor, migrations, example);
95+
return new ExampleImpl<>(key, accessor, migrations, comments, example);
8496
}
8597
}
8698

@@ -93,20 +105,30 @@ static class MaybeImpl<V> implements YamlValue<V>
93105
final String key;
94106
final YamlAccessor<V> accessor;
95107
final List<Migration> migrations;
96-
97-
MaybeImpl(String key, YamlAccessor<V> accessor, @NullOr List<Migration> migrations)
108+
final List<String> comments;
109+
110+
MaybeImpl(
111+
String key,
112+
YamlAccessor<V> accessor,
113+
@NullOr List<Migration> migrations,
114+
@NullOr List<String> comments
115+
)
98116
{
99117
this.key = key;
100118
this.accessor = accessor;
101119
this.migrations = (migrations == null) ? List.of() : List.copyOf(migrations);
120+
this.comments = (comments == null) ? List.of() : List.copyOf(comments);
102121
}
103-
122+
104123
@Override
105124
public String key() { return key; }
106-
125+
107126
@Override
108127
public List<Migration> migrations() { return migrations; }
109128

129+
@Override
130+
public List<String> comments() { return comments; }
131+
110132
@Override
111133
public Optional<V> get(ConfigurationSection storage) { return accessor.get(storage, key); }
112134

@@ -117,10 +139,16 @@ static class MaybeImpl<V> implements YamlValue<V>
117139
static class DefaultImpl<V> extends MaybeImpl<V> implements DefaultYamlValue<V>
118140
{
119141
V def;
120-
121-
DefaultImpl(String key, YamlAccessor<V> accessor, @NullOr List<Migration> migrations, V def)
142+
143+
DefaultImpl(
144+
String key,
145+
YamlAccessor<V> accessor,
146+
@NullOr List<Migration> migrations,
147+
@NullOr List<String> comments,
148+
V def
149+
)
122150
{
123-
super(key, accessor, migrations);
151+
super(key, accessor, migrations, comments);
124152
this.def = Objects.requireNonNull(def, "def");
125153
}
126154

@@ -131,9 +159,15 @@ static class DefaultImpl<V> extends MaybeImpl<V> implements DefaultYamlValue<V>
131159

132160
static class ExampleImpl<V> extends DefaultImpl<V> implements ExampleYamlValue<V>
133161
{
134-
ExampleImpl(String key, YamlAccessor<V> accessor, @NullOr List<Migration> migrations, V example)
162+
ExampleImpl(
163+
String key,
164+
YamlAccessor<V> accessor,
165+
@NullOr List<Migration> migrations,
166+
@NullOr List<String> comments,
167+
V example
168+
)
135169
{
136-
super(key, accessor, migrations, example);
170+
super(key, accessor, migrations, comments, example);
137171
}
138172
}
139173
}

config-values-bukkit/src/main/java/community/leaf/configvalues/bukkit/data/YamlDataFile.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import community.leaf.configvalues.bukkit.ExampleYamlValue;
1212
import community.leaf.configvalues.bukkit.YamlValue;
1313
import community.leaf.configvalues.bukkit.migrations.Migration;
14+
import community.leaf.configvalues.bukkit.util.Comments;
1415
import org.bukkit.configuration.ConfigurationSection;
1516
import org.bukkit.configuration.InvalidConfigurationException;
1617
import org.bukkit.configuration.file.YamlConfiguration;
@@ -110,6 +111,11 @@ public final void reload()
110111
exceptions.accept(e);
111112
}
112113
}
114+
else
115+
{
116+
// Nothing to load, so... it's loaded!
117+
isLoaded = true;
118+
}
113119

114120
if (reloadHandler != null) { reloadHandler.run(); }
115121
}
@@ -147,12 +153,14 @@ public void backupThenSave(Path backupsDirectoryPath)
147153
backupThenSave(backupsDirectoryPath, "");
148154
}
149155

156+
@SuppressWarnings("deprecation")
150157
public String header()
151158
{
152159
@NullOr String header = data.options().header();
153160
return (header != null) ? header : "";
154161
}
155162

163+
@SuppressWarnings("deprecation")
156164
public void header(String header)
157165
{
158166
data.options().header(header);
@@ -213,6 +221,9 @@ public void defaultValues(List<YamlValue<?>> defaults)
213221

214222
setAsDefaultIfUnset((DefaultYamlValue<?>) value);
215223
}
224+
225+
List<String> comments = value.comments();
226+
Comments.set(data, value.key(), (comments.isEmpty()) ? null : comments);
216227
}
217228
}
218229

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright © 2021-2022, RezzedUp <https://github.com/LeafCommunity/ConfigValues>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package community.leaf.configvalues.bukkit.util;
9+
10+
import org.bukkit.configuration.ConfigurationSection;
11+
import org.bukkit.configuration.file.YamlConfiguration;
12+
import pl.tlinkowski.annotation.basic.NullOr;
13+
14+
import java.util.List;
15+
16+
public class Comments
17+
{
18+
private static boolean SUPPORTED = false;
19+
20+
static
21+
{
22+
try
23+
{
24+
ConfigurationSection section = new YamlConfiguration();
25+
section.setComments("test", List.of("test"));
26+
SUPPORTED = true;
27+
}
28+
catch (NoSuchMethodError ignored) {}
29+
}
30+
31+
public static boolean isSupported() { return SUPPORTED; }
32+
33+
public static void set(ConfigurationSection section, String key, @NullOr List<String> lines)
34+
{
35+
if (SUPPORTED) { section.setComments(key, lines); }
36+
}
37+
}

examples/config-values-example-bukkit/src/main/java/community/leaf/examples/configvalues/bukkit/Config.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.rezzedup.util.constants.annotations.AggregatedResult;
55
import community.leaf.configvalues.bukkit.DefaultYamlValue;
66
import community.leaf.configvalues.bukkit.ExampleYamlValue;
7+
import community.leaf.configvalues.bukkit.data.Load;
78
import community.leaf.configvalues.bukkit.migrations.Migration;
89
import community.leaf.configvalues.bukkit.YamlValue;
910
import community.leaf.configvalues.bukkit.data.YamlDataFile;
@@ -16,6 +17,7 @@ public class Config extends YamlDataFile
1617

1718
public static final DefaultYamlValue<String> JOIN_MESSAGE =
1819
YamlValue.ofString("messages.join")
20+
.comments("A message to send when a player joins the game.")
1921
.defaults("Hey there %player%!\nWelcome back.");
2022

2123
public static final DefaultYamlValue<String> HELLO_MESSAGE =
@@ -24,6 +26,7 @@ public class Config extends YamlDataFile
2426
Migration.move("messages.hi"),
2527
Migration.move("messages.hello")
2628
)
29+
.comments("Greet the world!")
2730
.defaults("Hello world.");
2831

2932
public static final ExampleYamlValue<String> EXAMPLE_MESSAGE =
@@ -35,10 +38,14 @@ public class Config extends YamlDataFile
3538

3639
public Config(ExampleConfigPlugin plugin)
3740
{
38-
super(plugin.directory(), "config.yml");
41+
super(plugin.directory(), "config.yml", Load.NOW);
42+
43+
plugin.getLogger().info("Instantiated config.");
3944

4045
reloadsWith(() ->
4146
{
47+
plugin.getLogger().info("Reloading config...");
48+
4249
if (isInvalid()) { return; }
4350

4451
String configVersion = get(VERSION).orElse("");

pom.xml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,9 @@
5555
<license.header.url>https://github.com/LeafCommunity/ConfigValues</license.header.url>
5656

5757
<!-- Dependency versions -->
58-
<versions.minecraft.spigot>1.17.1-R0.1-SNAPSHOT</versions.minecraft.spigot>
58+
<versions.minecraft.spigot>1.19-R0.1-SNAPSHOT</versions.minecraft.spigot>
5959
</properties>
6060

61-
<!--
62-
<repositories>
63-
<repository>
64-
<id>ossrh-snapshots</id>
65-
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
66-
</repository>
67-
</repositories>
68-
-->
69-
7061
<dependencies>
7162
<!-- Nullness Annotations: @NullOr (via maven-central) -->
7263
<dependency>

0 commit comments

Comments
 (0)