From d5cefac65d11907d7de349a3a82de1a50d1614fd Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 16 Jan 2021 18:25:26 +0000 Subject: [PATCH] Fix Fabric permissions not being copied during player respawn (#2818) --- .../context/FabricPlayerCalculator.java | 6 --- .../fabric/event/RespawnPlayerCallback.java | 42 ---------------- .../fabric/mixin/PlayerManagerMixin.java | 49 ------------------- .../fabric/mixin/ServerPlayerEntityMixin.java | 32 ++++++++---- .../luckperms/fabric/model/MixinUser.java | 4 ++ .../src/main/resources/mixins.luckperms.json | 1 - 6 files changed, 26 insertions(+), 108 deletions(-) delete mode 100644 fabric/src/main/java/me/lucko/luckperms/fabric/event/RespawnPlayerCallback.java delete mode 100644 fabric/src/main/java/me/lucko/luckperms/fabric/mixin/PlayerManagerMixin.java diff --git a/fabric/src/main/java/me/lucko/luckperms/fabric/context/FabricPlayerCalculator.java b/fabric/src/main/java/me/lucko/luckperms/fabric/context/FabricPlayerCalculator.java index ac42b8be2..d4e9ef53b 100644 --- a/fabric/src/main/java/me/lucko/luckperms/fabric/context/FabricPlayerCalculator.java +++ b/fabric/src/main/java/me/lucko/luckperms/fabric/context/FabricPlayerCalculator.java @@ -30,7 +30,6 @@ import me.lucko.luckperms.common.context.contextset.ImmutableContextSetImpl; import me.lucko.luckperms.common.util.EnumNamer; import me.lucko.luckperms.fabric.LPFabricPlugin; import me.lucko.luckperms.fabric.event.PlayerChangeWorldCallback; -import me.lucko.luckperms.fabric.event.RespawnPlayerCallback; import net.luckperms.api.context.Context; import net.luckperms.api.context.ContextCalculator; @@ -62,7 +61,6 @@ public class FabricPlayerCalculator implements ContextCalculator - * Copyright (c) contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package me.lucko.luckperms.fabric.event; - -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; -import net.minecraft.server.network.ServerPlayerEntity; - -// TODO: Use Fabric API alternative when merged. -// https://github.com/FabricMC/fabric/pull/957 -public interface RespawnPlayerCallback { - Event EVENT = EventFactory.createArrayBacked(RespawnPlayerCallback.class, (callbacks) -> (newPlayer, oldPlayer, alive) -> { - for (RespawnPlayerCallback callback : callbacks) { - callback.onRespawn(newPlayer, oldPlayer, alive); - } - }); - - void onRespawn(ServerPlayerEntity oldPlayer, ServerPlayerEntity newPlayer, boolean alive); -} diff --git a/fabric/src/main/java/me/lucko/luckperms/fabric/mixin/PlayerManagerMixin.java b/fabric/src/main/java/me/lucko/luckperms/fabric/mixin/PlayerManagerMixin.java deleted file mode 100644 index 11b72ac2d..000000000 --- a/fabric/src/main/java/me/lucko/luckperms/fabric/mixin/PlayerManagerMixin.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * This file is part of LuckPerms, licensed under the MIT License. - * - * Copyright (c) lucko (Luck) - * Copyright (c) contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package me.lucko.luckperms.fabric.mixin; - -import me.lucko.luckperms.fabric.event.RespawnPlayerCallback; - -import net.minecraft.server.PlayerManager; -import net.minecraft.server.network.ServerPlayerEntity; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - -@Mixin(PlayerManager.class) -public abstract class PlayerManagerMixin { - - // Implement the callback for RespawnPlayerCallback - // We'll switch to Fabric's event when FabricMC/fabric#957 is merged. - @Inject(at = @At("TAIL"), method = "respawnPlayer", locals = LocalCapture.CAPTURE_FAILEXCEPTION) - private void luckperms_onRespawnPlayer(ServerPlayerEntity player, boolean alive, CallbackInfoReturnable cir) { - RespawnPlayerCallback.EVENT.invoker().onRespawn(player, cir.getReturnValue(), alive); // Transfer the old caches to the new player. - } - -} diff --git a/fabric/src/main/java/me/lucko/luckperms/fabric/mixin/ServerPlayerEntityMixin.java b/fabric/src/main/java/me/lucko/luckperms/fabric/mixin/ServerPlayerEntityMixin.java index 9bf69edcd..e47738fe1 100644 --- a/fabric/src/main/java/me/lucko/luckperms/fabric/mixin/ServerPlayerEntityMixin.java +++ b/fabric/src/main/java/me/lucko/luckperms/fabric/mixin/ServerPlayerEntityMixin.java @@ -45,7 +45,6 @@ import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; import java.util.Locale; @@ -73,6 +72,16 @@ public abstract class ServerPlayerEntityMixin implements MixinUser { // Used by PlayerChangeWorldCallback hook below. @Shadow public abstract ServerWorld getServerWorld(); + @Override + public User getLuckPermsUser() { + return this.luckperms$user; + } + + @Override + public QueryOptionsCache getQueryOptionsCache() { + return this.luckperms$queryOptions; + } + @Override public QueryOptionsCache getQueryOptionsCache(FabricContextManager contextManager) { if (this.luckperms$queryOptions == null) { @@ -122,20 +131,23 @@ public abstract class ServerPlayerEntityMixin implements MixinUser { return data.checkPermission(permission, PermissionCheckEvent.Origin.PLATFORM_PERMISSION_CHECK).result(); } - @Inject( - at = @At("HEAD"), - method = "setClientSettings" - ) + + @Inject(at = @At("TAIL"), method = "copyFrom") + private void luckperms_copyFrom(ServerPlayerEntity oldPlayer, boolean alive, CallbackInfo ci) { + MixinUser oldMixin = (MixinUser) oldPlayer; + this.luckperms$user = oldMixin.getLuckPermsUser(); + this.luckperms$queryOptions = oldMixin.getQueryOptionsCache(); + this.luckperms$queryOptions.invalidate(); + this.luckperms$locale = oldMixin.getCachedLocale(); + } + + @Inject(at = @At("HEAD"), method = "setClientSettings") private void luckperms_setClientSettings(ClientSettingsC2SPacket information, CallbackInfo ci) { String language = ((ClientSettingsC2SPacketAccessor) information).getLanguage(); this.luckperms$locale = TranslationManager.parseLocale(language); } - @Inject( - at = @At("TAIL"), - method = "worldChanged", - locals = LocalCapture.CAPTURE_FAILEXCEPTION - ) + @Inject(at = @At("TAIL"), method = "worldChanged") private void luckperms_onChangeDimension(ServerWorld targetWorld, CallbackInfo ci) { PlayerChangeWorldCallback.EVENT.invoker().onChangeWorld(this.getServerWorld(), targetWorld, (ServerPlayerEntity) (Object) this); } diff --git a/fabric/src/main/java/me/lucko/luckperms/fabric/model/MixinUser.java b/fabric/src/main/java/me/lucko/luckperms/fabric/model/MixinUser.java index 801f300f0..4215908d6 100644 --- a/fabric/src/main/java/me/lucko/luckperms/fabric/model/MixinUser.java +++ b/fabric/src/main/java/me/lucko/luckperms/fabric/model/MixinUser.java @@ -41,6 +41,10 @@ import java.util.Locale; */ public interface MixinUser { + User getLuckPermsUser(); + + QueryOptionsCache getQueryOptionsCache(); + /** * Gets (or creates using the manager) the objects {@link QueryOptionsCache}. * diff --git a/fabric/src/main/resources/mixins.luckperms.json b/fabric/src/main/resources/mixins.luckperms.json index a2d619141..b83dfbca7 100644 --- a/fabric/src/main/resources/mixins.luckperms.json +++ b/fabric/src/main/resources/mixins.luckperms.json @@ -4,7 +4,6 @@ "compatibilityLevel": "JAVA_8", "mixins": [ "ClientSettingsC2SPacketAccessor", - "PlayerManagerMixin", "ServerLoginNetworkHandlerAccessor", "ServerPlayerEntityMixin" ],