diff --git a/build.gradle b/build.gradle index 6a2622e..bc70b92 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/src/main/java/me/lucko/fabric/api/permissions/v0/OfflineOptionRequestEvent.java b/src/main/java/me/lucko/fabric/api/permissions/v0/OfflineOptionRequestEvent.java index 442b42c..69505f4 100644 --- a/src/main/java/me/lucko/fabric/api/permissions/v0/OfflineOptionRequestEvent.java +++ b/src/main/java/me/lucko/fabric/api/permissions/v0/OfflineOptionRequestEvent.java @@ -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; diff --git a/src/main/java/me/lucko/fabric/api/permissions/v0/OptionRequestEvent.java b/src/main/java/me/lucko/fabric/api/permissions/v0/OptionRequestEvent.java index 7e0d70e..88d9588 100644 --- a/src/main/java/me/lucko/fabric/api/permissions/v0/OptionRequestEvent.java +++ b/src/main/java/me/lucko/fabric/api/permissions/v0/OptionRequestEvent.java @@ -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; diff --git a/src/main/java/me/lucko/fabric/api/permissions/v0/Options.java b/src/main/java/me/lucko/fabric/api/permissions/v0/Options.java index 7678a77..d5eeef3 100644 --- a/src/main/java/me/lucko/fabric/api/permissions/v0/Options.java +++ b/src/main/java/me/lucko/fabric/api/permissions/v0/Options.java @@ -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; @@ -138,8 +135,7 @@ static T get(@NotNull CommandSource source, @NotNull String key, T defaultVa */ static @NotNull Optional 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); } /** @@ -154,8 +150,7 @@ static 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); } /** @@ -180,8 +175,7 @@ static String get(@NotNull Entity entity, @NotNull String key, String defaultVal */ static @NotNull Optional get(@NotNull Entity entity, @NotNull String key, @NotNull Function 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); } /** @@ -209,8 +203,7 @@ static String get(@NotNull Entity entity, @NotNull String key, String defaultVal @Contract("_, _, !null, _ -> !null") static T get(@NotNull Entity entity, @NotNull String key, T defaultValue, @NotNull Function 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); } /** diff --git a/src/main/java/me/lucko/fabric/api/permissions/v0/PermissionCheckEvent.java b/src/main/java/me/lucko/fabric/api/permissions/v0/PermissionCheckEvent.java index 42b5b63..3e473d4 100644 --- a/src/main/java/me/lucko/fabric/api/permissions/v0/PermissionCheckEvent.java +++ b/src/main/java/me/lucko/fabric/api/permissions/v0/PermissionCheckEvent.java @@ -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; /** diff --git a/src/main/java/me/lucko/fabric/api/permissions/v0/Permissions.java b/src/main/java/me/lucko/fabric/api/permissions/v0/Permissions.java index 188cab8..699e0c9 100644 --- a/src/main/java/me/lucko/fabric/api/permissions/v0/Permissions.java +++ b/src/main/java/me/lucko/fabric/api/permissions/v0/Permissions.java @@ -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; @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** diff --git a/src/main/java/me/lucko/fabric/api/permissions/v0/Util.java b/src/main/java/me/lucko/fabric/api/permissions/v0/Util.java new file mode 100644 index 0000000..a73793c --- /dev/null +++ b/src/main/java/me/lucko/fabric/api/permissions/v0/Util.java @@ -0,0 +1,48 @@ +/* + * This file is part of fabric-permissions-api, 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.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."); + } + } + +}