]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/interop/ClientInterop.java
Port to 1.19
[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.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.LiteralTextContent;
27 import net.minecraft.text.MutableText;
28 import net.minecraft.text.Text;
29 import net.minecraft.text.Texts;
30 import net.minecraft.text.TranslatableTextContent;
31 import net.minecraft.util.Formatting;
32 import net.minecraft.util.math.BlockPos;
33
34 public class ClientInterop {
35     public static void disconnectedFromRemoteServer() {
36         SaveGameStructureLoader.clear();
37         BiomeBorderHelper.onDisconnect();
38         EventBus.publish(new DisconnectedFromRemoteServer());
39     }
40
41     public static void render(MatrixStack matrixStack, ClientPlayerEntity player) {
42         ClientRenderer.render(matrixStack, DimensionId.from(player.getEntityWorld().getRegistryKey()));
43     }
44
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();
51                 try {
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 = MutableText.of(new LiteralTextContent(""))
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("...");
62                         }
63
64                         suggestion.append(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
65                         if (textLength < exception.getInput().length()) {
66                             suggestion.append(MutableText.of(new LiteralTextContent(exception.getInput().substring(textLength)))
67                                     .formatted(Formatting.RED, Formatting.UNDERLINE));
68                         }
69
70                         suggestion.append(MutableText.of(new TranslatableTextContent("command.context.here"))
71                                 .formatted(Formatting.RED, Formatting.ITALIC));
72                         commandSource.sendError(suggestion);
73                     }
74                 }
75             }
76             return true;
77         }
78         return false;
79     }
80
81     public static void updateWorldSpawnReceived(BlockPos blockPos) {
82         EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
83     }
84
85     public static int getRenderDistanceChunks() {
86         return MinecraftClient.getInstance().options.getViewDistance().getValue();
87     }
88
89     public static void handleSeedMessage(Text chatComponent) {
90         TypeHelper.doIfType(chatComponent, MutableText.class, message -> {
91             TypeHelper.doIfType(message.getContent(), TranslatableTextContent.class, content -> {
92                 if (!content.getKey().equals("commands.seed.success")) return;
93
94                 try {
95                     long seed = Long.parseLong(content.getArgs()[0].toString());
96                     SlimeChunkProvider.setSeed(seed);
97                 } catch (Exception ignored) {
98                 }
99             });
100         });
101     }
102
103     public static void registerClientCommands(CommandDispatcher<CommandSource> commandDispatcher) {
104         SeedCommand.register(commandDispatcher);
105         SpawningSphereCommand.register(commandDispatcher);
106         CustomCommand.register(commandDispatcher);
107         ConfigCommand.register(commandDispatcher);
108         StructuresCommand.register(commandDispatcher);
109     }
110
111     public static void receivedChunk(int chunkX, int chunkZ) {
112         SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
113         BiomeBorderHelper.onChunkLoaded(chunkX, chunkZ);
114     }
115
116     public static void unloadChunk(int chunkX, int chunkZ) {
117         BiomeBorderHelper.onChunkUnload(chunkX, chunkZ);
118     }
119
120     public static void saveLoaded(String fileName, long seed) {
121         displayScreen(null);
122         MinecraftClient.getInstance().mouse.lockCursor();
123
124         clearStructures();
125
126         SlimeChunkProvider.setSeed(seed);
127         SaveGameStructureLoader.loadSaveGame(fileName);
128     }
129
130     public static void clearStructures() {
131         EventBus.publish(new SaveLoaded());
132         SaveGameStructureLoader.clear();
133     }
134
135     public static void displayScreen(Screen screen) {
136         MinecraftClient.getInstance().setScreen(screen);
137     }
138
139     public static long getGameTime() {
140         return MinecraftClient.getInstance().world.getTime();
141     }
142 }