Skip to content

Commit

Permalink
Minor cleaning, remove redundant comments
Browse files Browse the repository at this point in the history
  • Loading branch information
RealTriassic committed Oct 17, 2024
1 parent 6b9011d commit 9da4cda
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 119 deletions.
18 changes: 9 additions & 9 deletions src/main/java/com/triassic/geyserdebuginfo/GeyserDebugInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ public void onPreInitialize(GeyserPreInitializeEvent event) {
return;
}

this.configContainer = new ConfigurationContainer(dataFolder, logger, Configuration.class);
this.configContainer = new ConfigurationContainer(dataFolder, logger);

if (!configContainer.load(Configuration.class)) {
logger.error("Failed to load the configuration. Please check config.yml for issues.");
extensionManager().disable(this);
return;
}

this.config = configContainer.get();

if (config == null) {
Expand All @@ -85,9 +92,6 @@ public void onPreInitialize(GeyserPreInitializeEvent event) {
logger.info("Enabled in " + (System.currentTimeMillis() - startTime) + "ms");
}

/**
* Defines and registers commands for the extension.
*/
@Subscribe
public void onDefineCommands(GeyserDefineCommandsEvent event) {
Stream.of(
Expand All @@ -96,18 +100,14 @@ public void onDefineCommands(GeyserDefineCommandsEvent event) {
).forEach(command -> event.register(command.createCommand()));
}

/**
* Called during the shutdown process.
* Cleans up resources and saves player data.
*/
@Subscribe
public void onShutdown(GeyserShutdownEvent event) {
bossBarManager.shutdown();
playerDataManager.savePlayerData();
}

public boolean reloadConfig() {
boolean reloaded = configContainer.reload();
boolean reloaded = configContainer.load(configContainer.get().getClass());
if (reloaded)
this.config = configContainer.get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public org.geysermc.geyser.api.command.Command createCommand() {
private void execute(@NonNull CommandSource commandSource, org.geysermc.geyser.api.command.Command command, @NonNull String[] strings) {
if (instance.reloadConfig()) {
commandSource.sendMessage("§aGeyserDebugInfo configuration has been reloaded.");
System.out.println(instance.getConfig().getConfigVersion());
} else {
commandSource.sendMessage("§cFailed to reload GeyserDebugInfo configuration, check console for details.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ public class ConfigurationContainer {
private final Path configFile;
private final ExtensionLogger logger;
private final YamlConfigurationLoader loader;
private final Class<? extends Configuration> configClass;
private final AtomicReference<Configuration> config = new AtomicReference<>();

public ConfigurationContainer(
final Path dataFolder,
final ExtensionLogger logger,
final Class<? extends Configuration> configClass) {
final ExtensionLogger logger
) {
this.logger = logger;
this.configClass = configClass;
this.configFile = dataFolder.resolve("config.yml");

this.loader = YamlConfigurationLoader.builder()
Expand All @@ -42,19 +40,11 @@ public ConfigurationContainer(
.shouldCopyDefaults(true)
.header(HEADER))
.build();

this.load();
}

/**
* Loads the configuration from the file.
* If loading fails, the previous configuration is retained.
*
* @return true if the configuration was loaded successfully, false otherwise.
*/
private boolean load() {
public boolean load(Class<? extends Configuration> clazz) {
try {
final Configuration loadedConfig = loadConfig();
final Configuration loadedConfig = load0(clazz);
config.set(loadedConfig);
return true;
} catch (Throwable e) {
Expand All @@ -63,34 +53,18 @@ private boolean load() {
}
}

/**
* Loads the configuration from the file and creates a new Configuration object.
*
* @return the loaded Configuration object.
* @throws IOException if an error occurs while reading or parsing the file.
*/
private Configuration loadConfig() throws IOException {
CommentedConfigurationNode node = loader.load();
Configuration loadedConfig = node.get(configClass);
private Configuration load0(Class<? extends Configuration> clazz) throws IOException {
final CommentedConfigurationNode node = loader.load();
final Configuration loadedConfig = node.get(clazz);

if (Files.notExists(configFile)) {
node.set(configClass, loadedConfig);
node.set(clazz, loadedConfig);
loader.save(node);
}

return loadedConfig;
}

/**
* Reloads the configuration from the file.
* If reloading fails, the previous configuration is retained.
*
* @return true if the configuration was reloaded successfully, false otherwise.
*/
public boolean reload() {
return load();
}

@Nullable
public Configuration get() {
return config.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* This class is responsible for creating, updating, and removing boss bars for each player.
*/
public class BossBarManager {

private final GeyserDebugInfo instance;
Expand All @@ -38,12 +35,6 @@ public BossBarManager(
executor.scheduleAtFixedRate(this::updateAllBossBars, 0, instance.getConfig().getDisplay().getBossBar().getRefreshInterval(), TimeUnit.MILLISECONDS);
}

/**
* Creates a boss bar for the specified player if it doesn't already exist.
*
* @param player The player for whom to create the boss bar.
* @throws IllegalArgumentException if the player is null.
*/
public void createBossBar(final @NotNull SessionPlayerEntity player) {
final GeyserSession session = player.getSession();

Expand All @@ -57,24 +48,10 @@ public void createBossBar(final @NotNull SessionPlayerEntity player) {
bossBars.put(player, bossBar);
}

/**
* Removes the boss bar for the specified player and updates the player data
* to indicate that the F3 feature is disabled by default.
*
* @param player The player whose boss bar is to be removed.
* @throws IllegalArgumentException if the player is null.
*/
public void removeBossBar(final @NotNull SessionPlayerEntity player) {
removeBossBar(player, true);
}

/**
* Removes the boss bar for the specified player.
*
* @param player The player whose boss bar is to be removed.
* @param updatePlayerData Whether to update the player data to indicate that the F3 feature is disabled.
* @throws IllegalArgumentException if the player is null.
*/
public void removeBossBar(final @NotNull SessionPlayerEntity player, boolean updatePlayerData) {
BossBar bossBar = bossBars.remove(player);
if (bossBar != null)
Expand All @@ -84,21 +61,12 @@ public void removeBossBar(final @NotNull SessionPlayerEntity player, boolean upd
playerDataManager.setF3Enabled(player.getUuid(), false);
}

/**
* Requests a bossbar update for all tracked bossbars.
*/
private void updateAllBossBars() {
for (Map.Entry<SessionPlayerEntity, BossBar> entry : bossBars.entrySet()) {
updateBossBar(entry.getKey());
}
}

/**
* Updates the boss bar with current up-to-date data.
*
* @param player The player whose boss bar is to be updated.
* @throws IllegalArgumentException if the player is null.
*/
private void updateBossBar(final @NotNull SessionPlayerEntity player) {
BossBar bossBar = bossBars.get(player);

Expand All @@ -113,9 +81,6 @@ private void updateBossBar(final @NotNull SessionPlayerEntity player) {
}
}

/**
* Shuts down the executor service, stopping all updates.
*/
public void shutdown() {
executor.shutdown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public void registerProvider(ModifierProvider provider) {
}
}

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

public void unregisterProvider(ModifierProvider provider) {
for (String modifier : provider.getModifiers()) {
modifiers.remove(modifier);
}
}

/**
* Resolves placeholders in the given text based on the provided Geyser session.
* Placeholders should be formatted as %provider_placeholder% (e.g., %chunk_x% or %position_x%).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ public class PlayerDataManager {
private final Map<UUID, Boolean> playerF3States;
private final boolean defaultEnabled;

/**
* Constructs a new PlayerDataManager and loads player data from the YAML file.
*
* @param dataFolder The data folder to use for storing the player data file.
* @param logger The logger used for logging errors and information during operations.
* @param defaultEnabled If true, track players with F3 disabled; otherwise, track players with F3 enabled.
*/
public PlayerDataManager(
final File dataFolder,
final ExtensionLogger logger,
Expand All @@ -40,21 +33,13 @@ public PlayerDataManager(
loadPlayerData();
}

/**
* Creates DumperOptions for the YAML configuration.
*
* @return DumperOptions configured for the YAML.
*/
private DumperOptions createDumperOptions() {
DumperOptions options = new DumperOptions();
options.setIndent(2);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
return options;
}

/**
* Loads player data from the YAML file.
*/
private void loadPlayerData() {
if (!playerDataFile.exists()) return;

Expand All @@ -70,9 +55,6 @@ private void loadPlayerData() {
}
}

/**
* Saves the current player F3 states to the YAML file.
*/
public void savePlayerData() {
try (FileWriter writer = new FileWriter(playerDataFile)) {
Map<String, Boolean> data = new HashMap<>();
Expand All @@ -88,12 +70,6 @@ public void savePlayerData() {
}
}

/**
* Sets the F3 enabled state for a player.
*
* @param playerUuid The UUID of the player.
* @param enabled The F3 enabled state to set.
*/
public void setF3Enabled(UUID playerUuid, boolean enabled) {
if ((defaultEnabled && enabled) || (!defaultEnabled && !enabled)) {
playerF3States.remove(playerUuid);
Expand All @@ -103,12 +79,6 @@ public void setF3Enabled(UUID playerUuid, boolean enabled) {
savePlayerData();
}

/**
* Checks if F3 is enabled for the specified player.
*
* @param playerUuid The UUID of the player.
* @return true if F3 is enabled, false otherwise.
*/
public boolean isF3Enabled(UUID playerUuid) {
return playerF3States.getOrDefault(playerUuid, defaultEnabled);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
import java.util.Collections;
import java.util.List;

/**
* Abstract class for defining modifier providers.
* Subclasses can provide specific modifier values
* to be used within text by the Geyser session.
*/
public abstract class ModifierProvider {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Abstract class for defining placeholder providers.
* Subclasses can provide specific placeholder values
* to be used within text by the Geyser session.
*/
public abstract class PlaceholderProvider {

/**
Expand Down

0 comments on commit 9da4cda

Please sign in to comment.