Skip to content

Commit

Permalink
Server language now is RU_RU!! translate locales very easy and automa…
Browse files Browse the repository at this point in the history
…tic.

todo: if ru_ru.json not exist
  • Loading branch information
neroduckale committed Apr 28, 2024
1 parent 177bab9 commit 93abfd7
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 83 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

include(implementation(annotationProcessor("io.github.llamalad7:mixinextras-fabric:0.3.5")))
}

processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.15.6

# Mod Properties
mod_version=1.2.1
mod_version=1.3.0
maven_group=ru.neroduckale
archives_base_name=nerodiscord

Expand Down
16 changes: 8 additions & 8 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pluginManagement {
repositories {
maven {
name = 'Fabric'
url = 'https://maven.fabricmc.net/'
}
mavenCentral()
gradlePluginPortal()
}
repositories {
maven {
name = 'Fabric'
url = 'https://maven.fabricmc.net/'
}
mavenCentral()
gradlePluginPortal()
}
}
6 changes: 6 additions & 0 deletions src/main/java/ru/neroduckale/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ru.neroduckale;

public class Config {
public static String webhook1 = "enter webhook url here";
public static String webhook2 = "enter webhook url here";
}
24 changes: 5 additions & 19 deletions src/main/java/ru/neroduckale/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraft.server.network.ServerPlayerEntity;

import java.awt.*;
import java.util.Objects;

import static ru.neroduckale.NeroDiscord.*;
import static ru.neroduckale.Utils.*;
Expand Down Expand Up @@ -43,10 +42,7 @@ public static void onPlayerLeave(ServerPlayNetworkHandler handler, MinecraftServ
public static void onDeath(LivingEntity livingEntity, DamageSource damageSource) {
if (livingEntity instanceof ServerPlayerEntity) {
var nickname = livingEntity.getDisplayName().getString();

LOGGER.info(livingEntity.getDisplayName().getString());
LOGGER.info(damageSource.getType().msgId());
var localized = getLocalizedDeathMessage(livingEntity, damageSource);
var localized = damageSource.getDeathMessage(livingEntity).getString();
SendEmbedWithPlayerAsServer(localized, nickname, Color.red);
}
}
Expand All @@ -59,19 +55,9 @@ public static void OnServerStopped(MinecraftServer minecraftServer) {
SendEmbedAsServer("Сервер закрыт!", Color.red);
}

public static void OnNewAdvancement(Advancement advancement, String nickname) {
var lclstr = advancement.getId().getPath();
lclstr = lclstr.replace("/", ".");
var localtestr = "advancements.%s".formatted(lclstr);
var localetitle = localtestr + ".title";
var localedesc = localtestr + ".description";
var translatedTitle = getJson(localetitle);
var translatedDescription = getJson(localedesc);
if (!Objects.equals(translatedTitle, "null") && !Objects.equals(translatedDescription, "null")) {
SendEmbedNewAdvancement(translatedTitle, translatedDescription, nickname);
return;
}
SendEmbedNewAdvancement(advancement.getDisplay().getTitle().getString(), advancement.getDisplay().getDescription().getString(), nickname);

public static void OnNewAdvancement(Advancement advancement, ServerPlayerEntity nickname) {
String title = advancement.getDisplay().getTitle().getString();
String description = advancement.getDisplay().getDescription().getString();
SendEmbedNewAdvancement(title, description, nickname.getDisplayName().getString());
}
}
14 changes: 2 additions & 12 deletions src/main/java/ru/neroduckale/NeroDiscord.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ru.neroduckale;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.fabricmc.api.ModInitializer;

import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
Expand All @@ -11,20 +9,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class NeroDiscord implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("nerodiscord");
public static DiscordWebhook webhook = new DiscordWebhook("enter webhook1 url");
public static DiscordWebhook serverwebhook = new DiscordWebhook("enter webhook2 url");
public static JsonObject jsonObject;
public static DiscordWebhook webhook = new DiscordWebhook(Config.webhook1);
public static DiscordWebhook serverwebhook = new DiscordWebhook(Config.webhook2);
@Override
public void onInitialize() {
var readIn = new BufferedReader(new InputStreamReader(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("assets/nerodiscord/ru_ru.json")), StandardCharsets.UTF_8));
jsonObject = JsonParser.parseReader(readIn).getAsJsonObject();
ServerMessageEvents.CHAT_MESSAGE.register(Events::onChatMessage);
ServerLifecycleEvents.SERVER_STARTED.register(Events::onServerStarted);
ServerPlayConnectionEvents.JOIN.register(Events::onPlayerJoin);
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/ru/neroduckale/Utils.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package ru.neroduckale;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;

import java.awt.*;
import java.io.IOException;
import java.util.Objects;

import static ru.neroduckale.NeroDiscord.*;

Expand Down Expand Up @@ -45,28 +41,6 @@ public static void SendEmbedWithPlayerAsServer(String message, String nickname,
}
}

