]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/interop/ClientInterop.java
Setup for 1.16.3 Fabric
[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.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.getEntityWorld().getRegistryKey()));
34     }
35
36     public static void renderDeferred() {
37         ClientRenderer.renderDeferred();
38     }
39
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();
46                 try {
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("...");
57                         }
58
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));
63                         }
64
65                         suggestion.append(new TranslatableText("command.context.here")
66                                 .formatted(Formatting.RED, Formatting.ITALIC));
67                         commandSource.sendError(suggestion);
68                     }
69                 }
70             }
71             return true;
72         }
73         return false;
74     }
75
76     public static void updateWorldSpawnReceived(BlockPos blockPos) {
77         EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
78     }
79
80     public static int getRenderDistanceChunks() {
81         return MinecraftClient.getInstance().options.viewDistance;
82     }
83
84     public static void handleSeedMessage(Text chatComponent) {
85         TypeHelper.doIfType(chatComponent, TranslatableText.class, message -> {
86             if (!message.getKey().equals("commands.seed.success")) return;
87
88             try {
89                 long seed = Long.parseLong(message.getArgs()[0].toString());
90                 SlimeChunkProvider.setSeed(seed);
91             } catch (Exception ignored) {
92             }
93         });
94     }
95
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);
102     }
103
104     public static void receivedChunk(int chunkX, int chunkZ) {
105         SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
106     }
107
108     public static void saveLoaded(String fileName, long seed) {
109         displayScreen(null);
110         MinecraftClient.getInstance().mouse.lockCursor();
111
112         clearStructures();
113
114         SlimeChunkProvider.setSeed(seed);
115         SaveGameStructureLoader.loadSaveGame(fileName);
116     }
117
118     public static void clearStructures() {
119         EventBus.publish(new SaveLoaded());
120         SaveGameStructureLoader.clear();
121     }
122
123     public static void displayScreen(Screen screen) {
124         MinecraftClient.getInstance().openScreen(screen);
125     }
126
127     public static long getGameTime() {
128         return MinecraftClient.getInstance().world.getTime();
129     }
130 }