add fabric-permission-api provider

This commit is contained in:
PssbleTrngle 2022-05-06 16:48:04 +02:00
parent 9c80489ec6
commit 5791632d35
3 changed files with 98 additions and 26 deletions

View File

@ -17,6 +17,10 @@ configurations {
implementation.extendsFrom(shadow) implementation.extendsFrom(shadow)
} }
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies { dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}" minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
@ -26,6 +30,8 @@ dependencies {
compileOnly group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2' compileOnly group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'
shadow project(path: ':DynmapCore', configuration: 'shadow') shadow project(path: ':DynmapCore', configuration: 'shadow')
modCompileOnly "me.lucko:fabric-permissions-api:0.1-SNAPSHOT"
} }
processResources { 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.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.FluidBlock; import net.minecraft.block.FluidBlock;
@ -48,6 +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.CustomServerLifecycleEvents;
import org.dynmap.fabric_1_18_2.event.PlayerEvents; 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.mixin.BiomeEffectsAccessor;
import org.dynmap.fabric_1_18_2.permissions.FabricPermissions;
import org.dynmap.fabric_1_18_2.permissions.FilePermissions; 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.OpPermissions;
import org.dynmap.fabric_1_18_2.permissions.PermissionProvider; import org.dynmap.fabric_1_18_2.permissions.PermissionProvider;
@ -137,13 +139,13 @@ public class DynmapPlugin {
int baseidx = 0; int baseidx = 0;
Iterator<BlockState> iter = bsids.iterator(); Iterator<BlockState> iter = bsids.iterator();
DynmapBlockState.Builder bld = new DynmapBlockState.Builder(); DynmapBlockState.Builder bld = new DynmapBlockState.Builder();
while (iter.hasNext()) { while (iter.hasNext()) {
BlockState bs = iter.next(); BlockState bs = iter.next();
int idx = bsids.getRawId(bs); int idx = bsids.getRawId(bs);
if (idx >= stateByID.length) { if (idx >= stateByID.length) {
int plen = 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); Arrays.fill(stateByID, plen, stateByID.length, DynmapBlockState.AIR);
} }
Block b = bs.getBlock(); Block b = bs.getBlock();
@ -173,16 +175,26 @@ public class DynmapPlugin {
//Log.info("statename=" + bn + "[" + statename + "], lightAtten=" + lightAtten); //Log.info("statename=" + bn + "[" + statename + "], lightAtten=" + lightAtten);
// Fill in base attributes // Fill in base attributes
bld.setBaseState(basebs).setStateIndex(idx - baseidx).setBlockName(bn).setStateName(statename).setMaterial(mat.toString()).setLegacyBlockID(idx).setAttenuatesLight(lightAtten); bld.setBaseState(basebs).setStateIndex(idx - baseidx).setBlockName(bn).setStateName(statename).setMaterial(mat.toString()).setLegacyBlockID(idx).setAttenuatesLight(lightAtten);
if (mat.isSolid()) { bld.setSolid(); } if (mat.isSolid()) {
if (mat == Material.AIR) { bld.setAir(); } bld.setSolid();
if (mat == Material.WOOD) { bld.setLog(); } }
if (mat == Material.LEAVES) { bld.setLeaves(); } if (mat == Material.AIR) {
if ((!bs.getFluidState().isEmpty()) && !(bs.getBlock() instanceof FluidBlock)) { bld.setAir();
bld.setWaterlogged(); }
} 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 DynmapBlockState dbs = bld.build(); // Build state
stateByID[idx] = dbs; stateByID[idx] = dbs;
if (basebs == null) { basebs = dbs; } if (basebs == null) {
basebs = dbs;
}
} }
} }
// for (int gidx = 0; gidx < DynmapBlockState.getGlobalIndexMax(); gidx++) { // for (int gidx = 0; gidx < DynmapBlockState.getGlobalIndexMax(); gidx++) {
@ -348,18 +360,16 @@ public class DynmapPlugin {
Log.verboseinfo("biome[" + i + "]: hum=" + hum + ", tmp=" + tmp + ", mult=" + Integer.toHexString(watermult)); Log.verboseinfo("biome[" + i + "]: hum=" + hum + ", tmp=" + tmp + ", mult=" + Integer.toHexString(watermult));
BiomeMap bmap = BiomeMap.NULL; BiomeMap bmap = BiomeMap.NULL;
if (rl != null) { // If resource location, lookup by this if (rl != null) { // If resource location, lookup by this
bmap = BiomeMap.byBiomeResourceLocation(rl); bmap = BiomeMap.byBiomeResourceLocation(rl);
} } else {
else { bmap = BiomeMap.byBiomeID(i);
bmap = BiomeMap.byBiomeID(i);
} }
if (bmap.isDefault() || (bmap == BiomeMap.NULL)) { if (bmap.isDefault() || (bmap == BiomeMap.NULL)) {
bmap = new BiomeMap((rl != null) ? BiomeMap.NO_INDEX : i, id, tmp, hum, rl); bmap = new BiomeMap((rl != null) ? BiomeMap.NO_INDEX : i, id, tmp, hum, rl);
Log.verboseinfo("Add custom biome [" + bmap.toString() + "] (" + i + ")"); Log.verboseinfo("Add custom biome [" + bmap.toString() + "] (" + i + ")");
cnt++; cnt++;
} } else {
else {
bmap.setTemperature(tmp); bmap.setTemperature(tmp);
bmap.setRainfall(hum); bmap.setRainfall(hum);
} }
@ -395,11 +405,17 @@ public class DynmapPlugin {
/* Set up player login/quit event handler */ /* Set up player login/quit event handler */
registerPlayerLoginListener(); registerPlayerLoginListener();
/* Initialize permissions handler */ if (FabricLoader.getInstance().isModLoaded("fabric-permissions-api-v0")) {
permissions = FilePermissions.create(); Log.info("Using fabric-permissions-api for access control");
if (permissions == null) { permissions = new FabricPermissions();
permissions = new OpPermissions(new String[]{"webchat", "marker.icons", "marker.list", "webregister", "stats", "hide.self", "show.self"}); } 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 */ /* Get and initialize data folder */
File dataDirectory = new File("dynmap"); File dataDirectory = new File("dynmap");
@ -628,14 +644,14 @@ public class DynmapPlugin {
FabricWorld fw = getWorld(world, false); FabricWorld fw = getWorld(world, false);
ChunkPos chunkPos = chunk.getPos(); ChunkPos chunkPos = chunk.getPos();
int ymax = Integer.MIN_VALUE; int ymax = Integer.MIN_VALUE;
int ymin = Integer.MAX_VALUE; int ymin = Integer.MAX_VALUE;
ChunkSection[] sections = chunk.getSectionArray(); ChunkSection[] sections = chunk.getSectionArray();
for (int i = 0; i < sections.length; i++) { for (int i = 0; i < sections.length; i++) {
if ((sections[i] != null) && (!sections[i].isEmpty())) { if ((sections[i] != null) && (!sections[i].isEmpty())) {
int sy = sections[i].getYOffset(); int sy = sections[i].getYOffset();
if (sy < ymin) ymin = sy; if (sy < ymin) ymin = sy;
if ((sy+16) > ymax) ymax = sy + 16; if ((sy + 16) > ymax) ymax = sy + 16;
} }
} }
if (ymax != Integer.MIN_VALUE) { if (ymax != Integer.MIN_VALUE) {

View File

@ -0,0 +1,50 @@
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.DynmapCore;
import org.dynmap.Log;
import org.dynmap.fabric_1_18_2.DynmapMod;
import org.dynmap.fabric_1_18_2.DynmapPlugin;
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) {
Log.info("Requesting offline permissions: " + String.join(",", perms) + " for " + player);
return perms.stream()
.filter(perm -> hasOfflinePermission(player, perm))
.collect(Collectors.toSet());
}
@Override
public boolean hasOfflinePermission(String player, String perm) {
Log.info("Requesting offline permission: " + perm + " for " + player);
return DynmapPlugin.plugin.isOp(player.toLowerCase());
}
@Override
public boolean has(PlayerEntity player, String permission) {
Log.info("Requesting privilege: " + permission);
return player != null && Permissions.check(player, permissionKey(permission));
}
@Override
public boolean hasPermissionNode(PlayerEntity player, String permission) {
if (player != null) {
String name = player.getName().getString().toLowerCase();
Log.info("Requesting permission node: " + permission + " for " + name);
return DynmapPlugin.plugin.isOp(name);
}
Log.info("Requesting permission node: " + permission);
return false;
}
}