Skip to content

Commit d4a7270

Browse files
committed
Add example yaml value type.
1 parent d385df7 commit d4a7270

5 files changed

Lines changed: 62 additions & 2 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright © 2021, 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;
9+
10+
import com.rezzedup.util.constants.types.TypeCapture;
11+
12+
public interface ExampleYamlValue<V> extends DefaultYamlValue<V>
13+
{
14+
static TypeCapture<ExampleYamlValue<?>> type()
15+
{
16+
return YamlValues.EXAMPLE_TYPE;
17+
}
18+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,7 @@ interface Builder<V>
100100
YamlValue<V> maybe();
101101

102102
DefaultYamlValue<V> defaults(V value);
103+
104+
ExampleYamlValue<V> example(V value);
103105
}
104106
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ final class YamlValues
3030

3131
static final TypeCapture<DefaultYamlValue<?>> DEFAULT_TYPE = new TypeCapture<>() {};
3232

33+
static final TypeCapture<ExampleYamlValue<?>> EXAMPLE_TYPE = new TypeCapture<>() {};
34+
3335
//
3436
// Builders
3537
//
@@ -74,6 +76,12 @@ public DefaultYamlValue<V> defaults(V def)
7476
{
7577
return new DefaultImpl<>(key, accessor, migrations, def);
7678
}
79+
80+
@Override
81+
public ExampleYamlValue<V> example(V example)
82+
{
83+
return new ExampleImpl<>(key, accessor, migrations, example);
84+
}
7785
}
7886

7987
//
@@ -120,4 +128,12 @@ static class DefaultImpl<V> extends MaybeImpl<V> implements DefaultYamlValue<V>
120128
@Override
121129
public V getDefaultValue() { return def; }
122130
}
131+
132+
static class ExampleImpl<V> extends DefaultImpl<V> implements ExampleYamlValue<V>
133+
{
134+
ExampleImpl(String key, YamlAccessor<V> accessor, @NullOr List<Migration> migrations, V example)
135+
{
136+
super(key, accessor, migrations, example);
137+
}
138+
}
123139
}

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package community.leaf.configvalues.bukkit.data;
99

1010
import community.leaf.configvalues.bukkit.DefaultYamlValue;
11+
import community.leaf.configvalues.bukkit.ExampleYamlValue;
1112
import community.leaf.configvalues.bukkit.YamlValue;
1213
import community.leaf.configvalues.bukkit.migrations.Migration;
1314
import org.bukkit.configuration.ConfigurationSection;
@@ -38,6 +39,7 @@ public class YamlDataFile implements UpdatableYamlDataSource
3839
private int reloads = 0;
3940
private boolean isLoaded = false;
4041
private boolean isUpdated = false;
42+
private boolean isNewlyCreated = false;
4143
private @NullOr Exception invalid = null;
4244
private @NullOr Runnable reloadHandler = null;
4345

@@ -68,6 +70,8 @@ public YamlDataFile(Path directoryPath, String name, Consumer<Exception> excepti
6870
@Override
6971
public void updated(boolean state) { this.isUpdated = state; }
7072

73+
public boolean isNewlyCreated() { return isNewlyCreated; }
74+
7175
public boolean isInvalid() { return invalid != null; }
7276

7377
public @NullOr Exception getInvalidReason() { return invalid; }
@@ -107,7 +111,11 @@ public final void reload()
107111

108112
public void save()
109113
{
114+
boolean pre = Files.isRegularFile(getFilePath());
110115
write(getFilePath(), toYamlString(), exceptions);
116+
boolean post = Files.isRegularFile(getFilePath());
117+
118+
this.isNewlyCreated = !pre && post;
111119
}
112120

113121
public void backupThenSave(Path backupsDirectoryPath, String additionalNameInfo)
@@ -127,6 +135,11 @@ public void backupThenSave(Path backupsDirectoryPath, String additionalNameInfo)
127135
save();
128136
}
129137

138+
public void backupThenSave(Path backupsDirectoryPath)
139+
{
140+
backupThenSave(backupsDirectoryPath, "");
141+
}
142+
130143
public String header()
131144
{
132145
@NullOr String header = data.options().header();
@@ -184,8 +197,15 @@ public void defaultValues(List<YamlValue<?>> defaults)
184197
for (YamlValue<?> value : defaults)
185198
{
186199
migrate(value, data);
187-
if (!(value instanceof DefaultYamlValue<?>)) { continue; }
188-
setAsDefaultIfUnset((DefaultYamlValue<?>) value);
200+
201+
if (value instanceof DefaultYamlValue<?>)
202+
{
203+
// Successfully loaded values from disk already, don't set example value.
204+
// Examples should only be set when the file is created for the first time.
205+
if (value instanceof ExampleYamlValue<?> && isLoaded) { continue; }
206+
207+
setAsDefaultIfUnset((DefaultYamlValue<?>) value);
208+
}
189209
}
190210
}
191211

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.rezzedup.util.constants.Aggregates;
44
import com.rezzedup.util.constants.annotations.AggregatedResult;
55
import community.leaf.configvalues.bukkit.DefaultYamlValue;
6+
import community.leaf.configvalues.bukkit.ExampleYamlValue;
67
import community.leaf.configvalues.bukkit.migrations.Migration;
78
import community.leaf.configvalues.bukkit.YamlValue;
89
import community.leaf.configvalues.bukkit.data.YamlDataFile;
@@ -25,6 +26,9 @@ public class Config extends YamlDataFile
2526
)
2627
.defaults("Hello world.");
2728

29+
public static final ExampleYamlValue<String> EXAMPLE_MESSAGE =
30+
YamlValue.ofString("messages.example").example("I only get set once!");
31+
2832
@AggregatedResult
2933
private static final List<YamlValue<?>> VALUES = Aggregates.list(Config.class, YamlValue.type());
3034

0 commit comments

Comments
 (0)