]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/interop/ClientInterop.java
7ab06b9ba6e105ee50300d617faf9318008b38e4
[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.Minecraft;
16 import net.minecraft.client.entity.player.ClientPlayerEntity;
17 import net.minecraft.client.gui.screen.Screen;
18 import net.minecraft.client.network.play.ClientPlayNetHandler;
19 import net.minecraft.command.CommandSource;
20 import net.minecraft.command.ISuggestionProvider;
21 import net.minecraft.util.math.BlockPos;
22 import net.minecraft.util.text.*;
23 import net.minecraft.util.text.event.ClickEvent;
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             ClientPlayNetHandler connection = Minecraft.getInstance().getConnection();
39             if (connection != null) {
40                 CommandDispatcher<ISuggestionProvider> commandDispatcher = connection.func_195515_i();
41                 CommandSource commandSource = Minecraft.getInstance().player.getCommandSource();
42                 try {
43                     commandDispatcher.execute(message.substring(1), commandSource);
44                 } catch (CommandSyntaxException exception) {
45                     commandSource.sendErrorMessage(TextComponentUtils.toTextComponent(exception.getRawMessage()));
46                     if (exception.getInput() != null && exception.getCursor() >= 0) {
47                         ITextComponent suggestion = new StringTextComponent("")
48                                 .applyTextStyle(TextFormatting.GRAY)
49                                 .applyTextStyle(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.appendText("...");
53                         }
54
55                         suggestion.appendText(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
56                         if (textLength < exception.getInput().length()) {
57                             suggestion.appendSibling(new StringTextComponent(exception.getInput().substring(textLength))
58                                     .applyTextStyles(TextFormatting.RED, TextFormatting.UNDERLINE));
59                         }
60
61                         suggestion.appendSibling(new TranslationTextComponent("command.context.here")
62                                 .applyTextStyles(TextFormatting.RED, TextFormatting.ITALIC));
63                         commandSource.sendErrorMessage(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 Minecraft.getInstance().gameSettings.renderDistanceChunks;
78     }
79
80     public static void handleSeedMessage(ITextComponent chatComponent) {
81         TypeHelper.doIfType(chatComponent, TranslationTextComponent.class, message -> {
82             if (!message.getKey().equals("commands.seed.success")) return;
83
84             try {
85                 long seed = Long.parseLong(message.getFormatArgs()[0].toString());
86                 SlimeChunkProvider.setSeed(seed);
87             } catch (Exception ignored) {
88             }
89         });
90     }
91
92     public static void registerClientCommands(CommandDispatcher<ISuggestionProvider> 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         Minecraft.getInstance().mouseHelper.grabMouse();
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         Minecraft.getInstance().displayGuiScreen(screen);
121     }
122 }