Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 154
src/client/java/com/mapps/MappsClient.java
package com.mapps;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.ClientCommands;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
public class MappsClient implements ClientModInitializer {
private Path observationsFile;
@Override
public void onInitializeClient() {
observationsFile = FabricLoader.getInstance().getConfigDir().resolve("mapps-observations.tsv");
ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext) -> dispatcher.register(
ClientCommands.literal("mapps")
.executes(context -> {
context.getSource().sendFeedback(Component.literal(
"Usage: /mapps mark <label>, /mapps list, or /mapps clear"));
return 1;
})
.then(ClientCommands.literal("mark")
.then(ClientCommands.argument("label", StringArgumentType.word())
.executes(context -> {
var player = Minecraft.getInstance().player;
if (player == null) {
context.getSource().sendFeedback(Component.literal("Mapps needs a loaded world to mark a position."));
return 0;
}
var position = player.blockPosition();
String label = StringArgumentType.getString(context, "label");
String observation = label + "\t" + position.getX() + "\t" + position.getY() + "\t" + position.getZ();
try {
Files.createDirectories(observationsFile.getParent());
Files.writeString(observationsFile, observation + System.lineSeparator(), StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
context.getSource().sendFeedback(Component.literal("Mapps recorded: " + observation.replace('\t', ' ')));
return 1;
} catch (IOException exception) {
context.getSource().sendFeedback(Component.literal("Mapps could not save the observation: " + exception.getMessage()));
return 0;
}
})))
.then(ClientCommands.literal("list").executes(context -> {
try {
if (!Files.exists(observationsFile)) {
context.getSource().sendFeedback(Component.literal("Mapps has no saved observations."));
return 1;
}
List<String> observations = Files.readAllLines(observationsFile, StandardCharsets.UTF_8);
context.getSource().sendFeedback(Component.literal("Mapps observations: " + observations.size()));
for (int i = 0; i < observations.size(); i++) {
context.getSource().sendFeedback(Component.literal((i + 1) + ". " + observations.get(i).replace('\t', ' ')));
}
return 1;
} catch (IOException exception) {
context.getSource().sendFeedback(Component.literal("Mapps could not read observations: " + exception.getMessage()));
return 0;
}
}))
.then(ClientCommands.literal("clear").executes(context -> {
try {
Files.createDirectories(observationsFile.getParent());
Files.writeString(observationsFile, "", StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
context.getSource().sendFeedback(Component.literal("Mapps observations cleared."));
return 1;
} catch (IOException exception) {
context.getSource().sendFeedback(Component.literal("Mapps could not clear observations: " + exception.getMessage()));
return 0;
}
}))
));
}
}