Skip to content

Commit d7526ca

Browse files
committed
Added paper module
1 parent 9cbef55 commit d7526ca

25 files changed

Lines changed: 522 additions & 46 deletions

.idea/misc.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle.kts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import kotlin.io.path.Path
55
plugins {
66
java
77
`maven-publish`
8+
`java-library`
89
id("io.freefair.lombok") version "8.12.1"
910
id("com.gradleup.shadow") version "9.0.0-beta8"
1011
}
@@ -23,14 +24,24 @@ allprojects {
2324

2425
subprojects {
2526
apply(plugin = "java")
27+
apply(plugin = "java-library")
2628
apply(plugin = "io.freefair.lombok")
2729
apply(plugin = "maven-publish")
2830
apply(plugin = "com.gradleup.shadow")
2931

30-
java {
31-
sourceCompatibility = JavaVersion.VERSION_1_8
32-
targetCompatibility = JavaVersion.VERSION_1_8
33-
withSourcesJar()
32+
33+
if (project.name == "paper") {
34+
java {
35+
sourceCompatibility = JavaVersion.VERSION_21
36+
targetCompatibility = JavaVersion.VERSION_21
37+
withSourcesJar()
38+
}
39+
} else {
40+
java {
41+
sourceCompatibility = JavaVersion.VERSION_17
42+
targetCompatibility = JavaVersion.VERSION_17
43+
withSourcesJar()
44+
}
3445
}
3546

3647
tasks {

core/build.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
plugins {
2+
`java-library`
3+
}
14

25
dependencies {
3-
implementation("dev.dejvokep:boosted-yaml:1.3.7")
6+
api("dev.dejvokep:boosted-yaml:1.3.7")
47
}

core/src/main/java/net/j4c0b3y/api/config/StaticConfig.java

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import net.j4c0b3y.api.config.provider.context.SaveContext;
1212
import net.j4c0b3y.api.config.utils.ClassUtils;
1313
import net.j4c0b3y.api.config.utils.Pair;
14+
import org.jetbrains.annotations.NotNull;
15+
import org.jetbrains.annotations.Nullable;
1416

1517
import java.io.File;
1618
import java.io.IOException;
@@ -22,6 +24,7 @@
2224
import java.lang.reflect.AnnotatedElement;
2325
import java.lang.reflect.Field;
2426
import java.lang.reflect.Modifier;
27+
import java.nio.file.Path;
2528
import java.util.*;
2629

2730
/**
@@ -83,7 +86,7 @@ public abstract class StaticConfig {
8386
* @param defaults The optional default resource to be loaded from.
8487
* @param handler The config handler to use for config settings.
8588
*/
86-
public StaticConfig(File file, InputStream defaults, ConfigHandler handler) {
89+
public StaticConfig(@NotNull File file, @Nullable InputStream defaults, @NotNull ConfigHandler handler) {
8790
this.file = file;
8891
this.handler = handler;
8992

@@ -103,10 +106,32 @@ public StaticConfig(File file, InputStream defaults, ConfigHandler handler) {
103106
* @param handler The config handler to use for config settings.
104107
*/
105108
@SuppressWarnings("unused")
106-
public StaticConfig(File file, ConfigHandler handler) {
109+
public StaticConfig(@NotNull File file, @NotNull ConfigHandler handler) {
107110
this(file, null, handler);
108111
}
109112

113+
/**
114+
* Creates a new static config, registers it to the config handler.
115+
*
116+
* @param path The path reference to the file to be used for the configuration document.
117+
* @param defaults The optional default resource to be loaded from.
118+
* @param handler The config handler to use for config settings.
119+
*/
120+
public StaticConfig(@NotNull Path path, @Nullable InputStream defaults, @NotNull ConfigHandler handler) {
121+
this(path.toFile(), defaults, handler);
122+
}
123+
124+
/**
125+
* Creates a new static config, registers it to the config handler.
126+
*
127+
* @param path The path reference to the file to be used for the configuration document.
128+
* @param handler The config handler to use for config settings.
129+
*/
130+
@SuppressWarnings("unused")
131+
public StaticConfig(@NotNull Path path, @NotNull ConfigHandler handler) {
132+
this(path.toFile(), null, handler);
133+
}
134+
110135
/**
111136
* @return The header to be shown at the top of the document file.
112137
*/
@@ -127,6 +152,13 @@ public List<String> getFooter() {
127152
return Arrays.asList(footer.value());
128153
}
129154

155+
public final void initialize() {
156+
this.load();
157+
this.save();
158+
}
159+
160+
public void afterLoad() {}
161+
130162
/**
131163
* Creates, relocates and loads the document keys into the static fields.
132164
*/
@@ -136,12 +168,12 @@ public void load() {
136168
document.reload();
137169

138170
// Perform all registered value relocations.
139-
relocate();
171+
this.relocate();
140172

141173
// If fields are empty, perform the initialization.
142174
if (sections.isEmpty()) {
143175
sections.put("", new LinkedHashSet<>());
144-
initialize(getClass(), "");
176+
this.initialize(getClass(), "");
145177
}
146178

147179
// Load and set each field value from the document.
@@ -167,13 +199,6 @@ public void load() {
167199
}
168200
}
169201

170-
// Save the field values to the config document.
171-
save();
172-
173-
// If configured, we should format values.
174-
if (handler.isFormatValues()) {
175-
format();
176-
}
177202
} catch (Exception exception) {
178203
// Load was unsuccessful, prevent saving.
179204
success = false;
@@ -182,6 +207,8 @@ public void load() {
182207

183208
// Load was successful, allow saving.
184209
success = true;
210+
211+
this.afterLoad();
185212
}
186213

187214
/**
@@ -209,10 +236,15 @@ public void save() {
209236
document.wipeComments(document);
210237

211238
// Recurse through the static fields, setting the values in the document.
212-
step("");
239+
this.step("");
213240

214241
// Set the additional custom comments specified by the user.
215-
setComments();
242+
this.setComments();
243+
244+
// If configured, we should format values.
245+
if (handler.isFormatValues()) {
246+
this.format();
247+
}
216248

217249
// Save the document values and comments to file.
218250
document.save();
@@ -285,8 +317,7 @@ private void step(String path) throws ReflectiveOperationException {
285317
for (AnnotatedElement member : sections.get(path)) {
286318
String route = getRoute(member, path);
287319

288-
if (member instanceof Field) {
289-
Field field = (Field) member;
320+
if (member instanceof Field field) {
290321

291322
boolean hidden = field.isAnnotationPresent(Hidden.class);
292323
boolean present = document.contains(route);
@@ -329,8 +360,9 @@ private void step(String path) throws ReflectiveOperationException {
329360

330361
/**
331362
* Formats the values in the config file.
363+
* This does not save the document
332364
*/
333-
public void format() throws IOException {
365+
protected void format() throws IOException {
334366
try {
335367
boolean removed = false;
336368

@@ -380,8 +412,6 @@ public void format() throws IOException {
380412
);
381413
}
382414

383-
// Save the formatted values to file.
384-
document.save();
385415
} catch (Exception exception) {
386416
throw new IOException("Format failed for file '" + file.getName() + "'.", exception);
387417
}

core/src/main/java/net/j4c0b3y/api/config/provider/TypeProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import net.j4c0b3y.api.config.provider.context.LoadContext;
44
import net.j4c0b3y.api.config.provider.context.SaveContext;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
57

68
/**
79
* Used for (de)serialization between the document and static fields.
@@ -17,13 +19,13 @@ public interface TypeProvider<T> {
1719
* @param context The load context, containing the object.
1820
* @return The deserialized type provider generic instance.
1921
*/
20-
T load(LoadContext context);
22+
@NotNull T load(@NotNull LoadContext context);
2123

2224
/**
2325
* Serializes the type provider's generic class instance to a yaml object.
2426
*
2527
* @param context The generic save context, containing the instance.
2628
* @return The serialized yaml object.
2729
*/
28-
Object save(SaveContext<T> context);
30+
@Nullable Object save(@NotNull SaveContext<T> context);
2931
}

core/src/main/java/net/j4c0b3y/api/config/provider/impl/BooleanProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.j4c0b3y.api.config.provider.TypeProvider;
44
import net.j4c0b3y.api.config.provider.context.LoadContext;
55
import net.j4c0b3y.api.config.provider.context.SaveContext;
6+
import org.jetbrains.annotations.NotNull;
67

78
import java.util.Arrays;
89
import java.util.List;
@@ -26,7 +27,7 @@ public class BooleanProvider implements TypeProvider<Boolean> {
2627
private final static List<String> FALSE_VALUES = Arrays.asList("false", "no", "0");
2728

2829
@Override
29-
public Boolean load(LoadContext context) {
30+
public @NotNull Boolean load(@NotNull LoadContext context) {
3031
String lowered = String.valueOf(context.getObject()).toLowerCase();
3132

3233
// Return true if the lowered string is a true value.
@@ -48,7 +49,7 @@ public Boolean load(LoadContext context) {
4849
}
4950

5051
@Override
51-
public Object save(SaveContext<Boolean> context) {
52+
public @NotNull Object save(@NotNull SaveContext<Boolean> context) {
5253
return context.getObject();
5354
}
5455
}

core/src/main/java/net/j4c0b3y/api/config/provider/impl/CollectionProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.j4c0b3y.api.config.provider.context.LoadContext;
77
import net.j4c0b3y.api.config.provider.context.SaveContext;
88
import net.j4c0b3y.api.config.utils.ClassUtils;
9+
import org.jetbrains.annotations.NotNull;
910

1011
import java.lang.reflect.Field;
1112
import java.util.ArrayList;
@@ -55,7 +56,7 @@ public CollectionProvider(Field field, Supplier<T> supplier, Class<T> ignored, C
5556
}
5657

5758
@Override
58-
public T load(LoadContext context) {
59+
public @NotNull T load(@NotNull LoadContext context) {
5960
// Get the provider associated with the generic.
6061
TypeProvider<E> provider = handler.provide(generic);
6162

@@ -82,7 +83,7 @@ public T load(LoadContext context) {
8283
}
8384

8485
@Override
85-
public Object save(SaveContext<T> context) {
86+
public @NotNull Object save(@NotNull SaveContext<T> context) {
8687
// Get the provider associated with the generic.
8788
TypeProvider<E> provider = handler.provide(generic);
8889

core/src/main/java/net/j4c0b3y/api/config/provider/impl/EnumProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.j4c0b3y.api.config.provider.TypeProvider;
55
import net.j4c0b3y.api.config.provider.context.LoadContext;
66
import net.j4c0b3y.api.config.provider.context.SaveContext;
7+
import org.jetbrains.annotations.NotNull;
78

89
/**
910
* Used for providing enum values.
@@ -20,13 +21,13 @@ public class EnumProvider<T extends Enum<T>> implements TypeProvider<T> {
2021
private final Class<T> type;
2122

2223
@Override
23-
public T load(LoadContext context) {
24+
public @NotNull T load(@NotNull LoadContext context) {
2425
// Load using the context string, transformed to upper case.
2526
return Enum.valueOf(type, String.valueOf(context.getObject()).toUpperCase());
2627
}
2728

2829
@Override
29-
public Object save(SaveContext<T> context) {
30+
public @NotNull Object save(@NotNull SaveContext<T> context) {
3031
// Save using the enum name.
3132
return context.getObject().name();
3233
}

core/src/main/java/net/j4c0b3y/api/config/provider/impl/MapProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.j4c0b3y.api.config.provider.TypeProvider;
77
import net.j4c0b3y.api.config.provider.context.LoadContext;
88
import net.j4c0b3y.api.config.provider.context.SaveContext;
9+
import org.jetbrains.annotations.NotNull;
910

1011
import java.util.LinkedHashMap;
1112
import java.util.Map;
@@ -31,14 +32,12 @@ public class MapProvider<E, T extends Map<String, E>> implements TypeProvider<T>
3132

3233
@Override
3334
@SuppressWarnings("unchecked")
34-
public T load(LoadContext context) {
35+
public @NotNull T load(@NotNull LoadContext context) {
3536
// Throw if the map is not a section instance.
36-
if (!(context.getObject() instanceof Section)) {
37+
if (!(context.getObject() instanceof Section section)) {
3738
throw new IllegalArgumentException("Mapped route must resolve to section.");
3839
}
3940

40-
Section section = (Section) context.getObject();
41-
4241
// An ordered map for our deserialized generic entries.
4342
Map<String, E> entries = new LinkedHashMap<>();
4443

@@ -54,7 +53,7 @@ public T load(LoadContext context) {
5453
}
5554

5655
@Override
57-
public Object save(SaveContext<T> saveContext) {
56+
public @NotNull Object save(@NotNull SaveContext<T> saveContext) {
5857
// An ordered map for our serialized yaml object entries.
5958
Map<String, Object> entries = new LinkedHashMap<>();
6059
TypeProvider<E> provider = handler.provide(generic);

core/src/main/java/net/j4c0b3y/api/config/provider/impl/MessageProvider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.j4c0b3y.api.config.provider.TypeProvider;
55
import net.j4c0b3y.api.config.provider.context.LoadContext;
66
import net.j4c0b3y.api.config.provider.context.SaveContext;
7+
import org.jetbrains.annotations.NotNull;
78

89
import java.util.ArrayList;
910
import java.util.Collection;
@@ -19,7 +20,7 @@
1920
public class MessageProvider implements TypeProvider<Message> {
2021

2122
@Override
22-
public Message load(LoadContext context) {
23+
public @NotNull Message load(@NotNull LoadContext context) {
2324
// If the object is a collection.
2425
if (context.getObject() instanceof Collection) {
2526
List<String> parsed = new ArrayList<>();
@@ -39,12 +40,12 @@ public Message load(LoadContext context) {
3940
}
4041

4142
@Override
42-
public Object save(SaveContext<Message> context) {
43+
public @NotNull Object save(@NotNull SaveContext<Message> context) {
4344
Message message = context.getObject();
4445

4546
// If the message is one line, return the first line as a string.
4647
if (message.getLines().size() == 1) {
47-
return message.getLines().get(0);
48+
return message.getLines().getFirst();
4849
}
4950

5051
// If the message is multi-line, return all lines as a list.

0 commit comments

Comments
 (0)