]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/interop/ClientInterop.java
Update to 1.17.1 (#124)
[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.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;
33
34 public class ClientInterop {
35     public static void disconnectedFromRemoteServer() {
36         SaveGameStructureLoader.clear();
37         EventBus.publish(new DisconnectedFromRemoteServer());
38     }
39
40     public static void render(MatrixStack matrixStack, ClientPlayerEntity player) {
41         ClientRenderer.render(matrixStack, DimensionId.from(player.getEntityWorld().getRegistryKey()));
42     }
43
44     public static void renderDeferred() {
45         ClientRenderer.renderDeferred();
46     }
47
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();
54                 try {
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("...");
65                         }
66
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));
71                         }
72
73                         suggestion.append(new TranslatableText("command.context.here")
74                                 .formatted(Formatting.RED, Formatting.ITALIC));
75                         commandSource.sendError(suggestion);
76                     }
77                 }
78             }
79             return true;
80         }
81         return false;
82     }
83
84     public static void updateWorldSpawnReceived(BlockPos blockPos) {
85         EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
86     }
87
88     public static int getRenderDistanceChunks() {
89         return MinecraftClient.getInstance().options.viewDistance;
90     }
91
92     public static void handleSeedMessage(Text chatComponent) {
93         TypeHelper.doIfType(chatComponent, TranslatableText.class, message -> {
94             if (!message.getKey().equals("commands.seed.success")) return;
95
96             try {
97                 long seed = Long.parseLong(message.getArgs()[0].toString());
98                 SlimeChunkProvider.setSeed(seed);
99             } catch (Exception ignored) {
100             }
101         });
102     }
103
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);
110     }
111
112     public static void receivedChunk(int chunkX, int chunkZ) {
113         SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
114     }
115
116     public static void saveLoaded(String fileName, long seed) {
117         displayScreen(null);
118         MinecraftClient.getInstance().mouse.lockCursor();
119
120         clearStructures();
121
122         SlimeChunkProvider.setSeed(seed);
123         SaveGameStructureLoader.loadSaveGame(fileName);
124     }
125
126     public static void clearStructures() {
127         EventBus.publish(new SaveLoaded());
128         SaveGameStructureLoader.clear();
129     }
130
131     public static void displayScreen(Screen screen) {
132         MinecraftClient.getInstance().setScreen(screen);
133     }
134
135     public static long getGameTime() {
136         return MinecraftClient.getInstance().world.getTime();
137     }
138 }