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.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.getEntityWorld().getRegistryKey()));
36 public static void renderDeferred() {
37 ClientRenderer.renderDeferred();
40 public static boolean interceptChatMessage(String message) {
41 if (message.startsWith("/bbor:")) {
42 ClientPlayNetworkHandler connection = MinecraftClient.getInstance().getNetworkHandler();
43 if (connection != null) {
44 CommandDispatcher<CommandSource> commandDispatcher = connection.getCommandDispatcher();
45 ServerCommandSource commandSource = MinecraftClient.getInstance().player.getCommandSource();
47 commandDispatcher.execute(message.substring(1), commandSource);
48 } catch (CommandSyntaxException exception) {
49 commandSource.sendError(Texts.toText(exception.getRawMessage()));
50 if (exception.getInput() != null && exception.getCursor() >= 0) {
51 MutableText suggestion = new LiteralText("")
52 .formatted(Formatting.GRAY)
53 .styled(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message)));
54 int textLength = Math.min(exception.getInput().length(), exception.getCursor());
55 if (textLength > 10) {
56 suggestion.append("...");
59 suggestion.append(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
60 if (textLength < exception.getInput().length()) {
61 suggestion.append(new LiteralText(exception.getInput().substring(textLength))
62 .formatted(Formatting.RED, Formatting.UNDERLINE));
65 suggestion.append(new TranslatableText("command.context.here")
66 .formatted(Formatting.RED, Formatting.ITALIC));
67 commandSource.sendError(suggestion);
76 public static void updateWorldSpawnReceived(BlockPos blockPos) {
77 EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
80 public static int getRenderDistanceChunks() {
81 return MinecraftClient.getInstance().options.viewDistance;
84 public static void handleSeedMessage(Text chatComponent) {
85 TypeHelper.doIfType(chatComponent, TranslatableText.class, message -> {
86 if (!message.getKey().equals("commands.seed.success")) return;
89 long seed = Long.parseLong(message.getArgs()[0].toString());
90 SlimeChunkProvider.setSeed(seed);
91 } catch (Exception ignored) {
96 public static void registerClientCommands(CommandDispatcher<CommandSource> commandDispatcher) {
97 SeedCommand.register(commandDispatcher);
98 SpawningSphereCommand.register(commandDispatcher);
99 CustomCommand.register(commandDispatcher);
100 ConfigCommand.register(commandDispatcher);
101 StructuresCommand.register(commandDispatcher);
104 public static void receivedChunk(int chunkX, int chunkZ) {
105 SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
108 public static void saveLoaded(String fileName, long seed) {
110 MinecraftClient.getInstance().mouse.lockCursor();
114 SlimeChunkProvider.setSeed(seed);
115 SaveGameStructureLoader.loadSaveGame(fileName);
118 public static void clearStructures() {
119 EventBus.publish(new SaveLoaded());
120 SaveGameStructureLoader.clear();
123 public static void displayScreen(Screen screen) {
124 MinecraftClient.getInstance().openScreen(screen);
127 public static long getGameTime() {
128 return MinecraftClient.getInstance().world.getTime();