Skip to content

Commit

Permalink
Port to 1.20.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Erdragh committed Apr 28, 2024
1 parent 142b4a0 commit 0d8664d
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 85 deletions.
21 changes: 2 additions & 19 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,6 @@ jobs:
name: build-changelog
path: prepared_changelog.md

dependencies:
runs-on: ubuntu-latest
if: ${{github.ref == 'refs/heads/version/1.20.1' && github.event_name != 'pull_request'}}
steps:
- name: checkout repository
uses: actions/checkout@v4

- name: setup jdk 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'temurin'

- name: Build with Gradle and generate dependency Graph
uses: gradle/gradle-build-action@v2
with:
arguments: dependencies
dependency-graph: 'generate-and-submit'

publish:
if: ${{ github.event.inputs.publish }}
needs:
Expand Down Expand Up @@ -143,9 +124,11 @@ jobs:
curseforge-dependencies: |
sqlite-jdbc(optional)
kotlin-for-forge(required)
forge-config-api-port-fabric(required)
modrinth-dependencies: |
sqlite-jdbc(optional)
kotlin-for-forge(required)
forge-config-api-port(required)
- name: Upload Fabric Releases (Curse/Modrinth)
id: fabric_release
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/dependencies.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: build
on:
push:
branches: [ "main", "develop" ]

permissions:
contents: write

jobs:
dependencies:
runs-on: ubuntu-latest
steps:
- name: checkout repository
uses: actions/checkout@v4

- name: setup jdk 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'temurin'

