Skip to content

Commit

Permalink
GH-21 Add support to reloadable holograms using command. (#21)
Browse files Browse the repository at this point in the history
* Add ReloadService to separate reload logic.

* Follow rollczi suggestions.

* move package

* move package
  • Loading branch information
vLuckyyy authored Oct 23, 2023
1 parent 4f24cd7 commit e780b65
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/main/java/com/eternalcode/lobbyheads/HeadsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +18,7 @@
import java.util.HashSet;
import java.util.Set;

public class ConfigurationService {
public class ConfigurationService implements Reloadable {

private final Set<OkaeriConfig> configs = new HashSet<>();

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/eternalcode/lobbyheads/reload/ReloadService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.eternalcode.lobbyheads.reload;

import java.util.HashSet;
import java.util.Set;

public class ReloadService {

private final Set<Reloadable> reloadables = new HashSet<>();

public ReloadService register(Reloadable reloadable) {
this.reloadables.add(reloadable);
return this;
}

public void reload() {
for (Reloadable reloadable : this.reloadables) {
reloadable.reload();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.eternalcode.lobbyheads.reload;

public interface Reloadable {

void reload();

}

0 comments on commit e780b65

Please sign in to comment.