Solved i made something wrong by using a Boolean

Discussion in 'Spigot Help' started by MrGermanLP1000, Jan 12, 2019 at 12:16 PM.

  1. The Plugin perform a command if a user is joining this server.
    Now i want that i can enable and disable it.

    But i dont know why, it doesn´t work. so i ask here now.
    Sry for my Bad Englisch too :)

    Here are the Importent Classes:

    Class where the command for enable and disable and the join event are in:

    Code (Text):
    package main.events;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import main.Configs;

    public class Joincmd implements Listener, CommandExecutor{
       
        boolean allows = true;        /*Configs.config.getBoolean("Joinaktiv");*/
        /*public String prf = Configs.messagescfg.getString("Prefix").replaceAll("&", "§");*/
        public String prf = "tst ";
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            if(allows){
                if (p.hasPermission(Configs.messagescfg.getString("JoinCommandRechte").replaceAll("&", "§")))
                {
                  p.performCommand(Configs.messagescfg.getString("JoinCommand"));
                } else {
                  p.performCommand(Configs.messagescfg.getString("JoinSpawnCommand"));
                }
            } else if(!allows){
                Bukkit.broadcastMessage("tst");
            }
        }

        public boolean onCommand(CommandSender sender, Command command, String Lable, String[] args) {
           
            if ((Lable.equalsIgnoreCase("JoinCMD")) && (sender.hasPermission(Configs.messagescfg.getString("JoinCommandEdit")))) {
               
                if(allows == true) {
                   
                    sender.sendMessage("off");
                    allows = false;
                   
                   
                   
                } else if(allows == false) {
                   
                    sender.sendMessage("on");
                    allows = true;
                   
                }
               
               
                }
           
            return true;
        }
    }
     
     
    #1 MrGermanLP1000, Jan 12, 2019 at 12:16 PM
    Last edited: Jan 12, 2019 at 12:43 PM
  2. Could you put your code in the
    Code (Text):
    [code]
    tags? Hastebin doesn't seem to load for me... :confused:
     
  3. i updatet it with code
     
  4. I also tried it with an int now so when the int is 1 its on and on 2 its off and it still not work
     
  5. You haven't even told us anything... just "it doesn't work" is not much to go on
     
  6. when a guy joins the server the guy should automaticly use a command, and i want that the automaticly progress can be aktivated and deactivated per command. The Problem is that its activated all the time by using it with the code i posted before


    ---
    Sry for my englisch
     
  7. humm, sure that if you set allows to true, it will always activate it..
     
  8. Yeah and the part with the messages works correctly so the messages switch when the boolean change from true to false or false to true.
    The only thing who is not working is that the automatic join command is not deactivated.

    I Mean the messages at the command so if i perform the command /joincmd i get the message for true and for false correctly.
     
  9. ow okay, so you'll have to always use allows from your configuration file because using it hard coded could have side effects (on reload per exemple)
    Code (Text):

    boolean isAllowed() { return Configs.config.getBoolean("Joinaktiv"); }
    void setAllowed(boolean allow) {
        Configs.config.set("Joinaktiv", allow);
        // and finally save your configuration ;)
    }
    should be okay ^^'
     
  10. Sorry but my Brain is sludge right now i search like 2 or 3 Days for this Problem so
    i understand the reload part and saw it as i tested the plugin before.
    And the code you write i understand too but the problem i have now is, that the "config" is not called config its atm called messagescfg and messages so i maybe have to change the code you write before.
    To understand easier what i mean here the joincmd, messages and the config classes.

    newest joincmd:
    Code (Text):
    package main.events;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import main.Configs;

    public class Joincmd implements Listener, CommandExecutor{
     
        boolean allows = true;

        public String prf = Configs.messagescfg.getString("Prefix").replaceAll("&", "§");
     
        @EventHandler
        public void onJoin1(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            if(allows) {
                p.performCommand("help");
            } else if(!allows){
                p.sendMessage(Configs.messagescfg.getString("KeineRechte").replaceAll("&", "§"));
            }
        }
     

        @Override
        public boolean onCommand(CommandSender sender, Command command, String Label, String[] args) {
            if(Label.equalsIgnoreCase("JoinCMD")) {
                if(sender.hasPermission(Configs.messagescfg.getString("JoinCommandEdit"))) {
                    if(allows) {
                        sender.sendMessage(prf + Configs.messagescfg.getString("JoinCommandDeaktivert").replaceAll("&", "§"));
                        allows = false;
                    } else if(!allows){
                        sender.sendMessage(prf + Configs.messagescfg.getString("JoinCommandAktiv").replaceAll("&", "§"));
                        allows = true;
                    }
                } else {
                    sender.sendMessage(prf + Configs.messagescfg.getString("KeineRechte").replaceAll("&", "§"));
                }
            }
            return true;
        }
    }
     
    messages:
    Code (Text):
    package main;

    import org.bukkit.event.Listener;

    public class Mess implements Listener
    {
          public static void loadMessages()
          {
            if (!Configs.messages.exists())
            {
                /*     Messages   */
              Configs.messagescfg.set("Prefix", "&6&l&oERA&r&7Extra &7&l>&r ");
              Configs.messagescfg.set("JoinCommandAktiv", "&7Der Command beim &aJoinen &7 wurde &2Aktiviert.");
              Configs.messagescfg.set("JoinCommandDeaktivert", "&7Der Command beim &aJoinen &7 wurde &4Deaktiviert.");
              Configs.messagescfg.set("KeineRechte", "&7Du hast nicht genug &aRechte &7 um diesen Command auszuführen.");
           
                  /*     Commands   */
              Configs.messagescfg.set("JoinCommand", "Help");
              Configs.messagescfg.set("JoinSpawnCommand", "Spawn");
           
                  /*     Rechte     */
              Configs.messagescfg.set("JoinCommandRechte", "Beispiel.Recht");
              Configs.messagescfg.set("JoinCommandEdit", "Beispiel.Recht");
           
                  /* Ja/Nein Abfrage */
              /*Configs.messagescfg.set("Joinaktiv", "1");*/
              try
              {
                Configs.messagescfg.save(Configs.messages);
              }
              catch (Exception e)
              {
                e.printStackTrace();
              }
            }
          }
        }
    Config:
    Code (Text):
    package main;

    import java.io.File;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.Listener;

    public class Configs implements Listener {
          public static File messages = new File("plugins/ERAExtra", "messages.yml");
          public static FileConfiguration messagescfg = YamlConfiguration.loadConfiguration(messages);
    }
     
    #10 MrGermanLP1000, Jan 12, 2019 at 7:03 PM
    Last edited: Jan 12, 2019 at 7:54 PM
  11. Strahan

    Benefactor

    It sounds like you're making it harder than it needs to be. As mentioned before, you'd want to use your config to store this so state will persist across reloads. Just make a variable you use to reference whether or not to do your special login thing, load it from config when the server starts and when you toggle it with your command flush back to disk.

    Here is an example where I show the same functionality. It's a testing plugin so it's all one class, but you get the idea.

     
    • Informative Informative x 1
  12. Thank you so much the Hard Part worked now :)
    but now i have the problem that i cant save it so easy like you with: saveConfig(); because the command is not in the main class.

    But the main problem is fixxed so i will close this thread :)
     
    #12 MrGermanLP1000, Jan 13, 2019 at 12:56 PM
    Last edited: Jan 13, 2019 at 3:16 PM
  13. Strahan

    Benefactor

    You can use dependency injection to pass an instance of the main class to the class you need it in. You can also make a static instance in the main class itself then reference back to that, though people typically harp on avoiding static. Personally, I like dependency injection. Works fine for me. Example:

    Code (Text):
    public class Main extends JavaPlugin {
      public void onEnable() {
        PluginManager pm = getServer().getPluginManager();
        pm.registerEvents(new MyListenerClass(this), this);
      }
    }

    public class MyListenerClass implements Listener {
      Main plugin;

      public MyListenerClass(Main plugin) {
        this.plugin = plugin;
      }

      @EventHandler
      public void onPlayerJoin(PlayerJoinEvent e) {
        plugin.getLogger().info("Someone joined!");
      }
    }
     
  14. does it work when i use the message.yml not the config.yml? i mean the saveConfig(); part?
     
  15. Strahan

    Benefactor

    Well, you'd have to use whatever config management you rolled, not the built in then.
     

Share This Page