Skip to content

Commit

Permalink
fix: Improve message formatting (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hikari16665 authored Jul 18, 2024
1 parent a7672cc commit 8f46d1b
Show file tree
Hide file tree
Showing 23 changed files with 214 additions and 261 deletions.
12 changes: 6 additions & 6 deletions src/main/java/com/github/zly2006/enclosure/config/Converter.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.github.zly2006.enclosure.config;

import com.github.zly2006.enclosure.*;
import com.github.zly2006.enclosure.utils.TrT;
import com.mojang.authlib.GameProfile;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtDouble;
import net.minecraft.nbt.NbtList;
import net.minecraft.registry.RegistryKey;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.world.World;
import org.yaml.snakeyaml.Yaml;

Expand Down Expand Up @@ -189,18 +189,18 @@ private static void convertToList(Map<String, Object> data, EnclosureList enclos
MutableText status = null;
for (EnclosureArea area : enclosureList.getAreas()) {
if (enclosure.equals(area)) {
status = Text.literal(ServerMain.INSTANCE.getTranslation().get("enclosure.message.existed").getAsString());
status = TrT.of("enclosure.message.existed");
} else if (enclosure.intersect(area)) {
status = Text.literal(ServerMain.INSTANCE.getTranslation().get("enclosure.message.intersected").getAsString()).append(area.getFullName());
status = TrT.of("enclosure.message.intersected", area.getFullName());
} else if (enclosure.getName().equals(area.getName())) {
status = Text.literal(ServerMain.INSTANCE.getTranslation().get("enclosure.message.name_in_use").getAsString());
status = TrT.of("enclosure.message.name_in_use");
}
}
if (status == null) {
enclosureList.addArea(enclosure);
} else {
LOGGER.error("There was a error land which named \"" + enclosure.getFullName() + "\" while converting.");
LOGGER.error("Error type:" + status);
LOGGER.error("There was a error land which named \"{}\" while converting.", enclosure.getFullName());
LOGGER.error("Error type: {}", status);
}
}
});
Expand Down
41 changes: 11 additions & 30 deletions src/main/java/com/github/zly2006/enclosure/gui/EnclosureScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.github.zly2006.enclosure.EnclosureView;
import com.github.zly2006.enclosure.network.config.UUIDCacheS2CPacket;
import com.github.zly2006.enclosure.utils.UtilsKt;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -54,7 +56,7 @@ protected void init() {
.size(100, 20)
.position(5, 35)
.build());
playerWidget = addDrawableChild(ButtonWidget.builder(Text.translatable("enclosure.widget.player"), button -> {
playerWidget = addDrawableChild(ButtonWidget.builder(Text.translatable("enclosure.widget.showplayer"), button -> {
assert client != null;
button.active = false;
unlistedWidget.active = true;
Expand Down Expand Up @@ -98,37 +100,16 @@ protected void init() {
}, 5, 5, width - 10));
}
textWidgets.add(new ClickableTextWidget(client, this, Text.empty()
.append(Text.literal(area.getFullName()).styled(style -> style.withColor(Formatting.GOLD)))
.append(" ")
.append(Text.translatable("enclosure.info.created_by"))
.append(" ")
.append(owner == null ?
Text.translatable("enclosure.message.unknown_user").styled(style -> style.withColor(Formatting.RED)) :
Text.literal(owner).styled(style -> style.withColor(Formatting.GOLD)))
.append(", ")
.append(Text.translatable("enclosure.info.created_on"))
.append(Text.literal(new SimpleDateFormat().format(area.getCreatedOn())).styled(style -> style.withColor(Formatting.GOLD))),
.append(Text.translatable("enclosure.info.created",
Text.literal(area.getFullName()).styled(style -> style.withColor(Formatting.GOLD)),
(owner == null ?
Text.translatable("enclosure.message.unknown_user").styled(style -> style.withColor(Formatting.RED)) : Text.literal(owner).styled(style -> style.withColor(Formatting.GOLD))),
Text.literal(new SimpleDateFormat().format(area.getCreatedOn())).styled(style -> style.withColor(Formatting.GOLD))
)),
null, null,
5, 5, width - 10));
textWidgets.add(new ClickableTextWidget(client, this, Text.translatable("enclosure.message.select.from")
.append(Text.literal("[").styled(style -> style.withColor(Formatting.DARK_GREEN)))
.append(Text.literal(String.valueOf(area.getMinX())).styled(style -> style.withColor(Formatting.GREEN)))
.append(Text.literal(", ").styled(style -> style.withColor(Formatting.DARK_GREEN)))
.append(Text.literal(String.valueOf(area.getMinY())).styled(style -> style.withColor(Formatting.GREEN)))
.append(Text.literal(", ").styled(style -> style.withColor(Formatting.DARK_GREEN)))
.append(Text.literal(String.valueOf(area.getMinZ())).styled(style -> style.withColor(Formatting.GREEN)))
.append(Text.literal("]").styled(style -> style.withColor(Formatting.DARK_GREEN)))
.append(Text.translatable("enclosure.message.select.to"))
.append(Text.literal("[").styled(style -> style.withColor(Formatting.DARK_GREEN)))
.append(Text.literal(String.valueOf(area.getMaxX())).styled(style -> style.withColor(Formatting.GREEN)))
.append(Text.literal(", ").styled(style -> style.withColor(Formatting.DARK_GREEN)))
.append(Text.literal(String.valueOf(area.getMaxY())).styled(style -> style.withColor(Formatting.GREEN)))
.append(Text.literal(", ").styled(style -> style.withColor(Formatting.DARK_GREEN)))
.append(Text.literal(String.valueOf(area.getMaxZ())).styled(style -> style.withColor(Formatting.GREEN)))
.append(Text.literal("]").styled(style -> style.withColor(Formatting.DARK_GREEN)))
.append(Text.translatable("enclosure.message.select.world"))
.append(Text.literal(handler.worldId.toString()).styled(style -> style.withColor(Formatting.GOLD))),
Text.translatable("enclosure.widget.selection_render.hover"),
MutableText selectionText = UtilsKt.formatSelection(handler.worldId, area.getMinX(), area.getMinY(), area.getMinZ(), area.getMaxX(), area.getMaxY(), area.getMaxZ());
textWidgets.add(new ClickableTextWidget(client, this, selectionText, Text.translatable("enclosure.widget.selection_render.hover"),
button -> {
assert client.player != null;
client.player.networkHandler.sendChatCommand("enclosure select land " + handler.fullName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,11 @@ public void close() {
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
renderInGameBackground(context);
super.render(context, mouseX, mouseY, delta);
MutableText title = Text.translatable("enclosure.widget.set_permission").append(" ");
if (CONSOLE.equals(uuid)) {
title.append(Text.translatable("enclosure.widget.global"));
}
else {
title.append(Text.translatable("enclosure.widget.player"))
.append(" ")
.append(UUIDCacheS2CPacket.getName(uuid));
}
title.append(" ")
.append(Text.translatable("enclosure.widget.in_enclosure"))
.append(" ")
.append(fullName);

MutableText title = Text.translatable("enclosure.widget.set_permission",
(CONSOLE.equals(uuid) ? Text.translatable("enclosure.widget.global") : Text.translatable("enclosure.widget.player", UUIDCacheS2CPacket.getName(uuid))),
fullName
);
context.drawText(textRenderer, title, 10, 10, 0xffffff, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

import com.github.zly2006.enclosure.ServerMain;
import com.google.common.collect.ImmutableMap;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.util.Language;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.function.BiConsumer;

@Mixin(Language.class)
public class MixinLanguage {
@SuppressWarnings({"rawtypes", "unchecked"})
@Inject(method = "create", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(value = "INVOKE", target = "Lnet/minecraft/util/Language;load(Ljava/util/function/BiConsumer;Ljava/lang/String;)V"))
private static void create(CallbackInfoReturnable<Language> cir, ImmutableMap.Builder builder, BiConsumer biConsumer) {
@Inject(method = "create", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/Language;load(Ljava/util/function/BiConsumer;Ljava/lang/String;)V"))
private static void create(CallbackInfoReturnable<Language> cir, @Local ImmutableMap.Builder<String, String> builder) {
if (ServerMain.INSTANCE.getCommonConfig().injectServerLanguage) {
ServerMain.INSTANCE.getTranslation().entrySet().forEach(entry ->
builder.put(entry.getKey(), entry.getValue().getAsString()));
Expand Down
12 changes: 5 additions & 7 deletions src/main/kotlin/com/github/zly2006/enclosure/Enclosure.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import com.github.zly2006.enclosure.command.Session
import com.github.zly2006.enclosure.utils.Serializable2Text.SerializationSettings
import com.github.zly2006.enclosure.utils.TrT
import com.github.zly2006.enclosure.utils.clickRun
import com.github.zly2006.enclosure.utils.hoverText
import net.minecraft.nbt.NbtCompound
import net.minecraft.registry.RegistryWrapper.WrapperLookup
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.server.world.ServerWorld
import net.minecraft.text.MutableText
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import net.minecraft.util.math.BlockPos

Expand Down Expand Up @@ -59,15 +59,13 @@ class Enclosure : EnclosureArea {
val text = super.serialize(settings, player)
if (subEnclosures.areas.isNotEmpty()) {
text.append("\n")
text.append(TrT.of("enclosure.message.sub_lands"))
for (area in subEnclosures.areas) {
text.append(area.serialize(SerializationSettings.Name, player).styled {
val subLandsText = subEnclosures.areas.fold(Text.empty()) { acc, area ->
acc.append(area.serialize(SerializationSettings.NameHover, player).styled {
it.withColor(Formatting.GOLD)
.hoverText(area.serialize(SerializationSettings.Hover, player))
.clickRun("/enclosure info ${area.fullName}")
})
text.append(" ")
}).append(" ")
}
text.append(TrT.of("enclosure.message.sub_lands", subLandsText))
}
return text
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import net.minecraft.nbt.NbtList
import net.minecraft.registry.RegistryWrapper
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.server.world.ChunkTicketType
import net.minecraft.server.world.ServerWorld
import net.minecraft.text.ClickEvent
import net.minecraft.text.MutableText
import net.minecraft.text.Style
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import net.minecraft.util.Identifier
Expand Down Expand Up @@ -403,7 +401,7 @@ open class EnclosureArea : PersistentState, EnclosureView {

SerializationSettings.BarredFull -> {
val bar = Text.literal("------------------------------")
.styled { style: Style -> style.withColor(Formatting.YELLOW).withBold(true) }
.styled { it.withColor(Formatting.YELLOW).withBold(true) }
Text.empty()
.append(bar)
.append("\n")
Expand Down
16 changes: 11 additions & 5 deletions src/main/kotlin/com/github/zly2006/enclosure/PermissionHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ interface PermissionHolder : Serializable2Text {
fun hasPubPerm(perm: Permission): Boolean {
if (!perm.target.fitEnclosure()) {
throw PermissionTargetException(
TrT.of("enclosure.message.permission_target_error")+(Text.literal(perm.target.name))
TrT.of("enclosure.message.permission_target_error",
Text.literal(perm.target.name)
)
)
}
return hasPerm(CONSOLE, perm)
Expand All @@ -69,11 +71,15 @@ interface PermissionHolder : Serializable2Text {
}
if (uuid == CONSOLE && !perm.target.fitEnclosure()) {
throw PermissionTargetException(
TrT.of("enclosure.message.permission_target_error").append(perm.target.name)
TrT.of("enclosure.message.permission_target_error",
perm.target.name
)
)
} else if (uuid != CONSOLE && !perm.target.fitPlayer()) {
throw PermissionTargetException(
TrT.of("enclosure.message.permission_target_error").append(perm.target.name)
TrT.of("enclosure.message.permission_target_error",
perm.target.name
)
)
}
perm.setValue(permissionsMap[uuid]!!, value)
Expand All @@ -89,9 +95,9 @@ interface PermissionHolder : Serializable2Text {
val text = Text.literal("")
map.forEach { (key: String?, value: Boolean) ->
if (value) {
text.append(Text.literal(key).setStyle(Style.EMPTY.withColor(Formatting.GREEN)))
text.append(Text.literal(key).green())
} else {
text.append(Text.literal(key).setStyle(Style.EMPTY.withColor(Formatting.RED)))
text.append(Text.literal(key).red())
}
text.append(" ")
}
Expand Down
20 changes: 1 addition & 19 deletions src/main/kotlin/com/github/zly2006/enclosure/ServerMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ import net.minecraft.server.command.CommandManager
import net.minecraft.server.network.ServerPlayNetworkHandler
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.server.world.ServerWorld
import net.minecraft.text.Style
import net.minecraft.text.Text
import net.minecraft.text.TextColor
import net.minecraft.util.*
import net.minecraft.util.hit.BlockHitResult
import net.minecraft.util.hit.HitResult
Expand Down Expand Up @@ -108,23 +106,7 @@ object ServerMain: ModInitializer {
val item: Item,
val entity: Entity?
)
val HEADER: Text = Text.empty()
.append(Text.literal("[").styled { style: Style ->
style.withColor(
TextColor.fromRgb(0x00FF00)
)
})
.append(Text.literal("Enclosure").styled { style: Style ->
style.withColor(
TextColor.fromRgb(0x00FFFF)
)
})
.append(Text.literal("]").styled { style: Style ->
style.withColor(
TextColor.fromRgb(0x00FF00)
)
})
.append(Text.literal(" ").formatted(Formatting.RESET))

internal val enclosures: MutableMap<RegistryKey<World>, EnclosureList> = HashMap()
var operationItem: Item? = null
var playerSessions: MutableMap<UUID, Session> = HashMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ fun BuilderScope<*>.registerAbout() {
})
source.sendMessage(TrT.of("enclosure.about.team_page"))
source.sendMessage(
TrT.of("enclosure.about.version.server").append(MOD_VERSION.friendlyString)
TrT.of("enclosure.about.version.server", MOD_VERSION.friendlyString)
)
if (player != null && EnclosureInstalledC2SPacket.isInstalled(player)) {
val version = EnclosureInstalledC2SPacket.clientVersion(player)
source.sendMessage(TrT.of("enclosure.about.version.client").append(version!!.friendlyString))
source.sendMessage(TrT.of("enclosure.about.version.client", version!!.friendlyString))
}
source.sendMessage(TrT.of("enclosure.about.copyright"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.github.zly2006.enclosure.exceptions.PermissionTargetException
import com.github.zly2006.enclosure.minecraftServer
import com.github.zly2006.enclosure.utils.TrT
import com.github.zly2006.enclosure.utils.checkPermission
import com.github.zly2006.enclosure.utils.clickRun
import com.github.zly2006.enclosure.utils.hoverText
import com.mojang.brigadier.Command
import com.mojang.brigadier.arguments.ArgumentType
Expand All @@ -16,7 +17,9 @@ import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.exceptions.CommandSyntaxException
import net.minecraft.server.command.CommandManager
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.text.*
import net.minecraft.text.MutableText
import net.minecraft.text.Style
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import net.minecraft.util.math.BlockPos

Expand Down Expand Up @@ -48,7 +51,7 @@ class BuilderScope<T: argT>(var parent: T) {
throw e
} catch (e: Throwable) {
LOGGER.error("Error while executing command: " + it.input, e)
error(TrT.of("enclosure.message.error").append("${e.javaClass.simpleName}: ${e.message}"), it)
error(TrT.of("enclosure.message.error","${e.javaClass.simpleName}: ${e.message}"), it)
}
}
}
Expand All @@ -66,11 +69,10 @@ class BuilderScope<T: argT>(var parent: T) {
val firstPage = page == 1
val lastPage = page >= totalPage

val ret: MutableText = TrT.of("enclosure.menu.page.0")
.append(page.toString())
.append(TrT.of("enclosure.menu.page.1"))
.append(totalPage.toString())
.append("\n")
val ret: MutableText = TrT.of("enclosure.menu.page",
page.toString(),
totalPage.toString()
).append("\n")

var i: Int = size * (page - 1)
while (i < size * page && i < list.size) {
Expand All @@ -85,16 +87,16 @@ class BuilderScope<T: argT>(var parent: T) {
if (firstPage) Style.EMPTY.withColor(Formatting.GRAY)
else Style.EMPTY.withColor(Formatting.DARK_GREEN)
.hoverText(Text.of("Page ${page - 1}"))
.withClickEvent(ClickEvent(ClickEvent.Action.RUN_COMMAND, "$command ${page - 1}"))
.clickRun("$command ${page - 1}")
)
)
ret.append(" ")
ret.append(
TrT.of("enclosure.menu.next").setStyle(
if (lastPage) Style.EMPTY.withColor(Formatting.GRAY)
else Style.EMPTY.withColor(Formatting.DARK_GREEN)
.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.of("Page ${page + 1}")))
.withClickEvent(ClickEvent(ClickEvent.Action.RUN_COMMAND, "$command ${page + 1}"))
.hoverText(Text.of("Page ${page + 1}"))
.clickRun("$command ${page + 1}")
)
)
}
Expand Down
Loading

0 comments on commit 8f46d1b

Please sign in to comment.