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 BiomeBorderHelper.onDisconnect();
38 EventBus.publish(new DisconnectedFromRemoteServer());
41 public static void render(MatrixStack matrixStack, ClientPlayerEntity player) {
42 ClientRenderer.render(matrixStack, DimensionId.from(player.getEntityWorld().getRegistryKey()));
45 public static boolean interceptChatMessage(String message) {
46 if (message.startsWith("/bbor:")) {
47 ClientPlayNetworkHandler connection = MinecraftClient.getInstance().getNetworkHandler();
48 if (connection != null) {
49 CommandDispatcher<CommandSource> commandDispatcher = connection.getCommandDispatcher();
50 ServerCommandSource commandSource = MinecraftClient.getInstance().player.getCommandSource();
52 commandDispatcher.execute(message.substring(1), commandSource);
53 } catch (CommandSyntaxException exception) {
54 commandSource.sendError(Texts.toText(exception.getRawMessage()));
55 if (exception.getInput() != null && exception.getCursor() >= 0) {
56 MutableText suggestion = new LiteralText("")
57 .formatted(Formatting.GRAY)
58 .styled(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message)));
59 int textLength = Math.min(exception.getInput().length(), exception.getCursor());
60 if (textLength > 10) {
61 suggestion.append("...");
64 suggestion.append(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
65 if (textLength < exception.getInput().length()) {
66 suggestion.append(new LiteralText(exception.getInput().substring(textLength))
67 .formatted(Formatting.RED, Formatting.UNDERLINE));
70 suggestion.append(new TranslatableText("command.context.here")
71 .formatted(Formatting.RED, Formatting.ITALIC));
72 commandSource.sendError(suggestion);
81 public static void updateWorldSpawnReceived(BlockPos blockPos) {
82 EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
85 public static int getRenderDistanceChunks() {
86 return MinecraftClient.getInstance().options.viewDistance;
89 public static void handleSeedMessage(Text chatComponent) {
90 TypeHelper.doIfType(chatComponent, TranslatableText.class, message -> {
91 if (!message.getKey().equals("commands.seed.success")) return;
94 long seed = Long.parseLong(message.getArgs()[0].toString());
95 SlimeChunkProvider.setSeed(seed);
96 } catch (Exception ignored) {
101 public static void registerClientCommands(CommandDispatcher<CommandSource> commandDispatcher) {
102 SeedCommand.register(commandDispatcher);
103 SpawningSphereCommand.register(commandDispatcher);
104 CustomCommand.register(commandDispatcher);
105 ConfigCommand.register(commandDispatcher);
106 StructuresCommand.register(commandDispatcher);
109 public static void receivedChunk(int chunkX, int chunkZ) {
110 SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
111 BiomeBorderHelper.onChunkLoaded(chunkX, chunkZ);
114 public static void unloadChunk(int chunkX, int chunkZ) {
115 BiomeBorderHelper.onChunkUnload(chunkX, chunkZ);
118 public static void saveLoaded(String fileName, long seed) {
120 MinecraftClient.getInstance().mouse.lockCursor();
124 SlimeChunkProvider.setSeed(seed);
125 SaveGameStructureLoader.loadSaveGame(fileName);
128 public static void clearStructures() {
129 EventBus.publish(new SaveLoaded());
130 SaveGameStructureLoader.clear();
133 public static void displayScreen(Screen screen) {
134 MinecraftClient.getInstance().setScreen(screen);
137 public static long getGameTime() {
138 return MinecraftClient.getInstance().world.getTime();