Skip to content

Commit

Permalink
Port to 1.19.1-pre5
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jul 16, 2022
1 parent 0d115a7 commit c6e286b
Show file tree
Hide file tree
Showing 26 changed files with 418 additions and 212 deletions.
14 changes: 12 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ archivesBaseName = project.archives_base_name
version = project.mod_version
group = project.maven_group


repositories {
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
Expand All @@ -34,8 +35,13 @@ dependencies {
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

modCompileOnly("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")
modLocalRuntime("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")
//modCompileOnly "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
//modLocalRuntime("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")

modCompileOnly fabricApi.module("fabric-api-base", project.fabric_version)
modLocalRuntime fabricApi.module("fabric-api-base", project.fabric_version)

modCompileOnly fabricApi.module("fabric-message-api-v1", project.fabric_version)

modImplementation include("eu.pb4:placeholder-api:2.0.0-beta.7+1.19")
modImplementation include("me.lucko:fabric-permissions-api:0.1-SNAPSHOT")
Expand All @@ -45,6 +51,10 @@ dependencies {
// You may need to force-disable transitiveness on them.
}

loom {
enableTransitiveAccessWideners = false
}

processResources {
inputs.property "version", project.version

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

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.19.1-pre3
yarn_mappings=1.19.1-pre3+build.1
minecraft_version=1.19.1-pre5
yarn_mappings=1.19.1-pre5+build.1
loader_version=0.14.8

#Fabric api
fabric_version=0.57.1+1.19.1
fabric_version=0.57.2+1.19.1

# Mod Properties
mod_version = 1.3.3+1.19.1
mod_version = 1.4.0+1.19.1
maven_group = eu.pb4
archives_base_name = styled-chat

# Dependencies
is_stable = true
is_stable = false
2 changes: 0 additions & 2 deletions src/main/java/eu/pb4/styledchat/StyledChatEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import eu.pb4.placeholders.api.parsers.TextParserV1;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.network.message.MessageType;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;

public class StyledChatEvents {
/**
Expand Down
35 changes: 16 additions & 19 deletions src/main/java/eu/pb4/styledchat/StyledChatMod.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
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;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.message.MessageType;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Decoration;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.BuiltinRegistries;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.util.registry.SimpleRegistry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Optional;


public class StyledChatMod implements ModInitializer {
public static final Logger LOGGER = LogManager.getLogger("Styled Chat");
public static MinecraftServer server = null;
public static String VERSION = FabricLoader.getInstance().getModContainer("styledchat").get().getMetadata().getVersion().getFriendlyString();

public static RegistryKey<MessageType> MESSAGE_TYPE = RegistryKey.of(Registry.MESSAGE_TYPE_KEY, new Identifier("styled_chat", "generic_hack"));
public static boolean USE_FABRIC_API = true;

public static RegistryKey<MessageType> MESSAGE_TYPE_ID = RegistryKey.of(Registry.MESSAGE_TYPE_KEY, new Identifier("styled_chat", "generic_hack"));
public static MessageType MESSAGE_TYPE = new MessageType(Decoration.ofChat("%s"), Decoration.ofChat("%s"));

@Override
public void onInitialize() {
this.crabboardDetection();
Commands.register();
Placeholders.registerChangeEvent((id, removed) -> ConfigManager.clearCached());

}

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

ServerLifecycleEvents.SERVER_STOPPED.register((s) -> {
server = null;
});
public static void serverStarting(MinecraftServer s) {
crabboardDetection();
ConfigManager.loadConfig();
server = s;
}

Placeholders.registerChangeEvent((id, removed) -> ConfigManager.clearCached());
public static void serverStopped(MinecraftServer s) {
server = null;
}

private void crabboardDetection() {

private static void crabboardDetection() {
if (FabricLoader.getInstance().isModLoaded("cardboard")) {
LOGGER.error("");
LOGGER.error("Cardboard detected! This mod doesn't work with it!");
Expand Down
52 changes: 37 additions & 15 deletions src/main/java/eu/pb4/styledchat/StyledChatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
import eu.pb4.styledchat.ducks.ExtSignedMessage;
import eu.pb4.styledchat.parser.SpoilerNode;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.class_7597;
import net.minecraft.command.EntitySelector;
import net.minecraft.network.message.MessageDecorator;
import net.minecraft.network.message.MessageSignature;
import net.minecraft.network.message.MessageType;
import net.minecraft.network.message.SignedMessage;
import net.minecraft.network.packet.s2c.play.ChatSuggestionsS2CPacket;
import net.minecraft.scoreboard.Team;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.filter.FilteredMessage;
Expand Down Expand Up @@ -61,7 +60,7 @@ public final class StyledChatUtils {
public static final String FORMAT_PERMISSION_UNSAFE = "styledchat.unsafe_format.";
public static final Pattern EMOTE_PATTERN = Pattern.compile("[:](?<id>[^:]+)[:]");
public static final Text EMPTY_TEXT = Text.empty();
private static final Set<RegistryKey<MessageType>> DECORABLE = Set.of(MessageType.CHAT, MessageType.EMOTE_COMMAND, MessageType.MSG_COMMAND_INCOMING, MessageType.MSG_COMMAND_OUTGOING, MessageType.SAY_COMMAND, MessageType.TEAM_MSG_COMMAND);
private static final Set<RegistryKey<MessageType>> DECORABLE = Set.of(MessageType.CHAT, MessageType.EMOTE_COMMAND, MessageType.MSG_COMMAND_INCOMING, MessageType.MSG_COMMAND_OUTGOING, MessageType.SAY_COMMAND, MessageType.TEAM_MSG_COMMAND_INCOMING, MessageType.TEAM_MSG_COMMAND_OUTGOING);

public static TextNode parseText(String input) {
return !input.isEmpty() ? Placeholders.parseNodes(TextParserUtils.formatNodes(input)) : null;
Expand All @@ -85,7 +84,7 @@ public static TextParserV1 createParser(ServerCommandSource source) {
parser.register(SPOILER_TEXT_TAG);
}

StyledChatEvents.FORMATTING_CREATION_EVENT.invoker().onFormattingBuild(source, parser);
//StyledChatEvents.FORMATTING_CREATION_EVENT.invoker().onFormattingBuild(source, parser);

return parser;
}
Expand All @@ -100,12 +99,21 @@ public static Text formatFor(PlaceholderContext context, String input) {
if (config.configData.enableMarkdown || config.configData.legacyChatFormatting) {
input = legacyFormatMessage(input, parser.getTags().stream().map((x) -> x.name()).collect(Collectors.toSet()));
}
input = StyledChatEvents.PRE_MESSAGE_CONTENT.invoker().onPreMessage(input, context);

if (StyledChatMod.USE_FABRIC_API) {
input = StyledChatEvents.PRE_MESSAGE_CONTENT.invoker().onPreMessage(input, context);
}

var emotes = getEmotes(context);

var value = additionalParsing(new ParentNode(parser.parseNodes(new LiteralNode(input))));

if (StyledChatMod.USE_FABRIC_API) {
value = StyledChatEvents.MESSAGE_CONTENT.invoker().onMessage(value, context);
}


var text = Placeholders.parseText(
StyledChatEvents.MESSAGE_CONTENT.invoker().onMessage(additionalParsing(new ParentNode(parser.parseNodes(new LiteralNode(input)))), context),
value,
context,
EMOTE_PATTERN,
(id) -> emotes.containsKey(id) ? ((ctx, arg) -> PlaceholderResult.value(Placeholders.parseText(emotes.get(id), ctx))) : null
Expand Down Expand Up @@ -200,7 +208,7 @@ public static <T> MessageDecorator getCommandDecorator(String context, ServerCom
return CompletableFuture.completedFuture(switch (context) {
case "msg" -> {
try {
yield config.getPrivateMessageSent(
yield config.getPrivateMessageReceived(
source.getDisplayName(),
((EntitySelector) argumentGetter.apply("targets", EntitySelector.class)).getPlayers(source).get(0).getDisplayName(),
input, source
Expand All @@ -212,7 +220,7 @@ public static <T> MessageDecorator getCommandDecorator(String context, ServerCom
}
case "teammsg" -> {
try {
yield config.getTeamChatSent(((Team) source.getEntity().getScoreboardTeam()).getFormattedName(),
yield config.getTeamChatReceived(((Team) source.getEntity().getScoreboardTeam()).getFormattedName(),
source.getDisplayName(),
input, source
);
Expand Down Expand Up @@ -292,12 +300,16 @@ public static void modifyForSending(FilteredMessage<SignedMessage> message, Serv
((ExtSignedMessage) (Object) message.raw()).styledChat_setArg("override", StyledChatUtils.formatMessage(message.raw(), source, type));

if (message.raw() != message.filtered()) {
((ExtSignedMessage) (Object) message.raw()).styledChat_setArg("override", StyledChatUtils.formatMessage(message.filtered(), source, type));
((ExtSignedMessage) (Object) message.filtered()).styledChat_setArg("override", StyledChatUtils.formatMessage(message.filtered(), source, type));
}
}

public static void modifyForSending(SignedMessage message, ServerCommandSource source, RegistryKey<MessageType> type) {
((ExtSignedMessage) (Object) message).styledChat_setArg("override", StyledChatUtils.formatMessage(message, source, type));
try {
((ExtSignedMessage) (Object) message).styledChat_setArg("override", StyledChatUtils.formatMessage(message, source, type));
} catch (Exception e) {
e.printStackTrace();
}
}

public static Text formatMessage(SignedMessage message, ServerCommandSource source, RegistryKey<MessageType> type) {
Expand Down Expand Up @@ -332,7 +344,7 @@ public static Text formatMessage(SignedMessage message, ServerCommandSource sour
yield Text.empty();
}
}
case "team_msg_command" -> {
case "team_msg_command_incoming" -> {
try {
yield config.getTeamChatReceived(((Team) source.getEntity().getScoreboardTeam()).getFormattedName(),
source.getDisplayName(),
Expand All @@ -343,6 +355,16 @@ public static Text formatMessage(SignedMessage message, ServerCommandSource sour
}
}

case "team_msg_command_outgoing" -> {
try {
yield config.getTeamChatSent(((Team) source.getEntity().getScoreboardTeam()).getFormattedName(),
source.getDisplayName(),
input, source
);
} catch (Exception e) {
yield Text.literal("");
}
}
case "say_command" -> config.getSayCommand(source, input);

case "emote_command" -> config.getMeCommand(source, input);
Expand All @@ -367,12 +389,12 @@ public static FilteredMessage<SignedMessage> toEventMessage(FilteredMessage<Sign
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, MessageSignature.none(message.raw().signature().sender())));
return FilteredMessage.permitted(SignedMessage.ofUnsigned(message.raw().createMetadata(), input));
}

public static void sendAutocompliton(ServerPlayerEntity player) {
var config = ConfigManager.getConfig();
player.networkHandler.sendPacket(new class_7597(class_7597.class_7598.REMOVE, new ArrayList<>(config.allPossibleAutoCompletionKeys)));
player.networkHandler.sendPacket(new ChatSuggestionsS2CPacket(ChatSuggestionsS2CPacket.Action.REMOVE, new ArrayList<>(config.allPossibleAutoCompletionKeys)));

var set = new HashSet<String>();

Expand All @@ -398,7 +420,7 @@ public static void sendAutocompliton(ServerPlayerEntity player) {
}

if (!set.isEmpty()) {
player.networkHandler.sendPacket(new class_7597(class_7597.class_7598.ADD, new ArrayList<>(set)));
player.networkHandler.sendPacket(new ChatSuggestionsS2CPacket(ChatSuggestionsS2CPacket.Action.ADD, new ArrayList<>(set)));
}
}
}
78 changes: 39 additions & 39 deletions src/main/java/eu/pb4/styledchat/command/Commands.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
package eu.pb4.styledchat.command;


import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import eu.pb4.placeholders.api.PlaceholderContext;
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.v2.CommandRegistrationCallback;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Util;

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

public class Commands {
public static void register() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(
literal("styledchat")
.requires(Permissions.require("styledchat.main", true))
.executes(Commands::about)

.then(literal("reload")
.requires(Permissions.require("styledchat.reload", 3))
.executes(Commands::reloadConfig)
)
);

dispatcher.register(
literal("tellform")
.requires(Permissions.require("styledchat.tellform", 2))

.then(argument("targets", EntityArgumentType.players())
.then(argument("message", StringArgumentType.greedyString())
.executes((context) -> {
int i = 0;
Text parsed;

var ctx = context.getSource().getPlayer() != null ? PlaceholderContext.of(context.getSource().getPlayer()) : PlaceholderContext.of(context.getSource().getServer());

parsed = StyledChatUtils.formatFor(ctx, context.getArgument("message", String.class));

for (var player : EntityArgumentType.getPlayers(context, "targets")) {
player.sendMessage(parsed);
}

return i;
public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
dispatcher.register(
literal("styledchat")
.requires(Permissions.require("styledchat.main", true))
.executes(Commands::about)

.then(literal("reload")
.requires(Permissions.require("styledchat.reload", 3))
.executes(Commands::reloadConfig)
)
);

dispatcher.register(
literal("tellform")
.requires(Permissions.require("styledchat.tellform", 2))

.then(argument("targets", EntityArgumentType.players())
.then(argument("message", StringArgumentType.greedyString())
.executes((context) -> {
int i = 0;
Text parsed;

var ctx = context.getSource().getPlayer() != null ? PlaceholderContext.of(context.getSource().getPlayer()) : PlaceholderContext.of(context.getSource().getServer());

parsed = StyledChatUtils.formatFor(ctx, context.getArgument("message", String.class));

for (var player : EntityArgumentType.getPlayers(context, "targets")) {
player.sendMessage(parsed);
}
)
)
)
);
});

return i;
}
)
)
)
);

}

private static int reloadConfig(CommandContext<ServerCommandSource> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ConfigData {
public boolean parseLinksInChat = true;
public boolean enableMarkdown = true;
public boolean allowModdedDecorators = true;
public boolean sendFullMessageInChatPreview = true;
public boolean sendFullMessageInChatPreview = false;
public boolean sendAutoCompletionForTags = false;
public boolean sendAutoCompletionForTagAliases = false;
public boolean sendAutoCompletionForEmotes = true;
Expand Down
Loading

0 comments on commit c6e286b

Please sign in to comment.