Skip to content

Commit

Permalink
Config.Verify method, including Discord4J
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyagara committed Sep 20, 2024
1 parent 0044bc5 commit 9079a81
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 51 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Very much a work in progress and not thoroughly tested.

## TODO

- Include the Discord4J library in the jar, not sure how to do this.
- There are 122 advancements but the `progress` command shows all criterion for them, giving a much bigger number, might
rework how things are done in the class to fix this.
- Add Discord commands to retrieve general information about the server, TPS, uptime, etc.
Expand Down
32 changes: 12 additions & 20 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'com.gradleup.shadow' version '8.3.2'
id 'fabric-loom' version '1.7-SNAPSHOT'
id 'maven-publish'
}

version = project.mod_version
Expand Down Expand Up @@ -38,7 +38,17 @@ dependencies {
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

implementation "com.discord4j:discord4j-core:3.2.6"
shadow implementation("com.discord4j:discord4j-core:3.2.6")
}

shadowJar {
configurations = [project.configurations.shadow]
from(sourceSets.client.output)
}

remapJar {
dependsOn(shadowJar)
inputFile = tasks.shadowJar.archiveFile
}

processResources {
Expand Down Expand Up @@ -68,21 +78,3 @@ jar {
rename { "${it}_${project.base.archivesName.get()}"}
}
}

// configure the maven publication
publishing {
publications {
create("mavenJava", MavenPublication) {
artifactId = project.archives_base_name
from components.java
}
}

// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
repositories {
// Add repositories to publish to here.
// Notice: This block does NOT have the same function as the block in the top level.
// The repositories here will be used for publishing your artifact, not for
// retrieving dependencies.
}
}
11 changes: 1 addition & 10 deletions src/main/java/com/cooptweaks/Advancements.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import discord4j.rest.util.Color;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.advancement.Advancement;
import net.minecraft.advancement.AdvancementDisplay;
import net.minecraft.advancement.AdvancementEntry;
Expand Down Expand Up @@ -33,19 +32,11 @@ public final class Advancements {
public static Map<Identifier, AdvancementEntry> ALL_ADVANCEMENTS = new HashMap<>();
public static Map<String, AdvancementEntry> COMPLETED_ADVANCEMENTS = new HashMap<>();

// Folder where advancement progress per world(seed) is saved.
private static final Path ADVANCEMENTS_SAVES_PATH = FabricLoader.getInstance().getConfigDir().resolve("cooptweaks/saves/");
private static FileChannel CURRENT_SEED_FILE;

private static Advancements INSTANCE = null;

private Advancements() {
// Generating config folders.
try {
Files.createDirectories(ADVANCEMENTS_SAVES_PATH);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static Advancements getInstance() {
Expand Down Expand Up @@ -76,7 +67,7 @@ public void LoadAdvancements(MinecraftServer server) {

Server.LOGGER.info("Loaded {} advancements.", totalAdvancements);

Path save = ADVANCEMENTS_SAVES_PATH.resolve(String.valueOf(server.getOverworld().getSeed()));
Path save = Config.SAVES.resolve(String.valueOf(server.getOverworld().getSeed()));

if (!Files.exists(save)) {
try {
Expand Down
22 changes: 4 additions & 18 deletions src/main/java/com/cooptweaks/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import discord4j.gateway.intent.IntentSet;
import discord4j.rest.entity.RestChannel;
import discord4j.rest.util.Color;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.message.SignedMessage;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
Expand All @@ -27,9 +26,6 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.GlobalPos;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.*;

public final class Bridge {
Expand Down Expand Up @@ -64,25 +60,15 @@ public static Bridge getInstance() {
}

private Bridge() {
Path DISCORD_CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("cooptweaks/discord.toml");

try {
if (!Files.exists(DISCORD_CONFIG_PATH)) {
String defaultConfig = "token = " + System.lineSeparator() + "channel_id = " + System.lineSeparator() + "guild_id = ";
Files.writeString(DISCORD_CONFIG_PATH, defaultConfig, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

Map<String, String> config = Config.Parse(DISCORD_CONFIG_PATH);
public void Start() {
Map<String, String> config = Config.Parse(Config.DISCORD);
TOKEN = config.get("token");
CHANNEL_ID = config.get("channel_id");
GUILD_ID = config.get("guild_id");
}

public void Start() {
if (TOKEN.isEmpty() || CHANNEL_ID.isEmpty()) {
if (TOKEN.isEmpty() || CHANNEL_ID.isEmpty() || GUILD_ID.isEmpty()) {
Server.LOGGER.warn("Discord bot is not properly configured.");
return;
}
Expand Down
43 changes: 42 additions & 1 deletion src/main/java/com/cooptweaks/Config.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,55 @@
package com.cooptweaks;

import net.fabricmc.loader.api.FabricLoader;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// Simple parser for toml config files.
public class Config {
// Main config folder.
public static final Path MAIN = FabricLoader.getInstance().getConfigDir().resolve("cooptweaks");
// Folder where advancement progress per world(seed) is saved.
public static final Path SAVES = MAIN.resolve("saves");
// Discord bot config file.
public static final Path DISCORD = MAIN.resolve("discord.toml");

// Verify that the necessary config files exist.
// Folder "cooptweaks" which should contain:
// - 'saves' folder
// - 'discord.toml'
public static void Verify() {
if (!Files.exists(MAIN)) {
try {
Files.createDirectory(MAIN);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

if (!Files.exists(SAVES)) {
try {
Files.createDirectory(SAVES);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

if (!Files.exists(DISCORD)) {
try {
String defaultConfig = "token = " + System.lineSeparator() + "channel_id = " + System.lineSeparator() + "guild_id = ";
Files.writeString(DISCORD, defaultConfig, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

// Simple parser for toml config files.
public static Map<String, String> Parse(Path path) {
List<String> lines;

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/cooptweaks/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.cooptweaks.events.GrantCriterionCallback;
import com.cooptweaks.events.PlayerDeathCallback;
import discord4j.rest.util.Color;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
Expand All @@ -22,6 +21,8 @@ public class Server implements ModInitializer {

@Override
public void onInitialize() {
Config.Verify();

ServerLifecycleEvents.SERVER_STARTING.register((server) -> {
BRIDGE.Start();
});
Expand Down

0 comments on commit 9079a81

Please sign in to comment.