Skip to content

Commit

Permalink
Update to 1.20.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenny-Hui committed Dec 2, 2023
1 parent 13447ef commit f2da21d
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 36 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Better Tellraw
A mod that adds a **/btellraw** command which provides several enhancement over vanilla's tellraw.
This mod only affects the server-side environment. You may use it both on both singleplayer and a dedicated multiplayer server.
This mod adds a **/btellraw** command which provides several enhancement over vanilla's tellraw.
This mod only runs on the server-side environment. You may use it both on both Singleplayer and a dedicated multiplayer server.

## Enhancements
- Create, modify and send a text messages by referencing its id.
- [Placeholder API](https://placeholders.pb4.eu/user/default-placeholders/) support.
- [LuckPerm](https://modrinth.com/mod/luckperms) permission support.
- String formatting support.
- Send the tellraw to players within 2 positions easily.
- Send the tellraw to players within 2 positions (a box) easily.

## Usages
Please see the [Wiki](https://github.com/Kenny-Hui/BetterTellraw/wiki) for details.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.1-SNAPSHOT'
id 'fabric-loom' version '1.4-SNAPSHOT'
id 'maven-publish'
}

Expand Down
14 changes: 7 additions & 7 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.20.1
yarn_mappings=1.20.1+build.10
loader_version=0.14.22
placeholder_api_version=2.1.2+1.20.1
minecraft_version=1.20.3-rc1
yarn_mappings=1.20.3-rc1+build.2
loader_version=0.14.25
placeholder_api_version=2.3.0+1.20.3

# Mod Properties
mod_version = 1.0.2
maven_group = com.lx.bettertellraw
mod_version = 1.0.3
maven_group = com.lx862.btellraw
archives_base_name = better-tellraw

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.88.1+1.20.1
fabric_version=0.91.1+1.20.3
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.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
5 changes: 2 additions & 3 deletions src/main/java/com/lx862/btellraw/BetterTellraw.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.lx862.btellraw;

import com.lx862.btellraw.commands.btellraw;
import com.lx862.btellraw.commands.BtellrawCommand;
import com.lx862.btellraw.config.Config;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.text.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -15,7 +14,7 @@ public class BetterTellraw implements ModInitializer {
public void onInitialize() {
LOGGER.info("[BetterTellraw] BetterTellraw Loaded!");
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
btellraw.register(dispatcher);
BtellrawCommand.register(dispatcher);
});
Config.load();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.lx862.btellraw.data.TellrawEntry;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand All @@ -22,13 +23,18 @@
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.Box;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

public class btellraw {
public class BtellrawCommand {

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
LiteralCommandNode<ServerCommandSource> tellrawNode = CommandManager
Expand All @@ -39,7 +45,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
LiteralCommandNode<ServerCommandSource> reloadNode = CommandManager
.literal("reload")
.requires(Permissions.require("btw.reload", 2))
.executes(btellraw::reloadConfig)
.executes(BtellrawCommand::reloadConfig)
.build();

LiteralCommandNode<ServerCommandSource> sendNode = CommandManager
Expand All @@ -57,6 +63,23 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
.requires(Permissions.require("btw.modify", 2))
.build();

LiteralCommandNode<ServerCommandSource> previewNode = CommandManager
.literal("preview")
.requires(Permissions.require("btw.preview", 2))
.build();

LiteralCommandNode<ServerCommandSource> listNode = CommandManager
.literal("list")
.requires(Permissions.require("btw.list", 2))
.executes(BtellrawCommand::listTellraws)
.build();

LiteralCommandNode<ServerCommandSource> aboutNode = CommandManager
.literal("about")
.requires(Permissions.require("btw.about", 0))
.executes(BtellrawCommand::about)
.build();

LiteralCommandNode<ServerCommandSource> selectorNode = CommandManager
.literal("entity")
.build();
Expand Down Expand Up @@ -88,6 +111,11 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
.executes(ctx -> modifyTellraw(ctx, TextArgumentType.text())))
.build();

ArgumentCommandNode<ServerCommandSource, Integer> pageNode = CommandManager
.argument("page", IntegerArgumentType.integer(1))
.executes(BtellrawCommand::listTellraws)
.build();

ArgumentCommandNode<ServerCommandSource, String> addTellrawTextNode = CommandManager
.argument("fileName", StringArgumentType.string()).suggests((commandContext, SuggestionBuilder) -> CommandSource.suggestMatching(Config.tellrawList.values().stream().map(t -> t.fileName).toList(), SuggestionBuilder))
.then(CommandManager.argument("id", StringArgumentType.string())
Expand All @@ -105,23 +133,24 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {

ArgumentCommandNode<ServerCommandSource, String> tellrawID = CommandManager
.argument("tellrawID", StringArgumentType.string())
.executes(context -> run(StringArgumentType.getString(context, "tellrawID"), context, new String[]{}))
.executes(context -> sendTellraw(StringArgumentType.getString(context, "tellrawID"), context, new String[]{}))
.suggests((commandContext, suggestionsBuilder) -> CommandSource.suggestMatching(Config.tellrawList.keySet(), suggestionsBuilder))
.build();

ArgumentCommandNode<ServerCommandSource, Text> JSONTextNode = CommandManager
.argument("Text", TextArgumentType.text())
.executes(context -> run(TextArgumentType.getTextArgument(context, "Text"), context))
.executes(context -> sendTellraw(TextArgumentType.getTextArgument(context, "Text"), context))
.build();

ArgumentCommandNode<ServerCommandSource, String> placeholderNode = CommandManager
.argument("placeholders", StringArgumentType.string())
.executes(context -> run(StringArgumentType.getString(context, "tellrawID"), context, StringArgumentType.getString(context, "placeholders").split(",")))
.executes(context -> sendTellraw(StringArgumentType.getString(context, "tellrawID"), context, StringArgumentType.getString(context, "placeholders").split(",")))
.build();

dispatcher.getRoot().addChild(tellrawNode);

tellrawNode.addChild(reloadNode);
tellrawNode.addChild(aboutNode);
tellrawNode.addChild(addNode);
addNode.addChild(addTellrawNode);
addNode.addChild(addTellrawTextNode);
Expand All @@ -134,13 +163,16 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
entitiesNode.addChild(tellrawID);
entitiesNode.addChild(JSONTextNode);
tellrawID.addChild(placeholderNode);

sendNode.addChild(posNode);
posNode.addChild(pos1Node);
pos1Node.addChild(pos2Node);
pos2Node.addChild(tellrawID);
pos2Node.addChild(JSONTextNode);
tellrawID.addChild(placeholderNode);
tellrawNode.addChild(previewNode);
previewNode.addChild(tellrawID);
tellrawNode.addChild(listNode);
listNode.addChild(pageNode);
}

public static int reloadConfig(CommandContext<ServerCommandSource> context) {
Expand All @@ -149,7 +181,84 @@ public static int reloadConfig(CommandContext<ServerCommandSource> context) {
return 1;
}

public static int run(Collection<ServerPlayerEntity> players, Text msg, CommandContext<ServerCommandSource> context) {
public static int about(CommandContext<ServerCommandSource> context) {
context.getSource().sendFeedback(() -> Text.literal("Better Tellraw - Enhanced tellraw command and managed tellraw storage").formatted(Formatting.GOLD), false);
context.getSource().sendFeedback(() -> Text.literal("https://modrinth.com/mod/bettertellraw").formatted(Formatting.GREEN).formatted(Formatting.UNDERLINE).styled(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://modrinth.com/mod/bettertellraw"))), false);
return 1;
}

public static int listTellraws(CommandContext<ServerCommandSource> context) {
int tellrawPerPage = 8;
int pages = (int)Math.ceil(Config.tellrawList.size() / (double)tellrawPerPage);
int page = 0;
int offset = 0;

try {
int selectedPage = IntegerArgumentType.getInteger(context, "page")-1;

if(selectedPage > pages-1) {
context.getSource().sendFeedback(() -> Text.literal("Page " + (selectedPage+1) + " does not exists.").formatted(Formatting.RED), false);
return 1;
}
page = selectedPage;
offset = tellrawPerPage * selectedPage;
} catch (Exception e) {
// Page argument not supplied
}

// Separator
context.getSource().sendFeedback(() -> Text.literal("There are " + Config.tellrawList.size() + " tellraws loaded.").formatted(Formatting.GREEN), false);

int i = 0;
for(String id : Config.tellrawList.keySet().stream().sorted().toList()) {
if(i < offset) {
i++;
continue;
}

if(i > offset + tellrawPerPage-1) break;

String order = (i+1) + ". ";

MutableText finalText = Text.literal(order + id);
finalText.formatted(Formatting.YELLOW);
finalText.styled(style -> {
style = style.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/btellraw preview \"" + id + "\""));
style = style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal("Click to preview " + id).formatted(Formatting.GOLD)));
return style;
});

context.getSource().sendFeedback(() -> finalText, false);
i++;
}

int ordinalPage = page + 1;

MutableText leftArrow = Text.literal("←").formatted(Formatting.GOLD);
MutableText rightArrow = Text.literal("→").formatted(Formatting.GOLD);
MutableText pageText = Text.literal(" [ Page " + (ordinalPage) + "/" + pages + " ] ").formatted(Formatting.GOLD);

leftArrow.styled(style -> {
style = style.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/btellraw list " + (ordinalPage - 1)));
style = style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal("Previous page").formatted(Formatting.YELLOW)));
return style;
});
rightArrow.styled(style -> {
style = style.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/btellraw list " + (ordinalPage + 1)));
style = style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal("Next page").formatted(Formatting.YELLOW)));
return style;
});

MutableText finalText = Text.literal("");
if(page > 0) finalText.append(leftArrow);
finalText.append(pageText);
if(ordinalPage < pages) finalText.append(rightArrow);

context.getSource().sendFeedback(() -> finalText, false);
return 1;
}

