Skip to content

Commit

Permalink
Nether Portal Coords MOD
Browse files Browse the repository at this point in the history
  • Loading branch information
ItzjustElias committed Feb 22, 2024
1 parent 58b0503 commit b304fc7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 44 deletions.
105 changes: 64 additions & 41 deletions src/main/java/elias/portalcoordmod/PortalCoordMod.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,91 @@
package elias.portalcoordmod;

import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

public class PortalCoordMod {
public static void registerCommands() {
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
dispatcher.register(CommandManager.literal("netherportal")
.then(CommandManager.literal("overworld")
.executes(context -> executeNetherPortalCommand(
context.getSource(),
(int) context.getSource().getPosition().getX(),
(int) context.getSource().getPosition().getY(),
(int) context.getSource().getPosition().getZ(),
true
))
.then(CommandManager.argument("x", IntegerArgumentType.integer())
.then(CommandManager.argument("y", IntegerArgumentType.integer())
.then(CommandManager.argument("z", IntegerArgumentType.integer())
public class PortalCoordMod implements ClientModInitializer {
@Override
public void onInitializeClient() {
registerCommands();
}

static void registerCommands() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
dispatcher.register(ClientCommandManager.literal("netherportal")
.then(ClientCommandManager.literal("help")
.executes(context -> {
sendHelpMessage(context.getSource());
return 1;
}))
.then(ClientCommandManager.literal("overworld")
.then(ClientCommandManager.argument("x", IntegerArgumentType.integer(0))
.then(ClientCommandManager.argument("y", IntegerArgumentType.integer())
.then(ClientCommandManager.argument("z", IntegerArgumentType.integer())
.executes(context -> executeNetherPortalCommand(
context.getSource(),
IntegerArgumentType.getInteger(context, "x"),
IntegerArgumentType.getInteger(context, "y"),
IntegerArgumentType.getInteger(context, "z"),
true
))))))
))
)
)
.executes(context -> executeNetherPortalCommand(
context.getSource(),
(int) context.getSource().getPosition().getX(),
(int) context.getSource().getPosition().getY(),
(int) context.getSource().getPosition().getZ(),
true
))
)
)
.then(ClientCommandManager.argument("x", IntegerArgumentType.integer(0))
.then(ClientCommandManager.argument("y", IntegerArgumentType.integer())
.then(ClientCommandManager.argument("z", IntegerArgumentType.integer())
.executes(context -> executeNetherPortalCommand(
context.getSource(),
IntegerArgumentType.getInteger(context, "x"),
IntegerArgumentType.getInteger(context, "y"),
IntegerArgumentType.getInteger(context, "z"),
false
))
)
)
)
.executes(context -> executeNetherPortalCommand(
context.getSource(),
(int) context.getSource().getPosition().getX(),
(int) context.getSource().getPosition().getY(),
(int) context.getSource().getPosition().getZ(),
false
))
.then(CommandManager.literal("help")
.executes(context -> {
sendHelpMessage(context.getSource());
return 1;
})));
);
});
}

