1 package com.irtimaled.bbor.client.interop;
3 import com.irtimaled.bbor.client.ClientRenderer;
4 import com.irtimaled.bbor.client.commands.ConfigCommand;
5 import com.irtimaled.bbor.client.commands.CustomCommand;
6 import com.irtimaled.bbor.client.commands.SeedCommand;
7 import com.irtimaled.bbor.client.commands.SpawningSphereCommand;
8 import com.irtimaled.bbor.client.commands.StructuresCommand;
9 import com.irtimaled.bbor.client.events.DisconnectedFromRemoteServer;
10 import com.irtimaled.bbor.client.events.SaveLoaded;
11 import com.irtimaled.bbor.client.events.UpdateWorldSpawnReceived;
12 import com.irtimaled.bbor.client.providers.SlimeChunkProvider;
13 import com.irtimaled.bbor.common.EventBus;
14 import com.irtimaled.bbor.common.TypeHelper;
15 import com.irtimaled.bbor.common.models.DimensionId;
16 import com.mojang.brigadier.CommandDispatcher;
17 import com.mojang.brigadier.exceptions.CommandSyntaxException;
18 import net.minecraft.client.MinecraftClient;
19 import net.minecraft.client.gui.screen.Screen;
20 import net.minecraft.client.network.ClientPlayNetworkHandler;
21 import net.minecraft.client.network.ClientPlayerEntity;
22 import net.minecraft.client.util.math.MatrixStack;
23 import net.minecraft.command.CommandSource;
24 import net.minecraft.server.command.ServerCommandSource;
25 import net.minecraft.text.ClickEvent;
26 import net.minecraft.text.LiteralText;
27 import net.minecraft.text.MutableText;
28 import net.minecraft.text.Text;
29 import net.minecraft.text.Texts;
30 import net.minecraft.text.TranslatableText;
31 import net.minecraft.util.Formatting;
32 import net.minecraft.util.math.BlockPos;
34 public class ClientInterop {
35 public static void disconnectedFromRemoteServer() {
36 SaveGameStructureLoader.clear();
37 EventBus.publish(new DisconnectedFromRemoteServer());
40 public static void render(MatrixStack matrixStack, ClientPlayerEntity player) {
41 ClientRenderer.render(matrixStack, DimensionId.from(player.getEntityWorld().getRegistryKey()));
44 public static void renderDeferred() {
45 ClientRenderer.renderDeferred();
48 public static boolean interceptChatMessage(String message) {
49 if (message.startsWith("/bbor:")) {
50 ClientPlayNetworkHandler connection = MinecraftClient.getInstance().getNetworkHandler();
51 if (connection != null) {
52 CommandDispatcher<CommandSource> commandDispatcher = connection.getCommandDispatcher();
53 ServerCommandSource commandSource = MinecraftClient.getInstance().player.getCommandSource();
55 commandDispatcher.execute(message.substring(1), commandSource);
56 } catch (CommandSyntaxException exception) {
57 commandSource.sendError(Texts.toText(exception.getRawMessage()));
58 if (exception.getInput() != null && exception.getCursor() >= 0) {
59 MutableText suggestion = new LiteralText("")
60 .formatted(Formatting.GRAY)
61 .styled(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message)));
62 int textLength = Math.min(exception.getInput().length(), exception.getCursor());
63 if (textLength > 10) {
64 suggestion.append("...");
67 suggestion.append(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
68 if (textLength < exception.getInput().length()) {
69 suggestion.append(new LiteralText(exception.getInput().substring(textLength))
70 .formatted(Formatting.RED, Formatting.UNDERLINE));
73 suggestion.append(new TranslatableText("command.context.here")
74 .formatted(Formatting.RED, Formatting.ITALIC));
75 commandSource.sendError(suggestion);
84 public static void updateWorldSpawnReceived(BlockPos blockPos) {
85 EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
88 public static int getRenderDistanceChunks() {
89 return MinecraftClient.getInstance().options.viewDistance;
92 public static void handleSeedMessage(Text chatComponent) {
93 TypeHelper.doIfType(chatComponent, TranslatableText.class, message -> {
94 if (!message.getKey().equals("commands.seed.success")) return;
97 long seed = Long.parseLong(message.getArgs()[0].toString());
98 SlimeChunkProvider.setSeed(seed);
99 } catch (Exception ignored) {
104 public static void registerClientCommands(CommandDispatcher<CommandSource> commandDispatcher) {
105 SeedCommand.register(commandDispatcher);
106 SpawningSphereCommand.register(commandDispatcher);
107 CustomCommand.register(commandDispatcher);
108 ConfigCommand.register(commandDispatcher);
109 StructuresCommand.register(commandDispatcher);
112 public static void receivedChunk(int chunkX, int chunkZ) {
113 SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
116 public static void saveLoaded(String fileName, long seed) {
118 MinecraftClient.getInstance().mouse.lockCursor();
122 SlimeChunkProvider.setSeed(seed);
123 SaveGameStructureLoader.loadSaveGame(fileName);
126 public static void clearStructures() {
127 EventBus.publish(new SaveLoaded());
128 SaveGameStructureLoader.clear();
131 public static void displayScreen(Screen screen) {
132 MinecraftClient.getInstance().setScreen(screen);
135 public static long getGameTime() {
136 return MinecraftClient.getInstance().world.getTime();