Skip to content

Commit

Permalink
Fix some bugs, improve mod compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jun 30, 2022
1 parent 9c8c635 commit ddca7b4
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
modCompileOnly("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")
modLocalRuntime("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")

modImplementation include("eu.pb4:placeholder-api:2.0.0-beta.5+1.19")
modImplementation include("eu.pb4:placeholder-api:2.0.0-beta.7+1.19")
modImplementation include("me.lucko:fabric-permissions-api:0.1-SNAPSHOT")

//modRuntime "supercoder79:databreaker:0.2.7"
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ yarn_mappings=1.19+build.1
loader_version=0.14.6

#Fabric api
fabric_version=0.55.1+1.19
fabric_version=0.57.0+1.19

# Mod Properties
mod_version = 1.3.2+1.19
mod_version = 1.3.3+1.19
maven_group = eu.pb4
archives_base_name = styled-chat

Expand Down
14 changes: 4 additions & 10 deletions src/main/java/eu/pb4/styledchat/StyledChatMod.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.pb4.styledchat;

import com.mojang.serialization.Lifecycle;
import eu.pb4.placeholders.api.Placeholders;
import eu.pb4.styledchat.command.Commands;
import eu.pb4.styledchat.config.ConfigManager;
import net.fabricmc.api.ModInitializer;
Expand Down Expand Up @@ -32,24 +33,17 @@ public void onInitialize() {
this.crabboardDetection();
Commands.register();

BuiltinRegistries.add(BuiltinRegistries.MESSAGE_TYPE, MESSAGE_TYPE,
new MessageType(Optional.of(new MessageType.DisplayRule(Optional.of(Decoration.ofChat("%s")))),
Optional.empty(),
Optional.of(MessageType.NarrationRule.of(Decoration.ofChat("%s"), MessageType.NarrationRule.Kind.CHAT)))
);

ServerLifecycleEvents.SERVER_STARTING.register((s) -> {
this.crabboardDetection();
server = s;
});

ServerLifecycleEvents.SERVER_STARTED.register((s) -> {
ConfigManager.loadConfig();
server = s;
});

ServerLifecycleEvents.SERVER_STOPPED.register((s) -> {
server = null;
});

Placeholders.registerChangeEvent((id, removed) -> ConfigManager.clearCached());
}

