Skip to content

Commit

Permalink
Make PlaceholderManager#setPlaceholders static
Browse files Browse the repository at this point in the history
  • Loading branch information
RealTriassic committed Oct 19, 2024
1 parent efdde94 commit 82f0e1c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.triassic.geyserdebuginfo.command.commands.ToggleCommand;
import com.triassic.geyserdebuginfo.configuration.Configuration;
import com.triassic.geyserdebuginfo.configuration.ConfigurationContainer;
import com.triassic.geyserdebuginfo.display.DisplayManager;
import com.triassic.geyserdebuginfo.listener.PlayerJoinListener;
import com.triassic.geyserdebuginfo.manager.PlayerDataManager;
import com.triassic.geyserdebuginfo.placeholder.PlaceholderManager;
Expand Down Expand Up @@ -36,6 +37,8 @@ public class GeyserDebugInfo implements Extension {
private PlayerDataManager playerDataManager;
@Getter
private PlaceholderManager placeholderManager;
@Getter
private DisplayManager displayManager;

private ConfigurationContainer configContainer;

Expand Down Expand Up @@ -73,17 +76,18 @@ public void onPreInitialize(GeyserPreInitializeEvent event) {

this.playerDataManager = new PlayerDataManager(this.dataFolder().toFile(), logger(), false);
this.placeholderManager = new PlaceholderManager();
this.displayManager = new DisplayManager(this);
this.eventBus().register(new PlayerJoinListener(this));

Stream.of(
new PlayerPlaceholderProvider(),
new ServerPlaceholderProvider()
).forEach(placeholderManager::registerProvider);
).forEach(placeholderManager::register);

Stream.of(
new MathModifierProvider(),
new TextModifierProvider()
).forEach(placeholderManager::registerProvider);
).forEach(placeholderManager::register);

logger.info("Enabled in " + (System.currentTimeMillis() - startTime) + "ms");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@

