Bläddra i källkod

Add way for other mods to indicate that they are fake players (#202)

* Add way for other mods to indicate that they are fake players

* Implement "reasonable default" for fake players and fix inverted logic

Signed-off-by: Max <maxh2709@gmail.com>

Co-authored-by: Max <maxh2709@gmail.com>
(cherry picked from commit 42684fd87abbcec8c570291cb0b6fc8cc890355a)
(cherry picked from commit bfde8dee179e0728f85da8c0e6691ee3791b67b9)

(cherry picked from commit d3d656053dd6b94c33d3c4291ff00505e0edab50)
shedaniel 3 år sedan
förälder
incheckning
a93e4c0ab1

+ 31 - 0
fabric/src/main/java/me/shedaniel/architectury/hooks/fabric/FakePlayers.java

@@ -0,0 +1,31 @@
+/*
+ * This file is part of architectury.
+ * Copyright (C) 2020, 2021, 2022 architectury
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package me.shedaniel.architectury.hooks.fabric;
+
+import me.shedaniel.architectury.event.Event;
+import me.shedaniel.architectury.event.EventFactory;
+import me.shedaniel.architectury.event.EventResult;
+import net.minecraft.world.entity.player.Player;
+
+public interface FakePlayers {
+    Event<FakePlayers> EVENT = EventFactory.createEventResult();
+    
+    EventResult isFakePlayer(Player player);
+}

+ 8 - 1
fabric/src/main/java/me/shedaniel/architectury/hooks/fabric/PlayerHooksImpl.java

@@ -19,10 +19,17 @@
 
 package me.shedaniel.architectury.hooks.fabric;
 
+import net.minecraft.server.level.ServerPlayer;
 import net.minecraft.world.entity.player.Player;
 
 public class PlayerHooksImpl {
     public static boolean isFake(Player player) {
-        return false;
+        var result = FakePlayers.EVENT.invoker().isFakePlayer(player);
+        if (result.value() != null) {
+            return result.value();
+        }
+        // If no result has been returned, assume that player classes extending ServerPlayer
+        // (apart from ServerPlayer itself) are fake players, as a "reasonable default"
+        return player instanceof ServerPlayer && player.getClass() != ServerPlayer.class;
     }
 }