private static void sendHelpMessage(ServerCommandSource source) {
private static void sendHelpMessage(FabricClientCommandSource source) {
// Create a help message
Text helpMessage = Text.literal("")
.append(Text.literal("Nether Portal Coords v0.0.1\n").formatted(Formatting.BOLD, Formatting.RED))
.append(Text.literal("/netherportal ").formatted(Formatting.GOLD))
.append(Text.literal("Calculates Nether coordinates using player position.\n").formatted(Formatting.AQUA))
.append(Text.literal("-------------------------\n").formatted(Formatting.GRAY))
.append(Text.literal("/netherportal ").formatted(Formatting.GOLD))
.append(Text.literal("[").formatted(Formatting.GREEN))
.append(Text.literal("overworld").formatted(Formatting.GREEN))
.append(Text.literal("] ").formatted(Formatting.GREEN))
.append(Text.literal("Calculates overworld coordinates using player position.\n").formatted(Formatting.AQUA))
.append(Text.literal("[").formatted(Formatting.BLUE))
.append(Text.literal("x").formatted(Formatting.BLUE))
.append(Text.literal("] ").formatted(Formatting.BLUE))
.append(Text.literal("[").formatted(Formatting.BLUE))
.append(Text.literal("y").formatted(Formatting.BLUE))
.append(Text.literal("] ").formatted(Formatting.BLUE))
.append(Text.literal("[").formatted(Formatting.BLUE))
.append(Text.literal("z").formatted(Formatting.BLUE))
.append(Text.literal("] ").formatted(Formatting.BLUE))
.append(Text.literal("Calculates Nether coordinates using custom coordinates.\n").formatted(Formatting.AQUA))
.append(Text.literal("-------------------------\n").formatted(Formatting.GRAY))
.append(Text.literal("/netherportal ").formatted(Formatting.GOLD))
.append(Text.literal("[").formatted(Formatting.GREEN))
Expand All @@ -70,23 +100,16 @@ private static void sendHelpMessage(ServerCommandSource source) {
.append(Text.literal("[").formatted(Formatting.BLUE))
.append(Text.literal("z").formatted(Formatting.BLUE))
.append(Text.literal("] ").formatted(Formatting.BLUE))
.append(Text.literal("Calculates Nether or Overworld coordinates using custom coordinates.\n").formatted(Formatting.AQUA))
.append(Text.literal("Calculates Overworld coordinates using custom coordinates.\n").formatted(Formatting.AQUA))
.append(Text.literal("-------------------------\n").formatted(Formatting.GRAY))
.append(Text.literal("/netherportal help ").formatted(Formatting.GOLD))
.append(Text.literal("Shows this help message.\n").formatted(Formatting.AQUA));

source.sendFeedback(() -> helpMessage, false);
source.sendFeedback(helpMessage);
}

private static int executeNetherPortalCommand(ServerCommandSource source, int x, int y, int z, boolean toOverworld) {
private static int executeNetherPortalCommand(FabricClientCommandSource source, int x, int y, int z, boolean toOverworld) {
if (toOverworld) {
// If x, y, and z coordinates are not provided, use the player's current position
if (x == 0 && y == 0 && z == 0) {
x = (int) source.getPosition().getX();
y = (int) source.getPosition().getY();
z = (int) source.getPosition().getZ();
}

// Calculate Overworld coordinates
int overworldX = x * 8;
int overworldY = y;
Expand All @@ -97,7 +120,7 @@ private static int executeNetherPortalCommand(ServerCommandSource source, int x,
.append(Text.literal(String.format("%d, %d, %d", overworldX, overworldY, overworldZ)).formatted(Formatting.GREEN));

// Send the message to the player
source.sendFeedback(() -> message, false);
source.sendFeedback(message);
} else {
// Calculate Nether coordinates
int netherX = x / 8;
Expand All @@ -109,7 +132,7 @@ private static int executeNetherPortalCommand(ServerCommandSource source, int x,
.append(Text.literal(String.format("%d, %d, %d", netherX, netherY, netherZ)).formatted(Formatting.GREEN));

// Send the message to the player
source.sendFeedback(() -> message, false);
source.sendFeedback(message);
}

return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/elias/portalcoordmod/PortalMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class PortalMod implements ModInitializer {
// 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.
public static final Logger LOGGER = LoggerFactory.getLogger("portal-mod");
public static final Logger LOGGER = LoggerFactory.getLogger("nether-portal-coords");

@Override
public void onInitialize() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"schemaVersion": 1,
"id": "portal-mod",
"id": "nether-portal-coords",
"version": "${version}",
"name": "Nether Portal Coords",
"description": "Discover the secrets of the Nether with NetherPortal Coords!\n Easily calculate Nether and Overworld coordinates for your Minecraft adventures. Seamlessly navigate between dimensions and explore new realms.",
"description": "Discover the secrets of the Nether with NetherPortal Coords!\nEasily calculate Nether and Overworld coordinates for your Minecraft adventures. Seamlessly navigate between dimensions and explore new realms.",
"authors": [
"Elias"
],
Expand Down

0 comments on commit b304fc7

Please sign in to comment.