Skip to content

Commit

Permalink
Use Style instead of Formatting to style chat messages.
Browse files Browse the repository at this point in the history
Config accepts either a valid formatting string or style json.
Style json = the json used in things like tellraw.
Config reverts to defaults if style is invalid or undefined and warns in console log @ startup. No longer crashes.
  • Loading branch information
John-Paul-R committed Jun 21, 2021
1 parent e06e666 commit a5980dc
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 58 deletions.
68 changes: 60 additions & 8 deletions src/main/java/com/fibermc/essentialcommands/Config.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.fibermc.essentialcommands;

import com.google.gson.JsonDeserializer;
import com.google.gson.JsonParser;
import net.minecraft.text.Style;
import net.minecraft.util.Formatting;
import org.apache.logging.log4j.Level;

Expand All @@ -9,18 +12,17 @@
import java.io.IOException;
import java.util.AbstractMap.SimpleEntry;
import java.util.List;

import com.fibermc.essentialcommands.SortedProperties;
import java.util.Objects;

public class Config {

public static SortedProperties props = new SortedProperties();

private static String CONFIG_PATH = "./config/EssentialCommands.properties";

public static Formatting FORMATTING_DEFAULT;
public static Formatting FORMATTING_ACCENT;
public static Formatting FORMATTING_ERROR;
public static Style FORMATTING_DEFAULT;
public static Style FORMATTING_ACCENT;
public static Style FORMATTING_ERROR;
public static boolean ENABLE_BACK;
public static boolean ENABLE_HOME;
public static boolean ENABLE_SPAWN;
Expand Down Expand Up @@ -72,10 +74,21 @@ public static void initProperties() {
for (SimpleEntry<String, String> property : defProps) {
props.putIfAbsent(property.getKey(), property.getValue());
}
styleJsonDeserializer = new Style.Serializer();
jsonParser = new JsonParser();

FORMATTING_DEFAULT = Formatting.byName((String) props.get(defProps.get(0).getKey()));
FORMATTING_ACCENT = Formatting.byName((String) props.get(defProps.get(1).getKey()));
FORMATTING_ERROR = Formatting.byName((String) props.get(defProps.get(2).getKey()));
FORMATTING_DEFAULT = parseStyleOrDefault(
(String)props.get(defProps.get(0).getKey()),
defProps.get(0).getValue()
);
FORMATTING_ACCENT = parseStyleOrDefault(
(String)props.get(defProps.get(1).getKey()),
defProps.get(1).getValue()
);
FORMATTING_ERROR = parseStyleOrDefault(
(String)props.get(defProps.get(2).getKey()),
defProps.get(2).getValue()
);
ENABLE_BACK = Boolean.parseBoolean((String) props.get(defProps.get(3).getKey()));
ENABLE_HOME = Boolean.parseBoolean((String) props.get(defProps.get(4).getKey()));
ENABLE_SPAWN = Boolean.parseBoolean((String) props.get(defProps.get(5).getKey()));
Expand All @@ -89,6 +102,45 @@ public static void initProperties() {
USE_PERMISSIONS_API = Boolean.parseBoolean((String) props.get(defProps.get(13).getKey()));
CHECK_FOR_UPDATES = Boolean.parseBoolean((String) props.get(defProps.get(14).getKey()));
TELEPORT_INTERRUPT_ON_DAMAGED = Boolean.parseBoolean((String) props.get(defProps.get(15).getKey()));


Objects.requireNonNull(FORMATTING_DEFAULT);
Objects.requireNonNull(FORMATTING_ACCENT);
Objects.requireNonNull(FORMATTING_ERROR);
// FORMATTING_DEFAULT = FORMATTING_DEFAULT == null ? Formatting.byName(defProps.get(0).getValue()) : FORMATTING_DEFAULT;
// FORMATTING_ACCENT = FORMATTING_ACCENT == null ? Formatting.byName(defProps.get(1).getValue()) : FORMATTING_ACCENT;
// FORMATTING_ERROR = FORMATTING_ERROR == null ? Formatting.byName(defProps.get(2).getValue()) : FORMATTING_ERROR;
}

private static JsonDeserializer<Style> styleJsonDeserializer;
private static JsonParser jsonParser;
private static Style parseStyleOrDefault(String styleStr, String defaultStyleStr) {
Style outStyle = parseStyle(styleStr);
if (Objects.isNull(outStyle)) {
outStyle = parseStyle(defaultStyleStr);
EssentialCommands.log(
Level.WARN,
String.format("Could not load malformed style: '%s'. Using default, '%s'.", styleStr, defaultStyleStr)
);
}
return outStyle;
}

private static Style parseStyle(String styleStr) {
Style outStyle = null;
Formatting formatting = Formatting.byName(styleStr);
if (Objects.nonNull(formatting)) {
outStyle = Style.EMPTY.withFormatting(formatting);
}

if (Objects.isNull(outStyle)) {
outStyle = styleJsonDeserializer.deserialize(
jsonParser.parse(styleStr),
null, null
);
}

return outStyle;
}

public static void storeProperties() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void register() {
final String disabledString = "[EssentialCommands] This command is not enabled.";
Command<ServerCommandSource> disabledCommandCommand = context -> {
context.getSource().getPlayer().sendSystemMessage(
new LiteralText(disabledString).formatted(Config.FORMATTING_ERROR)
new LiteralText(disabledString).setStyle(Config.FORMATTING_ERROR)
, new UUID(0,0));
return 1;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public static void requestTeleport(QueuedTeleport queuedTeleport) {
((ServerPlayerEntityAccess) player).setEcQueuedTeleport(queuedTeleport);
TeleportRequestManager.getInstance().queueTeleport(queuedTeleport);
player.sendSystemMessage(
new LiteralText("Teleporting to ").formatted(Config.FORMATTING_DEFAULT)
.append(new LiteralText(queuedTeleport.getDestName()).formatted(Config.FORMATTING_ACCENT))
.append(new LiteralText(" in ").formatted(Config.FORMATTING_DEFAULT))
.append(new LiteralText(String.format("%.1f", Config.TELEPORT_DELAY)).formatted(Config.FORMATTING_ACCENT))
.append(new LiteralText(" seconds...")).formatted(Config.FORMATTING_DEFAULT),
new LiteralText("Teleporting to ").setStyle(Config.FORMATTING_DEFAULT)
.append(new LiteralText(queuedTeleport.getDestName()).setStyle(Config.FORMATTING_ACCENT))
.append(new LiteralText(" in ").setStyle(Config.FORMATTING_DEFAULT))
.append(new LiteralText(String.format("%.1f", Config.TELEPORT_DELAY)).setStyle(Config.FORMATTING_ACCENT))
.append(new LiteralText(" seconds...")).setStyle(Config.FORMATTING_DEFAULT),
new UUID(0,0)
);
}
Expand Down Expand Up @@ -61,9 +61,9 @@ private static void execTeleport(ServerPlayerEntity playerEntity, MinecraftLocat
dest.headYaw, dest.pitch
);
playerEntity.sendSystemMessage(
new LiteralText("Teleported to ").formatted(Config.FORMATTING_DEFAULT)
.append(dest.toLiteralTextSimple().formatted(Config.FORMATTING_ACCENT))
.append(new LiteralText(".").formatted(Config.FORMATTING_DEFAULT)),
new LiteralText("Teleported to ").setStyle(Config.FORMATTING_DEFAULT)
.append(dest.toLiteralTextSimple().setStyle(Config.FORMATTING_ACCENT))
.append(new LiteralText(".").setStyle(Config.FORMATTING_DEFAULT)),
new UUID(0,0)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void onPlayerDamaged(ServerPlayerEntity playerEntity, DamageSource damage

delayedTeleportQueue.remove(playerEntity.getUuid());
playerEntity.sendSystemMessage(
new LiteralText("Teleport interrupted. Reason: Damage Taken").formatted(Config.FORMATTING_ERROR),
new LiteralText("Teleport interrupted. Reason: Damage Taken").setStyle(Config.FORMATTING_ERROR),
new UUID(0, 0)
);
} catch (NullPointerException ignored) {}
Expand Down Expand Up @@ -124,7 +124,7 @@ public void queueTeleport(QueuedTeleport queuedTeleport) {
if (Objects.nonNull(prevValue)) {
prevValue.getPlayerData().getPlayer().sendSystemMessage(
new LiteralText("Teleport request canceled. Reason: New teleport started!")
.formatted(Config.FORMATTING_DEFAULT),
.setStyle(Config.FORMATTING_DEFAULT),
new UUID(0,0)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax
out=1;
} else {
player.sendSystemMessage(
new LiteralText("Could not execute 'back' command. No previous location found.").formatted(Config.FORMATTING_ERROR)
new LiteralText("Could not execute 'back' command. No previous location found.").setStyle(Config.FORMATTING_ERROR)
, new UUID(0, 0));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax
out = 1;
} else {
// senderPlayer.sendSystemMessage(
// new LiteralText("No home with the name '").formatted(Config.FORMATTING_ERROR)
// .append(new LiteralText(homeName).formatted(Config.FORMATTING_ACCENT))
// .append(new LiteralText("' could be found.").formatted(Config.FORMATTING_ERROR))
// new LiteralText("No home with the name '").setStyle(Config.FORMATTING_ERROR)
// .append(new LiteralText(homeName).setStyle(Config.FORMATTING_ACCENT))
// .append(new LiteralText("' could be found.").setStyle(Config.FORMATTING_ERROR))
// , new UUID(0, 0));
Message msg = new LiteralMessage("No home with the name '" + homeName + "' could be found.");
throw new CommandSyntaxException(new SimpleCommandExceptionType(msg), msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax
//inform command sender that the home has been removed
if (wasSuccessful) {
senderPlayer.sendSystemMessage(
new LiteralText("Home ").formatted(Config.FORMATTING_DEFAULT)
.append(new LiteralText(homeName).formatted(Config.FORMATTING_ACCENT))
.append(new LiteralText(" has been deleted.").formatted(Config.FORMATTING_DEFAULT))
new LiteralText("Home ").setStyle(Config.FORMATTING_DEFAULT)
.append(new LiteralText(homeName).setStyle(Config.FORMATTING_ACCENT))
.append(new LiteralText(" has been deleted.").setStyle(Config.FORMATTING_DEFAULT))
, new UUID(0, 0));
} else {
senderPlayer.sendSystemMessage(
new LiteralText("Home ").formatted(Config.FORMATTING_ERROR)
.append(new LiteralText(homeName).formatted(Config.FORMATTING_ACCENT))
.append(new LiteralText(" could not be deleted.").formatted(Config.FORMATTING_ERROR))
new LiteralText("Home ").setStyle(Config.FORMATTING_ERROR)
.append(new LiteralText(homeName).setStyle(Config.FORMATTING_ACCENT))
.append(new LiteralText(" could not be deleted.").setStyle(Config.FORMATTING_ERROR))
, new UUID(0, 0));
out = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax
//inform command sender that the home has been set
if (successCode == 1) {
senderPlayer.sendSystemMessage(
new LiteralText("Home '").formatted(Config.FORMATTING_DEFAULT)
.append(new LiteralText(homeName).formatted(Config.FORMATTING_ACCENT))
.append(new LiteralText("' set.").formatted(Config.FORMATTING_DEFAULT))
new LiteralText("Home '").setStyle(Config.FORMATTING_DEFAULT)
.append(new LiteralText(homeName).setStyle(Config.FORMATTING_ACCENT))
.append(new LiteralText("' set.").setStyle(Config.FORMATTING_DEFAULT))
, new UUID(0, 0));
} else if (successCode==0) {
senderPlayer.sendSystemMessage(
new LiteralText("Home '").formatted(Config.FORMATTING_ERROR)
.append(new LiteralText(homeName).formatted(Config.FORMATTING_ACCENT))
.append(new LiteralText("' could not be set. Home limit (").formatted(Config.FORMATTING_ERROR))
.append(new LiteralText(String.valueOf(Config.HOME_LIMIT)).formatted(Config.FORMATTING_ACCENT))
.append(new LiteralText(") reached.").formatted(Config.FORMATTING_ERROR))
new LiteralText("Home '").setStyle(Config.FORMATTING_ERROR)
.append(new LiteralText(homeName).setStyle(Config.FORMATTING_ACCENT))
.append(new LiteralText("' could not be set. Home limit (").setStyle(Config.FORMATTING_ERROR))
.append(new LiteralText(String.valueOf(Config.HOME_LIMIT)).setStyle(Config.FORMATTING_ACCENT))
.append(new LiteralText(") reached.").setStyle(Config.FORMATTING_ERROR))
, new UUID(0, 0));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax

//inform command sender that the home has been set
senderPlayer.sendSystemMessage(
new LiteralText("Spawn set at ").formatted(Config.FORMATTING_DEFAULT)
.append(new LiteralText(loc.pos.toString()).formatted(Config.FORMATTING_ACCENT))
new LiteralText("Spawn set at ").setStyle(Config.FORMATTING_DEFAULT)
.append(new LiteralText(loc.pos.toString()).setStyle(Config.FORMATTING_ACCENT))
, new UUID(0, 0));

return successCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax

//inform target player that teleport has been accepted via chat
targetPlayer.sendSystemMessage(
new LiteralText("Teleport request accepted.").formatted(Config.FORMATTING_DEFAULT)
new LiteralText("Teleport request accepted.").setStyle(Config.FORMATTING_DEFAULT)
, new UUID(0, 0));

//Conduct teleportation
Expand All @@ -45,13 +45,13 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax

//Send message to command sender confirming that request has been accepted
senderPlayer.sendSystemMessage(
new LiteralText("Teleport request accepted.").formatted(Config.FORMATTING_DEFAULT)
new LiteralText("Teleport request accepted.").setStyle(Config.FORMATTING_DEFAULT)
, new UUID(0, 0));
return 1;
} else {
//throw new CommandSyntaxException(type, message)
senderPlayer.sendSystemMessage(
new LiteralText("ERROR: Teleport failed.").formatted(Config.FORMATTING_ERROR)
new LiteralText("ERROR: Teleport failed.").setStyle(Config.FORMATTING_ERROR)
, new UUID(0, 0));
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax

//inform target player of tp request via chat
targetPlayer.sendSystemMessage(
new LiteralText(senderPlayer.getEntityName()).formatted(Config.FORMATTING_ACCENT)
new LiteralText(senderPlayer.getEntityName()).setStyle(Config.FORMATTING_ACCENT)
.append(new LiteralText(" has requested to teleport to you.")
.formatted(Config.FORMATTING_DEFAULT))
.setStyle(Config.FORMATTING_DEFAULT))
.append(new LiteralText("\nType '/tpaccept <name>' to accept or '/tpdeny <name>' to deny"
+" this request.").formatted(Config.FORMATTING_DEFAULT))
+" this request.").setStyle(Config.FORMATTING_DEFAULT))
, new UUID(0, 0)
);

Expand All @@ -41,9 +41,9 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax
//inform command sender that request has been sent
senderPlayer.sendSystemMessage(
new LiteralText("Teleport request has been sent to ")
.formatted(Config.FORMATTING_DEFAULT)
.setStyle(Config.FORMATTING_DEFAULT)
.append(new LiteralText(targetPlayer.getEntityName())
.formatted(Config.FORMATTING_ACCENT))
.setStyle(Config.FORMATTING_ACCENT))
, new UUID(0, 0)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax
if (targetPlayerData.getTpTarget().getPlayer().equals(senderPlayer)) {
//inform target player that teleport has been accepted via chat
targetPlayer.sendSystemMessage(
new LiteralText("Teleport request denied.").formatted(Config.FORMATTING_DEFAULT)
new LiteralText("Teleport request denied.").setStyle(Config.FORMATTING_DEFAULT)
, new UUID(0, 0));

//Clean up TPAsk
targetPlayerData.setTpTimer(-1);

//Send message to command sender confirming that request has been accepted
senderPlayer.sendSystemMessage(
new LiteralText("Teleport request denied.").formatted(Config.FORMATTING_DEFAULT)
new LiteralText("Teleport request denied.").setStyle(Config.FORMATTING_DEFAULT)
, new UUID(0, 0));
return 1;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntax
//inform command sender that the home has been removed
if (wasSuccessful) {
senderPlayer.sendSystemMessage(
new LiteralText("Warp ").formatted(Config.FORMATTING_DEFAULT)
.append(new LiteralText(warpName).formatted(Config.FORMATTING_ACCENT))
.append(new LiteralText(" has been deleted.").formatted(Config.FORMATTING_DEFAULT))
new LiteralText("Warp ").setStyle(Config.FORMATTING_DEFAULT)
.append(new LiteralText(warpName).setStyle(Config.FORMATTING_ACCENT))
.append(new LiteralText(" has been deleted.").setStyle(Config.FORMATTING_DEFAULT))
, new UUID(0, 0));
out = 1;
} else {
senderPlayer.sendSystemMessage(
new LiteralText("Warp ").formatted(Config.FORMATTING_ERROR)
.append(new LiteralText(warpName).formatted(Config.FORMATTING_ACCENT))
.append(new LiteralText(" could not be deleted. (Correct spelling?)").formatted(Config.FORMATTING_ERROR))
new LiteralText("Warp ").setStyle(Config.FORMATTING_ERROR)
.append(new LiteralText(warpName).setStyle(Config.FORMATTING_ACCENT))
.append(new LiteralText(" could not be deleted. (Correct spelling?)").setStyle(Config.FORMATTING_ERROR))
, new UUID(0, 0));
}

Expand Down
Loading

0 comments on commit a5980dc

Please sign in to comment.