Solved StringList Config Need Help

Discussion in 'Plugin Development' started by xYourFreindx, May 27, 2014.

Thread Status:
Not open for further replies.
  1. What I'm trying to do here, is make it so in the config there is something like this
    Code:
    CameraMode:
        Enabled: true
        PlayersInCM:
            AreInvincible: true
            AreVanished: true
            CanUseCommands: true
            AllowedCommands:
            - camera
            - cameramode
            - reload
    
    And they can add onto it or remove whatever they want, and while they're vanished, they are only allowed to use the commands listed in the config.
    My problem, is that I have absolutely no idea how to get each individual string and check for it.
    I've got....
    Code:java
    1.  
    2.  
    3. @EventHandler
    4. public void onPlayerCommandPreProccess(PlayerCommandPreprocessEvent e) {
    5. if (getConfig().getBoolean("CameraMode.PlayersInMC.CanUseCommands") == false) {
    6. if (flyplayers.contains(e.getPlayer().getUniqueId().toString())) {
    7. for (String command : allowedcmds) {
    8. if (!(e.getMessage().contains(command))) {
    9. e.setCancelled(true);
    10. e.getPlayer().sendMessage(ChatColor.RED + "Don't gots the perms");
    11. }
    12. }
    13. }
    14. }
    15. }
    I still don't know how to utilize getConfig().getStringList or if that would even work with this...
    Any help would be greatly appreciated.
     
  2. Offline

    zakarls

    xYourFreindx
    You could try this if I have understood you right
    Code:java
    1. List<String> vanishcmds = plugin.getConfig().getStringList("VanishPlugin.AllowedCommands");
    2. for(String command : vanishcmds){
    3. if(!e.getMessage().contains(command)){
    4. event.setCancelled(true)
    5. }
    6. }
     
  3. zakarls
    I tried with that, and edited my previous post according to how it is now laid out.
    What happens now, is that I can do regular commands like /fly or /sethome, it tells me THREE TIMES "You aint got perms" and when I do /cameramode it tells me ONE TIME "You ain't got perms" and when i do /camera, it tells me TWICE "You ain't got perms" and it will let me do /cameramode reload for some reason... Even when it isn't in teh config.
    I'm so confused... -.-

    public List<String> allowedcmds = this.getConfig().getStringList("CameraMode.PlayersInCM.AllowedCommands");

    bump

    lenis0012 Seeing answers you gave on other related issues such as this, I thought you might be able to offer some help in my current problem.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  4. Offline

    lenis0012

    xYourFreindx
    Ok buddy :)
    use this code on enable:
    Code:java
    1. FileConfiguration config = this.getConfig();
    2. config.addDefault("CameraMode.Enabled", true);
    3. config.addDefault("CameraMode.PlayersInCm.AreInvincible", true);
    4. config.addDefault("CameraMode.PlayersInCm.AreVanished", true);
    5. config.addDefault("CameraMode.PlayersInCm.CanUseCommands", true);
    6. config.addDefault("CameraMode.PlayersInCm.AllowedCommands", Arrays.asList(new String[] { "camera", "cameramode", "reload" }));
    7. config.options().copyDefaults(true);
    8. this.saveConfig();


    and this to apply the config values.
    make sure the fields exist:
    Code:java
    1. FileConfiguration config = plugin.getConfig();
    2. this.cameraModeEnabled = config.getBoolean("CameraMode.Enabled");
    3. this.playersInvincible = config.getBoolean("CameraMode.PlayersInCm.AreInvincible");
    4. this.playersVanished = config.getBoolean("CameraMode.PlayersInCm.AreVanished");
    5. this.playersCanUseCommands = config.getBoolean("CameraMode.PlayersInCm.CanUseCommands");
    6. this.allowedCommands = config.getStringList("CamerMode.PlayersInCm.AllowedCommands");
     
  5. lenis0012 I've already handled the defaults. As far as I can tell, the config layout is fine.
    I don't, however, have the second code clipping you mentioned, and I'm not sure of WHY I would need it, and where I would put it.
     
  6. Offline

    lenis0012

    create the fields in your listener.
    And put the code in your constuctor.
     
  7. lenis0012 Still confused as to why it is necessary.
    Sorry if my noobishness pains you.
     
  8. Offline

    zakarls

    xYourFreindx When you are doing this
    Code:java
    1. if(!e.getMessage().contains(command))

    It checks if the message contains that word.
    So cameramode contains
    - camera
    - cameramode
    Since it contains those 2, it will go through the process twice therefore sending you 2 messages.
    I hope this helps :) if not, can you post some more of your code so maybe I can see what you are doing?
     
  9. zakarls
    Nope... Doesn't help....
    I still don't know what I should do to fix it. I know not of any alternatives. What else would you like to see?
    I've posted everything that is related to the issue I'm trying to solve. Perhaps it's just difficult to understand because of how I laid it out in the first post? Would you like me to repost it?
     
  10. Offline

    zakarls

    xYourFreindx
    Yeah. If you could post possibly that entire class or just the section where the messages are blocked. Your first message just appears to be a snippet of what is actually needed to fix the problem.
     
  11. @zakarls​
    My first message IS the entire area where the message is blocked...​
    But here, let me make it clearer...​
    Where​
    Code:java
    1.  
    2.  
    3. @EventHandler
    4. public void onPlayerCommandPreProccess(PlayerCommandPreprocessEvent e) {
    5. if (getConfig().getBoolean("CameraMode.PlayersInMC.CanUseCommands") == false) {
    6. if (flyplayers.contains(e.getPlayer().getUniqueId().toString())) {
    7. for (String commands : allowedcmds) {
    8. if (!(e.getMessage().contains(commands))) {
    9. e.setCancelled(true);
    10. e.getPlayer().sendMessage(ChatColor.RED + reason);
    11. }
    12. }
    13. }
    14. }
    15. }

    Is what's handling the message blocking.​
     
  12. Offline

    zakarls

    xYourFreindx
    Hi again :p. I know what is wrong but im not sure how to fix it. So ill tell you it. For every command the player does it goes through the config list and checks it. Since you have 3 config options it will check commands 3 times. If you do regular commands it will check the command 3 times and since all 3 are wrong, it will send you the message 3 times. If you do a correct one like /reload or /camera it will send you the message 2 times since the other 2 are not in the message. If you do /cameramode, since it contains /camera and /cameramode it contains 2/3 of the required parameters. So it will only send you the message once.
    It lets you do /cameramode reload because it contains all 3. /camera /cameramode and reload
     
  13. zakarls xD That's funny. That's really really funny actually. I don't think you grasp how hilarious that all is to me now that you've pointed that out...
    Doesn't help me fix it, still stuck with a broken config... But I'm glad you replied. It was very enlightening.
     
  14. Offline

    zakarls

    xYourFreindx
    AHA. Found a way to fix it :D
    Replace this
    Code:java
    1. for (String commands : allowedcmds) {
    2. if (!(e.getMessage().contains(commands))) {
    3. e.setCancelled(true);
    4. e.getPlayer().sendMessage(ChatColor.RED + reason);
    5. }
    6. }

    With This
    Code:java
    1. List<String> allowedcmds = plugin.getConfig().getStringList("CameraMode.PlayeraInMC.CanUseCommands");
    2. String cmd = allowedcmds.toString();
    3. String[] command = e.getMessage().split(" ");
    4. String use = command[0].replace("/", "");
    5. if(cmd.contains(use)){
    6. e.setCancelled(true);
    7. e.getPlayer().sendMessage(ChatColor.RED + reason);
    8. }

    Then test
     
  15. Offline

    zakarls

    xYourFreindx
    I lied. Well... made a mistake. Change this line in my code:
    Code:
    if(cmd.contains(use)){
    to
    Code:
    if(!cmd.contains(use)){
     
Thread Status:
Not open for further replies.

Share This Page