Skip to content

Commit

Permalink
Fix OP permission fallback (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrexHD authored Oct 28, 2024
1 parent 73194bf commit e3f1900
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 28 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ repositories {
maven { url 'https://maven.fabricmc.net/' }
}

def minecraftVersion = '1.21.2'
def yarnBuild = 1
def minecraftVersion = '1.21.3'
def yarnBuild = 2
def loaderVersion = '0.16.7'
def fabricApiVersion = '0.106.1+1.21.2'
def fabricApiVersion = '0.106.1+1.21.3'

group = 'me.lucko'
version = '0.3.3-SNAPSHOT'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.fabricmc.fabric.api.util.TriState;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.command.CommandSource;

import org.jetbrains.annotations.NotNull;

import java.util.Optional;
Expand Down
15 changes: 4 additions & 11 deletions src/main/java/me/lucko/fabric/api/permissions/v0/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
import com.mojang.authlib.GameProfile;
import net.minecraft.command.CommandSource;
import net.minecraft.entity.Entity;
import net.minecraft.server.world.ServerWorld;

import net.minecraft.world.World;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -138,8 +135,7 @@ static <T> T get(@NotNull CommandSource source, @NotNull String key, T defaultVa
*/
static @NotNull Optional<String> get(@NotNull Entity entity, @NotNull String key) {
Objects.requireNonNull(entity, "entity");
World world = entity.getWorld();
return get(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), key);
return get(Util.commandSourceFromEntity(entity), key);
}

/**
Expand All @@ -154,8 +150,7 @@ static <T> T get(@NotNull CommandSource source, @NotNull String key, T defaultVa
@Contract("_, _, !null -> !null")
static String get(@NotNull Entity entity, @NotNull String key, String defaultValue) {
Objects.requireNonNull(entity, "entity");
World world = entity.getWorld();
return get(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), key, defaultValue);
return get(Util.commandSourceFromEntity(entity), key, defaultValue);
}

/**
Expand All @@ -180,8 +175,7 @@ static String get(@NotNull Entity entity, @NotNull String key, String defaultVal
*/
static <T> @NotNull Optional<T> get(@NotNull Entity entity, @NotNull String key, @NotNull Function<String, ? extends T> valueTransformer) {
Objects.requireNonNull(entity, "entity");
World world = entity.getWorld();
return get(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), key, valueTransformer);
return get(Util.commandSourceFromEntity(entity), key, valueTransformer);
}

/**
Expand Down Expand Up @@ -209,8 +203,7 @@ static String get(@NotNull Entity entity, @NotNull String key, String defaultVal
@Contract("_, _, !null, _ -> !null")
static <T> T get(@NotNull Entity entity, @NotNull String key, T defaultValue, @NotNull Function<String, ? extends T> valueTransformer) {
Objects.requireNonNull(entity, "entity");
World world = entity.getWorld();
return get(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), key, defaultValue, valueTransformer);
return get(Util.commandSourceFromEntity(entity), key, defaultValue, valueTransformer);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import net.fabricmc.fabric.api.event.EventFactory;
import net.fabricmc.fabric.api.util.TriState;
import net.minecraft.command.CommandSource;

import org.jetbrains.annotations.NotNull;

/**
Expand Down
15 changes: 4 additions & 11 deletions src/main/java/me/lucko/fabric/api/permissions/v0/Permissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
import net.minecraft.entity.Entity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.world.ServerWorld;

import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
Expand Down Expand Up @@ -146,8 +143,7 @@ static boolean check(@NotNull CommandSource source, @NotNull String permission)
*/
static @NotNull TriState getPermissionValue(@NotNull Entity entity, @NotNull String permission) {
Objects.requireNonNull(entity, "entity");
World world = entity.getWorld();
return getPermissionValue(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), permission);
return getPermissionValue(Util.commandSourceFromEntity(entity), permission);
}

/**
Expand All @@ -161,8 +157,7 @@ static boolean check(@NotNull CommandSource source, @NotNull String permission)
*/
static boolean check(@NotNull Entity entity, @NotNull String permission, boolean defaultValue) {
Objects.requireNonNull(entity, "entity");
World world = entity.getWorld();
return check(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), permission, defaultValue);
return check(Util.commandSourceFromEntity(entity), permission, defaultValue);
}

/**
Expand All @@ -176,8 +171,7 @@ static boolean check(@NotNull Entity entity, @NotNull String permission, boolean
*/
static boolean check(@NotNull Entity entity, @NotNull String permission, int defaultRequiredLevel) {
Objects.requireNonNull(entity, "entity");
World world = entity.getWorld();
return check(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), permission, defaultRequiredLevel);
return check(Util.commandSourceFromEntity(entity), permission, defaultRequiredLevel);
}

/**
Expand All @@ -190,8 +184,7 @@ static boolean check(@NotNull Entity entity, @NotNull String permission, int def
*/
static boolean check(@NotNull Entity entity, @NotNull String permission) {
Objects.requireNonNull(entity, "entity");
World world = entity.getWorld();
return check(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), permission);
return check(Util.commandSourceFromEntity(entity), permission);
}

/**
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/me/lucko/fabric/api/permissions/v0/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of fabric-permissions-api, 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.fabric.api.permissions.v0;

import net.minecraft.entity.Entity;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;

class Util {

static ServerCommandSource commandSourceFromEntity(Entity entity) {
if (entity instanceof ServerPlayerEntity) {
return ((ServerPlayerEntity) entity).getCommandSource();
}
World world = entity.getWorld();
if (world instanceof ServerWorld) {
return entity.getCommandSource((ServerWorld) world);
} else {
throw new IllegalArgumentException("Entity '" + entity + "' is not a server entity. Try passing a CommandSource directly instead.");
}
}

}

0 comments on commit e3f1900

Please sign in to comment.