Skip to content

Commit

Permalink
Implement config reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed May 14, 2024
1 parent 1f39254 commit 5c80209
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,23 @@ public void onEnable() {
this.config = new BannersConfig(this);
this.config.reload();

final Runnable load = () -> {
this.squaremapHook = new SquaremapHook();
this.squaremapHook.load();

this.bannerManager = new BannersManager(this);
this.bannerManager.load();
};

this.config.registerReloadCommand(() -> {
this.onDisable();
load.run();
});

//noinspection unused
Key loadme = Icons.WHITE;

squaremapHook = new SquaremapHook();
squaremapHook.load();

bannerManager = new BannersManager(this);
bannerManager.load();
load.run();

getServer().getPluginManager().registerEvents(new BannersListener(this), this);
getServer().getPluginManager().registerEvents(new WorldListener(this), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ public void onEnable() {
this.config = new ClaimChunkConfig(this);
this.config.reload();

this.squaremapHook = new SquaremapHook(this);
final Runnable load = () -> {
this.squaremapHook = new SquaremapHook(this);
};

this.config.registerReloadCommand(() -> {
this.onDisable();
load.run();
});

load.run();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public SquaremapHook(final SquaremapClaimChunk plugin) {

public void disable() {
this.tasks.values().forEach(SquaremapTask::disable);
SquaremapProvider.get().mapWorlds().forEach(w -> {
if (w.layerRegistry().hasEntry(CLAIM_CHUNK_LAYER_KEY)) {
w.layerRegistry().unregister(CLAIM_CHUNK_LAYER_KEY);
}
});
this.tasks.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ public void onEnable() {
this.config = new DeathSpotConfig(this);
this.config.reload();

if (!new File(this.getDataFolder(), "icon.png").exists()) {
this.saveResource("icon.png", false);
}
final Runnable load = () -> {
if (!new File(this.getDataFolder(), "icon.png").exists()) {
this.saveResource("icon.png", false);
}
this.squaremapHook = new SquaremapHook(this);
};

this.config.registerReloadCommand(() -> {
this.onDisable();
load.run();
});

this.getServer().getPluginManager().registerEvents(new PlayerListener(this), this);

this.squaremapHook = new SquaremapHook(this);
load.run();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public SquaremapHook(final DeathSpots plugin) {

public void disable() {
this.tasks.values().forEach(SquaremapTask::disable);
SquaremapProvider.get().mapWorlds().forEach(w -> {
if (w.layerRegistry().hasEntry(DEATH_SPOTS_LAYER_KEY)) {
w.layerRegistry().unregister(DEATH_SPOTS_LAYER_KEY);
}
});
this.tasks.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ public void onEnable() {
this.config = new EssXConfig(this);
this.config.reload();

if (!new File(this.getDataFolder(), "warp.png").exists()) {
this.saveResource("warp.png", false);
}
final Runnable load = () -> {
if (!new File(this.getDataFolder(), "warp.png").exists()) {
this.saveResource("warp.png", false);
}

this.squaremapHook = new SquaremapHook(this);
this.squaremapHook.load();
};

this.config.registerReloadCommand(() -> {
this.onDisable();
load.run();
});

this.squaremapHook = new SquaremapHook(this);
this.squaremapHook.load();
load.run();

this.getServer().getPluginManager().registerEvents(new EssentialsListener(this), this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static xyz.jpenilla.squaremap.api.Key.key;

public final class SquaremapHook {
public static final Key WARP_LAYER_KEY = key("essentials_warps");
public static final Key WARP_ICON_KEY = key("essentials_warp_icon");

private final Map<WorldIdentifier, SquaremapTask> tasks = new HashMap<>();
Expand Down Expand Up @@ -45,7 +46,7 @@ public void load() {
.showControls(worldConfig.warpsShowControls)
.defaultHidden(worldConfig.warpsControlsHidden)
.build();
mapWorld.layerRegistry().register(Key.of("essentials_warps"), provider);
mapWorld.layerRegistry().register(WARP_LAYER_KEY, provider);
SquaremapTask task = new SquaremapTask(mapWorld, worldConfig, provider);
task.runTaskTimerAsynchronously(this.plugin, 0, 20L * this.plugin.config().updateInterval);
this.tasks.put(mapWorld.identifier(), task);
Expand All @@ -54,6 +55,11 @@ public void load() {

public void disable() {
this.tasks.values().forEach(SquaremapTask::disable);
SquaremapProvider.get().mapWorlds().forEach(w -> {
if (w.layerRegistry().hasEntry(WARP_LAYER_KEY)) {
w.layerRegistry().unregister(WARP_LAYER_KEY);
}
});
this.tasks.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ public void onEnable() {
this.config = new GPConfig(this);
this.config.reload();

this.squaremapHook = new SquaremapHook(this);
final Runnable load = () -> {
this.squaremapHook = new SquaremapHook(this);
};

this.config.registerReloadCommand(() -> {
this.onDisable();
load.run();
});

load.run();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public SquaremapHook(SquaremapGriefPrevention plugin) {

public void disable() {
this.tasks.values().forEach(SquaremapTask::disable);
SquaremapProvider.get().mapWorlds().forEach(w -> {
if (w.layerRegistry().hasEntry(GP_LAYER_KEY)) {
w.layerRegistry().unregister(GP_LAYER_KEY);
}
});
this.tasks.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ public void onEnable() {
this.config = new SignsConfig(this);
this.config.reload();

Icons.register(this);
this.customIcons = CustomIcons.register(this);
final Runnable load = () -> {
Icons.register(this);
this.customIcons = CustomIcons.register(this);

this.layerProviderManager = new LayerProviderManager(this);
this.signManager = new SignManager(this);
this.layerProviderManager.load();
this.layerProviderManager = new LayerProviderManager(this);
this.signManager = new SignManager(this);
this.layerProviderManager.load();
};

this.config.registerReloadCommand(() -> {
this.onDisable();
load.run();
});

load.run();

this.getServer().getPluginManager().registerEvents(new SignListener(this), this);
this.getServer().getPluginManager().registerEvents(new WorldListener(this), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ public void onEnable() {
this.config = new WGConfig(this);
this.config.reload();

this.squaremapHook = new SquaremapHook(this);
final Runnable load = () -> {
this.squaremapHook = new SquaremapHook(this);
};

this.config.registerReloadCommand(() -> {
this.onDisable();
load.run();
});

this.getServer().getPluginManager().registerEvents(new EventListener(this), this);
}

Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ tasks {
minecraftVersion("1.20.1")
downloadPlugins {
hangar("squaremap", "1.2.0")
url("https://ci.enginehub.org/repository/download/bt10/22612:id/worldedit-bukkit-7.2.16-SNAPSHOT-dist.jar?guest=1")
url("https://ci.enginehub.org/repository/download/bt11/22585:id/worldguard-bukkit-7.0.10-SNAPSHOT-dist.jar?guest=1")
//url("https://ci.enginehub.org/repository/download/bt10/22612:id/worldedit-bukkit-7.2.16-SNAPSHOT-dist.jar?guest=1")
//url("https://ci.enginehub.org/repository/download/bt11/22585:id/worldguard-bukkit-7.0.10-SNAPSHOT-dist.jar?guest=1")
}
}
}
2 changes: 2 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ description = "Common code shared between all addon plugins in this repository"
dependencies {
api(platform("org.spongepowered:configurate-bom:4.1.2"))
api("org.spongepowered:configurate-yaml")
api(platform("org.incendo:cloud-minecraft-bom:2.0.0-beta.7"))
api("org.incendo:cloud-paper")

compileOnlyApi("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT") {
exclude("org.yaml", "snakeyaml")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.incendo.cloud.bukkit.BukkitCommandMeta;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.paper.PaperCommandManager;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.transformation.ConfigurationTransformation;
Expand All @@ -28,6 +35,7 @@
import xyz.jpenilla.squaremap.api.WorldIdentifier;

import static java.util.Objects.requireNonNull;
import static org.incendo.cloud.description.Description.description;

@SuppressWarnings("unused")
public abstract class Config<C extends Config<C, W>, W extends WorldConfig> {
Expand All @@ -38,6 +46,7 @@ public abstract class Config<C extends Config<C, W>, W extends WorldConfig> {
private final YamlConfigurationLoader loader;
private final @Nullable Class<W> worldConfigClass;
private final @Nullable Map<WorldIdentifier, W> worldConfigs;
private final Plugin plugin;
ConfigurationNode config;

protected Config(final Class<C> configClass, final Plugin plugin) {
Expand All @@ -58,6 +67,31 @@ protected Config(
.nodeStyle(NodeStyle.BLOCK)
.defaultOptions(options -> options.serializers(builder -> builder.register(ColorSerializer.INSTANCE)))
.build();
this.plugin = plugin;
}

public void registerReloadCommand(final Runnable callback) {
final PaperCommandManager<CommandSender> manager =
PaperCommandManager.createNative(this.plugin, ExecutionCoordinator.simpleCoordinator());

manager.command(
manager.commandBuilder(this.plugin.getName().toLowerCase(Locale.ROOT))
.literal("reload")
.permission(this.plugin.getName().toLowerCase(Locale.ROOT) + ".command.reload")
.meta(BukkitCommandMeta.BUKKIT_DESCRIPTION, this.plugin.getName() + " commands.")
.commandDescription(description("Reloads the " + this.plugin.getName() + " config."))
.handler(ctx -> {
ctx.sender().sendMessage(Component.text("Reloading " + this.plugin.getName() + "...", NamedTextColor.GREEN));
try {
this.reload();
callback.run();
} catch (final Exception e) {
ctx.sender().sendMessage(Component.text("Failed to reload. Check console for errors.", NamedTextColor.RED));
this.plugin.getSLF4JLogger().error("Failed to reload", e);
}
ctx.sender().sendMessage(Component.text("Done reloading " + this.plugin.getName() + ".", NamedTextColor.GREEN));
})
);
}

protected void addVersions(final ConfigurationTransformation.VersionedBuilder versionedBuilder) {
Expand Down

0 comments on commit 5c80209

Please sign in to comment.