Skip to content

Commit

Permalink
moving loadSavedAdvancements to level load event
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyagara committed Oct 3, 2024
1 parent 7efce48 commit 80a7d7d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 46 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ goal with this is to add on the coop experience I really enjoyed from Cooperativ
## TODO

- Enable/disable modules, allow option to disable the relay but not the syncing.
- Maybe also link the item in the Discord chat.
- Maybe use a small database library for storage as it might be useful for other ideas.
- Build system needs some work, shadowing is probably not done right, add sources to the artifacts.
- Add Discord commands to retrieve general information about the server, TPS, etc.
Expand Down
68 changes: 28 additions & 40 deletions common/src/main/java/com/cooptweaks/Advancements.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.advancement.*;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -69,36 +70,10 @@ private static synchronized void appendToSave(String text) {
}
}

public void loadAdvancements(MinecraftServer server) {
/** Loads all advancements from the server. */
public void loadServerAdvancements(MinecraftServer server) {
SERVER = server;

int totalAdvancements = loadServerAdvancements(server);
if (totalAdvancements == 0) {
Main.LOGGER.error("No advancements loaded from the server.");
return;
} else {
Main.LOGGER.info("Loaded {} advancements from the server.", totalAdvancements);
}

if (ALL_CRITERIA.isEmpty()) {
Main.LOGGER.error("No criteria loaded from the server.");
} else {
Main.LOGGER.info("Loaded {} criteria from the server.", ALL_CRITERIA.size());
}

try {
int savedAdvancements = loadSaveAdvancements(server);
if (savedAdvancements == 0) {
Main.LOGGER.info("No completed advancements data to load. Initialized new save file.");
} else {
Main.LOGGER.info("{} completed advancements loaded from the save file.", savedAdvancements);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static int loadServerAdvancements(MinecraftServer server) {
Collection<AdvancementEntry> advancements = server.getAdvancementLoader().getAdvancements();

for (AdvancementEntry entry : advancements) {
Expand All @@ -116,20 +91,29 @@ private static int loadServerAdvancements(MinecraftServer server) {
});
}

return ALL_ADVANCEMENTS.size();
if (ALL_CRITERIA.isEmpty()) {
Main.LOGGER.error("No criteria loaded from the server.");
} else {
Main.LOGGER.info("Loaded {} criteria from the server.", ALL_CRITERIA.size());
}

int totalAdvancements = ALL_ADVANCEMENTS.size();

if (totalAdvancements == 0) {
Main.LOGGER.error("No advancements loaded from the server.");
} else {
Main.LOGGER.info("Loaded {} advancements from the server.", totalAdvancements);
}
}

private static int loadSaveAdvancements(MinecraftServer server) throws IOException {
Path save = Configuration.ADVANCEMENTS_SAVE_PATH.resolve(String.valueOf(server.getOverworld().getSeed()));
/** Loads the completed advancements for the world from its save file. */
public void loadSavedAdvancements(ServerWorld server) throws IOException {
Path save = Configuration.ADVANCEMENTS_SAVE_PATH.resolve(String.valueOf(server.getSeed()));

if (!Files.exists(save)) {
try {
CURRENT_SEED_FILE = FileChannel.open(save, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (IOException e) {
throw new RuntimeException(e);
}

return 0;
CURRENT_SEED_FILE = FileChannel.open(save, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
Main.LOGGER.info("No completed advancements data to load. Initialized new save file.");
return;
}

BufferedReader reader = new BufferedReader(new FileReader(save.toString()));
Expand All @@ -150,7 +134,7 @@ private static int loadSaveAdvancements(MinecraftServer server) throws IOExcepti

reader.close();
CURRENT_SEED_FILE = FileChannel.open(save, StandardOpenOption.APPEND);
return COMPLETED_ADVANCEMENTS.size();
Main.LOGGER.info("Loaded {} completed advancements from the save file.", COMPLETED_ADVANCEMENTS.size());
}

/** Unloads the advancements and closes the save file. */
Expand All @@ -169,7 +153,7 @@ public void unload() {
}

public void SyncPlayerOnJoin(ServerPlayerEntity player, String name) {
if (COMPLETED_ADVANCEMENTS.isEmpty()) {
if (COMPLETED_ADVANCEMENTS.isEmpty() || ALL_ADVANCEMENTS.isEmpty()) {
return;
}

Expand All @@ -188,6 +172,10 @@ public void SyncPlayerOnJoin(ServerPlayerEntity player, String name) {
}

public void OnCriterion(ServerPlayerEntity currentPlayer, AdvancementEntry entry) {
if (ALL_ADVANCEMENTS.isEmpty()) {
return;
}

Identifier id = entry.id();

if (COMPLETED_ADVANCEMENTS.containsKey(id)) {
Expand Down
19 changes: 14 additions & 5 deletions common/src/main/java/com/cooptweaks/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public final class Main {
public static final String MOD_ID = "cooptweaks";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
Expand All @@ -23,15 +25,22 @@ public final class Main {
public static void init() {
Configuration.verify();

// Start the Discord bot when the server is starting.
LifecycleEvent.SERVER_BEFORE_START.register(server -> DISCORD.Start());

LifecycleEvent.SERVER_LEVEL_LOAD.register(world -> {
try {
ADVANCEMENTS.loadSavedAdvancements(world);
} catch (IOException e) {
throw new RuntimeException(e);
}
});

LifecycleEvent.SERVER_STARTED.register(server -> {
STARTUP = System.currentTimeMillis();
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);
ADVANCEMENTS.loadServerAdvancements(server);
DISCORD.NotifyStarted(server);
});

LifecycleEvent.SERVER_LEVEL_SAVE.register(world -> DISCORD.CyclePresence(world.getPlayers()));
Expand Down Expand Up @@ -69,7 +78,6 @@ public static void init() {
Dimension.updatePlayerCurrentDimension(name, dimensionId);
});

// Maybe use DECORATE event instead?
ChatEvent.RECEIVED.register((player, message) -> {
DISCORD.PlayerSentChatMessage(player, message);
return EventResult.pass();
Expand All @@ -96,6 +104,7 @@ public static void init() {
new LinkCommand().register(dispatcher, registryAccess, environment);
});

// Handle the Link packet sent from a client.
NetworkManager.registerReceiver(NetworkManager.Side.C2S, LinkPacket.PAYLOAD_ID, LinkPacket.CODEC, Link::handlePacket);
}
}

0 comments on commit 80a7d7d

Please sign in to comment.