From 94895bcd4e0bd6f97143ff64e6df733debedbc86 Mon Sep 17 00:00:00 2001
From: LatvianModder <latvianmodder@gmail.com>
Date: Mon, 26 Aug 2024 19:13:06 +0300
Subject: [PATCH] Bugfixes

---
 .../mods/kubejs/core/ClientPlayerKJS.java     | 19 +----
 .../kubejs/core/LocalClientPlayerKJS.java     | 73 +++++++++++++++++++
 .../kubejs/core/mixin/LocalPlayerMixin.java   | 27 ++-----
 .../mods/kubejs/web/local/KubeJSWeb.java      |  8 +-
 4 files changed, 85 insertions(+), 42 deletions(-)
 create mode 100644 src/main/java/dev/latvian/mods/kubejs/core/LocalClientPlayerKJS.java

diff --git a/src/main/java/dev/latvian/mods/kubejs/core/ClientPlayerKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/ClientPlayerKJS.java
index 5871d9eec..5a9b7a3a8 100644
--- a/src/main/java/dev/latvian/mods/kubejs/core/ClientPlayerKJS.java
+++ b/src/main/java/dev/latvian/mods/kubejs/core/ClientPlayerKJS.java
@@ -1,15 +1,10 @@
 package dev.latvian.mods.kubejs.core;
 
-import dev.latvian.mods.kubejs.KubeJS;
-import dev.latvian.mods.kubejs.net.SendDataFromClientPayload;
 import dev.latvian.mods.kubejs.player.PlayerStatsJS;
 import dev.latvian.mods.kubejs.util.NotificationToastData;
 import dev.latvian.mods.rhino.util.RemapPrefixForJS;
-import net.minecraft.client.Minecraft;
 import net.minecraft.client.player.AbstractClientPlayer;
-import net.minecraft.client.player.LocalPlayer;
 import net.minecraft.nbt.CompoundTag;
-import net.neoforged.neoforge.network.PacketDistributor;
 import org.jetbrains.annotations.Nullable;
 
 @RemapPrefixForJS("kjs$")
@@ -20,32 +15,24 @@ public interface ClientPlayerKJS extends PlayerKJS {
 	}
 
 	default boolean isSelf() {
-		return kjs$self() == KubeJS.PROXY.getClientPlayer();
+		return false;
 	}
 
 	@Override
 	default void kjs$sendData(String channel, @Nullable CompoundTag data) {
-		if (!channel.isEmpty()) {
-			PacketDistributor.sendToServer(new SendDataFromClientPayload(channel, data));
-		}
 	}
 
 	@Override
 	default PlayerStatsJS kjs$getStats() {
-		if (!isSelf()) {
-			throw new IllegalStateException("Can't access other client player stats!");
-		}
-
-		return new PlayerStatsJS(kjs$self(), ((LocalPlayer) kjs$self()).getStats());
+		throw new IllegalStateException("Can't access other client player stats!");
 	}
 
 	@Override
 	default boolean kjs$isMiningBlock() {
-		return isSelf() && Minecraft.getInstance().gameMode.isDestroying();
+		return false;
 	}
 
 	@Override
 	default void kjs$notify(NotificationToastData notification) {
-		notification.show();
 	}
 }
diff --git a/src/main/java/dev/latvian/mods/kubejs/core/LocalClientPlayerKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/LocalClientPlayerKJS.java
new file mode 100644
index 000000000..f512bf4b6
--- /dev/null
+++ b/src/main/java/dev/latvian/mods/kubejs/core/LocalClientPlayerKJS.java
@@ -0,0 +1,73 @@
+package dev.latvian.mods.kubejs.core;
+
+import dev.latvian.mods.kubejs.client.KubeSessionData;
+import dev.latvian.mods.kubejs.net.SendDataFromClientPayload;
+import dev.latvian.mods.kubejs.player.PlayerStatsJS;
+import dev.latvian.mods.kubejs.util.NotificationToastData;
+import dev.latvian.mods.rhino.util.RemapPrefixForJS;
+import net.minecraft.client.Minecraft;
+import net.minecraft.client.player.LocalPlayer;
+import net.minecraft.nbt.CompoundTag;
+import net.minecraft.resources.ResourceLocation;
+import net.neoforged.neoforge.network.PacketDistributor;
+import org.jetbrains.annotations.Nullable;
+
+@RemapPrefixForJS("kjs$")
+public interface LocalClientPlayerKJS extends ClientPlayerKJS {
+	@Override
+	default LocalPlayer kjs$self() {
+		return (LocalPlayer) this;
+	}
+
+	default Minecraft kjs$getMinecraft() {
+		return Minecraft.getInstance();
+	}
+
+	@Override
+	default void kjs$runCommand(String command) {
+		kjs$self().connection.sendCommand(command);
+	}
+
+	@Override
+	default void kjs$runCommandSilent(String command) {
+		kjs$self().connection.sendCommand(command);
+	}
+
+	@Override
+	default boolean isSelf() {
+		return true;
+	}
+
+	@Override
+	default void kjs$sendData(String channel, @Nullable CompoundTag data) {
+		if (!channel.isEmpty()) {
+			PacketDistributor.sendToServer(new SendDataFromClientPayload(channel, data));
+		}
+	}
+
+	@Override
+	default PlayerStatsJS kjs$getStats() {
+		return new PlayerStatsJS(kjs$self(), kjs$self().getStats());
+	}
+
+	@Override
+	default boolean kjs$isMiningBlock() {
+		return Minecraft.getInstance().gameMode.isDestroying();
+	}
+
+	@Override
+	default void kjs$notify(NotificationToastData notification) {
+		notification.show();
+	}
+
+	@Override
+	default void kjs$setActivePostShader(@Nullable ResourceLocation id) {
+		var sessionData = KubeSessionData.of(kjs$self().connection);
+
+		if (sessionData != null) {
+			sessionData.activePostShader = id;
+			var mc = kjs$getMinecraft();
+			mc.gameRenderer.checkEntityPostEffect(mc.options.getCameraType().isFirstPerson() ? mc.getCameraEntity() : null);
+		}
+	}
+}
diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/LocalPlayerMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/LocalPlayerMixin.java
index 00067e38e..68cfd35fd 100644
--- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/LocalPlayerMixin.java
+++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/LocalPlayerMixin.java
@@ -1,7 +1,7 @@
 package dev.latvian.mods.kubejs.core.mixin;
 
 import com.mojang.authlib.GameProfile;