public static int sendTellraw(Collection<ServerPlayerEntity> players, Text msg, CommandContext<ServerCommandSource> context) {
Text finalText = Placeholders.parseText(msg, PlaceholderContext.of(context.getSource().getServer()));

for (ServerPlayerEntity player : players) {
Expand All @@ -158,13 +267,17 @@ public static int run(Collection<ServerPlayerEntity> players, Text msg, CommandC
return 1;
}

public static int run(String msg, CommandContext<ServerCommandSource> context, String[] placeholder) throws CommandSyntaxException {
public static int sendTellraw(String msg, CommandContext<ServerCommandSource> context, String[] placeholder) {
Collection<ServerPlayerEntity> playerList;
try {
Box area = new Box(BlockPosArgumentType.getBlockPos(context, "pos1"), BlockPosArgumentType.getBlockPos(context, "pos2"));
Box area = new Box(BlockPosArgumentType.getBlockPos(context, "pos1").toCenterPos(), BlockPosArgumentType.getBlockPos(context, "pos2").toCenterPos());
playerList = context.getSource().getWorld().getEntitiesByClass(ServerPlayerEntity.class, area, e -> true);
} catch (Exception e) {
playerList = EntityArgumentType.getPlayers(context, "players");
try {
playerList = EntityArgumentType.getPlayers(context, "players");
} catch (Exception f) {
playerList = Collections.singletonList(context.getSource().getPlayer());
}
}

TellrawEntry tellraw = Config.tellrawList.get(msg);
Expand All @@ -184,24 +297,24 @@ public static int run(String msg, CommandContext<ServerCommandSource> context, S

Text tellrawComponent;
try {
tellrawComponent = Text.Serializer.fromJson(formattedString);
tellrawComponent = Text.Serialization.fromJson(formattedString);
} catch (Exception ignored) {
tellrawComponent = TextParserUtils.formatTextSafe(formattedString);
}

return run(playerList, tellrawComponent, context);
return sendTellraw(playerList, tellrawComponent, context);
}

public static int run(Text msg, CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
public static int sendTellraw(Text msg, CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
Collection<ServerPlayerEntity> playerList;
try {
Box area = new Box(BlockPosArgumentType.getBlockPos(context, "pos1"), BlockPosArgumentType.getBlockPos(context, "pos2"));
Box area = new Box(BlockPosArgumentType.getBlockPos(context, "pos1").toCenterPos(), BlockPosArgumentType.getBlockPos(context, "pos2").toCenterPos());
playerList = context.getSource().getWorld().getEntitiesByClass(ServerPlayerEntity.class, area, e -> true);
} catch (Exception e) {
playerList = EntityArgumentType.getPlayers(context, "players");
}

return run(playerList, msg, context);
return sendTellraw(playerList, msg, context);
}

public static int addTellraw(CommandContext<ServerCommandSource> context, ArgumentType type) {
Expand All @@ -217,7 +330,7 @@ public static int addTellraw(CommandContext<ServerCommandSource> context, Argume
Config.tellrawList.put(fullID, tellrawObj);
Config.saveConfig();
} else {
TellrawEntry tellrawObj = new TellrawEntry(StringArgumentType.getString(context, "fileName"), Text.Serializer.toJson(TextArgumentType.getTextArgument(context, "JSONText")), fullID, ID);
TellrawEntry tellrawObj = new TellrawEntry(StringArgumentType.getString(context, "fileName"), Text.Serialization.toJsonString(TextArgumentType.getTextArgument(context, "JSONText")), fullID, ID);
Config.tellrawList.put(fullID, tellrawObj);
Config.saveConfig();
}
Expand All @@ -237,7 +350,7 @@ public static int modifyTellraw(CommandContext<ServerCommandSource> context, Arg
if(type instanceof StringArgumentType) {
tellrawObj.content = StringArgumentType.getString(context, "text");
} else {
tellrawObj.content = Text.Serializer.toJson(TextArgumentType.getTextArgument(context, "JSONText"));
tellrawObj.content = Text.Serialization.toJsonString(TextArgumentType.getTextArgument(context, "JSONText"));
}
Config.tellrawList.put(ID, tellrawObj);
Config.saveConfig();
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/lx862/btellraw/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Config {

public static int load() {
if(!Files.exists(TELLRAW_DIR.toPath())) {
BetterTellraw.LOGGER.info("[BetterTellraw] Tellraws folder not found, generating one.");
BetterTellraw.LOGGER.info("[BetterTellraw] Tellraw folder not found, generating one.");
generateConfig();
/* Load the config again from the files we just generated. */
load();
Expand Down Expand Up @@ -47,8 +47,8 @@ public static void generateConfig() {
try {
Files.write(TELLRAW_DIR.toPath().resolve("example.json"), Collections.singleton(new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(jsonConfig)));
} catch (Exception e) {
BetterTellraw.LOGGER.warn("[BetterTellraw] Unable to generate Config File!");
e.printStackTrace();
BetterTellraw.LOGGER.warn("[BetterTellraw] Unable to generate config file!");
}
}

Expand Down Expand Up @@ -80,9 +80,9 @@ public static int readTellraws(Path tellrawLocation) {

for(Map.Entry<String, JsonElement> e : jsonConfig.entrySet()) {
String tellrawID = e.getKey();
String jsontext = e.getValue().getAsString();
String jsonText = e.getValue().getAsString();
String fullID = FilenameUtils.getBaseName(tellrawLocation.getFileName().toString()) + "." + tellrawID;
TellrawEntry tellrawObj = new TellrawEntry(FilenameUtils.getBaseName(tellrawLocation.getFileName().toString()), jsontext, fullID, tellrawID);
TellrawEntry tellrawObj = new TellrawEntry(FilenameUtils.getBaseName(tellrawLocation.getFileName().toString()), jsonText, fullID, tellrawID);
tellrawList.put(fullID, tellrawObj);
loadedTellraw++;
}
Expand Down

0 comments on commit f2da21d

Please sign in to comment.