Skip to content

Commit

Permalink
Merge pull request #7 from PotatoPresident/dev
Browse files Browse the repository at this point in the history
Permission Api
  • Loading branch information
PotatoPresident authored Jan 5, 2021
2 parents bb44d78 + 759a25f commit c318257
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repositories {
name = "NerdHubMC"
url = "https://maven.abusedmaster.xyz/"
}
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}

sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -34,6 +35,8 @@ dependencies {

modImplementation "com.github.emilyploszaj:trinkets:${project.trinkets_version}"

modImplementation "me.lucko:fabric-permissions-api:0.1-SNAPSHOT"
include "me.lucko:fabric-permissions-api:0.1-SNAPSHOT"
}

processResources {
Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.16.2
yarn_mappings=1.16.2+build.26
loader_version=0.9.1+build.205
minecraft_version=1.16.4
yarn_mappings=1.16.4+build.7
loader_version=0.10.8
# Mod Properties
mod_version=1.2.0
mod_version=1.3.0
maven_group=us.potatoboy
archives_base_name=InvView
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.18.0+build.397-1.16
fabric_version=0.29.3+1.16

trinkets_version=2.6.7
6 changes: 5 additions & 1 deletion src/main/java/us/potatoboy/invview/InvView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package us.potatoboy.invview;

import com.mojang.brigadier.tree.LiteralCommandNode;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand Down Expand Up @@ -32,23 +33,26 @@ public void onInitialize() {

LiteralCommandNode<ServerCommandSource> viewNode = CommandManager
.literal("view")
.requires((source -> source.hasPermissionLevel(2)))
.requires(Permissions.require("invview.view", 2))
.build();

LiteralCommandNode<ServerCommandSource> invNode = CommandManager
.literal("inv")
.requires(Permissions.require("invview.inv", 2))
.then(CommandManager.argument("target", GameProfileArgumentType.gameProfile())
.executes(ViewCommand::inv))
.build();

LiteralCommandNode<ServerCommandSource> echestNode = CommandManager
.literal("echest")
.requires(Permissions.require("invview.echest", 2))
.then(CommandManager.argument("target", GameProfileArgumentType.gameProfile())
.executes(ViewCommand::eChest))
.build();

LiteralCommandNode<ServerCommandSource> trinketNode = CommandManager
.literal("trinkets")
.requires(Permissions.require("invview.trinket", 2))
.then(CommandManager.argument("target", GameProfileArgumentType.gameProfile())
.executes(ViewCommand::trinkets))
.build();
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/us/potatoboy/invview/ViewCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.emi.trinkets.api.TrinketComponent;
import dev.emi.trinkets.api.TrinketSlots;
import dev.emi.trinkets.api.TrinketsApi;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.command.argument.GameProfileArgumentType;
import net.minecraft.entity.passive.AbstractDonkeyEntity;
import net.minecraft.entity.passive.HorseBaseEntity;
Expand All @@ -21,6 +22,8 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText;
import us.potatoboy.invview.gui.EnderChestScreenHandler;
import us.potatoboy.invview.gui.PlayerInventoryScreenHandler;
import us.potatoboy.invview.gui.TrinketScreenHandler;
Expand All @@ -36,6 +39,10 @@ public static int inv(CommandContext<ServerCommandSource> context) throws Comman
ServerPlayerEntity player = context.getSource().getPlayer();
ServerPlayerEntity requestedPlayer = getRequestedPlayer(context);

if (isProtected(context, requestedPlayer)) {
return -1;
}

NamedScreenHandlerFactory screenHandlerFactory = new SimpleNamedScreenHandlerFactory((syncId, inv, playerEntity) ->
new PlayerInventoryScreenHandler(syncId, player.inventory, requestedPlayer.inventory),
requestedPlayer.getDisplayName()
Expand All @@ -51,6 +58,10 @@ public static int eChest(CommandContext<ServerCommandSource> context) throws Com
ServerPlayerEntity requestedPlayer = getRequestedPlayer(context);
EnderChestInventory requestedEchest = requestedPlayer.getEnderChestInventory();

if (isProtected(context, requestedPlayer)) {
return -1;
}

player.openHandledScreen(new SimpleNamedScreenHandlerFactory((syncId, inv, playerEntity) ->
new EnderChestScreenHandler(syncId, player.inventory, requestedEchest, 3, requestedPlayer),
requestedPlayer.getDisplayName()
Expand Down Expand Up @@ -84,6 +95,10 @@ public static int trinkets(CommandContext<ServerCommandSource> context) throws C
ServerPlayerEntity player = context.getSource().getPlayer();
ServerPlayerEntity requestedPlayer = getRequestedPlayer(context);

if (isProtected(context, requestedPlayer)) {
return -1;
}

player.openHandledScreen(new SimpleNamedScreenHandlerFactory((syncId, inv, player1) ->
new TrinketScreenHandler(syncId, player.inventory, requestedPlayer),
requestedPlayer.getDisplayName()
Expand All @@ -102,4 +117,13 @@ private static ServerPlayerEntity getRequestedPlayer(CommandContext<ServerComman

return requestedPlayer;
}

private static boolean isProtected(CommandContext<ServerCommandSource> context, ServerPlayerEntity requested) throws CommandSyntaxException {
if (Permissions.check(requested, "invview.protected")) {
context.getSource().sendError(new LiteralText("Requested inventory is protected"));
return true;
}

return false;
}
}

0 comments on commit c318257

Please sign in to comment.