Fix Fabric permissions not being copied during player respawn (#2818)

This commit is contained in:
Luck 2021-01-16 18:25:26 +00:00
parent 2a44572fb2
commit d5cefac65d
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
6 changed files with 26 additions and 108 deletions

View File

@ -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<ServerPlayerEnt
public void registerListeners() {
PlayerChangeWorldCallback.EVENT.register(this::onWorldChange);
RespawnPlayerCallback.EVENT.register(this::onPlayerRespawn);
}
@Override
@ -112,8 +110,4 @@ public class FabricPlayerCalculator implements ContextCalculator<ServerPlayerEnt
this.plugin.getContextManager().invalidateCache(player);
}
private void onPlayerRespawn(ServerPlayerEntity oldPlayer, ServerPlayerEntity newPlayer, boolean alive) {
this.plugin.getContextManager().invalidateCache(oldPlayer);
this.plugin.getContextManager().invalidateCache(newPlayer);
}
}

View File

@ -1,42 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* 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<RespawnPlayerCallback> 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);
}

View File

@ -1,49 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* 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<ServerPlayerEntity> cir) {
RespawnPlayerCallback.EVENT.invoker().onRespawn(player, cir.getReturnValue(), alive); // Transfer the old caches to the new player.
}
}

View File

@ -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<ServerPlayerEntity> getQueryOptionsCache() {
return this.luckperms$queryOptions;
}
@Override
public QueryOptionsCache<ServerPlayerEntity> 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);
}

View File

@ -41,6 +41,10 @@ import java.util.Locale;
*/
public interface MixinUser {
User getLuckPermsUser();
QueryOptionsCache<ServerPlayerEntity> getQueryOptionsCache();
/**
* Gets (or creates using the manager) the objects {@link QueryOptionsCache}.
*

View File

@ -4,7 +4,6 @@
"compatibilityLevel": "JAVA_8",
"mixins": [
"ClientSettingsC2SPacketAccessor",
"PlayerManagerMixin",
"ServerLoginNetworkHandlerAccessor",
"ServerPlayerEntityMixin"
],