]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/interop/ClientInterop.java
Upgrade to 1.14.2
[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.mojang.brigadier.CommandDispatcher;
13 import com.mojang.brigadier.exceptions.CommandSyntaxException;
14 import net.minecraft.client.Minecraft;
15 import net.minecraft.client.entity.player.ClientPlayerEntity;
16 import net.minecraft.client.gui.screen.Screen;
17 import net.minecraft.client.network.play.ClientPlayNetHandler;
18 import net.minecraft.command.CommandSource;
19 import net.minecraft.command.ISuggestionProvider;
20 import net.minecraft.util.math.BlockPos;
21 import net.minecraft.util.text.*;
22 import net.minecraft.util.text.event.ClickEvent;
23
24 public class ClientInterop {
25     public static void disconnectedFromRemoteServer() {
26         SaveGameStructureLoader.clear();
27         EventBus.publish(new DisconnectedFromRemoteServer());
28     }
29
30     public static void render(float partialTicks, ClientPlayerEntity player) {
31         Player.setPosition(partialTicks, player);
32         ClientRenderer.render(player.dimension.getId());
33     }
34
35     public static boolean interceptChatMessage(String message) {
36         if (message.startsWith("/bbor:")) {
37             ClientPlayNetHandler connection = Minecraft.getInstance().getConnection();
38             if (connection != null) {
39                 CommandDispatcher<ISuggestionProvider> commandDispatcher = connection.func_195515_i();
40                 CommandSource commandSource = Minecraft.getInstance().player.getCommandSource();
41                 try {
42                     commandDispatcher.execute(message.substring(1), commandSource);
43                 } catch (CommandSyntaxException exception) {
44                     commandSource.sendErrorMessage(TextComponentUtils.toTextComponent(exception.getRawMessage()));
45                     if (exception.getInput() != null && exception.getCursor() >= 0) {
46                         ITextComponent suggestion = new StringTextComponent("")
47                                 .applyTextStyle(TextFormatting.GRAY)
48                                 .applyTextStyle(style -> style.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message)));
49                         int textLength = Math.min(exception.getInput().length(), exception.getCursor());
50                         if (textLength > 10) {
51                             suggestion.appendText("...");
52                         }
53
54                         suggestion.appendText(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
55                         if (textLength < exception.getInput().length()) {
56                             suggestion.appendSibling(new StringTextComponent(exception.getInput().substring(textLength))
57                                     .applyTextStyles(TextFormatting.RED, TextFormatting.UNDERLINE));
58                         }
59
60                         suggestion.appendSibling(new TranslationTextComponent("command.context.here")
61                                 .applyTextStyles(TextFormatting.RED, TextFormatting.ITALIC));
62                         commandSource.sendErrorMessage(suggestion);
63                     }
64                 }
65             }
66             return true;
67         }
68         return false;
69     }
70
71     public static void updateWorldSpawnReceived(BlockPos blockPos) {
72         EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
73     }
74
75     public static int getRenderDistanceChunks() {
76         return Minecraft.getInstance().gameSettings.renderDistanceChunks;
77     }
78
79     public static void handleSeedMessage(ITextComponent chatComponent) {
80         TypeHelper.doIfType(chatComponent, TranslationTextComponent.class, message -> {
81             if (!message.getKey().equals("commands.seed.success")) return;
82
83             try {
84                 long seed = Long.parseLong(message.getFormatArgs()[0].toString());
85                 SlimeChunkProvider.setSeed(seed);
86             } catch (Exception ignored) {
87             }
88         });
89     }
90
91     public static void registerClientCommands(CommandDispatcher<ISuggestionProvider> commandDispatcher) {
92         SeedCommand.register(commandDispatcher);
93         SpawningSphereCommand.register(commandDispatcher);
94         CustomCommand.register(commandDispatcher);
95         ConfigCommand.register(commandDispatcher);
96         StructuresCommand.register(commandDispatcher);
97     }
98
99     public static void receivedChunk(int chunkX, int chunkZ) {
100         SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
101     }
102
103     public static void saveLoaded(String fileName, long seed) {
104         displayScreen(null);
105         Minecraft.getInstance().mouseHelper.grabMouse();
106
107         clearStructures();
108
109         SlimeChunkProvider.setSeed(seed);
110         SaveGameStructureLoader.loadSaveGame(fileName);
111     }
112
113     public static void clearStructures() {
114         EventBus.publish(new SaveLoaded());
115         SaveGameStructureLoader.clear();
116     }
117
118     public static void displayScreen(Screen screen) {
119         Minecraft.getInstance().displayGuiScreen(screen);
120     }
121 }