Skip to content

Commit

Permalink
refactor: Move to ConfigLib, use lombok (#563)
Browse files Browse the repository at this point in the history
* refactor: move to configlib, adjust config structure

* fix: huskclaims typos

* fix: don't generate `server.yml` in local mode

* fix: NPE with server name
  • Loading branch information
WiIIiam278 authored Feb 12, 2024
1 parent c55631a commit 1723aa4
Show file tree
Hide file tree
Showing 84 changed files with 4,339 additions and 4,837 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ allprojects {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.2'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.10.2'

testCompileOnly 'org.jetbrains:annotations:24.1.0'
}

license {
Expand Down
7 changes: 4 additions & 3 deletions bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ dependencies {
compileOnly 'org.spigotmc:spigot-api:1.17.1-R0.1-SNAPSHOT'
compileOnly 'org.jetbrains:annotations:24.1.0'
compileOnly 'de.themoep:minedown-adventure:1.7.2-SNAPSHOT'
compileOnly 'net.william278:annotaml:2.0.5'
compileOnly 'net.william278:DesertWell:2.0.4'
compileOnly 'com.github.MilkBowl:VaultAPI:1.7.1'
compileOnly 'me.clip:placeholderapi:2.11.5'
compileOnly('net.essentialsx:EssentialsX:2.20.1') {
exclude group: 'org.spigotmc', module: 'spigot-api'
}
compileOnly 'org.projectlombok:lombok:1.18.30'

testImplementation 'com.github.seeseemelk:MockBukkit-v1.17:1.13.0'
testImplementation 'de.themoep:minedown-adventure:1.7.2-SNAPSHOT'
Expand All @@ -25,6 +25,8 @@ dependencies {
testImplementation "redis.clients:jedis:${jedis_version}"
testImplementation "org.xerial:sqlite-jdbc:${sqlite_driver_version}"
testImplementation "com.mysql:mysql-connector-j:${mysql_driver_version}"

annotationProcessor 'org.projectlombok:lombok:1.18.30'
}

shadowJar {
Expand All @@ -39,11 +41,10 @@ shadowJar {
relocate 'org.jetbrains', 'net.william278.huskhomes.libraries'
relocate 'org.intellij', 'net.william278.huskhomes.libraries'
relocate 'com.zaxxer', 'net.william278.huskhomes.libraries'
relocate 'net.william278.annotaml', 'net.william278.huskhomes.libraries.annotaml'
relocate 'net.william278.paginedown', 'net.william278.huskhomes.libraries.paginedown'
relocate 'net.william278.desertwell', 'net.william278.huskhomes.libraries.desertwell'
relocate 'de.exlll', 'net.william278.huskhomes.libraries'
relocate 'org.json', 'net.william278.huskhomes.libraries.json'
relocate 'dev.dejvokep.boostedyaml', 'net.william278.huskhomes.libraries.boostedyaml'
relocate 'org.yaml.snakeyaml', 'net.william278.huskhomes.libraries.snakeyaml'
relocate 'com.google.gson', 'net.william278.huskhomes.libraries.gson'
relocate 'org.bstats', 'net.william278.huskhomes.libraries.bstats'
Expand Down
188 changes: 39 additions & 149 deletions bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@

package net.william278.huskhomes;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.AudienceProvider;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.william278.annotaml.Annotaml;
import net.william278.desertwell.util.Version;
import net.william278.huskhomes.api.HuskHomesAPI;
import net.william278.huskhomes.command.BukkitCommand;
Expand All @@ -46,7 +50,6 @@
import net.william278.huskhomes.network.Broker;
import net.william278.huskhomes.network.PluginMessageBroker;
import net.william278.huskhomes.network.RedisBroker;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.World;
import net.william278.huskhomes.random.NormalDistributionEngine;
import net.william278.huskhomes.random.RandomTeleportEngine;
Expand All @@ -71,12 +74,14 @@
import space.arim.morepaperlib.scheduling.GracefulScheduling;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;

@Getter
@Setter
@NoArgsConstructor
public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask.Supplier, BukkitEventDispatcher,
PluginMessageListener, BukkitSafetyResolver {

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

private Set<SavedUser> savedUsers;
private final Set<SavedUser> savedUsers = Sets.newHashSet();
private final Map<String, List<String>> globalPlayerList = Maps.newConcurrentMap();
private final Set<UUID> currentlyOnWarmup = Sets.newConcurrentHashSet();

private Settings settings;
private Locales locales;
private Database database;
Expand All @@ -97,19 +105,15 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
private UnsafeBlocks unsafeBlocks;
private List<Hook> hooks;
private List<Command> commands;
private Map<String, List<String>> globalPlayerList;
private Set<UUID> currentlyOnWarmup;
@Getter(AccessLevel.NONE)
private Server server;
@Getter(AccessLevel.NONE)
private BukkitAudiences audiences;
@Getter(AccessLevel.NONE)
private MorePaperLib paperLib;
@Nullable
private Broker broker;

// Default public constructor
public BukkitHuskHomes() {
super();
}

// Super constructor for unit testing
@TestOnly
protected BukkitHuskHomes(@NotNull JavaPluginLoader loader, @NotNull PluginDescriptionFile description,
Expand All @@ -122,21 +126,15 @@ public void onEnable() {
// Create adventure audience
this.audiences = BukkitAudiences.create(this);
this.paperLib = new MorePaperLib(this);
this.savedUsers = new HashSet<>();
this.globalPlayerList = new HashMap<>();
this.currentlyOnWarmup = new HashSet<>();
this.validator = new Validator(this);

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

// Initialize the database
initialize(getSettings().getDatabaseType().getDisplayName() + " database connection", (plugin) -> {
this.database = switch (getSettings().getDatabaseType()) {
final Database.Type type = getSettings().getDatabase().getType();
initialize(type.getDisplayName() + " database connection", (plugin) -> {
this.database = switch (type) {
case MYSQL, MARIADB -> new MySqlDatabase(this);
case SQLITE -> new SqLiteDatabase(this);
case H2 -> new H2Database(this);
Expand All @@ -149,9 +147,10 @@ public void onEnable() {
this.manager = new Manager(this);

// Initialize the network messenger if proxy mode is enabled
if (getSettings().doCrossServer()) {
initialize(settings.getBrokerType().getDisplayName() + " message broker", (plugin) -> {
broker = switch (settings.getBrokerType()) {
final Settings.CrossServerSettings crossServer = getSettings().getCrossServer();
if (crossServer.isEnabled()) {
initialize(crossServer.getBrokerType().getDisplayName() + " message broker", (plugin) -> {
broker = switch (crossServer.getBrokerType()) {
case PLUGIN_MESSAGE -> new PluginMessageBroker(this);
case REDIS -> new RedisBroker(this);
};
Expand Down Expand Up @@ -205,7 +204,7 @@ public void registerHooks() {
HuskHomes.super.registerHooks();

// Hooks
if (getSettings().doEconomy() && isDependencyLoaded("Vault")) {
if (getSettings().getEconomy().isEnabled() && isDependencyLoaded("Vault")) {
getHooks().add(new VaultEconomyHook(this));
}
if (isDependencyLoaded("PlaceholderAPI")) {
Expand Down Expand Up @@ -267,52 +266,6 @@ public Audience getAudience(@NotNull UUID user) {
return audiences.player(user);
}

@NotNull
@Override
public Set<SavedUser> getSavedUsers() {
return savedUsers;
}

@NotNull
@Override
public Settings getSettings() {
return settings;
}

@Override
public void setSettings(@NotNull Settings settings) {
this.settings = settings;
}

@NotNull
@Override
public Locales getLocales() {
return locales;
}

@Override
public void setLocales(@NotNull Locales locales) {
this.locales = locales;
}

@Override
@NotNull
public Database getDatabase() {
return database;
}

@Override
@NotNull
public Validator getValidator() {
return validator;
}

@NotNull
@Override
public Manager getManager() {
return manager;
}

@NotNull
@Override
public Broker getMessenger() {
Expand All @@ -322,17 +275,6 @@ public Broker getMessenger() {
return broker;
}

@NotNull
@Override
public RandomTeleportEngine getRandomTeleportEngine() {
return randomTeleportEngine;
}

@Override
public void setRandomTeleportEngine(@NotNull RandomTeleportEngine randomTeleportEngine) {
this.randomTeleportEngine = randomTeleportEngine;
}

@Override
public Optional<Spawn> getServerSpawn() {
return Optional.ofNullable(serverSpawn);
Expand All @@ -343,81 +285,27 @@ public void setServerSpawn(@NotNull Spawn spawn) {
this.serverSpawn = spawn;
}

@Override
public void setServerSpawn(@NotNull Location location) {
try {
// Create or update the spawn.yml file
final File spawnFile = new File(getDataFolder(), "spawn.yml");
if (spawnFile.exists() && !spawnFile.delete()) {
log(Level.WARNING, "Failed to delete the existing spawn.yml file");
}
this.serverSpawn = Annotaml.create(spawnFile, new Spawn(location)).get();

// Update the world spawn location, too
BukkitAdapter.adaptLocation(location).ifPresent(bukkitLocation -> {
assert bukkitLocation.getWorld() != null : "World was null when setting server spawn";
bukkitLocation.getWorld().setSpawnLocation(bukkitLocation);
});
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
log(Level.WARNING, "Failed to save the server spawn.yml file", e);
}
}

@Override
@NotNull
public List<Hook> getHooks() {
return hooks;
}

@Override
public void setHooks(@NotNull List<Hook> hooks) {
this.hooks = hooks;
}

@Override
@NotNull
public Version getVersion() {
return Version.fromString(getDescription().getVersion(), "-");
}

@Override
@NotNull
public List<Command> getCommands() {
return commands;
}

@Override
@NotNull
public Map<String, List<String>> getGlobalPlayerList() {
return globalPlayerList;
}

@Override
@NotNull
public Set<UUID> getCurrentlyOnWarmup() {
return currentlyOnWarmup;
}

@Override
@NotNull
public String getServerName() {
return server.getName();
return server != null ? server.getName() : "server";
}

@Override
public void setServer(@NotNull Server server) {
public void setServerName(@NotNull Server server) {
this.server = server;
}

@Override
public void setUnsafeBlocks(@NotNull UnsafeBlocks unsafeBlocks) {
this.unsafeBlocks = unsafeBlocks;
}

@Override
@NotNull
public UnsafeBlocks getUnsafeBlocks() {
return unsafeBlocks;
public Path getConfigDirectory() {
return getDataFolder().toPath();
}

@Override
Expand All @@ -438,21 +326,23 @@ public void registerMetrics(int metricsId) {
try {
final Metrics metrics = new Metrics(this, metricsId);
metrics.addCustomChart(new SimplePie("bungee_mode",
() -> Boolean.toString(getSettings().doCrossServer())));
() -> Boolean.toString(getSettings().getCrossServer().isEnabled())
));

if (getSettings().doCrossServer()) {
if (getSettings().getCrossServer().isEnabled()) {
metrics.addCustomChart(new SimplePie("messenger_type",
() -> getSettings().getBrokerType().getDisplayName()));
() -> getSettings().getCrossServer().getBrokerType().getDisplayName())
);
}

metrics.addCustomChart(new SimplePie("language",
() -> getSettings().getLanguage().toLowerCase()));
metrics.addCustomChart(new SimplePie("database_type",
() -> getSettings().getDatabaseType().getDisplayName()));
() -> getSettings().getDatabase().getType().getDisplayName()));
metrics.addCustomChart(new SimplePie("using_economy",
() -> Boolean.toString(getSettings().doEconomy())));
() -> Boolean.toString(getSettings().getEconomy().isEnabled())));
metrics.addCustomChart(new SimplePie("using_map",
() -> Boolean.toString(getSettings().doMapHook())));
() -> Boolean.toString(getSettings().getMapHook().isEnabled())));

getMapHook().ifPresent(hook -> metrics.addCustomChart(new SimplePie("map_type", hook::getName)));
} catch (Throwable e) {
Expand All @@ -479,7 +369,7 @@ public void log(@NotNull Level level, @NotNull String message, @NotNull Throwabl
@Override
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, byte[] message) {
if (broker != null && broker instanceof PluginMessageBroker pluginMessenger
&& getSettings().getBrokerType() == Broker.Type.PLUGIN_MESSAGE) {
&& getSettings().getCrossServer().getBrokerType() == Broker.Type.PLUGIN_MESSAGE) {
pluginMessenger.onReceive(channel, BukkitUser.adapt(player, this), message);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void register() {
}

// Register commodore TAB completion
if (CommodoreProvider.isSupported() && plugin.getSettings().doBrigadierTabCompletion()) {
if (CommodoreProvider.isSupported() && plugin.getSettings().getGeneral().isBrigadierTabCompletion()) {
BrigadierUtil.registerCommodore(plugin, this, command);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ private String normalizeName(@NotNull String name) {
name = name.replaceAll(" ", "_");

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

// Ensure the name is not blank
Expand Down
Loading

0 comments on commit 1723aa4

Please sign in to comment.