-import dev.latvian.mods.kubejs.client.KubeSessionData;
+import dev.latvian.mods.kubejs.core.LocalClientPlayerKJS;
 import dev.latvian.mods.kubejs.kgui.action.ClientKGUIActions;
 import dev.latvian.mods.kubejs.kgui.action.KGUIActions;
 import dev.latvian.mods.rhino.util.RemapForJS;
@@ -9,19 +9,18 @@
 import net.minecraft.client.multiplayer.ClientPacketListener;
 import net.minecraft.client.player.LocalPlayer;
 import net.minecraft.core.BlockPos;
-import net.minecraft.resources.ResourceLocation;
 import net.minecraft.stats.StatsCounter;
 import net.minecraft.world.level.Level;
-import org.jetbrains.annotations.Nullable;
 import org.spongepowered.asm.mixin.Final;
 import org.spongepowered.asm.mixin.Mixin;
 import org.spongepowered.asm.mixin.Shadow;
 import org.spongepowered.asm.mixin.Unique;
+import org.spongepowered.asm.mixin.gen.Accessor;
 
 import java.util.Map;
 
 @Mixin(LocalPlayer.class)
-public abstract class LocalPlayerMixin extends AbstractClientPlayerMixin {
+public abstract class LocalPlayerMixin extends AbstractClientPlayerMixin implements LocalClientPlayerKJS {
 	@Unique
 	private KGUIActions kjs$kguiActions;
 
@@ -42,24 +41,8 @@ public LocalPlayerMixin(Level level, BlockPos blockPos, float f, GameProfile gam
 	protected Minecraft minecraft;
 
 	@Override
-	public void kjs$runCommand(String command) {
-		connection.sendCommand(command);
-	}
-
-	@Override
-	public void kjs$runCommandSilent(String command) {
-		connection.sendCommand(command);
-	}
-
-	@Override
-	public void kjs$setActivePostShader(@Nullable ResourceLocation id) {
-		var sessionData = KubeSessionData.of(connection);
-
-		if (sessionData != null) {
-			sessionData.activePostShader = id;
-			minecraft.gameRenderer.checkEntityPostEffect(minecraft.options.getCameraType().isFirstPerson() ? minecraft.getCameraEntity() : null);
-		}
-	}
+	@Accessor("minecraft")
+	public abstract Minecraft kjs$getMinecraft();
 
 	@Override
 	public KGUIActions kjs$getKgui() {
diff --git a/src/main/java/dev/latvian/mods/kubejs/web/local/KubeJSWeb.java b/src/main/java/dev/latvian/mods/kubejs/web/local/KubeJSWeb.java
index ea1e47563..acbae912a 100644
--- a/src/main/java/dev/latvian/mods/kubejs/web/local/KubeJSWeb.java
+++ b/src/main/java/dev/latvian/mods/kubejs/web/local/KubeJSWeb.java
@@ -15,11 +15,11 @@
 import dev.latvian.mods.kubejs.web.KJSWSSession;
 import dev.latvian.mods.kubejs.web.LocalWebServer;
 import dev.latvian.mods.kubejs.web.LocalWebServerRegistry;
-import net.minecraft.client.Minecraft;
 import net.minecraft.core.Holder;
 import net.minecraft.resources.ResourceKey;
 import net.minecraft.tags.TagKey;
 import net.neoforged.fml.ModList;
+import net.neoforged.neoforge.server.ServerLifecycleHooks;
 
 import java.util.ArrayList;
 import java.util.Optional;
@@ -81,10 +81,10 @@ public static void register(LocalWebServerRegistry registry) {
 	}
 
 	private static void reloadInternalServer() {
-		var mc = Minecraft.getInstance();
+		var server = ServerLifecycleHooks.getCurrentServer();
 
-		if (mc.player != null) {
-			mc.player.kjs$runCommand("/reload");
+		if (server != null) {
+			server.kjs$runCommand("/reload");
 		}
 	}