]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/interop/ClientInterop.java
9f3709cb9c57988f53b6425415180069c9c27572
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / interop / ClientInterop.java
1 package com.irtimaled.bbor.client.interop;
2
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;
24
25 public class ClientInterop {
26     public static void disconnectedFromRemoteServer() {
27         SaveGameStructureLoader.clear();
28         EventBus.publish(new DisconnectedFromRemoteServer());
29     }
30
31     public static void render(float partialTicks, ClientPlayerEntity player) {
32         Player.setPosition(partialTicks, player);
33         ClientRenderer.render(DimensionId.from(player.dimension));
34     }
35
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();
42                 try {
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("...");
53                         }
54
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));
59                         }
60
61                         suggestion.append(new TranslatableText("command.context.here")
62                                 .formatted(Formatting.RED, Formatting.ITALIC));
63                         commandSource.sendError(suggestion);
64                     }
65                 }
66             }
67             return true;
68         }
69         return false;
70     }
71
72     public static void updateWorldSpawnReceived(BlockPos blockPos) {
73         EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
74     }
75
76     public static int getRenderDistanceChunks() {
77         return MinecraftClient.getInstance().options.viewDistance;
78     }
79
80     public static void handleSeedMessage(Text chatComponent) {
81         TypeHelper.doIfType(chatComponent, TranslatableText.class, message -> {
82             if (!message.getKey().equals("commands.seed.success")) return;
83
84             try {
85                 long seed = Long.parseLong(message.getArgs()[0].toString());
86                 SlimeChunkProvider.setSeed(seed);
87             } catch (Exception ignored) {
88             }
89         });
90     }
91
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);
98     }
99
100     public static void receivedChunk(int chunkX, int chunkZ) {
101         SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
102     }
103
104     public static void saveLoaded(String fileName, long seed) {
105         displayScreen(null);
106         MinecraftClient.getInstance().mouse.lockCursor();
107
108         clearStructures();
109
110         SlimeChunkProvider.setSeed(seed);
111         SaveGameStructureLoader.loadSaveGame(fileName);
112     }
113
114     public static void clearStructures() {
115         EventBus.publish(new SaveLoaded());
116         SaveGameStructureLoader.clear();
117     }
118
119     public static void displayScreen(Screen screen) {
120         MinecraftClient.getInstance().openScreen(screen);
121     }
122
123     public static long getGameTime() {
124         return MinecraftClient.getInstance().world.getTime();
125     }
126 }