public class ToggleCommand extends AbstractCommand {

private final GeyserDebugInfo instance;
private final PlayerDataManager playerDataManager;
private final DisplayManager displayManager;

public ToggleCommand(final GeyserDebugInfo instance) {
super(instance, "toggle", "Toggle the display of the F3 debug menu", true, true);
this.instance = instance;
this.playerDataManager = instance.getPlayerDataManager();
this.displayManager = new DisplayManager(instance);
this.displayManager = instance.getDisplayManager();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.geysermc.geyser.session.GeyserSession;

import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
Expand All @@ -32,40 +33,34 @@ public DisplayManager(GeyserDebugInfo instance) {
public void subscribePlayer(final GeyserSession session, final DisplayType displayType) {
asyncExecutor.submit(() -> {
Display display = createDisplay(session, displayType);
activeDisplays.computeIfAbsent(session, k -> ConcurrentHashMap.newKeySet()).add(display);
Set<Display> displays = activeDisplays.computeIfAbsent(session, k -> ConcurrentHashMap.newKeySet());
displays.add(display);
display.startUpdating(executor);

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

public void unsubscribePlayer(final GeyserSession session, final DisplayType displayType) {
asyncExecutor.submit(() -> {
Set<Display> displays = activeDisplays.get(session);
if (displays != null) {
displays.stream()
.filter(display -> display.getDisplayType() == displayType)
.findFirst()
.ifPresent(display -> {
displays.remove(display);
display.stopUpdating();
if (displays.isEmpty()) {
activeDisplays.remove(session);
}
});
asyncExecutor.submit(() -> Optional.ofNullable(activeDisplays.get(session)).ifPresent(displays -> {
for (Display display : displays) {
if (display.getDisplayType() == displayType) {
displays.remove(display);
display.stopUpdating();
if (displays.isEmpty()) {
activeDisplays.remove(session);
}
playerDataManager.setDisplayEnabled(session.playerUuid(), displayType, false);
break;
}
}
});

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

private Display createDisplay(final GeyserSession session, final DisplayType displayType) {
return switch (displayType) {
case ACTIONBAR -> new ActionBarDisplay(instance, session);
case BOSSBAR -> {
long entityId = session.getEntityCache().getNextEntityId().incrementAndGet();
yield new BossBarDisplay(instance, session, entityId);
}
case BOSSBAR -> new BossBarDisplay(instance, session);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class ActionBarDisplay extends Display {

private final GeyserDebugInfo instance;
private final SetTitlePacket titlePacket;
private final PlaceholderManager placeholderManager;

public ActionBarDisplay(GeyserDebugInfo instance, @NotNull GeyserSession session) {
super(session, DisplayType.ACTIONBAR, 50);
Expand All @@ -25,12 +24,11 @@ public ActionBarDisplay(GeyserDebugInfo instance, @NotNull GeyserSession session
titlePacket.setPlatformOnlineId("");

this.instance = instance;
this.placeholderManager = instance.getPlaceholderManager();
}

@Override
public void updateDisplay() {
titlePacket.setText(placeholderManager.setPlaceholders(session, instance.getConfig().getDisplay().getActionBar().getText()));
titlePacket.setText(PlaceholderManager.setPlaceholders(session, instance.getConfig().getDisplay().getActionBar().getText()));
session.sendUpstreamPacket(titlePacket);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@ public class BossBarDisplay extends Display {

private final GeyserDebugInfo instance;
private final BossBar bossBar;
private final PlaceholderManager placeholderManager;

public BossBarDisplay(GeyserDebugInfo instance, @NotNull GeyserSession session, long entityId) {
public BossBarDisplay(GeyserDebugInfo instance, @NotNull GeyserSession session) {
super(session, DisplayType.BOSSBAR, 50);
long entityId = session.getEntityCache().getNextEntityId().incrementAndGet();
this.bossBar = new BossBar(session, entityId, Component.empty(), 1.0f, 1, 0, 0);
session.getEntityCache().addBossBar(session.playerUuid(), bossBar);

this.instance = instance;
this.placeholderManager = instance.getPlaceholderManager();
}

@Override
public void updateDisplay() {
List<String> displayFormat = instance.getConfig().getDisplay().getBossBar().getText();
String displayText = displayFormat.stream()
.map(line -> placeholderManager.setPlaceholders(session, line))
.map(line -> PlaceholderManager.setPlaceholders(session, line))
.collect(Collectors.joining("\n"));

bossBar.updateTitle(Component.text(displayText));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
package com.triassic.geyserdebuginfo.placeholder;

import org.geysermc.geyser.session.GeyserSession;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PlaceholderManager {

private final Map<String, PlaceholderProvider> providers = new ConcurrentHashMap<>();
private final Map<String, ModifierProvider> modifiers = new ConcurrentHashMap<>();

public void registerProvider(PlaceholderProvider provider) {
providers.put(provider.getIdentifier(), provider);
}

public void registerProvider(ModifierProvider provider) {
for (String modifier : provider.getModifiers()) {
modifiers.put(modifier, provider);
}
}

public void unregisterProvider(PlaceholderProvider provider) {
providers.remove(provider.getIdentifier());
}

public void unregisterProvider(ModifierProvider provider) {
for (String modifier : provider.getModifiers()) {
modifiers.remove(modifier);
}
}
private static final Map<String, PlaceholderProvider> providers = new ConcurrentHashMap<>();
private static final Map<String, ModifierProvider> modifiers = new ConcurrentHashMap<>();

/**
* Resolves placeholders in the given text based on the provided Geyser session.
Expand All @@ -38,7 +19,8 @@ public void unregisterProvider(ModifierProvider provider) {
* @param text The text containing placeholders to resolve.
* @return The text with placeholders resolved to their respective values.
*/
public String setPlaceholders(GeyserSession session, String text) {
@NotNull
public static String setPlaceholders(final GeyserSession session, final @NotNull String text) {
StringBuilder finalResult = new StringBuilder();
int cursor = 0;

Expand Down Expand Up @@ -88,4 +70,14 @@ public String setPlaceholders(GeyserSession session, String text) {

return finalResult.toString();
}

public void register(PlaceholderProvider provider) {
providers.put(provider.getIdentifier(), provider);
}

public void register(ModifierProvider provider) {
for (String modifier : provider.getModifiers()) {
modifiers.put(modifier, provider);
}
}
}

0 comments on commit 82f0e1c

Please sign in to comment.