1 package com.irtimaled.bbor.client.interop;
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;
23 public class ClientInterop {
24 public static void disconnectedFromRemoteServer() {
25 SaveGameStructureLoader.clear();
26 EventBus.publish(new DisconnectedFromRemoteServer());
29 public static void render(float partialTicks, EntityPlayerSP player) {
30 Player.setPosition(partialTicks, player);
31 ClientRenderer.render(player.dimension.getId());
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();
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("...");
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));
59 suggestion.appendSibling(new TextComponentTranslation("command.context.here")
60 .applyTextStyles(TextFormatting.RED, TextFormatting.ITALIC));
61 commandSource.sendErrorMessage(suggestion);
70 public static void updateWorldSpawnReceived(BlockPos blockPos) {
71 EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
74 public static int getRenderDistanceChunks() {
75 return Minecraft.getInstance().gameSettings.renderDistanceChunks;
78 public static void handleSeedMessage(ITextComponent chatComponent) {
79 TypeHelper.doIfType(chatComponent, TextComponentTranslation.class, message -> {
80 if (!message.getKey().equals("commands.seed.success")) return;
83 long seed = Long.parseLong(message.getFormatArgs()[0].toString());
84 SlimeChunkProvider.setSeed(seed);
85 } catch (Exception ignored) {
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);
99 public static void receivedChunk(int chunkX, int chunkZ) {
100 SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
103 public static void saveLoaded(String fileName, long seed) {
104 Minecraft minecraft = Minecraft.getInstance();
105 minecraft.displayGuiScreen(null);
106 minecraft.mouseHelper.grabMouse();
110 SlimeChunkProvider.setSeed(seed);
111 SaveGameStructureLoader.loadSaveGame(fileName);
114 public static void clearStructures() {
115 EventBus.publish(new SaveLoaded());
116 SaveGameStructureLoader.clear();