]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/interop/ClientInterop.java
e1d0fdf50009ab82b2bcc143393822412fa93984
[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.EntityPlayerSP;
16 import net.minecraft.client.network.NetHandlerPlayClient;
17 import net.minecraft.command.CommandSource;
18 import net.minecraft.command.ISuggestionProvider;
19 import net.minecraft.util.math.BlockPos;
20 import net.minecraft.util.text.*;
21 import net.minecraft.util.text.event.ClickEvent;
22
23 public class ClientInterop {
24     public static void disconnectedFromRemoteServer() {
25         SaveGameStructureLoader.clear();
26         EventBus.publish(new DisconnectedFromRemoteServer());
27     }
28
29     public static void render(float partialTicks, EntityPlayerSP player) {
30         Player.setPosition(partialTicks, player);
31         ClientRenderer.render(player.dimension.getId());
32     }
33
34     public static boolean interceptChatMessage(String message) {
35         if (message.startsWith("/bbor:")) {
36             NetHandlerPlayClient connection = Minecraft.getInstance().getConnection();
37             if (connection != null) {
38                 CommandDispatcher<ISuggestionProvider> commandDispatcher = connection.func_195515_i();
39                 CommandSource commandSource = Minecraft.getInstance().player.getCommandSource();
40                 try {
41                     commandDispatcher.execute(message.substring(1), commandSource);
42                 } catch (CommandSyntaxException exception) {
43                     commandSource.sendErrorMessage(TextComponentUtils.toTextComponent(exception.getRawMessage()));
44                     if (exception.getInput() != null && exception.getCursor() >= 0) {
45                         ITextComponent suggestion = new TextComponentString("")
46                                 .applyTextStyle(TextFormatting.GRAY)
47                                 .applyTextStyle(style -> style.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message)));
48                         int textLength = Math.min(exception.getInput().length(), exception.getCursor());
49                         if (textLength > 10) {
50                             suggestion.appendText("...");
51                         }
52
53                         suggestion.appendText(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
54                         if (textLength < exception.getInput().length()) {
55                             suggestion.appendSibling(new TextComponentString(exception.getInput().substring(textLength))
56                                     .applyTextStyles(TextFormatting.RED, TextFormatting.UNDERLINE));
57                         }
58
59                         suggestion.appendSibling(new TextComponentTranslation("command.context.here")
60                                 .applyTextStyles(TextFormatting.RED, TextFormatting.ITALIC));
61                         commandSource.sendErrorMessage(suggestion);
62                     }
63                 }
64             }
65             return true;
66         }
67         return false;
68     }
69
70     public static void updateWorldSpawnReceived(BlockPos blockPos) {
71         EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
72     }
73
74     public static int getRenderDistanceChunks() {
75         return Minecraft.getInstance().gameSettings.renderDistanceChunks;
76     }
77
78     public static void handleSeedMessage(ITextComponent chatComponent) {
79         TypeHelper.doIfType(chatComponent, TextComponentTranslation.class, message -> {
80             if (!message.getKey().equals("commands.seed.success")) return;
81
82             try {
83                 long seed = Long.parseLong(message.getFormatArgs()[0].toString());
84                 SlimeChunkProvider.setSeed(seed);
85             } catch (Exception ignored) {
86             }
87         });
88     }
89
90     public static void registerClientCommands(CommandDispatcher<ISuggestionProvider> commandDispatcher) {
91         SeedCommand.register(commandDispatcher);
92         SpawningSphereCommand.register(commandDispatcher);
93         BoxCommand.register(commandDispatcher);
94         BeaconCommand.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         Minecraft minecraft = Minecraft.getInstance();
105         minecraft.displayGuiScreen(null);
106         minecraft.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 }