Skip to content

Commit

Permalink
Update to 1.18
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Nov 26, 2021
1 parent a39fc5b commit 26e75ab
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
matrix:
# Use these Java versions
java: [
16 # Minimum supported by Minecraft
17 # Minimum supported by Minecraft
]
# and run on both Linux and Windows
os: [ubuntu-20.04, windows-latest]
Expand All @@ -32,7 +32,7 @@ jobs:
- name: build
run: ./gradlew build
- name: capture build artifacts
if: ${{ runner.os == 'Linux' && matrix.java == '16' }} # Only upload artifacts built from latest java on one OS
if: ${{ runner.os == 'Linux' && matrix.java == '17' }} # Only upload artifacts built from latest java on one OS
uses: actions/upload-artifact@v2
with:
name: Artifacts
Expand Down
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
plugins {
id 'fabric-loom' version '0.8-SNAPSHOT'
id 'fabric-loom' version '0.10-SNAPSHOT'
id 'maven-publish'
}

sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

archivesBaseName = project.archives_base_name
version = project.mod_version
Expand Down Expand Up @@ -34,10 +34,10 @@ dependencies {
modImplementation include(fabricApi.module("fabric-lifecycle-events-v1", project.fabric_version))
modImplementation include(fabricApi.module("fabric-command-api-v1", project.fabric_version))

modImplementation include("eu.pb4:placeholder-api:1.1.1+1.17.1")
modImplementation include("eu.pb4:placeholder-api:1.1.3+1.17.1")
modImplementation include("me.lucko:fabric-permissions-api:0.1-SNAPSHOT")

modRuntime "supercoder79:databreaker:0.2.7"
//modRuntime "supercoder79:databreaker:0.2.7"
// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them.
}
Expand All @@ -58,7 +58,7 @@ tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"

// Minecraft 1.17 (21w19a) upwards uses Java 16.
it.options.release = 16
it.options.release = 17
}

