Skip to content

Commit

Permalink
moving dimension logic to specific class, checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyagara committed Sep 26, 2024
1 parent 9effcac commit 35a670d
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 152 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ goal with this is to add on the coop experience I really enjoyed from Cooperativ
## TODO

- Build system needs some work, merging jars is really slow, shadowing is probably not done right, add sources to the artifacts.
- Prevent toast from showing for synced players, at the moment the player that was synced will get a toast notification after completing the already completed advancement.
- Add Discord commands to retrieve general information about the server, TPS, uptime, etc.
- Maybe move dimension logic to a separate class, maybe in the Discord package.
- Utils class might need to become a package as more features are added.

## Configuration

Expand Down
28 changes: 10 additions & 18 deletions common/src/main/java/com/cooptweaks/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.cooptweaks;

import com.cooptweaks.types.ConfigMap;
import dev.architectury.platform.Platform;

import java.io.IOException;
Expand All @@ -13,24 +12,24 @@ public final class Configuration {
private static final Path MAIN_PATH = Platform.getConfigFolder().resolve("cooptweaks");

/** Folder where advancement progress per world(seed) is saved. */
private static final Path ADVANCEMENTS_SAVE_PATH = MAIN_PATH.resolve("saves");
public static final Path ADVANCEMENTS_SAVE_PATH = MAIN_PATH.resolve("saves");

private static final Path DISCORD_PATH = MAIN_PATH.resolve("discord.toml");

private static ConfigMap DISCORD_CONFIG = null;
/** Discord bot configuration file. */
public static final Path DISCORD_PATH = MAIN_PATH.resolve("discord.toml");

/**
Verify that the necessary config files exist, if not, create them, generating the default config files.
Verify that the necessary config files exist, if not, generate the default config files.
<p>
The config folder "cooptweaks" which should contain:
The config folder "cooptweaks" should contain:
<ul>
<li>saves</li>
<li>discord.toml</li>
</ul>
*/
public Configuration() {
public static void Verify() {
if (!Files.exists(MAIN_PATH)) {
try {
Main.LOGGER.info("Creating config folder.");
Files.createDirectory(MAIN_PATH);
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -39,6 +38,7 @@ public Configuration() {

if (!Files.exists(ADVANCEMENTS_SAVE_PATH)) {
try {
Main.LOGGER.info("Creating advancements save folder.");
Files.createDirectory(ADVANCEMENTS_SAVE_PATH);
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -51,20 +51,12 @@ public Configuration() {
token =
application_id =
channel_id =""";

Main.LOGGER.info("Creating discord config file.");
Files.writeString(DISCORD_PATH, config, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

DISCORD_CONFIG = new ConfigMap(DISCORD_PATH);
}

public Path getAdvancementsSavePath() {
return ADVANCEMENTS_SAVE_PATH;
}

public ConfigMap getDiscordConfig() {
return DISCORD_CONFIG;
}
}
44 changes: 44 additions & 0 deletions common/src/main/java/com/cooptweaks/Dimension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.cooptweaks;

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

public final class Dimension {
/** Maps player names to their current dimension. */
private static final ConcurrentHashMap<String, String> PLAYER_CURRENT_DIMENSION_ID = new ConcurrentHashMap<>();

/** Maps dimension IDs to their names. */
private static final HashMap<String, String> DIMENSIONS = new HashMap<>(Map.of(
"minecraft:overworld", "Overworld",
"minecraft:the_nether", "Nether",
"minecraft:the_end", "End"
));

/**
Gets the dimension name of a player.
@param player The player's name.
@return The dimension name of the player. Example: "Overworld".
*/
public static String getPlayerDimension(String player) {
String dimensionId = getPlayerCurrentDimension(player);
String dimension = DIMENSIONS.get(dimensionId);

if (dimension == null) {
return dimensionId;
}

return dimension;
}

/** Gets the current dimension ID of a player. */
public static String getPlayerCurrentDimension(String name) {
return PLAYER_CURRENT_DIMENSION_ID.get(name);
}

/** Updates the current dimension ID of a player. */
public static void updatePlayerCurrentDimension(String name, String dimensionId) {
PLAYER_CURRENT_DIMENSION_ID.put(name, dimensionId);
}
}
48 changes: 18 additions & 30 deletions common/src/main/java/com/cooptweaks/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,33 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

public final class Main {
public static final String MOD_ID = "cooptweaks";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

public static final Configuration CONFIG = new Configuration();

public static final Discord DISCORD = Discord.getInstance();
public static final Advancements ADVANCEMENTS = Advancements.getInstance();

/** Maps player names to their current dimension. */
private static final ConcurrentHashMap<String, String> PLAYER_CURRENT_DIMENSION_ID = new ConcurrentHashMap<>();
public static final Discord DISCORD = new Discord();
public static final Advancements ADVANCEMENTS = new Advancements();

public static void init() {
LifecycleEvent.SERVER_BEFORE_START.register(server -> {
DISCORD.Start(CONFIG.getDiscordConfig());
});
Configuration.Verify();

LifecycleEvent.SERVER_BEFORE_START.register(server -> DISCORD.Start());

LifecycleEvent.SERVER_STARTED.register(server -> {
DISCORD.NotifyStarted(server);

// Requires the server to be started since the seed won't be available until then.
// This might be changed if manually reading the level.dat, haven't seen any issue from doing it this way yet.
ADVANCEMENTS.LoadAdvancements(server, CONFIG.getAdvancementsSavePath());
ADVANCEMENTS.LoadAdvancements(server);
});

LifecycleEvent.SERVER_STOPPING.register(server -> {
DISCORD.Stop();
});
LifecycleEvent.SERVER_STOPPING.register(server -> DISCORD.Stop());

PlayerEvent.PLAYER_JOIN.register(player -> {
String name = player.getName().getString();
String dimensionId = player.getWorld().getRegistryKey().getValue().toString();

updatePlayerCurrentDimension(name, dimensionId);
Dimension.updatePlayerCurrentDimension(name, dimensionId);
DISCORD.PlayerJoined(name);
ADVANCEMENTS.SyncPlayerOnJoin(player, name);
});
Expand All @@ -55,8 +46,15 @@ public static void init() {
String name = player.getName().getString();
String dimensionId = newWorld.getValue().toString();

updatePlayerCurrentDimension(name, dimensionId);
DISCORD.PlayerChangedDimension(name, Utils.getPlayerDimension(name));
Dimension.updatePlayerCurrentDimension(name, dimensionId);
DISCORD.PlayerChangedDimension(name, Dimension.getPlayerDimension(name));
});

PlayerEvent.PLAYER_RESPAWN.register((newPlayer, conqueredEnd, removalReason) -> {
String name = newPlayer.getName().getString();
String dimensionId = newPlayer.getWorld().getRegistryKey().getValue().toString();

Dimension.updatePlayerCurrentDimension(name, dimensionId);
});

// Maybe use DECORATE event instead?
Expand All @@ -71,7 +69,7 @@ public static void init() {
String dimensionId = entity.getWorld().getRegistryKey().getValue().toString();
BlockPos pos = entity.getBlockPos();

updatePlayerCurrentDimension(name, dimensionId);
Dimension.updatePlayerCurrentDimension(name, dimensionId);
DISCORD.PlayerDied(name, pos, source.getDeathMessage(entity));
}

Expand All @@ -81,14 +79,4 @@ public static void init() {
GrantCriterionCallback.EVENT.register(ADVANCEMENTS::OnCriterion);
CommandRegistrationEvent.EVENT.register(ADVANCEMENTS::RegisterCommands);
}

/** Gets the current dimension ID of a player. */
public static String getPlayerCurrentDimension(String name) {
return PLAYER_CURRENT_DIMENSION_ID.get(name);
}

/** Updates the current dimension ID of a player. */
public static void updatePlayerCurrentDimension(String name, String dimensionId) {
PLAYER_CURRENT_DIMENSION_ID.put(name, dimensionId);
}
}
39 changes: 0 additions & 39 deletions common/src/main/java/com/cooptweaks/Utils.java

This file was deleted.

34 changes: 15 additions & 19 deletions common/src/main/java/com/cooptweaks/advancements/Advancements.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.cooptweaks.advancements;

import com.cooptweaks.Configuration;
import com.cooptweaks.Main;
import com.cooptweaks.Utils;
import com.cooptweaks.discord.Discord;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
Expand Down Expand Up @@ -33,25 +33,12 @@
import java.util.concurrent.ConcurrentHashMap;

public final class Advancements {
private Advancements() {
}

private static Advancements INSTANCE = null;

public synchronized static Advancements getInstance() {
if (INSTANCE == null) {
INSTANCE = new Advancements();
}

return INSTANCE;
}

private static final Discord DISCORD = Main.DISCORD;

private static MinecraftServer SERVER;

public static final HashMap<Identifier, AdvancementEntry> ALL_ADVANCEMENTS = new HashMap<>(122);
public static final ConcurrentHashMap<String, AdvancementEntry> COMPLETED_ADVANCEMENTS = new ConcurrentHashMap<>(122);
private static final HashMap<Identifier, AdvancementEntry> ALL_ADVANCEMENTS = HashMap.newHashMap(122);
private static final ConcurrentHashMap<String, AdvancementEntry> COMPLETED_ADVANCEMENTS = new ConcurrentHashMap<>(122);

private static FileChannel CURRENT_SEED_FILE;

Expand All @@ -60,7 +47,7 @@ private static synchronized int appendToSave(String text) throws IOException {
return CURRENT_SEED_FILE.write(buffer);
}

public void LoadAdvancements(MinecraftServer server, Path savePath) {
public void LoadAdvancements(MinecraftServer server) {
SERVER = server;

int totalAdvancements = loadServerAdvancements(server);
Expand All @@ -72,7 +59,7 @@ public void LoadAdvancements(MinecraftServer server, Path savePath) {
}

try {
int savedAdvancements = loadSaveAdvancements(server, savePath);
int savedAdvancements = loadSaveAdvancements(server, Configuration.ADVANCEMENTS_SAVE_PATH);
if (savedAdvancements == 0) {
Main.LOGGER.info("No completed advancements data to load. Initialized new save file.");
} else {
Expand Down Expand Up @@ -212,8 +199,17 @@ public void RegisterCommands(CommandDispatcher<ServerCommandSource> dispatcher,
);
}

/**
Gets the advancements progress of the server.
@return The advancements progress of the server. Example: "10/122".
*/
public static String getAdvancementsProgress() {
return String.format("%d/%d", COMPLETED_ADVANCEMENTS.size(), ALL_ADVANCEMENTS.size());
}

private int progressCommand(CommandContext<ServerCommandSource> context) {
context.getSource().sendFeedback(() -> Text.literal(String.format("%s advancements completed so far.", Utils.getAdvancementsProgress())), false);
context.getSource().sendFeedback(() -> Text.literal(String.format("%s advancements completed so far.", getAdvancementsProgress())), false);
return 1;
}
}
Loading

0 comments on commit 35a670d

Please sign in to comment.