Add messages.java, add chat color to messages, rename TestCommands.

This commit is contained in:
kelson8 2024-03-17 04:08:55 -04:00
parent 1551562cee
commit 71a15ee04d
Signed by: kelson8
GPG Key ID: 3D738D9DD7239E13
3 changed files with 57 additions and 8 deletions

View File

@ -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.

View File

@ -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;
})));
}
}

View File

@ -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;
}
}