private void crabboardDetection() {
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/eu/pb4/styledchat/StyledChatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ public final class StyledChatUtils {
public static final String POS_TAG = "pos";
public static final String SPOILER_TAG = "spoiler";


public static final TextParserV1.TagNodeBuilder SPOILER_TAG_HANDLER = (tag, data, input, handlers, endAt) -> {
var out = TextParserV1.parseNodesWith(input, handlers, endAt);

return new TextParserV1.TagNodeValue(new SpoilerNode(out.nodes()), out.length());
};

public static final TextParserV1.TextTag SPOILER_TEXT_TAG = TextParserV1.TextTag.of(SPOILER_TAG, List.of("hide"), "styledchat", true, SPOILER_TAG_HANDLER);


public static final String FORMAT_PERMISSION_BASE = "styledchat.format.";
public static final String FORMAT_PERMISSION_UNSAFE = "styledchat.unsafe_format.";
public static final Pattern EMOTE_PATTERN = Pattern.compile("[:](?<id>[^:]+)[:]");
Expand All @@ -69,7 +73,7 @@ public static TextParserV1 createParser(ServerCommandSource source) {
Config config = ConfigManager.getConfig();

for (var entry : TextParserV1.DEFAULT.getTags()) {
if (config.defaultFormattingCodes.getBoolean(entry)
if (config.defaultFormattingCodes.getBoolean(entry.name())
|| Permissions.check(source, (entry.userSafe() ? FORMAT_PERMISSION_BASE : FORMAT_PERMISSION_UNSAFE) + entry.name(), entry.userSafe() ? 2 : 4)
|| Permissions.check(source, (entry.userSafe() ? FORMAT_PERMISSION_BASE : FORMAT_PERMISSION_UNSAFE) + ".type." + entry.type(), entry.userSafe() ? 2 : 4)
) {
Expand All @@ -79,8 +83,7 @@ public static TextParserV1 createParser(ServerCommandSource source) {

if (config.defaultFormattingCodes.getBoolean(SPOILER_TAG)
|| Permissions.check(source, FORMAT_PERMISSION_BASE + SPOILER_TAG, 2)) {

parser.register(TextParserV1.TextTag.of(SPOILER_TAG, List.of("hide"), "styledchat", true, SPOILER_TAG_HANDLER));
parser.register(SPOILER_TEXT_TAG);
}

StyledChatEvents.FORMATTING_CREATION_EVENT.invoker().onFormattingBuild(source, parser);
Expand Down Expand Up @@ -346,4 +349,13 @@ public static Text formatFor(ServerCommandSource source, String original) {
return formatFor(PlaceholderContext.of(source.getServer()), original);
}
}

public static FilteredMessage<SignedMessage> toEventMessage(FilteredMessage<SignedMessage> message, PlaceholderContext context) {
var ext = (ExtSignedMessage) (Object) message.raw();

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

return FilteredMessage.permitted(SignedMessage.of(input));
}
}
25 changes: 19 additions & 6 deletions src/main/java/eu/pb4/styledchat/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@ public class ConfigManager {
public static final int VERSION = 2;
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().setLenient().create();

private static Config CONFIG = null;
private static Config config = null;
private static ConfigData configData = null;

public static Config getConfig() {
return CONFIG;
if (config == null) {
if (configData == null) {
loadConfig();
}

config = new Config(configData);
}

return config;
}

public static void clearCached() {
config = null;
}

public static boolean loadConfig() {
config = null;
try {
ConfigData config;
File configFile = new File(FabricLoader.getInstance().getConfigDir().toFile(), "styled-chat.json");
Expand All @@ -44,14 +58,13 @@ public static boolean loadConfig() {
writer.write(GSON.toJson(config));
writer.close();


CONFIG = new Config(config);
configData = config;
return true;
} catch(Exception exception) {
StyledChatMod.LOGGER.error("Something went wrong while reading config! Make sure format is correct!");
exception.printStackTrace();
if (CONFIG == null) {
CONFIG = new Config(new ConfigData());
if (configData == null) {
configData = new ConfigData();
}
return false;
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/eu/pb4/styledchat/mixin/MessageTypeMixin.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package eu.pb4.styledchat.mixin;

import eu.pb4.styledchat.StyledChatMod;
import net.minecraft.network.message.MessageType;
import net.minecraft.text.Decoration;
import net.minecraft.util.registry.BuiltinRegistries;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryEntry;
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.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Optional;

@Mixin(MessageType.class)
public class MessageTypeMixin {
@ModifyArg(method = "initialize", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/message/MessageType;<init>(Ljava/util/Optional;Ljava/util/Optional;Ljava/util/Optional;)V"), index = 0)
private static Optional styledChat_replace(Optional<MessageType.DisplayRule> optional) {
return optional.isPresent() && optional.get().decoration().isPresent() ? Optional.of(MessageType.DisplayRule.of()) : optional;
@Inject(method = "initialize", at = @At("TAIL"))
private static void styledChat_replace(Registry<MessageType> registry, CallbackInfoReturnable<RegistryEntry<MessageType>> cir) {

BuiltinRegistries.add(registry, StyledChatMod.MESSAGE_TYPE,
new MessageType(Optional.of(new MessageType.DisplayRule(Optional.of(Decoration.ofChat("%s")))),
Optional.empty(),
Optional.of(MessageType.NarrationRule.of(Decoration.ofChat("%s"), MessageType.NarrationRule.Kind.CHAT)))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void styledChat_excludeSendingOfHiddenMessages2(Text message, Function<S
}
}

@Redirect(method = "broadcast(Lnet/minecraft/network/message/SignedMessage;Ljava/util/function/Function;Lnet/minecraft/network/message/MessageSender;Lnet/minecraft/util/registry/RegistryKey;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;logChatMessage(Lnet/minecraft/network/message/MessageSender;Lnet/minecraft/text/Text;)V"))
@Redirect(method = "broadcast(Lnet/minecraft/network/message/SignedMessage;Ljava/util/function/Function;Lnet/minecraft/network/message/MessageSender;Lnet/minecraft/util/registry/RegistryKey;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;logChatMessage(Lnet/minecraft/network/message/MessageSender;Lnet/minecraft/text/Text;)V"), require = 0)
private void styledChat_fixServerLogs(MinecraftServer instance, MessageSender sender, Text ignore, SignedMessage message) {
var out = ((ExtSignedMessage) (Object) message).styledChat_getArg("override");
if (out != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package eu.pb4.styledchat.mixin;

import eu.pb4.placeholders.api.PlaceholderContext;
import eu.pb4.styledchat.StyledChatUtils;
import eu.pb4.styledchat.other.WrappedEvent;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerMessageEvents.class)
public class ServerMessageEventsMixin {
@Mutable
@Shadow @Final public static Event<ServerMessageEvents.AllowChatMessage> ALLOW_CHAT_MESSAGE;

@Mutable
@Shadow @Final public static Event<ServerMessageEvents.AllowCommandMessage> ALLOW_COMMAND_MESSAGE;

@Mutable
@Shadow @Final public static Event<ServerMessageEvents.ChatMessage> CHAT_MESSAGE;

@Mutable
@Shadow @Final public static Event<ServerMessageEvents.CommandMessage> COMMAND_MESSAGE;

@Inject(method = "<clinit>", at = @At("TAIL"))
private static void styledChat_replaceEvents(CallbackInfo ci) {
ALLOW_CHAT_MESSAGE = new WrappedEvent<>(ALLOW_CHAT_MESSAGE,
e -> (message, sender, typeKey) -> e.invoker().allowChatMessage(StyledChatUtils.toEventMessage(message, PlaceholderContext.of(sender)), sender, typeKey));

ALLOW_COMMAND_MESSAGE = new WrappedEvent<>(ALLOW_COMMAND_MESSAGE,
e -> (message, sender, typeKey) -> e.invoker().allowCommandMessage(StyledChatUtils.toEventMessage(message, PlaceholderContext.of(sender)), sender, typeKey));

CHAT_MESSAGE = new WrappedEvent<>(CHAT_MESSAGE,
e -> (message, sender, typeKey) -> e.invoker().onChatMessage(StyledChatUtils.toEventMessage(message, PlaceholderContext.of(sender)), sender, typeKey));

COMMAND_MESSAGE = new WrappedEvent<>(COMMAND_MESSAGE,
e -> (message, sender, typeKey) -> e.invoker().onCommandMessage(StyledChatUtils.toEventMessage(message, PlaceholderContext.of(sender)), sender, typeKey));

}
}
19 changes: 19 additions & 0 deletions src/main/java/eu/pb4/styledchat/other/WrappedEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package eu.pb4.styledchat.other;

import net.fabricmc.fabric.api.event.Event;

import java.util.function.Function;

public final class WrappedEvent<T> extends Event<T> {
public final Event<T> event;

public WrappedEvent(Event<T> event, Function<Event<T>, T> argModifier) {
this.event = event;
this.invoker = argModifier.apply(event);
}

@Override
public void register(T listener) {
this.event.register(listener);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
],

"depends": {
"minecraft": ">=1.18-rc.1"
"minecraft": "1.19"
},
"custom": {
"modmenu": {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/styledchat.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"PlayerEntityMixin",
"PlayerManagerMixin",
"SayCommandMixin",
"ServerMessageEventsMixin",
"ServerPlayerEntityMixin",
"ServerPlayNetworkManagerMixin",
"SignedMessageMixin",
Expand Down

0 comments on commit ddca7b4

Please sign in to comment.