Merge pull request #3754 from PssbleTrngle/v3.0

Support for fabric-permission-api & LuckPerms
This commit is contained in:
mikeprimm 2022-06-05 16:54:31 -05:00 committed by GitHub
commit 85d558d932
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 203 additions and 29 deletions

View File

@ -17,6 +17,11 @@ configurations {
implementation.extendsFrom(shadow)
}
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
@ -26,6 +31,9 @@ dependencies {
compileOnly group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'
shadow project(path: ':DynmapCore', configuration: 'shadow')
modCompileOnly "me.lucko:fabric-permissions-api:0.1-SNAPSHOT"
compileOnly 'net.luckperms:api:5.4'
}
processResources {

View File

@ -7,6 +7,7 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.FluidBlock;
@ -48,9 +49,7 @@ import org.dynmap.fabric_1_18_2.event.CustomServerChunkEvents;
import org.dynmap.fabric_1_18_2.event.CustomServerLifecycleEvents;
import org.dynmap.fabric_1_18_2.event.PlayerEvents;
import org.dynmap.fabric_1_18_2.mixin.BiomeEffectsAccessor;
import org.dynmap.fabric_1_18_2.permissions.FilePermissions;
import org.dynmap.fabric_1_18_2.permissions.OpPermissions;
import org.dynmap.fabric_1_18_2.permissions.PermissionProvider;
import org.dynmap.fabric_1_18_2.permissions.*;
import org.dynmap.permissions.PermissionsHandler;
import org.dynmap.renderer.DynmapBlockState;
@ -143,7 +142,7 @@ public class DynmapPlugin {
int idx = bsids.getRawId(bs);
if (idx >= stateByID.length) {
int plen = stateByID.length;
stateByID = Arrays.copyOf(stateByID, idx*11/10); // grow array by 10%
stateByID = Arrays.copyOf(stateByID, idx * 11 / 10); // grow array by 10%
Arrays.fill(stateByID, plen, stateByID.length, DynmapBlockState.AIR);
}
Block b = bs.getBlock();
@ -173,16 +172,26 @@ public class DynmapPlugin {
//Log.info("statename=" + bn + "[" + statename + "], lightAtten=" + lightAtten);
// Fill in base attributes
bld.setBaseState(basebs).setStateIndex(idx - baseidx).setBlockName(bn).setStateName(statename).setMaterial(mat.toString()).setLegacyBlockID(idx).setAttenuatesLight(lightAtten);
if (mat.isSolid()) { bld.setSolid(); }
if (mat == Material.AIR) { bld.setAir(); }
if (mat == Material.WOOD) { bld.setLog(); }
if (mat == Material.LEAVES) { bld.setLeaves(); }
if (mat.isSolid()) {
bld.setSolid();
}
if (mat == Material.AIR) {
bld.setAir();
}
if (mat == Material.WOOD) {
bld.setLog();
}
if (mat == Material.LEAVES) {
bld.setLeaves();
}
if ((!bs.getFluidState().isEmpty()) && !(bs.getBlock() instanceof FluidBlock)) {
bld.setWaterlogged();
}
DynmapBlockState dbs = bld.build(); // Build state
stateByID[idx] = dbs;
if (basebs == null) { basebs = dbs; }
if (basebs == null) {
basebs = dbs;
}
}
}
// for (int gidx = 0; gidx < DynmapBlockState.getGlobalIndexMax(); gidx++) {
@ -350,16 +359,14 @@ public class DynmapPlugin {
BiomeMap bmap = BiomeMap.NULL;
if (rl != null) { // If resource location, lookup by this
bmap = BiomeMap.byBiomeResourceLocation(rl);
}
else {
} else {
bmap = BiomeMap.byBiomeID(i);
}
if (bmap.isDefault() || (bmap == BiomeMap.NULL)) {
bmap = new BiomeMap((rl != null) ? BiomeMap.NO_INDEX : i, id, tmp, hum, rl);
Log.verboseinfo("Add custom biome [" + bmap.toString() + "] (" + i + ")");
cnt++;
}
else {
} else {
bmap.setTemperature(tmp);
bmap.setRainfall(hum);
}
@ -395,11 +402,21 @@ public class DynmapPlugin {
/* Set up player login/quit event handler */
registerPlayerLoginListener();
if (FabricLoader.getInstance().isModLoaded("luckperms")) {
Log.info("Using luckperms for access control");
permissions = new LuckPermissions();
}
else if (FabricLoader.getInstance().isModLoaded("fabric-permissions-api-v0")) {
Log.info("Using fabric-permissions-api for access control");
permissions = new FabricPermissions();
} else {
/* Initialize permissions handler */
permissions = FilePermissions.create();
if (permissions == null) {
permissions = new OpPermissions(new String[]{"webchat", "marker.icons", "marker.list", "webregister", "stats", "hide.self", "show.self"});
}
}
/* Get and initialize data folder */
File dataDirectory = new File("dynmap");
@ -635,7 +652,7 @@ public class DynmapPlugin {
if ((sections[i] != null) && (!sections[i].isEmpty())) {
int sy = sections[i].getYOffset();
if (sy < ymin) ymin = sy;
if ((sy+16) > ymax) ymax = sy + 16;
if ((sy + 16) > ymax) ymax = sy + 16;
}
}
if (ymax != Integer.MIN_VALUE) {

View File

@ -0,0 +1,47 @@
package org.dynmap.fabric_1_18_2.permissions;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.entity.player.PlayerEntity;
import org.dynmap.Log;
import org.dynmap.fabric_1_18_2.DynmapPlugin;
import org.dynmap.json.simple.parser.JSONParser;
import java.util.Set;
import java.util.stream.Collectors;
public class FabricPermissions implements PermissionProvider {
private String permissionKey(String perm) {
return "dynmap." + perm;
}
@Override
public Set<String> hasOfflinePermissions(String player, Set<String> perms) {
return perms.stream()
.filter(perm -> hasOfflinePermission(player, perm))
.collect(Collectors.toSet());
}
@Override
public boolean hasOfflinePermission(String player, String perm) {
return DynmapPlugin.plugin.isOp(player.toLowerCase());
}
@Override
public boolean has(PlayerEntity player, String permission) {
if (player == null) return false;
String name = player.getName().getString().toLowerCase();
if (DynmapPlugin.plugin.isOp(name)) return true;
return Permissions.check(player, permissionKey(permission));
}
@Override
public boolean hasPermissionNode(PlayerEntity player, String permission) {
if (player != null) {
String name = player.getName().getString().toLowerCase();
return DynmapPlugin.plugin.isOp(name);
}
return false;
}
}

View File

@ -0,0 +1,102 @@
package org.dynmap.fabric_1_18_2.permissions;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.cacheddata.CachedPermissionData;
import net.luckperms.api.model.user.User;
import net.luckperms.api.util.Tristate;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
import org.dynmap.Log;
import org.dynmap.fabric_1_18_2.DynmapPlugin;
import org.dynmap.json.simple.JSONArray;
import org.dynmap.json.simple.JSONObject;
import org.dynmap.json.simple.parser.JSONParser;
import org.dynmap.json.simple.parser.ParseException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
public class LuckPermissions implements PermissionProvider {
private final JSONParser parser = new JSONParser();
private LuckPerms api = null;
private Optional<LuckPerms> getApi() {
if (api != null) return Optional.of(api);
try {
api = LuckPermsProvider.get();
return Optional.of(api);
} catch (Exception ex) {
Log.warning("Trying to access LuckPerms before it has loaded");
return Optional.empty();
}
}
private Optional<UUID> cachedUUID(String username) {
try {
BufferedReader reader = new BufferedReader(new FileReader(MinecraftServer.USER_CACHE_FILE));
JSONArray cache = (JSONArray) parser.parse(reader);
for (Object it : cache) {
JSONObject user = (JSONObject) it;
if (user.get("name").toString().equalsIgnoreCase(username)) {
String uuid = user.get("uuid").toString();
return Optional.of(UUID.fromString(uuid));
}
}
reader.close();
} catch (IOException | ParseException ex) {
Log.warning("Unable to read usercache.json");
}
return Optional.empty();
}
private String permissionKey(String perm) {
return "dynmap." + perm;
}
@Override
public Set<String> hasOfflinePermissions(String player, Set<String> perms) {
return perms.stream()
.filter(perm -> hasOfflinePermission(player, perm))
.collect(Collectors.toSet());
}
@Override
public boolean hasOfflinePermission(String player, String perm) {
if (DynmapPlugin.plugin.isOp(player.toLowerCase())) return true;
Optional<LuckPerms> api = getApi();
Optional<UUID> uuid = cachedUUID(player);
if (!uuid.isPresent() || !api.isPresent()) return false;
User user = api.get().getUserManager().loadUser(uuid.get()).join();
CachedPermissionData permissions = user.getCachedData().getPermissionData();
Tristate state = permissions.checkPermission(permissionKey(perm));
return state.asBoolean();
}
@Override
public boolean has(PlayerEntity player, String permission) {
if (player == null) return false;
String name = player.getName().getString().toLowerCase();
if (DynmapPlugin.plugin.isOp(name)) return true;
return Permissions.check(player, permissionKey(permission));
}
@Override
public boolean hasPermissionNode(PlayerEntity player, String permission) {
if (player != null) {
String name = player.getName().getString().toLowerCase();
return DynmapPlugin.plugin.isOp(name);
}
return false;
}
}