- name: Build with Gradle and generate dependency Graph
uses: gradle/gradle-build-action@v2
with:
arguments: dependencies
dependency-graph: 'generate-and-submit'
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.nio.charset.StandardCharsets
plugins {
// This is an Architectury repository, as such the relevant plugins are needed
id("architectury-plugin") version "3.4-SNAPSHOT"
id("dev.architectury.loom") version "1.5-SNAPSHOT" apply false
id("dev.architectury.loom") version "1.6-SNAPSHOT" apply false
// The shadow plugin is used in both Architectury and when including JDA and Exposed
id("com.github.johnrengelman.shadow") version "8.1.1" apply false
// Since this mod/bot is written in Kotlin and expected to run on Minecraft and as such
Expand Down
2 changes: 1 addition & 1 deletion common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ dependencies {
// Do NOT use other classes from fabric loader
modImplementation("net.fabricmc:fabric-loader:${fabricLoaderVersion}")

api("fuzs.forgeconfigapiport:forgeconfigapiport-common:$forgeConfigAPIVersion")
api("fuzs.forgeconfigapiport:forgeconfigapiport-common-neoforgeapi:$forgeConfigAPIVersion")
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ object LinkCommand : HandledSlashCommand {
} else {
WhitelistHandler.whitelist(event.user, minecraftID)
waitForSetup()
guild?.getRoleById(AstralBotConfig.DISCORD_ROLE.get())?.let {
guild?.getRoleById(AstralBotConfig.DISCORD_ROLE.get())?.let { role ->
try {
guild?.addRoleToMember(event.user, it)?.queue()
guild?.addRoleToMember(event.user, role)?.queue()
} catch (e: Exception) {
LOGGER.error("Failed to add role ${it.name} to member ${event.user.asMention}", e)
LOGGER.error("Failed to add role ${role.name} to member ${event.user.asMention}", e)
}
}
event.hook.setEphemeral(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dev.erdragh.astralbot.config

import dev.erdragh.astralbot.LOGGER
import dev.erdragh.astralbot.commands.discord.allCommands
import net.minecraftforge.common.ForgeConfigSpec
import net.neoforged.neoforge.common.ModConfigSpec
import java.net.URL

/**
Expand All @@ -12,89 +12,89 @@ import java.net.URL
* @author Erdragh
*/
object AstralBotConfig {
val SPEC: ForgeConfigSpec
val SPEC: ModConfigSpec

/**
* If the `DISCORD_TOKEN` environment variable isn't set, the bot will
* look for a token in this config option.
*/
val DISCORD_TOKEN: ForgeConfigSpec.ConfigValue<String>
val DISCORD_TOKEN: ModConfigSpec.ConfigValue<String>

/**
* Whether the default whitelisting process is respected or ignored.
* Setting this to `true` will *force* every user who wants to join
* the server to link their account, even Operators.
*/
val REQUIRE_LINK_FOR_WHITELIST: ForgeConfigSpec.BooleanValue
val REQUIRE_LINK_FOR_WHITELIST: ModConfigSpec.BooleanValue

/**
* The link to the Discord servers where users can run the /link command.
* Gets used when formatting the message shown to users who try to log in
* without being whitelisted.
*/
val DISCORD_LINK: ForgeConfigSpec.ConfigValue<String>
val DISCORD_LINK: ModConfigSpec.ConfigValue<String>

/**
* The ID of the discord channel where the messages are synchronized
*/
val DISCORD_CHANNEL: ForgeConfigSpec.ConfigValue<Long>
val DISCORD_CHANNEL: ModConfigSpec.ConfigValue<Long>

/**
* The ID of the Discord Guild (server) where this bot will be active.
* This is used to get the chat sync channel etc.
*/
val DISCORD_GUILD: ForgeConfigSpec.ConfigValue<Long>
val DISCORD_GUILD: ModConfigSpec.ConfigValue<Long>

/**
* The ID of the Discord role given to linked members
*/
val DISCORD_ROLE: ForgeConfigSpec.ConfigValue<Long>
val DISCORD_ROLE: ModConfigSpec.ConfigValue<Long>

/**
* If this is set to true the message sent into the Minecraft chat
* will be clickable and take you to the relevant message on Discord
*/
val CLICKABLE_MESSAGES: ForgeConfigSpec.BooleanValue
val CLICKABLE_MESSAGES: ModConfigSpec.BooleanValue

/**
* If this is on the embeds and attached files on a message will be
* handled and possibly displayed in messages sent to the players
*/
val HANDLE_EMBEDS: ForgeConfigSpec.BooleanValue
val HANDLE_EMBEDS: ModConfigSpec.BooleanValue

/**
* If this is on embeds that have a URL associated with them will
* be clickable.
*/
val CLICKABLE_EMBEDS: ForgeConfigSpec.BooleanValue
val CLICKABLE_EMBEDS: ModConfigSpec.BooleanValue

/**
* List of Strings containing the URLs that are blocked by default.
* If you read this and want to add more to the default list, feel
* free to open an Issue or Pull Request.
*/
private val URL_BLOCKLIST: ForgeConfigSpec.ConfigValue<List<String>>
private val URL_BLOCKLIST: ModConfigSpec.ConfigValue<List<String>>

/**
* List of the names of the commands that are enabled by default.
* On first startup of a server, it will be all available commands.
*/
val ENABLED_COMMANDS: ForgeConfigSpec.ConfigValue<List<String>>
val ENABLED_COMMANDS: ModConfigSpec.ConfigValue<List<String>>

/**
* Enables parsing Discord messages into Minecraft's Chat Components.
* This includes making links clickable, etc.
*/
val ENABLE_MARKDOWN_PARSING: ForgeConfigSpec.BooleanValue
val ENABLE_MARKDOWN_PARSING: ModConfigSpec.BooleanValue

/**
* Enables converting detected URLs into clickable links, requires
* [ENABLE_MARKDOWN_PARSING] to be enabled to do anything
*/
val ENABLE_AUTO_LINKS: ForgeConfigSpec.BooleanValue
val ENABLE_AUTO_LINKS: ModConfigSpec.BooleanValue

init {
val builder = ForgeConfigSpec.Builder()
val builder = ModConfigSpec.Builder()

builder.comment("AstralBot Config")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
package dev.erdragh.astralbot.config

import net.minecraftforge.common.ForgeConfigSpec
import net.neoforged.neoforge.common.ModConfigSpec

object AstralBotTextConfig {
val SPEC: ForgeConfigSpec
val SPEC: ModConfigSpec

val GENERIC_ERROR: ForgeConfigSpec.ConfigValue<String>
val GENERIC_SUCCESS: ForgeConfigSpec.ConfigValue<String>
val GENERIC_BLOCKED: ForgeConfigSpec.ConfigValue<String>
val GENERIC_ERROR: ModConfigSpec.ConfigValue<String>
val GENERIC_SUCCESS: ModConfigSpec.ConfigValue<String>
val GENERIC_BLOCKED: ModConfigSpec.ConfigValue<String>

val FAQ_ERROR: ForgeConfigSpec.ConfigValue<String>
val FAQ_NO_REGISTERED: ForgeConfigSpec.ConfigValue<String>
val FAQ_ERROR: ModConfigSpec.ConfigValue<String>
val FAQ_NO_REGISTERED: ModConfigSpec.ConfigValue<String>

val TICK_REPORT: ForgeConfigSpec.ConfigValue<String>
val TICK_REPORT: ModConfigSpec.ConfigValue<String>

val PLAYER_MESSAGE: ForgeConfigSpec.ConfigValue<String>
val PLAYER_MESSAGE: ModConfigSpec.ConfigValue<String>

val DISCORD_MESSAGE: ForgeConfigSpec.ConfigValue<String>
val DISCORD_REPLY: ForgeConfigSpec.ConfigValue<String>
val DISCORD_EMBEDS: ForgeConfigSpec.ConfigValue<String>
val DISCORD_MESSAGE: ModConfigSpec.ConfigValue<String>
val DISCORD_REPLY: ModConfigSpec.ConfigValue<String>
val DISCORD_EMBEDS: ModConfigSpec.ConfigValue<String>

val RELOAD_ERROR: ForgeConfigSpec.ConfigValue<String>
val RELOAD_SUCCESS: ForgeConfigSpec.ConfigValue<String>
val RELOAD_ERROR: ModConfigSpec.ConfigValue<String>
val RELOAD_SUCCESS: ModConfigSpec.ConfigValue<String>

val LINK_NO_MINECRAFT: ForgeConfigSpec.ConfigValue<String>
val LINK_MINECRAFT_TAKEN: ForgeConfigSpec.ConfigValue<String>
val LINK_DISCORD_TAKEN: ForgeConfigSpec.ConfigValue<String>
val LINK_SUCCESSFUL: ForgeConfigSpec.ConfigValue<String>
val LINK_ERROR: ForgeConfigSpec.ConfigValue<String>
val LINK_NO_MINECRAFT: ModConfigSpec.ConfigValue<String>
val LINK_MINECRAFT_TAKEN: ModConfigSpec.ConfigValue<String>
val LINK_DISCORD_TAKEN: ModConfigSpec.ConfigValue<String>
val LINK_SUCCESSFUL: ModConfigSpec.ConfigValue<String>
val LINK_ERROR: ModConfigSpec.ConfigValue<String>

val LINK_COMMAND_MESSAGE: ForgeConfigSpec.ConfigValue<String>
val LINK_COMMAND_ALREADY_LINKED: ForgeConfigSpec.ConfigValue<String>
val LINK_COMMAND_MESSAGE: ModConfigSpec.ConfigValue<String>
val LINK_COMMAND_ALREADY_LINKED: ModConfigSpec.ConfigValue<String>

val UNLINK_UNLINKED: ForgeConfigSpec.ConfigValue<String>
val UNLINK_NOPERMS: ForgeConfigSpec.ConfigValue<String>
val UNLINK_UNLINKED: ModConfigSpec.ConfigValue<String>
val UNLINK_NOPERMS: ModConfigSpec.ConfigValue<String>

init {
val builder = ForgeConfigSpec.Builder()
val builder = ModConfigSpec.Builder()
val whitespaceRegex = Regex("\n[ \t]+")

GENERIC_ERROR = builder.comment("Generic error message sent to Discord")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import net.minecraft.network.chat.Component
import net.minecraft.network.chat.HoverEvent
import net.minecraft.network.chat.MutableComponent
import net.minecraft.server.MinecraftServer
import net.minecraft.server.level.ClientInformation
import net.minecraft.server.level.ServerPlayer
import net.minecraft.world.item.ItemStack
import java.text.DecimalFormat
Expand All @@ -31,7 +32,7 @@ import kotlin.math.min
*/
class MinecraftHandler(private val server: MinecraftServer) : ListenerAdapter() {
private val playerNames = HashSet<String>(server.maxPlayers);
private val notchPlayer = byName("Notch")?.let { ServerPlayer(this.server, this.server.allLevels.elementAt(0), it) }
private val notchPlayer = byName("Notch")?.let { ServerPlayer(this.server, this.server.allLevels.elementAt(0), it, ClientInformation.createDefault()) }


companion object {
Expand Down Expand Up @@ -83,12 +84,12 @@ class MinecraftHandler(private val server: MinecraftServer) : ListenerAdapter()
*/
fun tickReport(): String {
// Idea from the TPSCommand in Forge
return AstralBotTextConfig.TICK_REPORT.get().replace("{{mspt}}", numberFormat.format(server.averageTickTime))
return AstralBotTextConfig.TICK_REPORT.get().replace("{{mspt}}", numberFormat.format(server.averageTickTimeNanos * 1000))
.replace(
"{{tps}}", numberFormat.format(
min(
20.0,
1000.0 / server.averageTickTime
1000.0 / (server.averageTickTimeNanos * 1000)
)
)
)
Expand Down Expand Up @@ -153,7 +154,7 @@ class MinecraftHandler(private val server: MinecraftServer) : ListenerAdapter()
if (player != null)
AstralBotTextConfig.PLAYER_MESSAGE.get()
.replace("{{message}}", formatComponentToMarkdown(message))
.replace("{{fullName}}", escape(player.displayName.string))
.replace("{{fullName}}", escape(player.displayName?.string ?: player.name.string))
.replace("{{name}}", escape(player.name.string))
else escape(message.string)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import net.minecraft.world.item.TooltipFlag
import org.commonmark.parser.Parser
import java.awt.Color
import java.util.regex.Pattern
import kotlin.jvm.optionals.getOrNull

// Pattern for recognizing a URL, based off RFC 3986
// Source: https://stackoverflow.com/questions/5713558/detect-and-extract-url-from-a-string
Expand Down Expand Up @@ -99,7 +100,7 @@ fun formatHoverItems(stack: ItemStack, knownItems: MutableList<ItemStack>, playe
fun formatHoverEntity(entity: HoverEvent.EntityTooltipInfo): MessageEmbed? {
if (entity.type == EntityType.PLAYER) return null
return EmbedBuilder()
.setTitle(entity.name?.string)
.setTitle(entity.name.map(Component::getString).getOrNull())
.setDescription(entity.type.description.string)
.let { builder: EmbedBuilder ->
val mobCategory = entity.type.category
Expand Down
3 changes: 2 additions & 1 deletion common/src/main/kotlin/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
requires brigadier;

// For accessing the config
requires forgeconfigapiport.common;
requires forgeconfigapiport.common.neoforgeapi;

requires java.desktop;
}
2 changes: 1 addition & 1 deletion fabric/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
fabricApiVersion=0.91.0
fabricApiVersion=0.97.0
fabricKotlinVersion=1.10.19+kotlin.1.9.23
8 changes: 4 additions & 4 deletions fabric/src/main/kotlin/dev/erdragh/astralbot/fabric/BotMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import dev.erdragh.astralbot.commands.minecraft.registerMinecraftCommands
import dev.erdragh.astralbot.config.AstralBotConfig
import dev.erdragh.astralbot.config.AstralBotTextConfig
import dev.erdragh.astralbot.handlers.DiscordMessageComponent
import fuzs.forgeconfigapiport.api.config.v2.ForgeConfigRegistry
import fuzs.forgeconfigapiport.fabric.api.neoforge.v4.NeoForgeConfigRegistry
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents
import net.minecraft.server.level.ServerPlayer
import net.minecraftforge.fml.config.ModConfig
import net.neoforged.fml.config.ModConfig

object BotMod : ModInitializer {
override fun onInitialize() {
ForgeConfigRegistry.INSTANCE.register(MODID, ModConfig.Type.SERVER, AstralBotConfig.SPEC)
ForgeConfigRegistry.INSTANCE.register(MODID, ModConfig.Type.SERVER, AstralBotTextConfig.SPEC, "astralbot-text.toml")
NeoForgeConfigRegistry.INSTANCE.register(MODID, ModConfig.Type.SERVER, AstralBotConfig.SPEC)
NeoForgeConfigRegistry.INSTANCE.register(MODID, ModConfig.Type.SERVER, AstralBotTextConfig.SPEC, "astralbot-text.toml")

ServerLifecycleEvents.SERVER_STARTED.register {
LOGGER.info("Starting AstralBot on Fabric")
Expand Down
6 changes: 6 additions & 0 deletions forge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
val minecraftVersion: String by project
val forgeVersion: String by project
val kotlinForgeVersion: String by project
val forgeConfigAPIVersion: String by project

forge(group = "net.minecraftforge", name = "forge", version = "$minecraftVersion-$forgeVersion")
// Adds KFF as dependency and Kotlin libs
Expand All @@ -51,4 +52,9 @@ dependencies {
exclude(group = "org.jetbrains.kotlin")
exclude(group = "org.slf4j")
}

// On 1.20.2 upwards, the forge config api port either reimplements neo's
// or lexforge's config API. I chose to use Neo's by default, resulting in
// an additional dependency on the lexforge side.
modApi("fuzs.forgeconfigapiport:forgeconfigapiport-forge:$forgeConfigAPIVersion")
}
Loading

0 comments on commit 0d8664d

Please sign in to comment.