123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package me.km.permissions;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.UUID;
- import me.kcm.command.ICommandManager;
- import me.km.KajetansMod;
- import me.km.api.Module;
- import me.km.snuviscript.ScriptEvents;
- import net.minecraft.command.ICommand;
- import net.minecraft.command.ICommandSender;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.server.MinecraftServer;
- import net.minecraft.util.text.TextComponentString;
- import net.minecraft.util.text.TextFormatting;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- @SideOnly(Side.SERVER)
- public class PermissionManager extends Module implements ICommandManager
- {
- private final HashMap<UUID, ArrayList<Integer>> playerGroups;
- private final ArrayList<HashSet<String>> stringGroupPerms;
-
- public PermissionManager(String mname, String prefix, TextFormatting color)
- {
- super(mname, prefix, color);
- playerGroups = new HashMap<>();
- stringGroupPerms = new ArrayList<>();
- }
- public void clear()
- {
- playerGroups.clear();
- stringGroupPerms.clear();
- }
-
- // -------------------------------------------------------------------------
- // Permission-Check
- // -------------------------------------------------------------------------
-
- @Override
- public boolean hasPermission(ICommandSender cs, String perm)
- {
- if(KajetansMod.debugMode)
- {
- return true;
- }
- if(cs instanceof EntityPlayer)
- {
- ArrayList<Integer> groups = playerGroups.get(((EntityPlayer) cs).getUniqueID());
- if(groups == null)
- {
- //falling back to default group
- if(!stringGroupPerms.isEmpty())
- {
- return stringGroupPerms.get(0).contains(perm);
- }
- else
- {
- return false;
- }
- }
- // check for valid group id is done at the registry
- return groups.stream().anyMatch(i -> stringGroupPerms.get(i).contains(perm));
- }
- return cs instanceof MinecraftServer;
- }
- // -------------------------------------------------------------------------
- // Permission-Registry
- // -------------------------------------------------------------------------
-
- public void registerPlayerGroup(UUID uuid, int id)
- {
- if(id < 0 || id >= stringGroupPerms.size())
- {
- throw new IllegalArgumentException("'" + id + "' is no valid group id");
- }
- ArrayList<Integer> groups = playerGroups.get(uuid);
- if(groups == null)
- {
- groups = new ArrayList<>();
- // adding default group
- groups.add(0);
- playerGroups.put(uuid, groups);
- }
- groups.add(id);
- }
-
- public void registerGroupPermission(int id, String perm)
- {
- if(perm.isEmpty())
- {
- throw new IllegalArgumentException("empty permission string");
- }
- if(id >= stringGroupPerms.size())
- {
- HashSet<String> set = new HashSet<>();
- set.add(perm);
- stringGroupPerms.add(set);
- return;
- }
- stringGroupPerms.get(id).add(perm);
- }
-
- // -------------------------------------------------------------------------
- // command manager
- // -------------------------------------------------------------------------
-
- @Override
- public boolean executeCustomCommand(ICommandSender cs, String command, String[] args)
- {
- System.out.println(command + " " + String.join(" ", args));
- if(cs instanceof EntityPlayer && KajetansMod.scripts.isRegisteredScriptCommand(command))
- {
- KajetansMod.scripts.getEvent(ScriptEvents.class).onCustomCommand((EntityPlayer) cs, command, args);
- return true;
- }
- return false;
- }
- @Override
- public void printMissingCommand(ICommandSender cs, String command)
- {
- cs.sendMessage(new TextComponentString("Keine Command " + command));
- }
- @Override
- public void printMissingPermission(ICommandSender cs, ICommand command)
- {
- cs.sendMessage(new TextComponentString("Keine Perms für " + command.getName()));
- }
- }
|