#Help with a command blocker plugin

1 messages · Page 1 of 1 (latest)

gentle mulch
#

ok

#

so

dapper iron
#

What are you trying to achieve?

#

I think im not in the picture

gentle mulch
#

just get support

dapper iron
#

Yeah but on what topic XD. Im not quite sure what you are trying to do

gentle mulch
#

hold on a sec

#

help with a command blocker plugin

dapper iron
#

What isnt working?

gentle mulch
#

i'm trying to get a command's permission by the command name

#

googling i found bukkit.getServer().getPluginCommand()

#

but it doesen't work with all the plugins in my server

#

i opened a plugin's plugin.yml file and i found all the plugin's permission

dapper iron
#
  @EventHandler
  public void onCommand(PlayerCommandPreprocessEvent event) {
    String commandName = ...;
    String permission =  Bukkit.getCommandMap().getCommand(commandName).getPermission();
  }
gentle mulch
#

that's what i did

#

and yet it returns null with most of my plugin when i try to get the permission

dapper iron
#

Then this command does not have a permission associated with it

#

So everyone can send this command

gentle mulch
#

i know it may seem sus

#

but

#

can you join my server (it's a public server)

#

with domain, discord and everything

dapper iron
#

mc server?

gentle mulch
#

yes

dapper iron
#

version?

gentle mulch
#

1.18.1

dapper iron
#

fine...

gentle mulch
#

and don't worry, we patched log4j

dapper iron
#

whats the command?

#

Wouldnt matter because i as a client have to patch it as well

gentle mulch
#

try to use a random command

dapper iron
#

right send me the domain then

gentle mulch
#

lemme add u to the whitelist

dapper iron
#

Gestankbratwurst

gentle mulch
#

i'm starting lunar

dapper iron
#

"maintenance"

gentle mulch
#

yeah

#

added

#

try to log in

#

i'm the admin

#

@dapper iron ayt?

dapper iron
#

Cant join ;D

gentle mulch
#

why?

dapper iron
gentle mulch
#

ok

#

try now

dapper iron
gentle mulch
#

yeah

dapper iron
#

...

#

one moment

gentle mulch
#

so?

#

am i trying to do something impossible?

dapper iron
#

This can be done in half the amount of lines

gentle mulch
#

i know

dapper iron
#

So you want to have a file where you can specify the permissions yourself? For all plugins?

gentle mulch
#

but i started coding in java 2 days ago

#

i have a config file

#

commandblocker:
enabled: true
allowed-cmds:
- "/example"
permission-blocked-commands:
# /command , permission
- "/example example.use"

dapper iron
#

why "permission-blocked-commands" and not just "blocked-commands"?

gentle mulch
#

allowed commands are line above that

dapper iron
#

Ok but why do you need the "permission-blocked-commands" section at all?

gentle mulch
#

it's another thing i was going to add

#

it's just a scratch

dapper iron
#

I dont get the purpose of it

gentle mulch
#

ok

#

but

#

for the other thing

dapper iron
#

Let me show you an example

gentle mulch
#

there is no way to get a command's permission by it's name?

#

unless i won't declare it by myself

dapper iron
#
public class CommandListener implements Listener {

  private static final String BYPASS_PERMISSION = "orientalcmds.cmd.bypass";

  private final List<String> allowedCommands = Main.getInstance().getConfig().getStringList("commandblocker.allowed-cmds");
  
  @EventHandler
  public void commandEvent(PlayerCommandPreprocessEvent event) {
    Player player = event.getPlayer();

    if (player.hasPermission(BYPASS_PERMISSION)) {
      return;
    }

    String[] content = event.getMessage().split(" ");
    String commandInput = content[0];

    if (!allowedCommands.contains(commandInput)) {
      // Handle disallowed command here
    }
  }

}
gentle mulch
#

in the class there was a custom message for two commands (/backpack and /claim) cause the were level blocked

#

(you had to reach a certain level to access them)

#

but ok

#

doing this i will block every command that is not in my whitelist

#

but what if there is some command i want my staff to use but not the players?

dapper iron
#
public class CommandListener implements Listener {

  private static final String BYPASS_PERMISSION = "orientalcmds.cmd.bypass";

  private final List<String> allowedCommands = Main.getInstance().getConfig().getStringList("commandblocker.allowed-cmds");
  // Put your level requirements in here
  private final Map<String, Integer> levelRequirements = new HashMap<>();

  @EventHandler
  public void commandEvent(PlayerCommandPreprocessEvent event) {
    Player player = event.getPlayer();

    if (player.hasPermission(BYPASS_PERMISSION)) {
      return;
    }

    String[] content = event.getMessage().split(" ");
    String commandInput = content[0];

    int playerLevel = player.getLevel();
    int requiredLevel = levelRequirements.getOrDefault(commandInput, 0);

    if (!allowedCommands.contains(commandInput)) {
      // Handle disallowed command here
    }

    if (playerLevel < requiredLevel) {
      // Handle players level is too low
    }
    
  }

}
gentle mulch
#

for the level thing, don't worry

#

it was working

gentle mulch
#

considering that they're both deopped

dapper iron
#

one moment

#
public class CommandListener implements Listener {

  private static final String BYPASS_PERMISSION = "orientalcmds.cmd.bypass";

  private final List<String> allowedCommands = Main.getInstance().getConfig().getStringList("commandblocker.allowed-cmds");
  // Put your level threshold in here
  private final Map<String, Integer> levelRequirements = new HashMap<>();
  // Put your command bypasses in here
  private final Map<String, String> commandBypasses = new HashMap<>();

  @EventHandler
  public void commandEvent(PlayerCommandPreprocessEvent event) {
    Player player = event.getPlayer();

    String[] content = event.getMessage().split(" ");
    String commandInput = content[0];

    String commandBypassPerm = commandBypasses.get(commandInput);

    if (player.hasPermission(BYPASS_PERMISSION) || (commandBypassPerm != null && player.hasPermission(commandBypassPerm))) {
      return;
    }

    int playerLevel = player.getLevel();
    int requiredLevel = levelRequirements.getOrDefault(commandInput, 0);

    if (!allowedCommands.contains(commandInput)) {
      // Handle disallowed command here
    }

    if (playerLevel < requiredLevel) {
      // Handle players level is too low
    }

  }

}
gentle mulch
#

what if i put in my config file another whitelist for staff commands

dapper iron
#
public class CommandListener implements Listener {

  private static final String BYPASS_PERMISSION = "orientalcmds.cmd.bypass";

  private final List<String> allowedCommands = Main.getInstance().getConfig().getStringList("commandblocker.allowed-cmds");
  // Put your level threshold in here
  private final Map<String, Integer> levelRequirements = new HashMap<>();
  // Put your command bypasses in here
  private final Map<String, String> commandBypasses = new HashMap<>();

  @EventHandler
  public void commandEvent(PlayerCommandPreprocessEvent event) {
    Player player = event.getPlayer();

    String[] content = event.getMessage().split(" ");
    String commandInput = content[0];

    String commandBypassPerm = commandBypasses.get(commandInput);

    if (player.hasPermission(BYPASS_PERMISSION) || (commandBypassPerm != null && player.hasPermission(commandBypassPerm))) {
      return;
    }

    int playerLevel = player.getLevel();
    int requiredLevel = levelRequirements.getOrDefault(commandInput, 0);

    if (!allowedCommands.contains(commandInput)) {
      player.sendMessage("You dont have the required permission");
      event.setCancelled(true);
      return;
    }

    if (playerLevel < requiredLevel) {
      player.sendMessage(String.format("You need at least level %d for this command", requiredLevel));
      event.setCancelled(true);
    }

  }

}
gentle mulch
#

ok thx

dapper iron
gentle mulch
#

my plugin would block commands

dapper iron
#

Yes but give them your custom commands

gentle mulch
#

if they're not in the whitelist

dapper iron
#

why?

gentle mulch
#

that other list

#

private final Map<String, String> commandBypasses = new HashMap<>();

dapper iron
#

Oh i see...

gentle mulch
#

so i have to use: private final List<String> allowedCommands = Main.getInstance().getConfig().getStringList("commandblocker.allowed-cmds");

#

to get from the config file the staff commands whitelist

#

so players cant execute and see staff commands

#

but staff can execute and see them without any problem

dapper iron
#

just give your staff the bypass persmissions for those commands then

gentle mulch
#

if i give bypass even a trainee can see every command

#

including plugins

dapper iron
#

private final Map<String, String> commandBypasses = new HashMap<>();
This map contains for example:

"/help" - "oriental.command.bypass.help"
"/spawn" - "oriental.command.bypass.spawn"

Then you can just give your staff those two permissions

gentle mulch
#

ok, so give bypass permission for singular commands

dapper iron
#

Yes thats what the map is for. There is a "master" bypass permission (the constant) and there is also a bypass permission for every command

gentle mulch
#

ok, but i want to be able to modify them whitout having to modify the source code if that's possibile

dapper iron
#

Yes. Just read the maps from a config

gentle mulch
#

i can loop my whitelist in the config file for the command names

#

and create a permission with that name

dapper iron
#

Your config should look a bit like this:

blocked-commands:
  - "help"
  - "spawn"
command-bypass-permissions:
  help: "oriental.command.bypass.help"
  spawn: "oriental.command.bypass.spawn"
command-level-requirements:
  backpack: 10
  autocraft: 20
#

You can also scrap the "command-bypass-permissions" and just generate them using the command

gentle mulch
#

i was thinking like

dapper iron
#
blocked-commands:
  - "help"
  - "spawn"
command-level-requirements:
  backpack: 10
  autocraft: 20
gentle mulch
#

like that

#

ok thanks

dapper iron
#
  private static final String BYPASS_PERMISSION = "orientalcmds.cmd.bypass";

  private final List<String> allowedCommands;
  private final Map<String, Integer> levelRequirements = new HashMap<>();
  private final Map<String, String> commandBypasses = new HashMap<>();

  public CommandListener() {
    allowedCommands = Main.getInstance().getConfig().getStringList("commandblocker.allowed-cmds");
    for (String command : allowedCommands) {
      String bypassPerm = "oriental.command.bypass." + command.substring(1);
      commandBypasses.put(command, bypassPerm);
    }
    // Fill level req map here
  }
gentle mulch
#

also

#

could you send an example of how to decleare an hasmap?

#

nvm, i'll google it

#

thanks

gentle mulch
#

@dapper iron thx again, it works