diff --git a/src/main/java/dev/micle/totem_of_reviving/item/totem/TotemItem.java b/src/main/java/dev/micle/totem_of_reviving/item/totem/TotemItem.java index 960a422..7a585a7 100644 --- a/src/main/java/dev/micle/totem_of_reviving/item/totem/TotemItem.java +++ b/src/main/java/dev/micle/totem_of_reviving/item/totem/TotemItem.java @@ -1,8 +1,10 @@ package dev.micle.totem_of_reviving.item.totem; +import dev.micle.totem_of_reviving.TotemOfReviving; import dev.micle.totem_of_reviving.component.TotemData; import dev.micle.totem_of_reviving.setup.Config; import dev.micle.totem_of_reviving.setup.ModDataComponents; +import dev.micle.totem_of_reviving.util.LangAsset; import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; import net.minecraft.network.chat.Component; @@ -20,6 +22,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Rarity; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.level.Level; +import org.apache.commons.codec.language.bm.Lang; import org.jetbrains.annotations.NotNull; import org.lwjgl.glfw.GLFW; @@ -133,57 +136,65 @@ public abstract class TotemItem extends Item { Optional targetUUID = totemData.getTargetUUID(); if (targetUUID.isEmpty()) { - return Component.literal(ChatFormatting.WHITE + "Unable to find player!"); + return LangAsset.MESSAGE_UNABLE_TO_FIND_PLAYER.getComponent().withStyle(ChatFormatting.RED); } ServerPlayer target = playerList.getPlayer(targetUUID.get()); if (target == null) { - return Component.literal(ChatFormatting.WHITE + "Unable to find player!"); + return LangAsset.MESSAGE_UNABLE_TO_FIND_PLAYER.getComponent().withStyle(ChatFormatting.RED); } - setTotemData(itemStack, new TotemData( + totemData = new TotemData( totemData.getTargetIndex(), totemData.getTargetStringUUID(), totemData.getTargetName(), target.getStats().getValue(Stats.CUSTOM.get(Stats.DEATHS)), totemData.getCharge() - )); + ); + setTotemData(itemStack, totemData); if (!target.isSpectator()) { - return Component.literal(ChatFormatting.GRAY + target.getDisplayName().getString() + ChatFormatting.WHITE + " is not dead!"); + return LangAsset.MESSAGE_PLAYER_IS_NOT_DEAD.getComponent( + Component.literal(totemData.getTargetName()).withStyle(ChatFormatting.GRAY) + ).withStyle(ChatFormatting.WHITE); } try (ServerLevel targetLevel = target.serverLevel()) { if (!targetLevel.equals(user.serverLevel()) && !config.getCanReviveAcrossDimensions()) { - return Component.literal(ChatFormatting.GRAY + target.getDisplayName().getString() + ChatFormatting.WHITE + " is in a different dimension!"); + return LangAsset.MESSAGE_PLAYER_IS_IN_ANOTHER_DIMENSION.getComponent( + Component.literal(totemData.getTargetName()).withStyle(ChatFormatting.GRAY) + ).withStyle(ChatFormatting.WHITE); } } catch (IOException e) { - return Component.literal(ChatFormatting.WHITE + "Unable to get " + ChatFormatting.GRAY + target.getDisplayName().getString() + "'s " + ChatFormatting.WHITE + " level!"); + return LangAsset.MESSAGE_UNABLE_TO_GET_PLAYER_WORLD.getComponent( + Component.literal(totemData.getTargetName()).withStyle(ChatFormatting.DARK_RED) + ).withStyle(ChatFormatting.RED); } if (!totemItem.canAffordTarget(totemData)) { - return Component.literal(ChatFormatting.WHITE + "Not enough charge!"); + return LangAsset.MESSAGE_NOT_ENOUGH_CHARGE.getComponent().withStyle(ChatFormatting.WHITE); } try (ServerLevel userLevel = user.serverLevel()) { target.teleportTo(userLevel, user.position().x, user.position().y, user.position().z, user.getYRot(), user.getXRot()); target.setGameMode(userLevel.getServer().getDefaultGameType()); } catch (IOException e) { - return Component.literal(ChatFormatting.WHITE + "Unable to get your level!"); + return LangAsset.MESSAGE_UNABLE_TO_GET_YOUR_WORLD.getComponent().withStyle(ChatFormatting.RED); } - setTotemData(itemStack, new TotemData( + totemData = new TotemData( totemData.getTargetIndex(), totemData.getTargetStringUUID(), totemData.getTargetName(), totemData.getTargetDeaths(), totemData.getCharge() - totemItem.getTargetCost(totemData) - )); - + ); setTotemData(itemStack, totemData); itemStack.hurtAndBreak(1, user, slot); - return Component.literal(ChatFormatting.WHITE + "Successfully revived " + ChatFormatting.GRAY + target.getDisplayName().getString() + "!"); + return LangAsset.MESSAGE_SUCCESSFULLY_REVIVED_TARGET.getComponent( + Component.literal(totemData.getTargetName()).withStyle(ChatFormatting.DARK_GREEN) + ).withStyle(ChatFormatting.GREEN); } @Override diff --git a/src/main/java/dev/micle/totem_of_reviving/util/LangAsset.java b/src/main/java/dev/micle/totem_of_reviving/util/LangAsset.java new file mode 100644 index 0000000..a704cce --- /dev/null +++ b/src/main/java/dev/micle/totem_of_reviving/util/LangAsset.java @@ -0,0 +1,41 @@ +package dev.micle.totem_of_reviving.util; + +import dev.micle.totem_of_reviving.TotemOfReviving; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.network.chat.contents.TranslatableContents; + +public enum LangAsset { + ITEM_GROUP_MAIN("itemGroup", "main"), + ITEM_STRAW_TOTEM("item", "straw_totem"), + ITEM_IRON_TOTEM("item", "iron_totem"), + ITEM_DIAMOND_TOTEM("item", "diamond_totem"), + ITEM_NETHERITE_TOTEM("item", "netherite_totem"), + ITEM_STRAW_CHARGE("item", "straw_charge"), + ITEM_IRON_CHARGE("item", "iron_charge"), + ITEM_DIAMOND_CHARGE("item", "diamond_charge"), + ITEM_NETHERITE_CHARGE("item", "netherite_charge"), + MESSAGE_UNABLE_TO_FIND_PLAYER("message", "unable_to_find_player"), + MESSAGE_PLAYER_IS_NOT_DEAD("message", "player_is_not_dead"), + MESSAGE_PLAYER_IS_IN_ANOTHER_DIMENSION("message", "player_is_in_another_dimension"), + MESSAGE_UNABLE_TO_GET_PLAYER_WORLD("message", "unable_to_get_player_world"), + MESSAGE_NOT_ENOUGH_CHARGE("message", "not_enough_charge"), + MESSAGE_UNABLE_TO_GET_YOUR_WORLD("message", "unable_to_get_your_world"), + MESSAGE_SUCCESSFULLY_REVIVED_TARGET("message", "successfully_revived_target"); + + private final String category; + private final String key; + + private LangAsset(String category, String key) { + this.category = category; + this.key = key; + } + + public MutableComponent getComponent() { + return getComponent(TranslatableContents.NO_ARGS); + } + + public MutableComponent getComponent(Object ...args) { + return Component.translatable(String.format("%s.%s.%s", category, TotemOfReviving.MOD_ID, key), args); + } +} diff --git a/src/main/resources/assets/totem_of_reviving/lang/en_us.json b/src/main/resources/assets/totem_of_reviving/lang/en_us.json index bd1aab9..86cd72c 100644 --- a/src/main/resources/assets/totem_of_reviving/lang/en_us.json +++ b/src/main/resources/assets/totem_of_reviving/lang/en_us.json @@ -8,5 +8,11 @@ "item.totem_of_reviving.iron_charge": "Iron reviving charge", "item.totem_of_reviving.diamond_charge": "Diamond reviving charge", "item.totem_of_reviving.netherite_charge": "Netherite reviving charge", - "advanced_tooltips": "advanced_tooltips" + "message.totem_of_reviving.unable_to_find_player": "Unable to find player!", + "message.totem_of_reviving.player_is_not_dead": "%s is not dead!", + "message.totem_of_reviving.player_is_in_another_dimension": "%s is in a different dimension!", + "message.totem_of_reviving.unable_to_get_player_world": "Unable to get %s's world!", + "message.totem_of_reviving.not_enough_charge": "Not enough charge!", + "message.totem_of_reviving.unable_to_get_your_world": "Unable to get your world!", + "message.totem_of_reviving.successfully_revived_target": "Successfully revived %s!" } \ No newline at end of file