Skip to content

Commit

Permalink
Update PlayerDataManager to handle multiple display types
Browse files Browse the repository at this point in the history
  • Loading branch information
RealTriassic committed Oct 19, 2024
1 parent 1438b85 commit 2d31908
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.triassic.geyserdebuginfo.configuration.Configuration;
import com.triassic.geyserdebuginfo.configuration.ConfigurationContainer;
import com.triassic.geyserdebuginfo.listener.PlayerJoinListener;
import com.triassic.geyserdebuginfo.placeholder.PlaceholderManager;
import com.triassic.geyserdebuginfo.manager.PlayerDataManager;
import com.triassic.geyserdebuginfo.placeholder.PlaceholderManager;
import com.triassic.geyserdebuginfo.placeholder.modifiers.MathModifierProvider;
import com.triassic.geyserdebuginfo.placeholder.modifiers.TextModifierProvider;
import com.triassic.geyserdebuginfo.placeholder.placeholders.PlayerPlaceholderProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import com.triassic.geyserdebuginfo.GeyserDebugInfo;
import com.triassic.geyserdebuginfo.command.AbstractCommand;
import com.triassic.geyserdebuginfo.display.DisplayType;
import com.triassic.geyserdebuginfo.display.DisplayManager;
import com.triassic.geyserdebuginfo.display.DisplayType;
import com.triassic.geyserdebuginfo.manager.PlayerDataManager;
import org.geysermc.geyser.api.command.Command;
import org.geysermc.geyser.api.command.CommandSource;
import org.geysermc.geyser.session.GeyserSession;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public class ToggleCommand extends AbstractCommand {

private final GeyserDebugInfo instance;
Expand All @@ -26,10 +28,35 @@ public ToggleCommand(final GeyserDebugInfo instance) {
@Override
protected void execute(@NotNull CommandSource source, @NotNull Command command, @NotNull String[] args) {
final GeyserSession session = (GeyserSession) source.connection();
final UUID playerUuid = session.playerUuid();

if (args.length == 0) {
for (DisplayType displayType : DisplayType.values()) {
if (playerDataManager.isDisplayEnabled(playerUuid, displayType)) {
displayManager.unsubscribePlayer(session, displayType);
} else {
displayManager.subscribePlayer(session, displayType);
}
}
return;
}

DisplayType displayType;
switch (args[0]) {
case "actionbar" -> displayManager.subscribePlayer(session, DisplayType.ACTIONBAR);
case "bossbar" -> displayManager.subscribePlayer(session, DisplayType.BOSSBAR);
case "actionbar":
displayType = DisplayType.ACTIONBAR;
break;
case "bossbar":
displayType = DisplayType.BOSSBAR;
break;
default:
return;
}

if (playerDataManager.isDisplayEnabled(playerUuid, displayType)) {
displayManager.unsubscribePlayer(session, displayType);
} else {
displayManager.subscribePlayer(session, displayType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.triassic.geyserdebuginfo.GeyserDebugInfo;
import com.triassic.geyserdebuginfo.display.displays.ActionBarDisplay;
import com.triassic.geyserdebuginfo.display.displays.BossBarDisplay;
import com.triassic.geyserdebuginfo.manager.PlayerDataManager;
import org.geysermc.geyser.session.GeyserSession;

import java.util.Map;
Expand All @@ -18,19 +19,23 @@ public class DisplayManager {
private final Map<GeyserSession, Set<Display>> activeDisplays;
private final ScheduledExecutorService executor;
private final ExecutorService asyncExecutor;
private final PlayerDataManager playerDataManager;

public DisplayManager(GeyserDebugInfo instance) {
this.instance = instance;
this.activeDisplays = new ConcurrentHashMap<>();
this.executor = Executors.newScheduledThreadPool(10);
this.asyncExecutor = Executors.newCachedThreadPool();
this.playerDataManager = instance.getPlayerDataManager();
}

public void subscribePlayer(final GeyserSession session, final DisplayType displayType) {
asyncExecutor.submit(() -> {
Display display = createDisplay(session, displayType);
activeDisplays.computeIfAbsent(session, k -> ConcurrentHashMap.newKeySet()).add(display);
display.startUpdating(executor);

playerDataManager.setDisplayEnabled(session.playerUuid(), displayType, true);
});
}

Expand All @@ -50,6 +55,8 @@ public void unsubscribePlayer(final GeyserSession session, final DisplayType dis
});
}
});

playerDataManager.setDisplayEnabled(session.playerUuid(), displayType, false);
}

private Display createDisplay(final GeyserSession session, final DisplayType displayType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public void onJoin(final SessionJoinEvent event) {
final GeyserSession session = (GeyserSession) event.connection();
final SessionPlayerEntity player = session.getPlayerEntity();

if (playerDataManager.isF3Enabled(player.getUuid())) {
// TODO: Re-implement this.
}
// TODO: Re-implement this.
}

@Subscribe
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.triassic.geyserdebuginfo.manager;

import com.triassic.geyserdebuginfo.display.DisplayType;
import org.geysermc.geyser.api.extension.ExtensionLogger;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
Expand All @@ -16,7 +17,7 @@ public class PlayerDataManager {
private final File playerDataFile;
private final ExtensionLogger logger;
private final Yaml yaml;
private final Map<UUID, Boolean> playerF3States;
private final Map<UUID, Map<DisplayType, Boolean>> playerDisplayStates;
private final boolean defaultEnabled;

public PlayerDataManager(
Expand All @@ -27,10 +28,10 @@ public PlayerDataManager(
this.playerDataFile = new File(dataFolder, "playerdata.yml");
this.logger = logger;
this.yaml = new Yaml(createDumperOptions());
this.playerF3States = new HashMap<>();
this.playerDisplayStates = new HashMap<>();
this.defaultEnabled = defaultEnabled;

loadPlayerData();
load();
}

private DumperOptions createDumperOptions() {
Expand All @@ -40,46 +41,60 @@ private DumperOptions createDumperOptions() {
return options;
}

private void loadPlayerData() {
private void load() {
if (!playerDataFile.exists()) return;

try (FileReader reader = new FileReader(playerDataFile)) {
Map<String, Boolean> data = yaml.load(reader);
Map<String, Map<String, Boolean>> data = yaml.load(reader);
if (data != null) {
for (Map.Entry<String, Boolean> entry : data.entrySet()) {
playerF3States.put(UUID.fromString(entry.getKey()), entry.getValue());
for (Map.Entry<String, Map<String, Boolean>> entry : data.entrySet()) {
UUID playerUuid = UUID.fromString(entry.getKey());
Map<DisplayType, Boolean> displayStates = new HashMap<>();
for (Map.Entry<String, Boolean> stateEntry : entry.getValue().entrySet()) {
displayStates.put(DisplayType.valueOf(stateEntry.getKey()), stateEntry.getValue());
}
playerDisplayStates.put(playerUuid, displayStates);
}
}
} catch (Throwable error) {
logger.error("Failed to load playerdata file", error);
logger.error("Failed to load player data file", error);
}
}

public void savePlayerData() {
public void save() {
try (FileWriter writer = new FileWriter(playerDataFile)) {
Map<String, Boolean> data = new HashMap<>();
for (Map.Entry<UUID, Boolean> entry : playerF3States.entrySet()) {
Boolean state = entry.getValue();
if ((defaultEnabled && !state) || (!defaultEnabled && state)) {
data.put(entry.getKey().toString(), state);
Map<String, Map<String, Boolean>> data = new HashMap<>();
for (Map.Entry<UUID, Map<DisplayType, Boolean>> entry : playerDisplayStates.entrySet()) {
UUID playerUuid = entry.getKey();
Map<String, Boolean> stateMap = new HashMap<>();
for (Map.Entry<DisplayType, Boolean> stateEntry : entry.getValue().entrySet()) {
Boolean enabled = stateEntry.getValue();
if (enabled != defaultEnabled) {
stateMap.put(stateEntry.getKey().name(), enabled);
}
}
if (!stateMap.isEmpty()) {
data.put(playerUuid.toString(), stateMap);
}
}
yaml.dump(data, writer);
} catch (Throwable error) {
logger.error("Failed to save playerdata to file", error);
logger.error("Failed to save player data to file", error);
}
}

public void setF3Enabled(UUID playerUuid, boolean enabled) {
if ((defaultEnabled && enabled) || (!defaultEnabled && !enabled)) {
playerF3States.remove(playerUuid);
} else {
playerF3States.put(playerUuid, enabled);
}
savePlayerData();
public void setDisplayEnabled(UUID playerUuid, DisplayType displayType, boolean enabled) {
playerDisplayStates
.computeIfAbsent(playerUuid, k -> new HashMap<>())
.put(displayType, enabled);

save();
}

public boolean isF3Enabled(UUID playerUuid) {
return playerF3States.getOrDefault(playerUuid, defaultEnabled);
public boolean isDisplayEnabled(UUID playerUuid, DisplayType displayType) {
return playerDisplayStates
.getOrDefault(playerUuid, new HashMap<>())
.getOrDefault(displayType, defaultEnabled);
}
}

0 comments on commit 2d31908

Please sign in to comment.