Skip to content

Commit

Permalink
Make displays more modular, add actionbar support
Browse files Browse the repository at this point in the history
  • Loading branch information
RealTriassic committed Oct 19, 2024
1 parent 0e38db2 commit c91d4da
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.triassic.geyserdebuginfo.configuration.Configuration;
import com.triassic.geyserdebuginfo.configuration.ConfigurationContainer;
import com.triassic.geyserdebuginfo.listener.PlayerJoinListener;
import com.triassic.geyserdebuginfo.manager.BossBarManager;
import com.triassic.geyserdebuginfo.manager.PlaceholderManager;
import com.triassic.geyserdebuginfo.manager.PlayerDataManager;
import com.triassic.geyserdebuginfo.placeholder.modifiers.MathModifierProvider;
Expand Down Expand Up @@ -34,8 +33,6 @@ public class GeyserDebugInfo implements Extension {
@Getter
private Configuration config;
@Getter
private BossBarManager bossBarManager;
@Getter
private PlayerDataManager playerDataManager;
@Getter
private PlaceholderManager placeholderManager;
Expand Down Expand Up @@ -76,7 +73,6 @@ public void onPreInitialize(GeyserPreInitializeEvent event) {

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

Stream.of(
Expand All @@ -102,7 +98,6 @@ public void onDefineCommands(GeyserDefineCommandsEvent event) {

@Subscribe
public void onShutdown(GeyserShutdownEvent event) {
bossBarManager.shutdown();
playerDataManager.savePlayerData();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,34 @@

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

public class ToggleCommand extends AbstractCommand {

private final BossBarManager bossBarManager;
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.bossBarManager = instance.getBossBarManager();
this.instance = instance;
this.playerDataManager = instance.getPlayerDataManager();
this.displayManager = new DisplayManager(instance);
}

@Override
protected void execute(@NotNull CommandSource source, @NotNull Command command, @NotNull String[] args) {
final GeyserSession session = (GeyserSession) source.connection();
final SessionPlayerEntity player = session.getPlayerEntity();

if (player != null && !playerDataManager.isF3Enabled(player.getUuid())) {
bossBarManager.createBossBar(player);
} else {
bossBarManager.removeBossBar(player);
playerDataManager.setF3Enabled(player.getUuid(), false);
switch (args[0]) {
case "actionbar" -> displayManager.subscribePlayer(session, DisplayType.ACTIONBAR);
case "bossbar" -> displayManager.subscribePlayer(session, DisplayType.BOSSBAR);
}
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/triassic/geyserdebuginfo/display/Display.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.triassic.geyserdebuginfo.display;

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

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public abstract class Display {

protected final GeyserSession session;
@Getter
protected final DisplayType displayType;
protected final long refreshInterval;
private ScheduledFuture<?> updateTask;

public Display(@NotNull GeyserSession session, @NotNull DisplayType displayType, long refreshInterval) {
this.session = session;
this.displayType = displayType;
this.refreshInterval = refreshInterval;
}

/**
* Updates the display with the given text.
*/
public abstract void updateDisplay();

/**
* Removes the display from the player.
*/
public abstract void removeDisplay();

/**
* Starts the scheduled update task for the display.
*/
public void startUpdating(ScheduledExecutorService executor) {
updateTask = executor.scheduleAtFixedRate(this::updateDisplay, 0, refreshInterval, TimeUnit.MILLISECONDS);
}

/**
* Stops the scheduled update task for the display.
*/
public void stopUpdating() {
if (updateTask != null) {
updateTask.cancel(false);
updateTask = null;
}
removeDisplay();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.triassic.geyserdebuginfo.display;

public enum DisplayType {
ACTIONBAR,
BOSSBAR
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.triassic.geyserdebuginfo.display.displays;

import com.triassic.geyserdebuginfo.GeyserDebugInfo;
import com.triassic.geyserdebuginfo.display.Display;
import com.triassic.geyserdebuginfo.display.DisplayType;
import com.triassic.geyserdebuginfo.manager.PlaceholderManager;
import org.cloudburstmc.protocol.bedrock.packet.SetTitlePacket;
import org.geysermc.geyser.session.GeyserSession;
import org.jetbrains.annotations.NotNull;

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);

this.titlePacket = new SetTitlePacket();
titlePacket.setType(SetTitlePacket.Type.ACTIONBAR);
titlePacket.setFadeOutTime(1);
titlePacket.setStayTime(2);
titlePacket.setXuid("");
titlePacket.setPlatformOnlineId("");

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

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

@Override
public void removeDisplay() {
titlePacket.setText("");
session.sendUpstreamPacket(titlePacket);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.triassic.geyserdebuginfo.display.displays;

import com.triassic.geyserdebuginfo.GeyserDebugInfo;
import com.triassic.geyserdebuginfo.display.Display;
import com.triassic.geyserdebuginfo.display.DisplayType;
import com.triassic.geyserdebuginfo.manager.PlaceholderManager;
import net.kyori.adventure.text.Component;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.BossBar;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.stream.Collectors;

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) {
super(session, DisplayType.BOSSBAR, 50);
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))
.collect(Collectors.joining("\n"));

bossBar.updateTitle(Component.text(displayText));
}

@Override
public void removeDisplay() {
bossBar.removeBossBar();
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,36 @@
package com.triassic.geyserdebuginfo.listener;

import com.triassic.geyserdebuginfo.GeyserDebugInfo;
import com.triassic.geyserdebuginfo.manager.BossBarManager;
import com.triassic.geyserdebuginfo.manager.PlayerDataManager;
import org.geysermc.event.subscribe.Subscribe;
import org.geysermc.geyser.api.event.bedrock.SessionDisconnectEvent;
import org.geysermc.geyser.api.event.bedrock.SessionJoinEvent;
import org.geysermc.geyser.entity.type.player.SessionPlayerEntity;
import org.geysermc.geyser.session.GeyserSession;

/**
* This class is responsible for listening for player connection events.
*/
public class PlayerJoinListener {

private final BossBarManager bossBarManager;
private final PlayerDataManager playerDataManager;

public PlayerJoinListener(
final GeyserDebugInfo instance
) {
this.bossBarManager = instance.getBossBarManager();
this.playerDataManager = instance.getPlayerDataManager();
}

/**
* Called when a player joins.
*/
@Subscribe
public void onJoin(final SessionJoinEvent event) {
final GeyserSession session = (GeyserSession) event.connection();
final SessionPlayerEntity player = session.getPlayerEntity();

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

/**
* Called when a player disconnects.
*/
@Subscribe
public void onDisconnect(final SessionDisconnectEvent event) {
final GeyserSession session = (GeyserSession) event.connection();
bossBarManager.removeBossBar(session.getPlayerEntity(), false);
// TODO: Remove Bossbar
}
}

This file was deleted.

Loading

0 comments on commit c91d4da

Please sign in to comment.