]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/interop/ClientInterop.java
General performance improvements and serverside fixes
[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 boolean interceptChatMessage(String message) {
45         if (message.startsWith("/bbor:")) {
46             ClientPlayNetworkHandler connection = MinecraftClient.getInstance().getNetworkHandler();
47             if (connection != null) {
48                 CommandDispatcher<CommandSource> commandDispatcher = connection.getCommandDispatcher();
49                 ServerCommandSource commandSource = MinecraftClient.getInstance().player.getCommandSource();
50                 try {
51                     commandDispatcher.execute(message.substring(1), commandSource);
52                 } catch (CommandSyntaxException exception) {
53                     commandSource.sendError(Texts.toText(exception.getRawMessage()));
54                     if (exception.getInput() != null && exception.getCursor() >= 0) {
55                         MutableText suggestion = new LiteralText("")
56                                 .formatted(Formatting.GRAY)
57                                 .styled(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message)));
58                         int textLength = Math.min(exception.getInput().length(), exception.getCursor());
59                         if (textLength > 10) {
60                             suggestion.append("...");
61                         }
62
63                         suggestion.append(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
64                         if (textLength < exception.getInput().length()) {
65                             suggestion.append(new LiteralText(exception.getInput().substring(textLength))
66                                     .formatted(Formatting.RED, Formatting.UNDERLINE));
67                         }
68
69                         suggestion.append(new TranslatableText("command.context.here")
70                                 .formatted(Formatting.RED, Formatting.ITALIC));
71                         commandSource.sendError(suggestion);
72                     }
73                 }
74             }
75             return true;
76         }
77         return false;
78     }
79
80     public static void updateWorldSpawnReceived(BlockPos blockPos) {
81         EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
82     }
83
84     public static int getRenderDistanceChunks() {
85         return MinecraftClient.getInstance().options.viewDistance;
86     }
87
88     public static void handleSeedMessage(Text chatComponent) {
89         TypeHelper.doIfType(chatComponent, TranslatableText.class, message -> {
90             if (!message.getKey().equals("commands.seed.success")) return;
91
92             try {
93                 long seed = Long.parseLong(message.getArgs()[0].toString());
94                 SlimeChunkProvider.setSeed(seed);
95             } catch (Exception ignored) {
96             }
97         });
98     }
99
100     public static void registerClientCommands(CommandDispatcher<CommandSource> commandDispatcher) {
101         SeedCommand.register(commandDispatcher);
102         SpawningSphereCommand.register(commandDispatcher);
103         CustomCommand.register(commandDispatcher);
104         ConfigCommand.register(commandDispatcher);
105         StructuresCommand.register(commandDispatcher);
106     }
107
108     public static void receivedChunk(int chunkX, int chunkZ) {
109         SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
110         BiomeBorderHelper.onChunkLoaded(chunkX, chunkZ);
111     }
112
113     public static void unloadChunk(int chunkX, int chunkZ) {
114         BiomeBorderHelper.onChunkUnload(chunkX, chunkZ);
115     }
116
117     public static void saveLoaded(String fileName, long seed) {
118         displayScreen(null);
119         MinecraftClient.getInstance().mouse.lockCursor();
120
121         clearStructures();
122
123         SlimeChunkProvider.setSeed(seed);
124         SaveGameStructureLoader.loadSaveGame(fileName);
125     }
126
127     public static void clearStructures() {
128         EventBus.publish(new SaveLoaded());
129         SaveGameStructureLoader.clear();
130     }
131
132     public static void displayScreen(Screen screen) {
133         MinecraftClient.getInstance().setScreen(screen);
134     }
135
136     public static long getGameTime() {
137         return MinecraftClient.getInstance().world.getTime();
138     }
139 }