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.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;
25 public class ClientInterop {
26 public static void disconnectedFromRemoteServer() {
27 SaveGameStructureLoader.clear();
28 EventBus.publish(new DisconnectedFromRemoteServer());
31 public static void render(float partialTicks, ClientPlayerEntity player) {
32 Player.setPosition(partialTicks, player);
33 ClientRenderer.render(DimensionId.from(player.dimension));
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();
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("...");
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));
61 suggestion.appendSibling(new TranslationTextComponent("command.context.here")
62 .applyTextStyles(TextFormatting.RED, TextFormatting.ITALIC));
63 commandSource.sendErrorMessage(suggestion);
72 public static void updateWorldSpawnReceived(BlockPos blockPos) {
73 EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
76 public static int getRenderDistanceChunks() {
77 return Minecraft.getInstance().gameSettings.renderDistanceChunks;
80 public static void handleSeedMessage(ITextComponent chatComponent) {
81 TypeHelper.doIfType(chatComponent, TranslationTextComponent.class, message -> {
82 if (!message.getKey().equals("commands.seed.success")) return;
85 long seed = Long.parseLong(message.getFormatArgs()[0].toString());
86 SlimeChunkProvider.setSeed(seed);
87 } catch (Exception ignored) {
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);
100 public static void receivedChunk(int chunkX, int chunkZ) {
101 SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
104 public static void saveLoaded(String fileName, long seed) {
106 Minecraft.getInstance().mouseHelper.grabMouse();
110 SlimeChunkProvider.setSeed(seed);
111 SaveGameStructureLoader.loadSaveGame(fileName);
114 public static void clearStructures() {
115 EventBus.publish(new SaveLoaded());
116 SaveGameStructureLoader.clear();
119 public static void displayScreen(Screen screen) {
120 Minecraft.getInstance().displayGuiScreen(screen);
123 public static long getGameTime() {
124 return Minecraft.getInstance().world.getGameTime();