Skip to content

Commit c7e9c31

Browse files
committed
Added common extensions and helpers.
1 parent fa56739 commit c7e9c31

11 files changed

Lines changed: 310 additions & 84 deletions

File tree

src/main/java/com/example/examplemod/ExampleMod.java

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.mairwunnx.projectessentialscore
2+
3+
import net.minecraftforge.fml.common.Mod
4+
5+
@Suppress("unused")
6+
@Mod("project_essentials_core")
7+
internal class EntryPoint : EssBase() {
8+
init {
9+
modInstance = this
10+
modVersion = "1.14.4-1.0.0.0"
11+
logBaseInfo()
12+
validateForgeVersion()
13+
}
14+
15+
internal companion object {
16+
internal lateinit var modInstance: EntryPoint
17+
}
18+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.mairwunnx.projectessentialscore
2+
3+
import com.mairwunnx.projectessentialscore.extensions.capitalizeWords
4+
import net.minecraftforge.fml.common.Mod
5+
import net.minecraftforge.versions.forge.ForgeVersion
6+
import org.apache.logging.log4j.LogManager
7+
8+
/**
9+
* Base Project Essentials modification class.
10+
* @since 1.14.4-1.0.0.0
11+
*/
12+
@Suppress("unused", "MemberVisibilityCanBePrivate")
13+
abstract class EssBase {
14+
private val logger = LogManager.getLogger()
15+
16+
var modId = "project_essentials_null"
17+
var modName = "Project Essentials Null"
18+
var modVersion = "1.14.4-0.0.0.0"
19+
var modModuleName = "Null"
20+
var modMaintainer = "MairwunNx (Pavel Erokhin)"
21+
var modTargetForge = "28.0.X"
22+
var modTargetForgeRegex = "^28\\.0\\..\\d+|28\\.0\\.[\\d]\$"
23+
var modTargetMC = "1.14.4"
24+
var modSources = "https://github.com/ProjectEssentials/ProjectEssentials-Null/"
25+
var modTelegram = "https://t.me/minecraftforge"
26+
27+
init {
28+
modId = this.javaClass.getAnnotation(Mod::class.java).value
29+
modName = modId.replace("_", " ").capitalizeWords()
30+
modModuleName = modName.split(" ").last()
31+
modSources = "https://github.com/ProjectEssentials/ProjectEssentials-$modModuleName/"
32+
}
33+
34+
/**
35+
* Print base modification information to log.
36+
* @since 1.14.4-1.0.0.0
37+
*/
38+
fun logBaseInfo() {
39+
logger.info("$modName starting initializing ...")
40+
logger.info(" - Mod Id: $modId")
41+
logger.info(" - Version: $modVersion")
42+
logger.info(" - Maintainer: $modMaintainer")
43+
logger.info(" - Target Forge version: $modTargetForge")
44+
logger.info(" - Target Minecraft version: $modTargetMC")
45+
logger.info(" - Source code: $modSources")
46+
logger.info(" - Telegram chat: $modTelegram")
47+
}
48+
49+
/**
50+
* Validate forge version on compatibility with loaded mod.
51+
* If validation failed, then you will be notified with
52+
* messages in logger with level WARN.
53+
* @since 1.14.4-1.0.0.0
54+
*/
55+
fun validateForgeVersion() {
56+
logger.info("Checking forge version for compatibility with mod ...")
57+
if (Regex(modTargetForgeRegex).matches(ForgeVersion.getVersion())) {
58+
logger.info("Forge version is compatibility with mod.")
59+
} else {
60+
logger.warn("Forge version may be incompatible with $modName $modVersion!")
61+
logger.warn(" - update or downgrade forge version.")
62+
logger.warn(" - update or downgrade mod version.")
63+
logger.warn(" - or just create issue on github.")
64+
}
65+
}
66+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.mairwunnx.projectessentialscore.enums
2+
3+
/**
4+
* This mainly serves to get the true configuration path.
5+
* @see com.mairwunnx.projectessentialscore.helpers.getRootPath
6+
* @since 1.14.4-1.0.0.0
7+
*/
8+
enum class ForgeRootPaths {
9+
/**
10+
* When sending this enum element, the path relative
11+
* to the game client will be returned.
12+
* @see com.mairwunnx.projectessentialscore.helpers.getRootPath
13+
*/
14+
CLIENT,
15+
/**
16+
* When sending this enum element, the path relative
17+
* to the game server will be returned.
18+
* @see com.mairwunnx.projectessentialscore.helpers.getRootPath
19+
*/
20+
SERVER
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@file:Suppress("unused")
2+
3+
package com.mairwunnx.projectessentialscore.extensions
4+
5+
import com.mojang.brigadier.context.CommandContext
6+
import net.minecraft.command.CommandSource
7+
import net.minecraft.entity.player.ServerPlayerEntity
8+
9+
/**
10+
* @return true if command sender is player.
11+
* @since 1.14.4-1.0.0.0
12+
*/
13+
fun CommandContext<CommandSource>.isPlayerSender(): Boolean =
14+
this.source.entity is ServerPlayerEntity
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
@file:Suppress("unused")
2+
3+
package com.mairwunnx.projectessentialscore.extensions
4+
5+
import net.minecraft.command.CommandSource
6+
import net.minecraft.entity.player.ServerPlayerEntity
7+
import net.minecraftforge.event.CommandEvent
8+
9+
/**
10+
* Return command name as string.
11+
*
12+
* Example: player execute command **`/heal MairwunNx`**,
13+
* then you get **`heal`** as string.
14+
* @since 1.14.4-1.0.0.0
15+
*/
16+
val CommandEvent.commandName: String
17+
get() = this.executedCommand
18+
.replace("/", "")
19+
.split(" ")[0]
20+
21+
/**
22+
* Return fully executed command as string.
23+
*
24+
* Example: player execute command **`/heal MairwunNx`**,
25+
* then you get **`/heal MairwunNx`** as string.
26+
* @since 1.14.4-1.0.0.0
27+
*/
28+
val CommandEvent.executedCommand: String
29+
get() = this.parseResults.reader.string
30+
31+
/**
32+
* Return **`ServerPlayerEntity`** class instance from
33+
* **`CommandEvent`** class instance.
34+
* @since 1.14.4-1.0.0.0
35+
*/
36+
val CommandEvent.player: ServerPlayerEntity
37+
get() = this.parseResults.context.source.asPlayer()
38+
39+
/**
40+
* Return command **`source`** from **`CommandEvent`**
41+
* class instance.
42+
* @since 1.14.4-1.0.0.0
43+
*/
44+
val CommandEvent.source: CommandSource
45+
get() = this.parseResults.context.source
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@file:Suppress("unused")
2+
3+
package com.mairwunnx.projectessentialscore.extensions
4+
5+
import net.minecraft.command.CommandSource
6+
import net.minecraft.util.text.TranslationTextComponent
7+
8+
/**
9+
* Send localized message to player without logging.
10+
* @param moduleName mod module name.
11+
* @param commandSource command source instance.
12+
* @param l10nString localized string without `project_essentials_`.
13+
* @param args additional arguments for localized string `(%s literals)`.
14+
* @since 1.14.4-1.0.0.0
15+
*/
16+
fun sendMsg(
17+
moduleName: String,
18+
commandSource: CommandSource,
19+
l10nString: String,
20+
vararg args: String
21+
) {
22+
commandSource.sendFeedback(
23+
TranslationTextComponent(
24+
"project_essentials_$moduleName.$l10nString", *args
25+
), false
26+
)
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@file:Suppress("unused")
2+
3+
package com.mairwunnx.projectessentialscore.extensions
4+
5+
/**
6+
* Capitalize each word in string.
7+
* @return capitalized each word string.
8+
* @since 1.14.4-1.0.0.0
9+
*/
10+
fun String.capitalizeWords(): String =
11+
split(" ").joinToString(" ") { it.capitalize() }
12+
13+
/**
14+
* Return empty string.
15+
* @since 1.14.4-1.0.0.0
16+
*/
17+
val String.Companion.empty get() = ""
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.mairwunnx.projectessentialscore.helpers
2+
3+
import com.mairwunnx.projectessentialscore.enums.ForgeRootPaths
4+
import net.minecraft.client.Minecraft
5+
import java.io.File
6+
7+
private val clientRootDir by lazy {
8+
Minecraft.getInstance().gameDir.absolutePath
9+
}
10+
private val serverRootDir by lazy {
11+
File(".").absolutePath
12+
}
13+
14+
/**
15+
* @return absolutely path to configuration root dir.
16+
* @since 1.14.4-1.0.0.0
17+
*/
18+
fun getRootPath(pathType: ForgeRootPaths): String {
19+
return when (pathType) {
20+
ForgeRootPaths.CLIENT -> clientRootDir
21+
ForgeRootPaths.SERVER -> serverRootDir
22+
}
23+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@file:Suppress("unused")
2+
3+
package com.mairwunnx.projectessentialscore.helpers
4+
5+
/**
6+
* Error message with reason: not have permissions.
7+
* @since 1.14.4-1.0.0.0
8+
*/
9+
const val PERMISSION_LEVEL =
10+
"Player (%0) failed to executing \"/%1\" command".plus(
11+
"\n - Reason: permission level executing command more than player permission level."
12+
)
13+
/**
14+
* Error message with reason: command cooldown not expired.
15+
* @since 1.14.4-1.0.0.0
16+
*/
17+
const val COOLDOWN_NOT_EXPIRED =
18+
"Player (%0) failed to executing \"/%1\" command".plus(
19+
"\n - Reason: command cooldown not expired."
20+
)
21+
/**
22+
* Error message with reason: typed command disabled.
23+
* @since 1.14.4-1.0.0.0
24+
*/
25+
const val DISABLED_COMMAND =
26+
"Player (%0) failed to executing \"/%1\" command".plus(
27+
"\n - Reason: it command disabled by mod configuration."
28+
)
29+
/**
30+
* Error message with reason: disabled command argument.
31+
* @since 1.14.4-1.0.0.0
32+
*/
33+
const val DISABLED_COMMAND_ARG =
34+
"Player (%0) failed to executing \"/%1\" command".plus(
35+
"\n - Reason: arguments for it command disabled by mod configuration."
36+
)
37+
/**
38+
* Error message with reason: only player can execute command.
39+
* @since 1.14.4-1.0.0.0
40+
*/
41+
const val ONLY_PLAYER_CAN =
42+
"Server failed to executing \"/%0\" command".plus(
43+
"\n - Reason: command should only be used by the player."
44+
).plus(
45+
"\n - Solution: try use command with argument or target."
46+
)

0 commit comments

Comments
 (0)