1 package com.irtimaled.bbor.client.interop;
3 import com.irtimaled.bbor.client.ClientRenderer;
4 import com.irtimaled.bbor.client.Player;
5 import com.irtimaled.bbor.client.commands.*;
6 import com.irtimaled.bbor.client.events.DisconnectedFromRemoteServer;
7 import com.irtimaled.bbor.client.events.SaveLoaded;
8 import com.irtimaled.bbor.client.events.UpdateWorldSpawnReceived;
9 import com.irtimaled.bbor.client.providers.SlimeChunkProvider;
10 import com.irtimaled.bbor.common.EventBus;
11 import com.irtimaled.bbor.common.TypeHelper;
12 import com.irtimaled.bbor.common.models.DimensionId;
13 import com.mojang.brigadier.CommandDispatcher;
14 import com.mojang.brigadier.exceptions.CommandSyntaxException;
15 import net.minecraft.client.MinecraftClient;
16 import net.minecraft.client.gui.screen.Screen;
17 import net.minecraft.client.network.ClientPlayNetworkHandler;
18 import net.minecraft.client.network.ClientPlayerEntity;
19 import net.minecraft.server.command.CommandSource;
20 import net.minecraft.server.command.ServerCommandSource;
21 import net.minecraft.text.*;
22 import net.minecraft.util.Formatting;
23 import net.minecraft.util.math.BlockPos;
25 public class ClientInterop {
26 public static void disconnectedFromRemoteServer() {
27 SaveGameStructureLoader.clear();
28 EventBus.publish(new DisconnectedFromRemoteServer());
31 public static void render(float partialTicks, ClientPlayerEntity player) {
32 Player.setPosition(partialTicks, player);
33 ClientRenderer.render(DimensionId.from(player.dimension));
36 public static boolean interceptChatMessage(String message) {
37 if (message.startsWith("/bbor:")) {
38 ClientPlayNetworkHandler connection = MinecraftClient.getInstance().getNetworkHandler();
39 if (connection != null) {
40 CommandDispatcher<CommandSource> commandDispatcher = connection.getCommandDispatcher();
41 ServerCommandSource commandSource = MinecraftClient.getInstance().player.getCommandSource();
43 commandDispatcher.execute(message.substring(1), commandSource);
44 } catch (CommandSyntaxException exception) {
45 commandSource.sendError(Texts.toText(exception.getRawMessage()));
46 if (exception.getInput() != null && exception.getCursor() >= 0) {
47 Text suggestion = new LiteralText("")
48 .formatted(Formatting.GRAY)
49 .styled(style -> style.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message)));
50 int textLength = Math.min(exception.getInput().length(), exception.getCursor());
51 if (textLength > 10) {
52 suggestion.append("...");
55 suggestion.append(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
56 if (textLength < exception.getInput().length()) {
57 suggestion.append(new LiteralText(exception.getInput().substring(textLength))
58 .formatted(Formatting.RED, Formatting.UNDERLINE));
61 suggestion.append(new TranslatableText("command.context.here")
62 .formatted(Formatting.RED, Formatting.ITALIC));
63 commandSource.sendError(suggestion);
72 public static void updateWorldSpawnReceived(BlockPos blockPos) {
73 EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
76 public static int getRenderDistanceChunks() {
77 return MinecraftClient.getInstance().options.viewDistance;
80 public static void handleSeedMessage(Text chatComponent) {
81 TypeHelper.doIfType(chatComponent, TranslatableText.class, message -> {
82 if (!message.getKey().equals("commands.seed.success")) return;
85 long seed = Long.parseLong(message.getArgs()[0].toString());
86 SlimeChunkProvider.setSeed(seed);
87 } catch (Exception ignored) {
92 public static void registerClientCommands(CommandDispatcher<CommandSource> commandDispatcher) {
93 SeedCommand.register(commandDispatcher);
94 SpawningSphereCommand.register(commandDispatcher);
95 CustomCommand.register(commandDispatcher);
96 ConfigCommand.register(commandDispatcher);
97 StructuresCommand.register(commandDispatcher);
100 public static void receivedChunk(int chunkX, int chunkZ) {
101 SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
104 public static void saveLoaded(String fileName, long seed) {
106 MinecraftClient.getInstance().mouse.lockCursor();
110 SlimeChunkProvider.setSeed(seed);
111 SaveGameStructureLoader.loadSaveGame(fileName);
114 public static void clearStructures() {
115 EventBus.publish(new SaveLoaded());
116 SaveGameStructureLoader.clear();
119 public static void displayScreen(Screen screen) {
120 MinecraftClient.getInstance().openScreen(screen);
123 public static long getGameTime() {
124 return MinecraftClient.getInstance().world.getTime();