public static String getLocalizedDeathMessage(LivingEntity p_19343_, DamageSource dms) {
LivingEntity livingentity = p_19343_.getAttacker();
String localestr = "death.attack." + dms.getType().msgId();
String nicknameKilled = p_19343_.getDisplayName().getString();
String translated;
translated = getJson(localestr);
if (Objects.equals(translated, "null")) return "%s умер на сво".formatted(nicknameKilled);
LOGGER.error(translated);
if (livingentity != null) {
return String.format(translated, nicknameKilled, livingentity.getDisplayName().getString());
}

return String.format(translated, nicknameKilled);
}

public static String getJson(String member) {
if (jsonObject.has(member)) {
return jsonObject.get(member).getAsString();
}
return "null";
}

public static void SendEmbedNewAdvancement(String title, String description, String nickname) {
DiscordWebhook.EmbedObject embed = new DiscordWebhook.EmbedObject()
.setAuthor("%s получил новое достижение!".formatted(nickname), "", "https://ely.by/services/skins-renderer?url=http://skinsystem.ely.by/skins/%s&scale=10&renderFace=1".formatted(nickname))
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/ru/neroduckale/mixin/ExampleMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,26 @@

import net.minecraft.advancement.Advancement;
import net.minecraft.advancement.PlayerAdvancementTracker;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayerEntity;
import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ru.neroduckale.Events;

import static ru.neroduckale.NeroDiscord.*;
@Mixin(PlayerAdvancementTracker.class)
public class ExampleMixin {

@Shadow @Final private PlayerManager playerManager;

@Shadow private ServerPlayerEntity owner;
@Shadow private ServerPlayerEntity owner;

@Shadow @Final private static Logger LOGGER;

@Inject(at = @At("RETURN"), method = "grantCriterion")
@Inject(at = @At("RETURN"), method = "grantCriterion")
private void grantCriterion(Advancement advancement, String criterionName, CallbackInfoReturnable<Boolean> cir) {
if (advancement.getDisplay() == null) {
return;
}
if (owner.getAdvancementTracker().getProgress(advancement).isDone()) {
Events.OnNewAdvancement(advancement, owner.getDisplayName().getString());
Events.OnNewAdvancement(advancement, owner);
}
}
}
}

14 changes: 14 additions & 0 deletions src/main/java/ru/neroduckale/mixin/LangMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.neroduckale.mixin;

import net.minecraft.util.Language;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.ModifyConstant;

@Mixin(Language.class)
public class LangMixin {
@ModifyConstant(method = "create", constant = @Constant(stringValue = "/assets/minecraft/lang/en_us.json"))
private static String changelang(String string) {
return "/assets/minecraft/lang/ru_ru.json";
}
}
16 changes: 16 additions & 0 deletions src/main/java/ru/neroduckale/mixin/ServerLangMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.neroduckale.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import net.fabricmc.fabric.impl.resource.loader.ServerLanguageUtil;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.*;

@Mixin(value = ServerLanguageUtil.class, remap = false)
public abstract class ServerLangMixin {


@ModifyArg(method = "getModLanguageFiles", at = @At(value = "INVOKE", target = "Lnet/fabricmc/loader/api/ModContainer;findPath(Ljava/lang/String;)Ljava/util/Optional;"))
private static String changelang(String constant, @Local String ns) {
return "/assets/" + ns + "/lang/ru_ru.json";
}
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"schemaVersion": 1,
"id": "nerodiscord",
"version": "${version}",
"version": "1.3.0",
"name": "NeroDiscord",
"description": "This is an example description! Tell everyone what your mod is about!",
"authors": [
Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/nerodiscord.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"package": "ru.neroduckale.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"ExampleMixin"
"ExampleMixin",
"LangMixin",
"ServerLangMixin"
],
"injectors": {
"defaultRequire": 1
}
}
}

0 comments on commit 93abfd7

Please sign in to comment.