#Help with a command blocker plugin
1 messages · Page 1 of 1 (latest)
just get support
Yeah but on what topic XD. Im not quite sure what you are trying to do
What isnt working?
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
@EventHandler
public void onCommand(PlayerCommandPreprocessEvent event) {
String commandName = ...;
String permission = Bukkit.getCommandMap().getCommand(commandName).getPermission();
}
that's what i did
and yet it returns null with most of my plugin when i try to get the permission
Then this command does not have a permission associated with it
So everyone can send this command
i know it may seem sus
but
can you join my server (it's a public server)
with domain, discord and everything
mc server?
yes
version?
1.18.1
fine...
and don't worry, we patched log4j
try to use a random command
i know
right send me the domain then
Gestankbratwurst
i'm starting lunar
"maintenance"
Cant join ;D
why?
This can be done in half the amount of lines
i know
So you want to have a file where you can specify the permissions yourself? For all plugins?
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"
why "permission-blocked-commands" and not just "blocked-commands"?
allowed commands are line above that
Ok but why do you need the "permission-blocked-commands" section at all?
I dont get the purpose of it
Let me show you an example
there is no way to get a command's permission by it's name?
unless i won't declare it by myself
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
}
}
}
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?
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
}
}
}
.
considering that they're both deopped
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
}
}
}
what if i put in my config file another whitelist for staff commands
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);
}
}
}
ok thx
You dont need to. Just give your staff the permissions you want
my plugin would block commands
Yes but give them your custom commands
if they're not in the whitelist
why?
that other list
private final Map<String, String> commandBypasses = new HashMap<>();
Oh i see...
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
just give your staff the bypass persmissions for those commands then
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
ok, so give bypass permission for singular commands
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
ok, but i want to be able to modify them whitout having to modify the source code if that's possibile
Yes. Just read the maps from a config
i can loop my whitelist in the config file for the command names
and create a permission with that name
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
i was thinking like
blocked-commands:
- "help"
- "spawn"
command-level-requirements:
backpack: 10
autocraft: 20
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
}
also
could you send an example of how to decleare an hasmap?
nvm, i'll google it
thanks
@dapper iron thx again, it works