Skip to content

Commit

Permalink
Add support for custom modded message types
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jan 13, 2023
1 parent 3df6be2 commit 38e8ca2
Show file tree
Hide file tree
Showing 18 changed files with 325 additions and 94 deletions.
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '0.12-SNAPSHOT'
id 'fabric-loom' version '1.+'
id 'maven-publish'
id "com.modrinth.minotaur" version "2.+"
id 'com.matthewprenger.cursegradle' version '1.4.0'
Expand Down Expand Up @@ -48,8 +48,7 @@ dependencies {
modImplementation include("me.lucko:fabric-permissions-api:0.2-SNAPSHOT")
modImplementation include("eu.pb4:player-data-api:0.2.2+1.19.3")


modCompileOnly("fr.catcore:server-translations-api:1.4.18+1.19.2")
modCompileOnly("fr.catcore:server-translations-api:1.4.19+1.19.3")
modCompileOnly("maven.modrinth:vanish:1.1.0")
//modLocalRuntime("fr.catcore:server-translations-api:1.4.17+1.19.2")

Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.19.3-rc1
yarn_mappings=1.19.3-rc1+build.1
minecraft_version=1.19.3
yarn_mappings=1.19.3+build.1
loader_version=0.14.11

#Fabric api
fabric_version=0.68.1+1.19.3

# Mod Properties

mod_version = 2.1.0+1.19.3
mod_version = 2.1.1+1.19.3
maven_group = eu.pb4
archives_base_name = styled-chat

Expand Down
32 changes: 32 additions & 0 deletions src/main/java/eu/pb4/styledchat/StyledChatStyles.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.styledchat.config.ConfigManager;
import net.minecraft.entity.passive.TameableEntity;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

Expand Down Expand Up @@ -174,4 +177,33 @@ public static Map<String, TextNode> getEmotes(ServerCommandSource source) {
return ConfigManager.getConfig().getEmotes(source);

}

public static Text getCustom(Identifier identifier, Text displayName, Text message, @Nullable Text receiver, ServerCommandSource source) {
if (source.isExecutedByPlayer()) {
var style = StyledChatUtils.getPersonalStyle(source.getPlayer()).getCustom(identifier, displayName, message, receiver, source);
if (style != null) {
return style;
}
}

var out = ConfigManager.getConfig().getCustom(identifier, displayName, message, receiver, source);

if (out != null) {
return out;
}

var type = StyledChatMod.server.getRegistryManager().get(RegistryKeys.MESSAGE_TYPE).get(identifier);

if (type == null) {
return Text.empty();
}

var params = type.params(displayName);

if (receiver != null) {
params = params.withTargetName(receiver);
}

return type.chat().apply(message, params);
}
}
16 changes: 14 additions & 2 deletions src/main/java/eu/pb4/styledchat/StyledChatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ public static boolean isHandledByMod(RegistryKey<MessageType> typeKey) {

public static void modifyForSending(SignedMessage message, ServerCommandSource source, RegistryKey<MessageType> type) {
try {
((ExtSignedMessage) (Object) message).styledChat_setArg("override", StyledChatUtils.formatMessage(message, source, type));
ExtSignedMessage.setArg(message, "override", StyledChatUtils.formatMessage(message, source, type));
((ExtSignedMessage) (Object) message).styledChat_setType(type);
((ExtSignedMessage) (Object) message).styledChat_setSource(source);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -341,6 +343,9 @@ public static Text formatMessage(SignedMessage message, ServerCommandSource sour
? baseInput
: maybeFormatFor(source, ext.styledChat_getOriginal(), message.getContent());

if (baseInput == null) {
ext.styledChat_setArg("base_input", input);
}

return switch (type.getValue().getPath()) {
case "msg_command_incoming" -> {
Expand Down Expand Up @@ -394,7 +399,7 @@ public static Text formatMessage(SignedMessage message, ServerCommandSource sour

case "chat" -> StyledChatStyles.getChat(source.getPlayer(), input);

default -> input;
default -> StyledChatStyles.getCustom(type.getValue(), source.getDisplayName(), input, null, source);
};
}

Expand All @@ -415,6 +420,9 @@ public static SignedMessage toEventMessage(SignedMessage message, PlaceholderCon

var baseInput = ext.styledChat_getArg("base_input");
var input = baseInput != null && baseInput.getContent() != TextContent.EMPTY ? baseInput : formatFor(context, ext.styledChat_getOriginal());
if (baseInput == null) {
ext.styledChat_setArg("base_input", input);
}

return new SignedMessage(message.link(), null, MessageBody.ofUnsigned(message.getSignedContent()), input, null);
}
Expand Down Expand Up @@ -497,4 +505,8 @@ public static ChatStyle createStyleOf(ServerPlayerEntity player) {

return new ChatStyle(style);
}

public static MessageType.Parameters createParameters(Text override) {
return new MessageType.Parameters(StyledChatMod.getMessageType(), override, null);
}
}
31 changes: 26 additions & 5 deletions src/main/java/eu/pb4/styledchat/command/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@
import eu.pb4.placeholders.api.PlaceholderContext;
import eu.pb4.placeholders.api.Placeholders;
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.styledchat.StyledChatMod;
import eu.pb4.styledchat.StyledChatUtils;
import eu.pb4.styledchat.config.ConfigManager;
import eu.pb4.styledchat.config.data.ChatStyleData;
import eu.pb4.styledchat.other.GenericModInfo;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.IdentifierArgumentType;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.function.BiConsumer;
import java.util.function.Function;

import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
Expand All @@ -43,22 +47,22 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher, C
.requires(Permissions.require("styledchat.set", 3))
.then(fillWithProperties(argument("players", EntityArgumentType.players()),
(x, p) -> x.then(argument("value", StringArgumentType.greedyString())
.executes((ctx) -> Commands.setProperty(ctx, p))
.executes((ctx) -> Commands.setProperty(ctx, p.apply(ctx)))
))
)
)

.then(literal("get")
.requires(Permissions.require("styledchat.get", 3))
.then(fillWithProperties(argument("player", EntityArgumentType.player()),
(x, p) -> x.executes((ctx) -> Commands.getProperty(ctx, p))
(x, p) -> x.executes((ctx) -> Commands.getProperty(ctx, p.apply(ctx)))
))
)

.then(literal("clear")
.requires(Permissions.require("styledchat.clear", 3))
.then(fillWithProperties(argument("players", EntityArgumentType.players()),
(x, p) -> x.executes((ctx) -> Commands.clearProperty(ctx, p))
(x, p) -> x.executes((ctx) -> Commands.clearProperty(ctx, p.apply(ctx)))
).then(literal("*").executes((ctx) -> Commands.clearProperty(ctx, null))))
)
);
Expand Down Expand Up @@ -140,12 +144,29 @@ private static int clearProperty(CommandContext<ServerCommandSource> context, Ch
return players.size();
}

private static ArgumentBuilder<ServerCommandSource, ?> fillWithProperties(ArgumentBuilder<ServerCommandSource, ?> base, BiConsumer<ArgumentBuilder<ServerCommandSource, ?>, ChatStyleData.PropertyGetSet> command) {
private static ArgumentBuilder<ServerCommandSource, ?> fillWithProperties(ArgumentBuilder<ServerCommandSource, ?> base, BiConsumer<ArgumentBuilder<ServerCommandSource, ?>, Function<CommandContext<ServerCommandSource>, ChatStyleData.PropertyGetSet>> command) {
for (var prop : ChatStyleData.PROPERTIES.entrySet()) {
var x = literal(prop.getKey());
command.accept(x, prop.getValue());
command.accept(x, (ctx) -> prop.getValue());
base = base.then(x);
}

{
var x = argument("id", IdentifierArgumentType.identifier())
.suggests((context, builder) -> {
for (var id : context.getSource().getServer().getRegistryManager().get(RegistryKeys.MESSAGE_TYPE).getIds()) {
if (!id.getNamespace().equals("minecraft") && !id.equals(StyledChatMod.MESSAGE_TYPE_ID.getValue())) {
builder.suggest(id.toString());
}
}

return builder.buildFuture();
});

command.accept(x, (ctx) -> ChatStyleData.PropertyGetSet.ofCustom(IdentifierArgumentType.getIdentifier(ctx, "id").toString()));
base = base.then(literal("custom").then(x));
}

return base;
}

Expand Down
59 changes: 49 additions & 10 deletions src/main/java/eu/pb4/styledchat/config/ChatStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.predicate.api.BuiltinPredicates;
import eu.pb4.predicate.api.MinecraftPredicate;
import eu.pb4.predicate.api.PredicateContext;
import eu.pb4.predicate.api.PredicateRegistry;
import eu.pb4.styledchat.StyledChatUtils;
import eu.pb4.styledchat.config.data.ChatStyleData;
import eu.pb4.styledchat.config.data.ConfigData;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
import net.minecraft.entity.passive.TameableEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
Expand Down Expand Up @@ -52,6 +50,7 @@ public class ChatStyle {
public final TextNode mentionStyle;
public final Map<String, TextNode> emoticons = new HashMap<>();
public final Object2BooleanMap<String> formatting = new Object2BooleanOpenHashMap<>();
public final Map<Identifier, TextNode> custom = new HashMap<>();


public ChatStyle(ChatStyleData data, ChatStyle defaultStyle) {
Expand Down Expand Up @@ -88,6 +87,16 @@ public ChatStyle(ChatStyleData data, ChatStyle defaultStyle) {
for (var formatting : data.formatting.entrySet()) {
this.formatting.put(formatting.getKey(), formatting.getValue().booleanValue());
}

if (data.custom != null) {
for (var entry : data.custom.entrySet()) {
var id = Identifier.tryParse(entry.getKey());

if (id != null) {
this.custom.put(id, StyledChatUtils.parseText(entry.getValue()));
}
}
}
}

public ChatStyle(ChatStyleData data) {
Expand Down Expand Up @@ -123,6 +132,16 @@ public ChatStyle(ChatStyleData data) {
for (var formatting : data.formatting.entrySet()) {
this.formatting.put(formatting.getKey(), formatting.getValue().booleanValue());
}

if (data.custom != null) {
for (var entry : data.custom.entrySet()) {
var id = Identifier.tryParse(entry.getKey());

if (id != null) {
this.custom.put(id, StyledChatUtils.parseText(entry.getValue()));
}
}
}
}


Expand Down Expand Up @@ -419,6 +438,26 @@ public Text getTeamChatReceived(Text team, Text displayName, Text message, Serve
);
}

@Nullable
public Text getCustom(Identifier identifier, Text displayName, Text message, @Nullable Text receiver, ServerCommandSource source) {
var node = this.custom.get(identifier);

if (node == null) {
return null;
} else if (node == EmptyNode.INSTANCE) {
return StyledChatUtils.IGNORED_TEXT;
}

return Placeholders.parseText(
node,
PlaceholderContext.of(source),
Placeholders.PREDEFINED_PLACEHOLDER_PATTERN,
Map.of("receiver", receiver == null ? Text.empty() : receiver,
"displayName", displayName,
"message", message)
);
}

@Nullable
public TextNode getLink() {
return this.linkStyle;
Expand All @@ -444,12 +483,12 @@ public Text getPetDeath(TameableEntity entity, Text vanillaMessage) {
return null;
}

return Placeholders.parseText(
this.petDeath,
PlaceholderContext.of(entity),
Placeholders.PREDEFINED_PLACEHOLDER_PATTERN,
Map.of("pet", entity.getDisplayName(),
"default_message", vanillaMessage)
);
return Placeholders.parseText(
this.petDeath,
PlaceholderContext.of(entity),
Placeholders.PREDEFINED_PLACEHOLDER_PATTERN,
Map.of("pet", entity.getDisplayName(),
"default_message", vanillaMessage)
);
}
}
21 changes: 19 additions & 2 deletions src/main/java/eu/pb4/styledchat/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.*;

Expand Down Expand Up @@ -42,8 +44,8 @@ public Config(ConfigData data) {
}

}


for (var tag : TextParserV1.DEFAULT.getTags()) {
this.allPossibleAutoCompletionKeys.add("<" + tag.name() + ">");
if (tag.aliases() != null) {
Expand Down Expand Up @@ -368,4 +370,19 @@ public Object2BooleanOpenHashMap<String> getAllowedFormatting(ServerCommandSourc

return base;
}

@Nullable
public Text getCustom(Identifier identifier, Text displayName, Text message, @Nullable Text receiver, ServerCommandSource source) {
var context2 = PredicateContext.of(source);
for (var entry : this.permissionStyle) {
if (entry.require.test(context2).success()) {
var text = entry.getCustom(identifier, displayName, message, receiver, source);
if (text != null) {
return text;
}
}
}

return this.defaultStyle.getCustom(identifier, displayName, message, receiver, source);
}
}
1 change: 0 additions & 1 deletion src/main/java/eu/pb4/styledchat/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public static boolean loadConfig() {
ConfigData config;
var configFile = FabricLoader.getInstance().getConfigDir().resolve("styled-chat.json");


if (Files.exists(configFile)) {
String json = Files.readString(configFile, StandardCharsets.UTF_8);
VersionConfigData versionConfigData = GSON.fromJson(json, VersionConfigData.class);
Expand Down
Loading

0 comments on commit 38e8ca2

Please sign in to comment.