]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blobdiff - src/main/java/com/irtimaled/bbor/client/interop/ClientInterop.java
Update to 1.17.1 (#124)
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / interop / ClientInterop.java
index 440db44f03ef00eae863d6effa80f7cd644489a2..bbc29ce6abc7926698572caac051b62ba1e7e31b 100644 (file)
 package com.irtimaled.bbor.client.interop;
 
-import com.irtimaled.bbor.client.PlayerCoords;
+import com.irtimaled.bbor.client.ClientRenderer;
+import com.irtimaled.bbor.client.commands.ConfigCommand;
+import com.irtimaled.bbor.client.commands.CustomCommand;
+import com.irtimaled.bbor.client.commands.SeedCommand;
+import com.irtimaled.bbor.client.commands.SpawningSphereCommand;
+import com.irtimaled.bbor.client.commands.StructuresCommand;
 import com.irtimaled.bbor.client.events.DisconnectedFromRemoteServer;
-import com.irtimaled.bbor.client.events.Render;
+import com.irtimaled.bbor.client.events.SaveLoaded;
 import com.irtimaled.bbor.client.events.UpdateWorldSpawnReceived;
 import com.irtimaled.bbor.client.providers.SlimeChunkProvider;
 import com.irtimaled.bbor.common.EventBus;
-import net.minecraft.client.Minecraft;
-import net.minecraft.client.entity.EntityPlayerSP;
+import com.irtimaled.bbor.common.TypeHelper;
+import com.irtimaled.bbor.common.models.DimensionId;
+import com.mojang.brigadier.CommandDispatcher;
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.client.gui.screen.Screen;
+import net.minecraft.client.network.ClientPlayNetworkHandler;
+import net.minecraft.client.network.ClientPlayerEntity;
+import net.minecraft.client.util.math.MatrixStack;
+import net.minecraft.command.CommandSource;
+import net.minecraft.server.command.ServerCommandSource;
+import net.minecraft.text.ClickEvent;
+import net.minecraft.text.LiteralText;
+import net.minecraft.text.MutableText;
+import net.minecraft.text.Text;
+import net.minecraft.text.Texts;
+import net.minecraft.text.TranslatableText;
+import net.minecraft.util.Formatting;
 import net.minecraft.util.math.BlockPos;
 
 public class ClientInterop {
     public static void disconnectedFromRemoteServer() {
+        SaveGameStructureLoader.clear();
         EventBus.publish(new DisconnectedFromRemoteServer());
     }
 
-    public static void render(float partialTicks, EntityPlayerSP player) {
-        PlayerCoords.setPlayerPosition(partialTicks, player);
-        EventBus.publish(new Render(player.dimension));
+    public static void render(MatrixStack matrixStack, ClientPlayerEntity player) {
+        ClientRenderer.render(matrixStack, DimensionId.from(player.getEntityWorld().getRegistryKey()));
+    }
+
+    public static void renderDeferred() {
+        ClientRenderer.renderDeferred();
     }
 
     public static boolean interceptChatMessage(String message) {
-        if (message.startsWith("/bbor:seed")) {
-            if (message.length() > 11) {
-                String argument = message.substring(11);
-                Long seed = parseNumericSeed(argument);
-                if (seed == null) {
-                    seed = (long) argument.hashCode();
+        if (message.startsWith("/bbor:")) {
+            ClientPlayNetworkHandler connection = MinecraftClient.getInstance().getNetworkHandler();
+            if (connection != null) {
+                CommandDispatcher<CommandSource> commandDispatcher = connection.getCommandDispatcher();
+                ServerCommandSource commandSource = MinecraftClient.getInstance().player.getCommandSource();
+                try {
+                    commandDispatcher.execute(message.substring(1), commandSource);
+                } catch (CommandSyntaxException exception) {
+                    commandSource.sendError(Texts.toText(exception.getRawMessage()));
+                    if (exception.getInput() != null && exception.getCursor() >= 0) {
+                        MutableText suggestion = new LiteralText("")
+                                .formatted(Formatting.GRAY)
+                                .styled(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message)));
+                        int textLength = Math.min(exception.getInput().length(), exception.getCursor());
+                        if (textLength > 10) {
+                            suggestion.append("...");
+                        }
+
+                        suggestion.append(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
+                        if (textLength < exception.getInput().length()) {
+                            suggestion.append(new LiteralText(exception.getInput().substring(textLength))
+                                    .formatted(Formatting.RED, Formatting.UNDERLINE));
+                        }
+
+                        suggestion.append(new TranslatableText("command.context.here")
+                                .formatted(Formatting.RED, Formatting.ITALIC));
+                        commandSource.sendError(suggestion);
+                    }
                 }
-                SlimeChunkProvider.setSeed(seed);
             }
             return true;
         }
         return false;
     }
 
-    private static Long parseNumericSeed(String argument) {
-        try {
-            return Long.parseLong(argument);
-        } catch (final NumberFormatException ex) {
-            return null;
-        }
-    }
-
     public static void updateWorldSpawnReceived(BlockPos blockPos) {
         EventBus.publish(new UpdateWorldSpawnReceived(blockPos.getX(), blockPos.getZ()));
     }
 
     public static int getRenderDistanceChunks() {
-        return Minecraft.getInstance().gameSettings.renderDistanceChunks;
+        return MinecraftClient.getInstance().options.viewDistance;
+    }
+
+    public static void handleSeedMessage(Text chatComponent) {
+        TypeHelper.doIfType(chatComponent, TranslatableText.class, message -> {
+            if (!message.getKey().equals("commands.seed.success")) return;
+
+            try {
+                long seed = Long.parseLong(message.getArgs()[0].toString());
+                SlimeChunkProvider.setSeed(seed);
+            } catch (Exception ignored) {
+            }
+        });
+    }
+
+    public static void registerClientCommands(CommandDispatcher<CommandSource> commandDispatcher) {
+        SeedCommand.register(commandDispatcher);
+        SpawningSphereCommand.register(commandDispatcher);
+        CustomCommand.register(commandDispatcher);
+        ConfigCommand.register(commandDispatcher);
+        StructuresCommand.register(commandDispatcher);
+    }
+
+    public static void receivedChunk(int chunkX, int chunkZ) {
+        SaveGameStructureLoader.loadStructures(chunkX, chunkZ);
+    }
+
+    public static void saveLoaded(String fileName, long seed) {
+        displayScreen(null);
+        MinecraftClient.getInstance().mouse.lockCursor();
+
+        clearStructures();
+
+        SlimeChunkProvider.setSeed(seed);
+        SaveGameStructureLoader.loadSaveGame(fileName);
+    }
+
+    public static void clearStructures() {
+        EventBus.publish(new SaveLoaded());
+        SaveGameStructureLoader.clear();
+    }
+
+    public static void displayScreen(Screen screen) {
+        MinecraftClient.getInstance().setScreen(screen);
+    }
+
+    public static long getGameTime() {
+        return MinecraftClient.getInstance().world.getTime();
     }
 }