diff --git a/src/main/java/com/eternalcode/lobbyheads/HeadsPlugin.java b/src/main/java/com/eternalcode/lobbyheads/HeadsPlugin.java index 3bbbe9b..4ad1e51 100644 --- a/src/main/java/com/eternalcode/lobbyheads/HeadsPlugin.java +++ b/src/main/java/com/eternalcode/lobbyheads/HeadsPlugin.java @@ -3,6 +3,7 @@ import com.eternalcode.lobbyheads.adventure.AdventureLegacyColorProcessor; import com.eternalcode.lobbyheads.configuration.ConfigurationService; import com.eternalcode.lobbyheads.configuration.implementation.HeadsConfiguration; +import com.eternalcode.lobbyheads.reload.ReloadService; import com.eternalcode.lobbyheads.event.EventCaller; import com.eternalcode.lobbyheads.head.HeadController; import com.eternalcode.lobbyheads.head.HeadManager; @@ -70,7 +71,11 @@ public void onEnable() { BlockService blockService = new BlockService(config, notificationAnnouncer, this.headManager); - this.getCommand("heads").setExecutor(new HeadCommand(config, configurationService, notificationAnnouncer, blockService)); + ReloadService reloadService = new ReloadService() + .register(configurationService) + .register(hologramService); + + this.getCommand("heads").setExecutor(new HeadCommand(config, notificationAnnouncer, blockService, reloadService)); Stream.of( new HeadController(config, this.headManager, notificationAnnouncer), diff --git a/src/main/java/com/eternalcode/lobbyheads/configuration/ConfigurationService.java b/src/main/java/com/eternalcode/lobbyheads/configuration/ConfigurationService.java index b95cc0c..d2cf0cc 100644 --- a/src/main/java/com/eternalcode/lobbyheads/configuration/ConfigurationService.java +++ b/src/main/java/com/eternalcode/lobbyheads/configuration/ConfigurationService.java @@ -1,5 +1,6 @@ package com.eternalcode.lobbyheads.configuration; +import com.eternalcode.lobbyheads.reload.Reloadable; import com.eternalcode.lobbyheads.configuration.serializer.HeadSerializer; import com.eternalcode.lobbyheads.configuration.serializer.PositionSerializer; import eu.okaeri.configs.ConfigManager; @@ -17,7 +18,7 @@ import java.util.HashSet; import java.util.Set; -public class ConfigurationService { +public class ConfigurationService implements Reloadable { private final Set configs = new HashSet<>(); @@ -55,6 +56,7 @@ private Yaml createYaml() { return new Yaml(constructor, representer, dumperOptions, loaderOptions, resolver); } + @Override public void reload() { for (OkaeriConfig config : this.configs) { config.load(); diff --git a/src/main/java/com/eternalcode/lobbyheads/head/command/HeadCommand.java b/src/main/java/com/eternalcode/lobbyheads/head/command/HeadCommand.java index 274a642..c4071aa 100644 --- a/src/main/java/com/eternalcode/lobbyheads/head/command/HeadCommand.java +++ b/src/main/java/com/eternalcode/lobbyheads/head/command/HeadCommand.java @@ -1,11 +1,11 @@ package com.eternalcode.lobbyheads.head.command; -import com.eternalcode.lobbyheads.configuration.ConfigurationService; import com.eternalcode.lobbyheads.configuration.implementation.HeadsConfiguration; import com.eternalcode.lobbyheads.head.block.BlockService; import com.eternalcode.lobbyheads.notification.NotificationAnnouncer; import com.eternalcode.lobbyheads.position.Position; import com.eternalcode.lobbyheads.position.PositionAdapter; +import com.eternalcode.lobbyheads.reload.ReloadService; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.command.Command; @@ -21,16 +21,16 @@ public class HeadCommand implements CommandExecutor, TabCompleter { private static final String HEAD_MANAGEMENT_PERMISSION = "lobbyheads.admin"; private final HeadsConfiguration config; - private final ConfigurationService configurationService; private final NotificationAnnouncer notificationAnnouncer; private final BlockService blockService; + private final ReloadService reloadService; - public HeadCommand(HeadsConfiguration config, ConfigurationService configurationService, - NotificationAnnouncer notificationAnnouncer, BlockService blockService) { + public HeadCommand(HeadsConfiguration config, NotificationAnnouncer notificationAnnouncer, + BlockService blockService, ReloadService reloadService) { this.config = config; - this.configurationService = configurationService; this.notificationAnnouncer = notificationAnnouncer; this.blockService = blockService; + this.reloadService = reloadService; } @Override @@ -62,7 +62,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St case "add" -> this.blockService.createHeadBlock(location, player, convert); case "remove" -> this.blockService.removeHeadBlock(convert, player); case "reload" -> { - this.configurationService.reload(); + this.reloadService.reload(); this.notificationAnnouncer.sendMessage(player, this.config.messages.configurationReloaded); } default -> this.notificationAnnouncer.sendMessage(player, invalidUsage); diff --git a/src/main/java/com/eternalcode/lobbyheads/head/hologram/HologramService.java b/src/main/java/com/eternalcode/lobbyheads/head/hologram/HologramService.java index 69de0a9..f9960d4 100644 --- a/src/main/java/com/eternalcode/lobbyheads/head/hologram/HologramService.java +++ b/src/main/java/com/eternalcode/lobbyheads/head/hologram/HologramService.java @@ -2,6 +2,7 @@ import com.eternalcode.lobbyheads.adventure.AdventureLegacy; import com.eternalcode.lobbyheads.configuration.implementation.HeadsConfiguration; +import com.eternalcode.lobbyheads.reload.Reloadable; import com.eternalcode.lobbyheads.head.Head; import com.eternalcode.lobbyheads.head.HeadManager; import com.eternalcode.lobbyheads.position.Position; @@ -19,7 +20,7 @@ import java.util.UUID; -public class HologramService { +public class HologramService implements Reloadable { private static final String HOLOGRAM_NAME_PREFIX = "heads#%s,%s,%s,%s"; private static final int SPAWN_DISTANCE = 50; @@ -78,6 +79,11 @@ public void updateHologram(Position position, UUID uuid) { this.createHologram(this.server.getOfflinePlayer(uuid), position, this.config.headSection.headFormat); } + public void updateHolograms() { + this.hologramPool.getHolograms().forEach(this.hologramPool::remove); + this.loadHolograms(); + } + private Position getLocationOffset(Position position) { Location location = PositionAdapter.convert(position).clone().add(0.5, -0.3, 0.5); return PositionAdapter.convert(location); @@ -92,4 +98,9 @@ private void showHologramToPlayers(Hologram hologram) { private String getHologramName(Position position) { return String.format(HOLOGRAM_NAME_PREFIX, position.world(), position.x(), position.y(), position.z()); } + + @Override + public void reload() { + this.updateHolograms(); + } } diff --git a/src/main/java/com/eternalcode/lobbyheads/reload/ReloadService.java b/src/main/java/com/eternalcode/lobbyheads/reload/ReloadService.java new file mode 100644 index 0000000..5315a7a --- /dev/null +++ b/src/main/java/com/eternalcode/lobbyheads/reload/ReloadService.java @@ -0,0 +1,20 @@ +package com.eternalcode.lobbyheads.reload; + +import java.util.HashSet; +import java.util.Set; + +public class ReloadService { + + private final Set reloadables = new HashSet<>(); + + public ReloadService register(Reloadable reloadable) { + this.reloadables.add(reloadable); + return this; + } + + public void reload() { + for (Reloadable reloadable : this.reloadables) { + reloadable.reload(); + } + } +} diff --git a/src/main/java/com/eternalcode/lobbyheads/reload/Reloadable.java b/src/main/java/com/eternalcode/lobbyheads/reload/Reloadable.java new file mode 100644 index 0000000..3c11e1d --- /dev/null +++ b/src/main/java/com/eternalcode/lobbyheads/reload/Reloadable.java @@ -0,0 +1,7 @@ +package com.eternalcode.lobbyheads.reload; + +public interface Reloadable { + + void reload(); + +}