Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ This file is basically a checklist of things to do in the codebase.

## Chores

* Add comments
* fix possible race condition so that the bot doesn't start with nonexistent ids
* parse timestamp message
* figure out the black magic of `ServerMessageEvents.GAME_MESSAGE` (?)

## Implement

* Performance improvements, don't know how yet
* Performance improvements, don't know how yet
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ processResources {
}

tasks.withType(JavaCompile).configureEach {
it.options.release = 17
it.options.release = 21
}

java {
Expand Down
11 changes: 5 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ org.gradle.parallel=true

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3
minecraft_version=1.21.1
yarn_mappings=1.21.1+build.3
loader_version=0.16.10
tserver_mcver=1.21.1

# Mod Properties
mod_version=1.0.0+fabric
mod_version=1.1.0+fabric.1.21.1
maven_group=com.github.pinmacaroon.dchook
archives_base_name=dchook

# Dependencies
fabric_version=0.97.2+1.20.4

owo_version=0.12.6+1.20.3
fabric_version=0.116.7+1.21.1

org.gradle.warning.mode=None
51 changes: 31 additions & 20 deletions src/main/java/com/github/pinmacaroon/dchook/Hook.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public class Hook implements DedicatedServerModInitializer {
.create();
public static final Version VERSION = new Version.Builder()
.setMajorVersion(1)
.setMinorVersion(0)
.setMinorVersion(1)
.setPatchVersion(0)
.setBuildMetadata("fabric")
//.setPreReleaseVersion("alpha", "2")
.setBuildMetadata("fabric","1","21","1")
// .setPreReleaseVersion("newyear", "1")
.build();
public static final String DOCS_URL = "https://modrinth.com/mod/dchook";
public static final Random RANDOM = new Random(Instant.now().getEpochSecond());
Expand All @@ -45,6 +45,7 @@ public class Hook implements DedicatedServerModInitializer {
);

public static volatile Bot BOT;
public static boolean BOT_ENABLED = false;

private static MinecraftServer MINECRAFT_SERVER;

Expand All @@ -70,15 +71,8 @@ public void onInitializeServer() {
return;
}

if(ModConfigs.FUNCTIONS_BOT_ENABLED){
try {
BOT = new Bot(ModConfigs.FUNCTIONS_BOT_TOKEN);
} catch (Exception e){
LOGGER.error("couldn't initialise bot, two way chat disabled");
LOGGER.error("{}:{}", e.getClass().getName(), e.getMessage());
return;
}
}
if(ModConfigs.FUNCTIONS_BOT_ENABLED) bottedStart();
else botlessStart();

try {
HttpRequest get_webhook = HttpRequest.newBuilder()
Expand All @@ -96,14 +90,16 @@ public void onInitializeServer() {
);
return;
}
Thread bot_rutime_thread = new Thread(() -> {
while (BOT == null) {
Thread.onSpinWait();
}
BOT.setGUILD_ID(body.get("guild_id").getAsLong());
BOT.setCHANNEL_ID(body.get("channel_id").getAsLong());
});
bot_rutime_thread.start();
if (BOT_ENABLED) {
Thread bot_rutime_thread = new Thread(() -> {
while (BOT == null) {
Thread.onSpinWait();
}
BOT.setGUILD_ID(body.get("guild_id").getAsLong());
BOT.setCHANNEL_ID(body.get("channel_id").getAsLong());
});
bot_rutime_thread.start();
}
} catch (Exception e) {
LOGGER.error("{}:{}", e.getClass().getName(), e.getMessage());
throw new RuntimeException(e);
Expand All @@ -118,4 +114,19 @@ public void onInitializeServer() {

EventListeners.registerEventListeners();
}

private void botlessStart(){
LOGGER.info("two way chat has been disabled by the config (botless start)");
}

private void bottedStart(){
try {
BOT = new Bot(ModConfigs.FUNCTIONS_BOT_TOKEN);
BOT_ENABLED = true;
} catch (Exception e){
LOGGER.error("couldn't initialise bot, two way chat disabled! please check your bot token or send a bug report on github!");
LOGGER.error("{}:{}", e.getClass().getName(), e.getMessage());
e.printStackTrace();
}
}
}
57 changes: 0 additions & 57 deletions src/main/java/com/github/pinmacaroon/dchook/bot/CommandParser.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package com.github.pinmacaroon.dchook.bot.commands;

import com.github.pinmacaroon.dchook.Hook;
import com.github.pinmacaroon.dchook.conf.ModConfigs;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;

import java.text.MessageFormat;

public class ListCommand {
public static void run(SlashCommandInteractionEvent event) {
StringBuilder list = new StringBuilder();
list.append("""
There are currently **%d**/%d players online:\s""".formatted(
StringBuilder message = new StringBuilder();
message.append(MessageFormat.format(
ModConfigs.MESSAGES_BOT_LIST,
Hook.getGameServer().getPlayerManager().getCurrentPlayerCount(),
Hook.getGameServer().getPlayerManager().getMaxPlayerCount()
));
Hook.getGameServer().getPlayerManager().getPlayerList().forEach(
serverPlayerEntity -> list.append("`").append(serverPlayerEntity.getName().getString()).append("` ")
serverPlayerEntity -> message.append("`").append(serverPlayerEntity.getName().getString()).append("` ")
);
event.reply(list.toString()).setEphemeral(event.getOption("ephemeral", false, OptionMapping::getAsBoolean))
event.reply(message.toString()).setEphemeral(event.getOption("ephemeral", false, OptionMapping::getAsBoolean))
.queue();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.github.pinmacaroon.dchook.bot.commands;

import com.github.pinmacaroon.dchook.conf.ModConfigs;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.metadata.ModEnvironment;

import java.text.MessageFormat;
import java.util.concurrent.atomic.AtomicInteger;

public class ModsCommand {
Expand All @@ -24,9 +26,9 @@ public static void run(SlashCommandInteractionEvent event) {
});
String response;
if (mods_count.get() == 0) {
response = "The server currently has no required mods, you can join with a vanilla client!";
response = ModConfigs.MESSAGES_BOT_MODS_NONE;
} else {
response = "The server currently has " + mods_count.get() + " required mods:\n" + mod_list;
response = MessageFormat.format(ModConfigs.MESSAGES_BOT_MODS_LIST, mods_count.get()) + "\n" + mod_list;
}
if (response.length() > 2000) {
response = response.substring(0, 1995) + "[...]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.pinmacaroon.dchook.bot.Bot;
import com.github.pinmacaroon.dchook.conf.ModConfigs;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageReference;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.minecraft.text.MutableText;
Expand Down Expand Up @@ -34,9 +35,10 @@ private static MutableText renderMessage(Message message) {
MutableText reply;
MutableText content;

if (message.getMessageReference() != null) {
MessageReference r = message.getMessageReference();
if (r != null) {
reply = Text.literal("<@%s -> ".formatted(
message.getReferencedMessage().getAuthor().getName()
r.getMessage().getAuthor().getName()
));
} else {
reply = Text.literal("<");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
case "about" -> AboutCommand.run(event);
default -> event.reply("""
An internal error occurred! Please send a bug report: \
<https://pinmacaroon.github.io/hook/links.html>""").setEphemeral(true)
<https://github.com/pinmacaroon/hook/issues>""").setEphemeral(true)
.addFiles(FileUpload.fromData(new File("https://pinmacaroon.github.io/hook/res/works.png")))
.queue();
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/github/pinmacaroon/dchook/conf/ModConfigs.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public class ModConfigs {
public static String MESSAGES_SERVER_STOPPED;
public static String MESSAGES_SERVER_STARTED;
public static String MESSAGES_SERVER_STOPPING;
public static String MESSAGES_SERVER_WAYPOINT;
public static String MESSAGES_BOT_LIST;
public static String MESSAGES_BOT_MODS_LIST;
public static String MESSAGES_BOT_MODS_NONE;
public static boolean FUNCTIONS_ALLOWOOCMESSAGES;
public static boolean MESSAGES_SERVER_STARTING_ALLOWED;
public static boolean MESSAGES_SERVER_STOPPED_ALLOWED;
Expand Down Expand Up @@ -59,6 +63,14 @@ private static void createConfigs() {
configs.addKeyValuePair(new Pair<>("messages.server.stopped.allowed", true), "stop message allowed?");
configs.addKeyValuePair(new Pair<>("messages.server.started.allowed", true), "opened/fully started message allowed?");
configs.addKeyValuePair(new Pair<>("messages.server.stopping.allowed", true), "stopping message allowed?");
configs.addKeyValuePair(
new Pair<>("messages.server.waypoint", "Shared a waypoint called **{0} ({1})** at `{2}, {3}, {4}` from {5}!"),
"xaeos waypoint message.\n# {0}: waypoint name\n# {1}: waypoint letters\n# {2}, {3} and{4}: x, y and z\n# {5}: dimension name"
);
configs.addKeyValuePair(new Pair<>("messages.bot.list", "There are currently **{0}**/{1} players online: "), "the list command preamble message\n# {0}: online players\n# {1}: max players");
configs.addKeyValuePair(new Pair<>("messages.bot.mods.list", "The server currently has {0} required mods: "), "the mods command preamble message\n# {0}: number of mods");
configs.addKeyValuePair(new Pair<>("messages.bot.mods.none", "The server currently has no required mods, you can join with a vanilla client!"), "the mods command message when no mods are needed by the client");
configs.addDocumentationLine("note: the time command cannot be customised yet because im lazy :3");

configs.addBlankLine();
configs.addDocumentationLine("Something didn't work? See the documentation or report an issue at this url: <" + Hook.DOCS_URL + ">!");
Expand All @@ -70,6 +82,10 @@ private static void assignConfigs() {
MESSAGES_SERVER_STARTED = CONFIG.getOrDefault("messages.server.started", "messages.server.started");
MESSAGES_SERVER_STOPPED = CONFIG.getOrDefault("messages.server.stopped", "messages.server.stopped");
MESSAGES_SERVER_STOPPING = CONFIG.getOrDefault("messages.server.stopping", "messages.server.stopping");
MESSAGES_SERVER_WAYPOINT = CONFIG.getOrDefault("messages.server.waypoint", "messages.server.waypoint");
MESSAGES_BOT_LIST = CONFIG.getOrDefault("messages.bot.list", "messages.bot.list");
MESSAGES_BOT_MODS_LIST = CONFIG.getOrDefault("messages.bot.mods.list", "messages.bot.mods.list");
MESSAGES_BOT_MODS_NONE = CONFIG.getOrDefault("messages.bot.mods.none", "messages.bot.mods.none");
FUNCTIONS_ALLOWOOCMESSAGES = CONFIG.getOrDefault("functions.allow_ooc_messages", false);

MESSAGES_SERVER_STARTING_ALLOWED = CONFIG.getOrDefault("messages.server.starting.allowed", false);
Expand Down
Loading