Skip to content

Commit

Permalink
Small update
Browse files Browse the repository at this point in the history
- Added logo to the jar
- Emoticons work with /tellform
- Changed some default config values
- Some bugfixes and improvements in config error handling
  • Loading branch information
Patbox committed Jan 1, 2022
1 parent 2601d04 commit 9575a25
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 40 deletions.
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.18
yarn_mappings=1.18+build.1
loader_version=0.12.6
minecraft_version=1.18.1
yarn_mappings=1.18.1+build.1
loader_version=0.12.12

#Fabric api
fabric_version=0.43.1+1.18
fabric_version=0.45.0+1.18

# Mod Properties
mod_version = 1.2.2+1.18
mod_version = 1.2.3+1.18
maven_group = eu.pb4
archives_base_name = styled-chat

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/eu/pb4/styledchat/StyledChatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ public static Map<String, Text> getEmotes(MinecraftServer server) {
return new FakeMapServer(server, ConfigManager.getConfig().getEmotes(server.getCommandSource()));
}

public static Text formatFor(ServerPlayerEntity player, String input) {
return PlaceholderAPI.parsePredefinedText(
TextParser.parse(StyledChatUtils.formatMessage(input, getHandlers(player))),
EMOTE_PATTERN, getEmotes(player)
);
}

public static Text formatFor(MinecraftServer server, String input) {
return PlaceholderAPI.parsePredefinedText(
TextParser.parse(StyledChatUtils.formatMessage(input, TextParser.getRegisteredTags())),
EMOTE_PATTERN, getEmotes(server)
);
}