java {
Expand Down
12 changes: 7 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.17.1
yarn_mappings=1.17.1+build.1
loader_version=0.11.3
minecraft_version=1.18-rc1
yarn_mappings=1.18-rc1+build.1
loader_version=0.12.5

#Fabric api
fabric_version=0.43.1+1.18

# Mod Properties
mod_version = 1.2.0
mod_version = 1.2.1+1.18
maven_group = eu.pb4
archives_base_name = styled-chat

# Dependencies
fabric_version=0.40.1+1.17
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
85 changes: 76 additions & 9 deletions src/main/java/eu/pb4/styledchat/StyledChatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
import eu.pb4.styledchat.config.Config;
import eu.pb4.styledchat.config.ConfigManager;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.*;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

public final class StyledChatUtils {
Expand Down Expand Up @@ -62,14 +67,8 @@ public static Map<String, TextParser.TextFormatterHandler> getHandlers(ServerPla
}
}

if (config.defaultFormattingCodes.getBoolean(ITEM_TAG) ||
Permissions.check(source, FORMAT_PERMISSION_BASE + ITEM_TAG, 2)) {
handlers.put(ITEM_TAG, (tag, data, input, buildInHandlers, endAt) -> new GeneralUtils.TextLengthPair((MutableText) player.getStackInHand(Hand.MAIN_HAND).toHoverableText(), 0));
}

if (config.defaultFormattingCodes.getBoolean(SPOILER_TAG) ||
Permissions.check(source, FORMAT_PERMISSION_BASE + SPOILER_TAG, 2)) {
handlers.put(SPOILER_TAG, SPOILER_TAG_HANDLER);
if (handlers.containsKey("light_purple")) {
handlers.put("pink", handlers.get("light_purple"));
}

if (config.defaultFormattingCodes.getBoolean(POS_TAG) ||
Expand All @@ -96,6 +95,17 @@ public static Map<String, TextParser.TextFormatterHandler> getHandlers(ServerPla
return handlers;
}

/**
* I need to rework things within Placeholder API, but I'm too lazy to do it rn.
* Whatever I will do it this thing will be replaced with proper thing (I might keep it for sake of backwards compatibility)
*/
public static Map<String, Text> getEmotes(ServerPlayerEntity player) {
return new FakeMapPlayer(player, ConfigManager.getConfig().getEmotes(player.getCommandSource()));
}

public static Map<String, Text> getEmotes(MinecraftServer server) {
return new FakeMapServer(server, ConfigManager.getConfig().getEmotes(server.getCommandSource()));
}

public static String formatMessage(String input, Map<String, TextParser.TextFormatterHandler> handlers) {
var config = ConfigManager.getConfig();
Expand Down Expand Up @@ -131,7 +141,6 @@ public static String formatMessage(String input, Map<String, TextParser.TextForm

if (handlers.containsKey("italic")) {
input = input.replaceAll(getMarkdownRegex("*", "\\*"), "<italic>$2</italic>");
input = input.replaceAll(getMarkdownRegex("_", "_"), "<italic>$2</italic>");
}
}
} catch (Exception e) {
Expand All @@ -144,4 +153,62 @@ public static String formatMessage(String input, Map<String, TextParser.TextForm
private static String getMarkdownRegex(String base, String sides) {
return "(" + sides + ")(?<id>[^" + base +"]+)(" + sides + ")";
}

public static abstract class FakeMap extends AbstractMap<String, Text> {
private final Map<String, Text> texts;
private final Map<String, Text> cache = new HashMap<>();

public FakeMap(Map<String, Text> texts) {
this.texts = texts;
}

@Override
public Text get(Object key) {
var text = this.cache.get(key);
if (text != null) {
return text;
}

text = this.texts.get(key);
return text != null ? this.cache.put((String) key, this.getParsed(text)) : null;
}

@NotNull
@Override
public Set<Entry<String, Text>> entrySet() {
return texts.entrySet();
}

public abstract Text getParsed(Text text);
}


public static class FakeMapPlayer extends FakeMap {

private final ServerPlayerEntity player;

public FakeMapPlayer(ServerPlayerEntity player, Map<String, Text> texts) {
super(texts);
this.player = player;
}

@Override
public Text getParsed(Text text) {
return PlaceholderAPI.parseText(text, this.player);
}
}

public static class FakeMapServer extends FakeMap {
private final MinecraftServer server;

public FakeMapServer(MinecraftServer server, Map<String, Text> texts) {
super(texts);
this.server = server;
}

@Override
public Text getParsed(Text text) {
return PlaceholderAPI.parseText(text, this.server);
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/eu/pb4/styledchat/config/data/ConfigData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

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 java.util.*;

Expand Down Expand Up @@ -63,6 +67,8 @@ private static Map<String, String> getDefaultEmoticons() {
map.put("shears", "✂");
map.put("bucket", "\uD83E\uDEA3");
map.put("bell", "\uD83D\uDD14");
map.put(StyledChatUtils.ITEM_TAG, "[%player:equipment_slot/mainhand%]");
map.put(StyledChatUtils.POS_TAG, "%player:pos_x% %player:pos_y% %player:pos_z%");

return map;
}
Expand All @@ -71,6 +77,18 @@ public static ConfigData transform(ConfigData configData) {
for (Map.Entry<String, Boolean> entry : getDefaultFormatting().entrySet()) {
configData.defaultEnabledFormatting.putIfAbsent(entry.getKey(), entry.getValue());
}

if (configData.defaultEnabledFormatting.get(StyledChatUtils.ITEM_TAG)) {
configData.defaultEnabledFormatting.remove(StyledChatUtils.ITEM_TAG);
configData.emoticons.put(StyledChatUtils.ITEM_TAG, "[%player:equipment_slot/mainhand%]");
}

if (configData.defaultEnabledFormatting.get(StyledChatUtils.POS_TAG)) {
configData.defaultEnabledFormatting.remove(StyledChatUtils.POS_TAG);
configData.emoticons.put(StyledChatUtils.POS_TAG, "%player:pos_x% %player:pos_y% %player:pos_z%");
}


return configData;
}

Expand Down
15 changes: 13 additions & 2 deletions src/main/java/eu/pb4/styledchat/mixin/MeCommandMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;
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.callback.CallbackInfoReturnable;

import java.util.AbstractMap;
import java.util.Map;
import java.util.Set;

@Mixin(MeCommand.class)
public class MeCommandMixin {
Expand All @@ -25,14 +28,16 @@ private static void styledChat_formatText(CommandContext<ServerCommandSource> co

Text message;
Map<String, TextParser.TextFormatterHandler> formatting;
Map<String, Text> emotes;

try {
var player = source.getPlayer();
formatting = StyledChatUtils.getHandlers(player);
emotes = StyledChatUtils.getEmotes(player);
} catch (Exception e) {
formatting = TextParser.getRegisteredSafeTags();
emotes = StyledChatUtils.getEmotes(context.getSource().getServer());
}
var emotes = config.getEmotes(source);


if (formatting.size() != 0) {
Expand All @@ -44,7 +49,13 @@ private static void styledChat_formatText(CommandContext<ServerCommandSource> co
}

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ private static Text styledChat_formatText(Text message, ServerCommandSource sour
var config = ConfigManager.getConfig();
if (config.configData.formattingInPrivateMessages) {
Map<String, TextParser.TextFormatterHandler> formatting;
Map<String, Text> emotes;

try {
var player = source.getPlayer();
formatting = StyledChatUtils.getHandlers(player);
emotes = StyledChatUtils.getEmotes(player);
} catch (Exception e) {
formatting = TextParser.getRegisteredSafeTags();
emotes = StyledChatUtils.getEmotes(source.getServer());
}
var emotes = config.getEmotes(source);

if (formatting.size() != 0) {
var ogMessage = message.getString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class PlayerAdvancementTrackerMixin {

@Shadow private ServerPlayerEntity owner;

@ModifyArg(method = "grantCriterion", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcastChatMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"))
@ModifyArg(method = "grantCriterion", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"))
private Text styledChat_changeAdvancementMessage(Text text) {
TranslatableText translatableText = (TranslatableText) text;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/eu/pb4/styledchat/mixin/PlayerManagerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private void styledChat_removeStoredPlayer(ClientConnection connection, ServerPl
this.temporaryPlayer = null;
}

@ModifyArg(method = "onPlayerConnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcastChatMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"))
@ModifyArg(method = "onPlayerConnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"))
private Text styledChat_updatePlayerNameAfterMessage(Text text) {
if (this.temporaryPlayer.getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(Stats.LEAVE_GAME)) == 0) {
return ConfigManager.getConfig().getJoinFirstTime(this.temporaryPlayer);
Expand All @@ -49,7 +49,7 @@ private Text styledChat_updatePlayerNameAfterMessage(Text text) {
}
}

@Inject(method = "broadcastChatMessage", at = @At("HEAD"), cancellable = true)
@Inject(method = "broadcast", at = @At("HEAD"), cancellable = true)
private void styledChat_excludeSendingOfHiddenMessages(Text message, MessageType type, UUID sender, CallbackInfo ci) {
if (message instanceof TranslatableText text && text.getKey().equals(StyledChatUtils.IGNORED_TEXT_KEY)) {
ci.cancel();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/eu/pb4/styledchat/mixin/SayCommandMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ private static Text styledChat_formatText(Text inputX, CommandContext<ServerComm

Text message;
Map<String, TextParser.TextFormatterHandler> formatting;
Map<String, Text> emotes;

try {
var player = source.getPlayer();
formatting = StyledChatUtils.getHandlers(player);
emotes = StyledChatUtils.getEmotes(player);
} catch (Exception e) {
formatting = TextParser.getRegisteredSafeTags();
emotes = StyledChatUtils.getEmotes(context.getSource().getServer());
}
var emotes = config.getEmotes(source);

if (formatting.size() != 0) {
var formattedMessage = StyledChatUtils.formatMessage(inputAsString, formatting);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ServerPlayNetworkManagerMixin {
@Shadow
public ServerPlayerEntity player;

@ModifyArg(method = "onDisconnected", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcastChatMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"))
@ModifyArg(method = "onDisconnected", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"))
private Text styledChat_replaceDisconnectMessage(Text text) {
return ConfigManager.getConfig().getLeft(this.player);
}
Expand All @@ -40,7 +40,7 @@ private Text styledChat_replaceDisconnectMessage(Text text) {
private void styledChat_replaceChatMessage(PlayerManager playerManager, Text serverMessage, Function<ServerPlayerEntity, Text> playerMessageFactory, MessageType playerMessageType, UUID sender, TextStream.Message message) {
var handlers = StyledChatUtils.getHandlers(this.player);
Config config = ConfigManager.getConfig();
var emotes = config.getEmotes(this.player.getCommandSource());
var emotes = StyledChatUtils.getEmotes(this.player);

String rawMessage = message.getRaw();
String filteredMessage = message.getFiltered();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@Mixin(ServerPlayerEntity.class)
public class ServerPlayerEntityMixin {

@ModifyArg(method = "onDeath", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcastChatMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"))
@ModifyArg(method = "onDeath", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"))
private Text styledChat_replaceDeathMessage(Text text) {
return ConfigManager.getConfig().getDeath((ServerPlayerEntity) (Object) this, text);
}
Expand Down
Loading

0 comments on commit 26e75ab

Please sign in to comment.