Skip to content

Commit

Permalink
Add support for permissions api, add few more rules and setup modrint…
Browse files Browse the repository at this point in the history
…h releases
  • Loading branch information
Patbox committed Dec 17, 2021
1 parent db702db commit 5dccf4b
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:

- name: Build with Gradle
run: ./gradlew build
env:
MODRINTH: ${{ secrets.MODRINTH }}
CHANGELOG: ${{ github.event.release.body }}

- name: Upload GitHub release
uses: AButler/upload-release-assets@v2.0
Expand Down
23 changes: 23 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id 'fabric-loom' version '0.10-SNAPSHOT'
id "com.modrinth.minotaur" version "1.2.1"
}
import com.modrinth.minotaur.TaskModrinthUpload

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand All @@ -12,6 +14,7 @@ group = project.maven_group
repositories {
maven { url = "https://maven.nucleoid.xyz" }
maven { url = "https://maven.gegy.dev/" }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}

dependencies {
Expand All @@ -24,6 +27,7 @@ dependencies {
modImplementation include('xyz.nucleoid:stimuli:0.2.6+1.18')

modCompileOnly "dev.gegy:player-roles-api:1.4.2"
modCompileOnly "me.lucko:fabric-permissions-api:0.1-SNAPSHOT"

modRuntime("supercoder79:databreaker:0.2.8") {
exclude module: "fabric-loader"
Expand Down Expand Up @@ -52,3 +56,22 @@ jar {
rename { "${it}_${project.archivesBaseName}" }
}
}

task publishModrinth (type: TaskModrinthUpload){
onlyIf {
System.getenv("MODRINTH")
}

token = System.getenv("MODRINTH")
projectId = 'hsRVgp6Q'
versionNumber = version
changelog = System.getenv("CHANGELOG")
// On fabric, use 'remapJar' instead of 'jar'
uploadFile = remapJar
addGameVersion((String) project.minecraft_version)
addLoader('fabric')
}

remapJar {
finalizedBy publishModrinth
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ loader_version=0.12.8
fabric_version=0.43.1+1.18

# Mod Properties
mod_version=0.3.2
mod_version=0.3.3
maven_group=xyz.nucleoid
archives_base_name=leukocyte
36 changes: 36 additions & 0 deletions src/main/java/xyz/nucleoid/leukocyte/command/ProtectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
.then(RoleArgument.argument("role")
.executes(ProtectCommand::addRoleExclusion))
)

.then(literal("permission")
.then(argument("permission", StringArgumentType.word())
.executes(ProtectCommand::addPermissionExclusion))
)
))
.then(literal("remove")
.then(AuthorityArgument.argument("authority")
Expand All @@ -102,6 +107,11 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
.then(RoleArgument.argument("role")
.executes(ProtectCommand::removeRoleExclusion))
)

.then(literal("permission")
.then(argument("permission", StringArgumentType.word())
.executes(ProtectCommand::removePermissionExclusion))
)
))
)
.then(literal("display")
Expand Down Expand Up @@ -269,6 +279,32 @@ private static int removeRoleExclusion(CommandContext<ServerCommandSource> conte
return Command.SINGLE_SUCCESS;
}

private static int addPermissionExclusion(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var authority = AuthorityArgument.get(context, "authority");
var permission = StringArgumentType.getString(context, "permission");

if (authority.getExclusions().addPermission(permission)) {
context.getSource().sendFeedback(new LiteralText("Added '" + permission + "' exclusion to " + authority.getKey()), true);
} else {
context.getSource().sendError(new LiteralText("'" + permission + "' is already excluded"));
}

return Command.SINGLE_SUCCESS;
}

private static int removePermissionExclusion(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var authority = AuthorityArgument.get(context, "authority");
var permission = StringArgumentType.getString(context, "permission");

if (authority.getExclusions().removePermission(permission)) {
context.getSource().sendFeedback(new LiteralText("Removed '" + permission + "' exclusion from " + authority.getKey()), true);
} else {
context.getSource().sendError(new LiteralText("'" + permission + "' is not excluded"));
}

return Command.SINGLE_SUCCESS;
}

private static int listAuthorities(CommandContext<ServerCommandSource> context) {
var leukocyte = Leukocyte.get(context.getSource().getServer());

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/xyz/nucleoid/leukocyte/roles/PermissionAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package xyz.nucleoid.leukocyte.roles;

import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.network.ServerPlayerEntity;

public interface PermissionAccessor {
PermissionAccessor INSTANCE = FabricLoader.getInstance().isModLoaded("fabric-permissions-api-v0") ? new FabricPermissionsV0() : new None();

boolean hasPermission(ServerPlayerEntity player, String permission);

final class None implements PermissionAccessor {
None() {
}

@Override
public boolean hasPermission(ServerPlayerEntity player, String permission) {
return false;
}
}

final class FabricPermissionsV0 implements PermissionAccessor {
FabricPermissionsV0() {
}

@Override
public boolean hasPermission(ServerPlayerEntity player, String permission) {
return Permissions.check(player, permission);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import xyz.nucleoid.leukocyte.roles.PermissionAccessor;
import xyz.nucleoid.leukocyte.roles.RoleAccessor;
import xyz.nucleoid.stimuli.filter.EventFilter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.UUID;
import java.util.*;

public final class ProtectionExclusions {
private static final Codec<UUID> UUID_CODEC = Codec.STRING.xmap(UUID::fromString, UUID::toString);

public static final Codec<ProtectionExclusions> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
Codec.STRING.listOf().fieldOf("roles").forGetter(exclusions -> new ArrayList<>(exclusions.roles)),
Codec.STRING.listOf().optionalFieldOf("permissions", Collections.emptyList()).forGetter(exclusions -> new ArrayList<>(exclusions.permissions)),
UUID_CODEC.listOf().fieldOf("players").forGetter(exclusions -> new ArrayList<>(exclusions.players)),
Codec.BOOL.fieldOf("include_operators").forGetter(exclusions -> exclusions.includeOperators)
).apply(instance, ProtectionExclusions::new);
Expand All @@ -29,15 +28,18 @@ public final class ProtectionExclusions {
private final Set<UUID> players;

private boolean includeOperators;
private Set<String> permissions;

public ProtectionExclusions() {
this.roles = new ObjectOpenHashSet<>();
this.permissions = new ObjectOpenHashSet<>();
this.players = new ObjectOpenHashSet<>();
}

private ProtectionExclusions(Collection<String> roles, Collection<UUID> players, boolean includeOperators) {
private ProtectionExclusions(Collection<String> roles, Collection<String> permissions, Collection<UUID> players, boolean includeOperators) {
this.roles = new ObjectOpenHashSet<>(roles);
this.players = new ObjectOpenHashSet<>(players);
this.permissions = new ObjectOpenHashSet<>(permissions);
this.includeOperators = includeOperators;
}

Expand All @@ -63,6 +65,10 @@ public boolean removeRole(String role) {
return this.roles.remove(role);
}

public boolean addPermission(String permission) { return this.permissions.add(permission); }

public boolean removePermission(String permission) { return this.permissions.remove(permission); }

public boolean addPlayer(GameProfile profile) {
return this.players.add(profile.getId());
}
Expand All @@ -86,12 +92,18 @@ public boolean isExcluded(PlayerEntity player) {
return true;
}
}

for (var excludePermission : this.permissions) {
if (PermissionAccessor.INSTANCE.hasPermission(serverPlayer, excludePermission)) {
return true;
}
}
}

return false;
}

public ProtectionExclusions copy() {
return new ProtectionExclusions(this.roles, this.players, this.includeOperators);
return new ProtectionExclusions(this.roles, this.permissions, this.players, this.includeOperators);
}
}
8 changes: 8 additions & 0 deletions src/main/java/xyz/nucleoid/leukocyte/rule/ProtectionRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public final class ProtectionRule {
public static final ProtectionRule CRAFTING = register("crafting");
public static final ProtectionRule HUNGER = register("hunger");
public static final ProtectionRule FALL_DAMAGE = register("fall_damage");
public static final ProtectionRule FIRE_DAMAGE = register("fire_damage");
public static final ProtectionRule FREEZING_DAMAGE = register("freezing_damage");
public static final ProtectionRule DAMAGE = register("damage");

public static final ProtectionRule THROW_ITEMS = register("throw_items");
public static final ProtectionRule PICKUP_ITEMS = register("pickup_items");
Expand All @@ -39,6 +42,11 @@ public final class ProtectionRule {
public static final ProtectionRule FLUID_FLOW = register("fluid_flow");
public static final ProtectionRule ICE_MELT = register("ice_melt");

public static final ProtectionRule SPAWN_ANIMALS = register("spawn_animals");
public static final ProtectionRule SPAWN_MONSTERS = register("spawn_monsters");

public static final ProtectionRule EXPLOSION = register("explosion");

private final String key;

ProtectionRule(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import net.minecraft.block.Blocks;
import net.minecraft.block.TntBlock;
import net.minecraft.entity.Entity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.mob.Monster;
import net.minecraft.entity.passive.AnimalEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
Expand All @@ -14,6 +17,7 @@
import xyz.nucleoid.stimuli.event.block.BlockDropItemsEvent;
import xyz.nucleoid.stimuli.event.block.BlockPlaceEvent;
import xyz.nucleoid.stimuli.event.block.BlockUseEvent;
import xyz.nucleoid.stimuli.event.entity.EntitySpawnEvent;
import xyz.nucleoid.stimuli.event.entity.EntityUseEvent;
import xyz.nucleoid.stimuli.event.item.ItemCraftEvent;
import xyz.nucleoid.stimuli.event.item.ItemPickupEvent;
Expand All @@ -40,7 +44,7 @@ public void applyTo(ProtectionRuleMap rules, EventRegistrar events) {
this.forRule(events, rules.test(ProtectionRule.ATTACK))
.applySimple(PlayerAttackEntityEvent.EVENT, rule -> (attacker, hand, attacked, hitResult) -> rule);

this.forRule(events, rules.test(ProtectionRule.ATTACK))
this.forRule(events, rules.test(ProtectionRule.PVP))
.applySimple(PlayerAttackEntityEvent.EVENT, rule -> (attacker, hand, attacked, hitResult) -> {
return attacked instanceof PlayerEntity ? rule : ActionResult.PASS;
});
Expand All @@ -56,11 +60,33 @@ public void applyTo(ProtectionRuleMap rules, EventRegistrar events) {
return (player, source, amount) -> source == DamageSource.FALL ? rule : ActionResult.PASS;
});

this.forRule(events, rules.test(ProtectionRule.FIRE_DAMAGE))
.applySimple(PlayerDamageEvent.EVENT, rule -> {
return (player, source, amount) -> source == DamageSource.IN_FIRE || source == DamageSource.ON_FIRE ? rule : ActionResult.PASS;
});

this.forRule(events, rules.test(ProtectionRule.FREEZING_DAMAGE))
.applySimple(PlayerDamageEvent.EVENT, rule -> {
return (player, source, amount) -> source == DamageSource.FREEZE ? rule : ActionResult.PASS;
});

this.forRule(events, rules.test(ProtectionRule.DAMAGE))
.applySimple(PlayerDamageEvent.EVENT, rule -> {
return (player, source, amount) -> !source.isOutOfWorld() ? rule : ActionResult.PASS;
});

this.forRule(events, rules.test(ProtectionRule.THROW_ITEMS))
.applySimple(ItemThrowEvent.EVENT, rule -> (player, slot, stack) -> rule);
this.forRule(events, rules.test(ProtectionRule.PICKUP_ITEMS))
.applySimple(ItemPickupEvent.EVENT, rule -> (player, entity, stack) -> rule);

this.forRule(events, rules.test(ProtectionRule.SPAWN_MONSTERS))
.applySimple(EntitySpawnEvent.EVENT, rule -> entity -> entity instanceof Monster ? rule : ActionResult.PASS);

this.forRule(events, rules.test(ProtectionRule.SPAWN_ANIMALS))
.applySimple(EntitySpawnEvent.EVENT, rule -> entity -> entity instanceof AnimalEntity ? rule : ActionResult.PASS);


this.applyWorldRules(rules, events);
}

Expand All @@ -81,6 +107,9 @@ private void applyBlockRules(ProtectionRuleMap rules, EventRegistrar events) {
}
};
});

this.forRule(events, rules.test(ProtectionRule.EXPLOSION))
.applySimple(ExplosionDetonatedEvent.EVENT, rule -> (explosion, particles) -> explosion.clearAffectedBlocks());
}

private void applyInteractionRules(ProtectionRuleMap rules, EventRegistrar events) {
Expand Down

0 comments on commit 5dccf4b

Please sign in to comment.