From 71a15ee04d28c1b9fe3619007148c1c3ff089c75 Mon Sep 17 00:00:00 2001 From: kelson8 Date: Sun, 17 Mar 2024 04:08:55 -0400 Subject: [PATCH] Add messages.java, add chat color to messages, rename TestCommands. --- .../net/kelsoncraft/test/KelsonCraftTest.java | 7 +++- .../{CommandTest.java => TestCommands.java} | 20 +++++++--- .../net/kelsoncraft/test/util/Messages.java | 38 +++++++++++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) rename src/main/java/net/kelsoncraft/test/{CommandTest.java => TestCommands.java} (55%) create mode 100644 src/main/java/net/kelsoncraft/test/util/Messages.java diff --git a/src/main/java/net/kelsoncraft/test/KelsonCraftTest.java b/src/main/java/net/kelsoncraft/test/KelsonCraftTest.java index d030687..935b457 100644 --- a/src/main/java/net/kelsoncraft/test/KelsonCraftTest.java +++ b/src/main/java/net/kelsoncraft/test/KelsonCraftTest.java @@ -7,13 +7,16 @@ import net.fabricmc.fabric.api.registry.FuelRegistry; import net.kelsoncraft.test.block.CustomBlocks; import net.kelsoncraft.test.item.CustomItems; import net.kelsoncraft.test.item.CustomItemGroups; -import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.Text; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static net.minecraft.server.command.CommandManager.*; public class KelsonCraftTest implements ModInitializer { + + // Todo Change name of project to something else + // I need to change the name of this repo and project to something else. + // This logger is used to write text to the console and the log file. // It is considered best practice to use your mod id as the logger's name. // That way, it's clear which mod wrote info, warnings, and errors. @@ -84,7 +87,7 @@ public class KelsonCraftTest implements ModInitializer { }))); // This should add the teleport command, I got the idea from this: // https://github.com/Draylar/get-off-my-lawn/blob/1.17/src/main/java/draylar/goml/GetOffMyLawn.java#L30-L37 - CommandTest.init(); + TestCommands.init(); // CommandTest commandTest = new CommandTest(); //I wonder how to implement a teleport command, and a try to put this in another class. diff --git a/src/main/java/net/kelsoncraft/test/CommandTest.java b/src/main/java/net/kelsoncraft/test/TestCommands.java similarity index 55% rename from src/main/java/net/kelsoncraft/test/CommandTest.java rename to src/main/java/net/kelsoncraft/test/TestCommands.java index 8455271..f970472 100644 --- a/src/main/java/net/kelsoncraft/test/CommandTest.java +++ b/src/main/java/net/kelsoncraft/test/TestCommands.java @@ -1,21 +1,18 @@ package net.kelsoncraft.test; -import com.mojang.brigadier.CommandDispatcher; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; -import net.minecraft.command.CommandRegistryAccess; +import net.kelsoncraft.test.util.Messages; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.Text; -import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; - // https://www.reddit.com/r/fabricmc/comments/r3bd47/fabric_execute_commands/ -public class CommandTest { +public class TestCommands { // This idea came from here: // https://github.com/Draylar/get-off-my-lawn/blob/1.17/src/main/java/draylar/goml/command/ClaimCommand.java#L32-L98 - public CommandTest() { + public TestCommands() { } // This works like this, now I know how to add commands to other classes, add them like this then initialize them in the main class. @@ -25,6 +22,17 @@ public class CommandTest { context.getSource().sendFeedback(() -> Text.literal("Teleport to %s"), false); return 1; }))); + + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(CommandManager.literal("lightning") + .executes(context -> { + ServerCommandSource serverCommandSource = context.getSource(); +// String playerName = serverCommandSource.getDisplayName().toString(); + String playerName = serverCommandSource.getName(); + String lightningMsg = String.format(Messages.Kbp_Fabric() + "Strike lightning at your location %s.", playerName); + + context.getSource().sendFeedback(() -> Text.literal(lightningMsg), false); + return 1; + }))); } } diff --git a/src/main/java/net/kelsoncraft/test/util/Messages.java b/src/main/java/net/kelsoncraft/test/util/Messages.java new file mode 100644 index 0000000..2666a3f --- /dev/null +++ b/src/main/java/net/kelsoncraft/test/util/Messages.java @@ -0,0 +1,38 @@ +package net.kelsoncraft.test.util; + +import net.fabricmc.fabric.api.message.v1.ServerMessageEvents; +import net.minecraft.util.Formatting; + +public class Messages { + + public Messages() { + + } + // The idea for chat colors came from here: + // https://codeberg.org/PORTB/ChatColours/src/branch/1.19/src/main/java/portb/chatcolours/mixin/client/TextProcessingMixin.java + // And from the minecraft sources here: package net.minecraft.util.Formatting + + // Todo Figure out how to use ChatColors like in the spigot plugin: + // https://github.com/kelson8/KBP/blob/master/src/main/java/net/Kelsoncraft/KBP/util/Messages.java#L15 + // I think KBP-Fabric is a good name for the project, I'm not sure of a good name to come up with. +// private static final String KBP_Fabric = "[KBP-Fabric] "; + // Yes it works, I figured out how to do Chat Colors in Fabric 3-17-2024 @ 4:04AM + private static final String KBP_Fabric = Formatting.BLUE + "[KBP-Fabric] " + Formatting.WHITE; + + private static String KbpFabric_errormsg = KBP_Fabric + "Error: "; + + private static String modDev = "kelson8"; + + public static String Kbp_Fabric() { + return KBP_Fabric; + } + + public static String KbpFabric_errormsg() { + return KbpFabric_errormsg; + } + + public static String modDev() { + return modDev; + } + +}