Skip to content

Commit a12aa68

Browse files
committed
Fix OP permission fallback
- Uses ServerPlayerEntity specific getCommandSource method. - Throws an appropriate error if the permission api is invoked by a non server side entity
1 parent 73194bf commit a12aa68

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ repositories {
99
maven { url 'https://maven.fabricmc.net/' }
1010
}
1111

12-
def minecraftVersion = '1.21.2'
13-
def yarnBuild = 1
12+
def minecraftVersion = '1.21.3'
13+
def yarnBuild = 2
1414
def loaderVersion = '0.16.7'
15-
def fabricApiVersion = '0.106.1+1.21.2'
15+
def fabricApiVersion = '0.106.1+1.21.3'
1616

1717
group = 'me.lucko'
1818
version = '0.3.3-SNAPSHOT'

src/main/java/me/lucko/fabric/api/permissions/v0/Options.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626
package me.lucko.fabric.api.permissions.v0;
2727

2828
import com.mojang.authlib.GameProfile;
29+
import me.lucko.fabric.impl.permissions.v0.Util;
2930
import net.minecraft.command.CommandSource;
3031
import net.minecraft.entity.Entity;
31-
import net.minecraft.server.world.ServerWorld;
3232

33-
import net.minecraft.world.World;
3433
import org.jetbrains.annotations.Contract;
3534
import org.jetbrains.annotations.NotNull;
3635

@@ -138,8 +137,7 @@ static <T> T get(@NotNull CommandSource source, @NotNull String key, T defaultVa
138137
*/
139138
static @NotNull Optional<String> get(@NotNull Entity entity, @NotNull String key) {
140139
Objects.requireNonNull(entity, "entity");
141-
World world = entity.getWorld();
142-
return get(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), key);
140+
return get(Util.getCommandSource(entity), key);
143141
}
144142

145143
/**
@@ -154,8 +152,7 @@ static <T> T get(@NotNull CommandSource source, @NotNull String key, T defaultVa
154152
@Contract("_, _, !null -> !null")
155153
static String get(@NotNull Entity entity, @NotNull String key, String defaultValue) {
156154
Objects.requireNonNull(entity, "entity");
157-
World world = entity.getWorld();
158-
return get(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), key, defaultValue);
155+
return get(Util.getCommandSource(entity), key, defaultValue);
159156
}
160157

161158
/**
@@ -180,8 +177,7 @@ static String get(@NotNull Entity entity, @NotNull String key, String defaultVal
180177
*/
181178
static <T> @NotNull Optional<T> get(@NotNull Entity entity, @NotNull String key, @NotNull Function<String, ? extends T> valueTransformer) {
182179
Objects.requireNonNull(entity, "entity");
183-
World world = entity.getWorld();
184-
return get(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), key, valueTransformer);
180+
return get(Util.getCommandSource(entity), key, valueTransformer);
185181
}
186182

187183
/**
@@ -209,8 +205,7 @@ static String get(@NotNull Entity entity, @NotNull String key, String defaultVal
209205
@Contract("_, _, !null, _ -> !null")
210206
static <T> T get(@NotNull Entity entity, @NotNull String key, T defaultValue, @NotNull Function<String, ? extends T> valueTransformer) {
211207
Objects.requireNonNull(entity, "entity");
212-
World world = entity.getWorld();
213-
return get(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), key, defaultValue, valueTransformer);
208+
return get(Util.getCommandSource(entity), key, defaultValue, valueTransformer);
214209
}
215210

216211
/**

src/main/java/me/lucko/fabric/api/permissions/v0/Permissions.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
package me.lucko.fabric.api.permissions.v0;
2727

2828
import com.mojang.authlib.GameProfile;
29+
import me.lucko.fabric.impl.permissions.v0.Util;
2930
import net.fabricmc.fabric.api.util.TriState;
3031
import net.minecraft.command.CommandSource;
3132
import net.minecraft.entity.Entity;
3233
import net.minecraft.server.MinecraftServer;
3334
import net.minecraft.server.command.ServerCommandSource;
34-
import net.minecraft.server.world.ServerWorld;
3535

36-
import net.minecraft.world.World;
3736
import org.jetbrains.annotations.NotNull;
3837

3938
import java.util.Objects;
@@ -146,8 +145,7 @@ static boolean check(@NotNull CommandSource source, @NotNull String permission)
146145
*/
147146
static @NotNull TriState getPermissionValue(@NotNull Entity entity, @NotNull String permission) {
148147
Objects.requireNonNull(entity, "entity");
149-
World world = entity.getWorld();
150-
return getPermissionValue(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), permission);
148+
return getPermissionValue(Util.getCommandSource(entity), permission);
151149
}
152150

153151
/**
@@ -161,8 +159,7 @@ static boolean check(@NotNull CommandSource source, @NotNull String permission)
161159
*/
162160
static boolean check(@NotNull Entity entity, @NotNull String permission, boolean defaultValue) {
163161
Objects.requireNonNull(entity, "entity");
164-
World world = entity.getWorld();
165-
return check(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), permission, defaultValue);
162+
return check(Util.getCommandSource(entity), permission, defaultValue);
166163
}
167164

168165
/**
@@ -176,8 +173,7 @@ static boolean check(@NotNull Entity entity, @NotNull String permission, boolean
176173
*/
177174
static boolean check(@NotNull Entity entity, @NotNull String permission, int defaultRequiredLevel) {
178175
Objects.requireNonNull(entity, "entity");
179-
World world = entity.getWorld();
180-
return check(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), permission, defaultRequiredLevel);
176+
return check(Util.getCommandSource(entity), permission, defaultRequiredLevel);
181177
}
182178

183179
/**
@@ -190,8 +186,7 @@ static boolean check(@NotNull Entity entity, @NotNull String permission, int def
190186
*/
191187
static boolean check(@NotNull Entity entity, @NotNull String permission) {
192188
Objects.requireNonNull(entity, "entity");
193-
World world = entity.getWorld();
194-
return check(entity.getCommandSource(world instanceof ServerWorld ? (ServerWorld) world : null), permission);
189+
return check(Util.getCommandSource(entity), permission);
195190
}
196191

197192
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package me.lucko.fabric.impl.permissions.v0;
2+
3+
import net.minecraft.entity.Entity;
4+
import net.minecraft.server.command.ServerCommandSource;
5+
import net.minecraft.server.network.ServerPlayerEntity;
6+
import net.minecraft.server.world.ServerWorld;
7+
import net.minecraft.world.World;
8+
9+
public class Util {
10+
11+
public static ServerCommandSource getCommandSource(Entity entity) {
12+
if (entity instanceof ServerPlayerEntity) {
13+
return ((ServerPlayerEntity) entity).getCommandSource();
14+
}
15+
World world = entity.getWorld();
16+
if (world instanceof ServerWorld) {
17+
return entity.getCommandSource((ServerWorld) world);
18+
} else {
19+
throw new IllegalStateException("Entity " + entity + " is not a server entity");
20+
}
21+
}
22+
23+
}

0 commit comments

Comments
 (0)