public static String formatMessage(String input, Map<String, TextParser.TextFormatterHandler> handlers) {
var config = ConfigManager.getConfig();
if (handlers.containsKey(StyledChatUtils.LINK_TAG)) {
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/eu/pb4/styledchat/command/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,18 @@

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import eu.pb4.placeholders.TextParser;
import eu.pb4.styledchat.StyledChatMod;
import eu.pb4.styledchat.StyledChatUtils;
import eu.pb4.styledchat.config.ConfigManager;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.TextArgumentType;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Texts;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Util;

import java.util.Iterator;

import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;

Expand All @@ -45,7 +40,13 @@ public static void register() {
.then(argument("message", StringArgumentType.greedyString())
.executes((context) -> {
int i = 0;
var parsed = TextParser.parse(StyledChatUtils.formatMessage(context.getArgument("message", String.class), TextParser.getRegisteredTags()));
Text parsed;
try {
parsed = StyledChatUtils.formatFor(context.getSource().getPlayer(), context.getArgument("message", String.class));
} catch (Exception e) {
parsed = StyledChatUtils.formatFor(context.getSource().getServer(), context.getArgument("message", String.class));

}

for (var player : EntityArgumentType.getPlayers(context, "targets")) {
player.sendSystemMessage(parsed, Util.NIL_UUID);
Expand All @@ -64,7 +65,7 @@ private static int reloadConfig(CommandContext<ServerCommandSource> context) {
if (ConfigManager.loadConfig()) {
context.getSource().sendFeedback(new LiteralText("Reloaded config!"), false);
} else {
context.getSource().sendError(new LiteralText("Error accrued while reloading config!").formatted(Formatting.RED));
context.getSource().sendError(new LiteralText("Error occurred while reloading config! Check console for more information!").formatted(Formatting.RED));

}
return 1;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/eu/pb4/styledchat/config/ChatStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ public ChatStyle(ChatStyleData data) {
public Text getDisplayName(ServerPlayerEntity player, Text vanillaDisplayName) {
if (this.displayName == null) {
return null;
} else if (this.advancementGoal == StyledChatUtils.IGNORED_TEXT) {
} else if (this.displayName == StyledChatUtils.IGNORED_TEXT) {
return vanillaDisplayName;
}

return PlaceholderAPI.parsePredefinedText(
PlaceholderAPI.parseText(this.displayName, player),
PlaceholderAPI.PREDEFINED_PLACEHOLDER_PATTERN,
Map.of("vanillaDisplayName", vanillaDisplayName,
"player", vanillaDisplayName,
"default", vanillaDisplayName,
"name", player.getName())
);
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/eu/pb4/styledchat/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class ConfigManager {
public static final int VERSION = 2;
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().setLenient().create();

private static Config CONFIG;
private static Config CONFIG = null;

public static Config getConfig() {
return CONFIG;
}

public static boolean loadConfig() {
CONFIG = null;
try {
ConfigData config;
File configFile = new File(FabricLoader.getInstance().getConfigDir().toFile(), "styled-chat.json");


if (configFile.exists()) {
String json = IOUtils.toString(new InputStreamReader(new FileInputStream(configFile), "UTF-8"));
String json = IOUtils.toString(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8));
VersionConfigData versionConfigData = GSON.fromJson(json, VersionConfigData.class);

config = ConfigData.transform(switch (versionConfigData.CONFIG_VERSION_DONT_TOUCH_THIS) {
Expand All @@ -42,18 +42,19 @@ public static boolean loadConfig() {
config = new ConfigData();
}

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), "UTF-8"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), StandardCharsets.UTF_8));
writer.write(GSON.toJson(config));
writer.close();


CONFIG = new Config(config);
return true;
}
catch(IOException exception) {
StyledChatMod.LOGGER.error("Something went wrong while reading config!");
} catch(Exception exception) {
StyledChatMod.LOGGER.error("Something went wrong while reading config! Make sure format is correct!");
exception.printStackTrace();
CONFIG = new Config(new ConfigData());
if (CONFIG == null) {
CONFIG = new Config(new ConfigData());
}
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public class ChatStyleData {

private static ChatStyleData getDefault() {
ChatStyleData data = new ChatStyleData();
data.displayName = "${vanillaDisplayName}";
data.displayName = "${default}";
data.chat = "<${player}> ${message}";
data.join = "<yellow><lang:multiplayer.player.joined:'${player}'></yellow>";
data.joinRenamed = "<yellow><lang:multiplayer.player.joined.renamed:'${player}':'${old_name}'></yellow>";
data.joinFirstTime = "<yellow><lang:multiplayer.player.joined:'${player}'></yellow>";
data.left = "<yellow><lang:multiplayer.player.left:'${player}'></yellow>";
data.death = "${default_message}";
data.advancementTask = "<><lang:chat.type.advancement.task:'${player}':'${advancement}'>";
data.advancementTask = "<lang:chat.type.advancement.task:'${player}':'${advancement}'>";
data.advancementGoal = "<lang:chat.type.advancement.goal:'${player}':'${advancement}'>";
data.advancementChallenge = "<lang:chat.type.advancement.challenge:'${player}':'${advancement}'>";
data.teamChatSent = "<lang:'chat.type.team.sent':'<hover\\:\\'<lang\\:chat.type.team.hover>\\'><suggest_command\\:\\'/teammsg \\'>${team}':'${displayName}':'${message}'>";
Expand All @@ -51,6 +51,7 @@ public void fillMissing() {
this.chat = Objects.requireNonNullElse(this.chat, DEFAULT.chat);
this.join = Objects.requireNonNullElse(this.join, DEFAULT.join);
this.joinRenamed = Objects.requireNonNullElse(this.joinRenamed, DEFAULT.joinRenamed);
this.joinFirstTime = Objects.requireNonNullElse(this.joinFirstTime, DEFAULT.joinFirstTime);
this.left = Objects.requireNonNullElse(this.left, DEFAULT.left);
this.death = Objects.requireNonNullElse(this.death, DEFAULT.death);
this.advancementTask = Objects.requireNonNullElse(this.advancementTask, DEFAULT.advancementTask);
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/eu/pb4/styledchat/config/data/ConfigData.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package eu.pb4.styledchat.config.data;

import com.mojang.serialization.RecordBuilder;
import eu.pb4.placeholders.TextParser;
import eu.pb4.placeholders.util.GeneralUtils;
import eu.pb4.styledchat.StyledChatUtils;
import eu.pb4.styledchat.config.ConfigManager;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.text.MutableText;
import net.minecraft.util.Hand;
import net.minecraft.util.Formatting;

import java.util.*;

Expand All @@ -22,18 +18,17 @@ public class ConfigData {

public List<PermissionEmotes> permissionEmoticons = new ArrayList<>();

public boolean legacyChatFormatting = false;
public boolean legacyChatFormatting = true;
public boolean parseLinksInChat = true;
public boolean enableMarkdown = true;
public boolean formattingInPrivateMessages = true;
public boolean formattingInTeamMessages = true;
public String linkStyle = "<underline><c:#0000EE>${link}";
public String linkStyle = "<underline><c:#7878ff>${link}";
public String spoilerStyle = "<gray>${spoiler}";
public String spoilerSymbol = "▌";
public HashMap<String, Boolean> defaultEnabledFormatting = getDefaultFormatting();



private static HashMap<String, Boolean> getDefaultFormatting() {
HashMap<String, Boolean> map = new HashMap<>();
for (String string : TextParser.getRegisteredSafeTags().keySet()) {
Expand All @@ -49,6 +44,13 @@ private static HashMap<String, Boolean> getDefaultFormatting() {
map.put("strikethrough", true);
map.put("st", true);
map.put("underline", true);

for (var formatting : Formatting.values()) {
if (formatting.isColor()) {
map.put(formatting.getName(), true);
}
}

return map;
}

Expand Down
8 changes: 1 addition & 7 deletions src/main/java/eu/pb4/styledchat/mixin/MeCommandMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ private static void styledChat_formatText(CommandContext<ServerCommandSource> co
}

if (emotes.size() != 0) {
message = PlaceholderAPI.parsePredefinedText(message, StyledChatUtils.EMOTE_PATTERN, new AbstractMap<String, Text>() {
@NotNull
@Override
public Set<Entry<String, Text>> entrySet() {
return null;
}
});
message = PlaceholderAPI.parsePredefinedText(message, StyledChatUtils.EMOTE_PATTERN, emotes);
}


Expand Down
Binary file added src/main/resources/assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},

"license": "LGPLv3",

"icon": "./assets/icon.png",
"environment": "*",
"entrypoints": {
"main": [
Expand All @@ -27,5 +27,12 @@

"depends": {
"minecraft": ">=1.18-rc.1"
},
"custom": {
"modmenu": {
"links": {
"modmenu.discord": "https://pb4.eu/discord"
}
}
}
}

0 comments on commit 9575a25

Please sign in to comment.