Skip to content

Commit 1723aa4

Browse files
authored
refactor: Move to ConfigLib, use lombok (#563)
* refactor: move to configlib, adjust config structure * fix: huskclaims typos * fix: don't generate `server.yml` in local mode * fix: NPE with server name
1 parent c55631a commit 1723aa4

File tree

84 files changed

+4339
-4837
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+4339
-4837
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ allprojects {
9393
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2'
9494
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.2'
9595
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.10.2'
96+
97+
testCompileOnly 'org.jetbrains:annotations:24.1.0'
9698
}
9799

98100
license {

bukkit/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ dependencies {
1010
compileOnly 'org.spigotmc:spigot-api:1.17.1-R0.1-SNAPSHOT'
1111
compileOnly 'org.jetbrains:annotations:24.1.0'
1212
compileOnly 'de.themoep:minedown-adventure:1.7.2-SNAPSHOT'
13-
compileOnly 'net.william278:annotaml:2.0.5'
1413
compileOnly 'net.william278:DesertWell:2.0.4'
1514
compileOnly 'com.github.MilkBowl:VaultAPI:1.7.1'
1615
compileOnly 'me.clip:placeholderapi:2.11.5'
1716
compileOnly('net.essentialsx:EssentialsX:2.20.1') {
1817
exclude group: 'org.spigotmc', module: 'spigot-api'
1918
}
19+
compileOnly 'org.projectlombok:lombok:1.18.30'
2020

2121
testImplementation 'com.github.seeseemelk:MockBukkit-v1.17:1.13.0'
2222
testImplementation 'de.themoep:minedown-adventure:1.7.2-SNAPSHOT'
@@ -25,6 +25,8 @@ dependencies {
2525
testImplementation "redis.clients:jedis:${jedis_version}"
2626
testImplementation "org.xerial:sqlite-jdbc:${sqlite_driver_version}"
2727
testImplementation "com.mysql:mysql-connector-j:${mysql_driver_version}"
28+
29+
annotationProcessor 'org.projectlombok:lombok:1.18.30'
2830
}
2931

3032
shadowJar {
@@ -39,11 +41,10 @@ shadowJar {
3941
relocate 'org.jetbrains', 'net.william278.huskhomes.libraries'
4042
relocate 'org.intellij', 'net.william278.huskhomes.libraries'
4143
relocate 'com.zaxxer', 'net.william278.huskhomes.libraries'
42-
relocate 'net.william278.annotaml', 'net.william278.huskhomes.libraries.annotaml'
4344
relocate 'net.william278.paginedown', 'net.william278.huskhomes.libraries.paginedown'
4445
relocate 'net.william278.desertwell', 'net.william278.huskhomes.libraries.desertwell'
46+
relocate 'de.exlll', 'net.william278.huskhomes.libraries'
4547
relocate 'org.json', 'net.william278.huskhomes.libraries.json'
46-
relocate 'dev.dejvokep.boostedyaml', 'net.william278.huskhomes.libraries.boostedyaml'
4748
relocate 'org.yaml.snakeyaml', 'net.william278.huskhomes.libraries.snakeyaml'
4849
relocate 'com.google.gson', 'net.william278.huskhomes.libraries.gson'
4950
relocate 'org.bstats', 'net.william278.huskhomes.libraries.bstats'

bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java

Lines changed: 39 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919

2020
package net.william278.huskhomes;
2121

22+
import com.google.common.collect.Maps;
23+
import com.google.common.collect.Sets;
24+
import lombok.AccessLevel;
25+
import lombok.Getter;
26+
import lombok.NoArgsConstructor;
27+
import lombok.Setter;
2228
import net.kyori.adventure.audience.Audience;
23-
import net.kyori.adventure.platform.AudienceProvider;
2429
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
25-
import net.william278.annotaml.Annotaml;
2630
import net.william278.desertwell.util.Version;
2731
import net.william278.huskhomes.api.HuskHomesAPI;
2832
import net.william278.huskhomes.command.BukkitCommand;
@@ -46,7 +50,6 @@
4650
import net.william278.huskhomes.network.Broker;
4751
import net.william278.huskhomes.network.PluginMessageBroker;
4852
import net.william278.huskhomes.network.RedisBroker;
49-
import net.william278.huskhomes.position.Location;
5053
import net.william278.huskhomes.position.World;
5154
import net.william278.huskhomes.random.NormalDistributionEngine;
5255
import net.william278.huskhomes.random.RandomTeleportEngine;
@@ -71,12 +74,14 @@
7174
import space.arim.morepaperlib.scheduling.GracefulScheduling;
7275

7376
import java.io.File;
74-
import java.io.IOException;
75-
import java.lang.reflect.InvocationTargetException;
77+
import java.nio.file.Path;
7678
import java.util.*;
7779
import java.util.logging.Level;
7880
import java.util.stream.Collectors;
7981

82+
@Getter
83+
@Setter
84+
@NoArgsConstructor
8085
public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask.Supplier, BukkitEventDispatcher,
8186
PluginMessageListener, BukkitSafetyResolver {
8287

@@ -85,7 +90,10 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
8590
*/
8691
private static final int METRICS_ID = 8430;
8792

88-
private Set<SavedUser> savedUsers;
93+
private final Set<SavedUser> savedUsers = Sets.newHashSet();
94+
private final Map<String, List<String>> globalPlayerList = Maps.newConcurrentMap();
95+
private final Set<UUID> currentlyOnWarmup = Sets.newConcurrentHashSet();
96+
8997
private Settings settings;
9098
private Locales locales;
9199
private Database database;
@@ -97,19 +105,15 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
97105
private UnsafeBlocks unsafeBlocks;
98106
private List<Hook> hooks;
99107
private List<Command> commands;
100-
private Map<String, List<String>> globalPlayerList;
101-
private Set<UUID> currentlyOnWarmup;
108+
@Getter(AccessLevel.NONE)
102109
private Server server;
110+
@Getter(AccessLevel.NONE)
103111
private BukkitAudiences audiences;
112+
@Getter(AccessLevel.NONE)
104113
private MorePaperLib paperLib;
105114
@Nullable
106115
private Broker broker;
107116

108-
// Default public constructor
109-
public BukkitHuskHomes() {
110-
super();
111-
}
112-
113117
// Super constructor for unit testing
114118
@TestOnly
115119
protected BukkitHuskHomes(@NotNull JavaPluginLoader loader, @NotNull PluginDescriptionFile description,
@@ -122,21 +126,15 @@ public void onEnable() {
122126
// Create adventure audience
123127
this.audiences = BukkitAudiences.create(this);
124128
this.paperLib = new MorePaperLib(this);
125-
this.savedUsers = new HashSet<>();
126-
this.globalPlayerList = new HashMap<>();
127-
this.currentlyOnWarmup = new HashSet<>();
128129
this.validator = new Validator(this);
129130

130131
// Load settings and locales
131-
initialize("plugin config & locale files", (plugin) -> {
132-
if (!loadConfigs()) {
133-
throw new IllegalStateException("Failed to load config files. Please check the console for errors");
134-
}
135-
});
132+
initialize("plugin config & locale files", (plugin) -> loadConfigs());
136133

137134
// Initialize the database
138-
initialize(getSettings().getDatabaseType().getDisplayName() + " database connection", (plugin) -> {
139-
this.database = switch (getSettings().getDatabaseType()) {
135+
final Database.Type type = getSettings().getDatabase().getType();
136+
initialize(type.getDisplayName() + " database connection", (plugin) -> {
137+
this.database = switch (type) {
140138
case MYSQL, MARIADB -> new MySqlDatabase(this);
141139
case SQLITE -> new SqLiteDatabase(this);
142140
case H2 -> new H2Database(this);
@@ -149,9 +147,10 @@ public void onEnable() {
149147
this.manager = new Manager(this);
150148

151149
// Initialize the network messenger if proxy mode is enabled
152-
if (getSettings().doCrossServer()) {
153-
initialize(settings.getBrokerType().getDisplayName() + " message broker", (plugin) -> {
154-
broker = switch (settings.getBrokerType()) {
150+
final Settings.CrossServerSettings crossServer = getSettings().getCrossServer();
151+
if (crossServer.isEnabled()) {
152+
initialize(crossServer.getBrokerType().getDisplayName() + " message broker", (plugin) -> {
153+
broker = switch (crossServer.getBrokerType()) {
155154
case PLUGIN_MESSAGE -> new PluginMessageBroker(this);
156155
case REDIS -> new RedisBroker(this);
157156
};
@@ -205,7 +204,7 @@ public void registerHooks() {
205204
HuskHomes.super.registerHooks();
206205

207206
// Hooks
208-
if (getSettings().doEconomy() && isDependencyLoaded("Vault")) {
207+
if (getSettings().getEconomy().isEnabled() && isDependencyLoaded("Vault")) {
209208
getHooks().add(new VaultEconomyHook(this));
210209
}
211210
if (isDependencyLoaded("PlaceholderAPI")) {
@@ -267,52 +266,6 @@ public Audience getAudience(@NotNull UUID user) {
267266
return audiences.player(user);
268267
}
269268

270-
@NotNull
271-
@Override
272-
public Set<SavedUser> getSavedUsers() {
273-
return savedUsers;
274-
}
275-
276-
@NotNull
277-
@Override
278-
public Settings getSettings() {
279-
return settings;
280-
}
281-
282-
@Override
283-
public void setSettings(@NotNull Settings settings) {
284-
this.settings = settings;
285-
}
286-
287-
@NotNull
288-
@Override
289-
public Locales getLocales() {
290-
return locales;
291-
}
292-
293-
@Override
294-
public void setLocales(@NotNull Locales locales) {
295-
this.locales = locales;
296-
}
297-
298-
@Override
299-
@NotNull
300-
public Database getDatabase() {
301-
return database;
302-
}
303-
304-
@Override
305-
@NotNull
306-
public Validator getValidator() {
307-
return validator;
308-
}
309-
310-
@NotNull
311-
@Override
312-
public Manager getManager() {
313-
return manager;
314-
}
315-
316269
@NotNull
317270
@Override
318271
public Broker getMessenger() {
@@ -322,17 +275,6 @@ public Broker getMessenger() {
322275
return broker;
323276
}
324277

325-
@NotNull
326-
@Override
327-
public RandomTeleportEngine getRandomTeleportEngine() {
328-
return randomTeleportEngine;
329-
}
330-
331-
@Override
332-
public void setRandomTeleportEngine(@NotNull RandomTeleportEngine randomTeleportEngine) {
333-
this.randomTeleportEngine = randomTeleportEngine;
334-
}
335-
336278
@Override
337279
public Optional<Spawn> getServerSpawn() {
338280
return Optional.ofNullable(serverSpawn);
@@ -343,81 +285,27 @@ public void setServerSpawn(@NotNull Spawn spawn) {
343285
this.serverSpawn = spawn;
344286
}
345287

346-
@Override
347-
public void setServerSpawn(@NotNull Location location) {
348-
try {
349-
// Create or update the spawn.yml file
350-
final File spawnFile = new File(getDataFolder(), "spawn.yml");
351-
if (spawnFile.exists() && !spawnFile.delete()) {
352-
log(Level.WARNING, "Failed to delete the existing spawn.yml file");
353-
}
354-
this.serverSpawn = Annotaml.create(spawnFile, new Spawn(location)).get();
355-
356-
// Update the world spawn location, too
357-
BukkitAdapter.adaptLocation(location).ifPresent(bukkitLocation -> {
358-
assert bukkitLocation.getWorld() != null : "World was null when setting server spawn";
359-
bukkitLocation.getWorld().setSpawnLocation(bukkitLocation);
360-
});
361-
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
362-
log(Level.WARNING, "Failed to save the server spawn.yml file", e);
363-
}
364-
}
365-
366-
@Override
367-
@NotNull
368-
public List<Hook> getHooks() {
369-
return hooks;
370-
}
371-
372-
@Override
373-
public void setHooks(@NotNull List<Hook> hooks) {
374-
this.hooks = hooks;
375-
}
376-
377288
@Override
378289
@NotNull
379290
public Version getVersion() {
380291
return Version.fromString(getDescription().getVersion(), "-");
381292
}
382293

383-
@Override
384-
@NotNull
385-
public List<Command> getCommands() {
386-
return commands;
387-
}
388-
389-
@Override
390-
@NotNull
391-
public Map<String, List<String>> getGlobalPlayerList() {
392-
return globalPlayerList;
393-
}
394-
395-
@Override
396-
@NotNull
397-
public Set<UUID> getCurrentlyOnWarmup() {
398-
return currentlyOnWarmup;
399-
}
400-
401294
@Override
402295
@NotNull
403296
public String getServerName() {
404-
return server.getName();
297+
return server != null ? server.getName() : "server";
405298
}
406299

407300
@Override
408-
public void setServer(@NotNull Server server) {
301+
public void setServerName(@NotNull Server server) {
409302
this.server = server;
410303
}
411304

412-
@Override
413-
public void setUnsafeBlocks(@NotNull UnsafeBlocks unsafeBlocks) {
414-
this.unsafeBlocks = unsafeBlocks;
415-
}
416-
417305
@Override
418306
@NotNull
419-
public UnsafeBlocks getUnsafeBlocks() {
420-
return unsafeBlocks;
307+
public Path getConfigDirectory() {
308+
return getDataFolder().toPath();
421309
}
422310

423311
@Override
@@ -438,21 +326,23 @@ public void registerMetrics(int metricsId) {
438326
try {
439327
final Metrics metrics = new Metrics(this, metricsId);
440328
metrics.addCustomChart(new SimplePie("bungee_mode",
441-
() -> Boolean.toString(getSettings().doCrossServer())));
329+
() -> Boolean.toString(getSettings().getCrossServer().isEnabled())
330+
));
442331

443-
if (getSettings().doCrossServer()) {
332+
if (getSettings().getCrossServer().isEnabled()) {
444333
metrics.addCustomChart(new SimplePie("messenger_type",
445-
() -> getSettings().getBrokerType().getDisplayName()));
334+
() -> getSettings().getCrossServer().getBrokerType().getDisplayName())
335+
);
446336
}
447337

448338
metrics.addCustomChart(new SimplePie("language",
449339
() -> getSettings().getLanguage().toLowerCase()));
450340
metrics.addCustomChart(new SimplePie("database_type",
451-
() -> getSettings().getDatabaseType().getDisplayName()));
341+
() -> getSettings().getDatabase().getType().getDisplayName()));
452342
metrics.addCustomChart(new SimplePie("using_economy",
453-
() -> Boolean.toString(getSettings().doEconomy())));
343+
() -> Boolean.toString(getSettings().getEconomy().isEnabled())));
454344
metrics.addCustomChart(new SimplePie("using_map",
455-
() -> Boolean.toString(getSettings().doMapHook())));
345+
() -> Boolean.toString(getSettings().getMapHook().isEnabled())));
456346

457347
getMapHook().ifPresent(hook -> metrics.addCustomChart(new SimplePie("map_type", hook::getName)));
458348
} catch (Throwable e) {
@@ -479,7 +369,7 @@ public void log(@NotNull Level level, @NotNull String message, @NotNull Throwabl
479369
@Override
480370
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, byte[] message) {
481371
if (broker != null && broker instanceof PluginMessageBroker pluginMessenger
482-
&& getSettings().getBrokerType() == Broker.Type.PLUGIN_MESSAGE) {
372+
&& getSettings().getCrossServer().getBrokerType() == Broker.Type.PLUGIN_MESSAGE) {
483373
pluginMessenger.onReceive(channel, BukkitUser.adapt(player, this), message);
484374
}
485375
}

bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void register() {
9696
}
9797

9898
// Register commodore TAB completion
99-
if (CommodoreProvider.isSupported() && plugin.getSettings().doBrigadierTabCompletion()) {
99+
if (CommodoreProvider.isSupported() && plugin.getSettings().getGeneral().isBrigadierTabCompletion()) {
100100
BrigadierUtil.registerCommodore(plugin, this, command);
101101
}
102102
}

bukkit/src/main/java/net/william278/huskhomes/importer/EssentialsXImporter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ private String normalizeName(@NotNull String name) {
114114
name = name.replaceAll(" ", "_");
115115

116116
// Remove unicode characters
117-
if (plugin.getSettings().doRestrictNames()) {
118-
name = name.replaceAll("[^A-Za-z0-9_-]", "");
117+
if (plugin.getSettings().getGeneral().getNames().isRestrict()) {
118+
name = name.replaceAll(plugin.getSettings().getGeneral().getNames().getRegex(), "");
119119
}
120120

121121
// Ensure the name is not blank

0 commit comments

Comments
 (0)