Solved Problem with plugin toggle command

Discussion in 'Plugin Development' started by BushGamingYT, Jan 8, 2019.

  1. Offline

    BushGamingYT

    Basically, I have made a plugin, but the way I made it toggleable changes it for the whole server instead of per player and I was wondering if something could help me with changing it to per player
    Code:
    package com.EggThrow.plugin;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    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.PlayerEggThrowEvent;
    import org.bukkit.plugin.PluginManager;
    
    public class Main extends JavaPlugin implements Listener {
      
        @Override
        public void onEnable() {
            org.bukkit.Bukkit.getPluginManager().registerEvents(this, this);
        }
      
        public boolean ChickenToggled = true;
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equals("ChickenToggle")) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    if (ChickenToggled = true) {
                        ChickenToggled = false;
                        player.sendMessage(ChatColor.GOLD + "Chicken has been Toggled off");
                    }else {
                        ChickenToggled = true;
                        player.sendMessage(ChatColor.GOLD + "Chicken has been Toggled on");
                    }
                }else {
                    System.out.println("You can't throw eggs your not a player");
                }
            }
      
            return false;
        }
    
        @EventHandler
        public void onThrow(PlayerEggThrowEvent e) {
            if (ChickenToggled = true) {
                Player player = e.getPlayer();
                player.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "" + ChatColor.BOLD + "EGG!");
            }
        }
    }
    
    someone told me to store data in a map and send this
    Code:
    // you have to use a Map to store the data.
    Map<String, Boolean>
    // to store the boolean or replace it use
    map.set(playername, boolean)
    // to get the boolean stored use
    map.get(playername)
    
    but I'm new to java and I don't know where to put them or how to use them, could someone please edit my code to use the map thing and make it toggleable. I'll put a version with no chickentoggle = true/false below

    Code:
    package com.EggThrow.plugin;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    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.PlayerEggThrowEvent;
    import org.bukkit.plugin.PluginManager;
    
    public class Main extends JavaPlugin implements Listener {
      
        @Override
        public void onEnable() {
            org.bukkit.Bukkit.getPluginManager().registerEvents(this, this);
        }
      
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equals("ChickenToggle")) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
            }else {
                System.out.println("You can't throw eggs your not a player");
            }
      
            return false;
        }
    
        @EventHandler
        public void onThrow(PlayerEggThrowEvent e) {
            Player player = e.getPlayer();
            player.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "" + ChatColor.BOLD + "EGG!");
        }
    }
    
     
    Last edited: Jan 8, 2019
  2. Offline

    The_Spaceman

    you can make a static HashMap<UUID, Boolean> and put it in your main class

    With map.getOrDefault(player.getUUID(), false /*default value for of the player does not exist in the mao*/) you get the boolean.
     
  3. Offline

    BushGamingYT

    I have no clue where i should put it and how to make the command remember the players uuid and if that command is toggled true/false, could you show me where to put it in the bottem code box please
     
  4. Offline

    DerDonut

    So you initialize the map for example like:
    HashMap<String, Boolean> chickenToggle = new HashMap<String, Boolean>();
    (You can decide if you want to store the player as an player object or by his name)

    and then basically you just have to replace:
    Code:Java
    1. if(ChickenToggled == true) {
    2. //by
    3. if(chickenToggle .get(player.getName() == true) {
    4. //and
    5. ChickenToggled = true;
    6. //by
    7. chickenToggle .set(player.getName(), true);


    also note that to compare objects you have to use '==' instead of '='. The last one is used to assign values
     
    Last edited: Jan 8, 2019
  5. Offline

    BushGamingYT

    Omg thank you so much, iv been looking online for ages but never knew what to replace, also I was ameant to put 2 but must of forgot xD
     
  6. Offline

    The_Spaceman

    this should be:
    if(chickenToggle.getOrDefault(player.getName(), false /*default value if player is not registered in the map*/)) {

    the first one does not work (forgotten ')'), and == true is the same as not comparing it... its the same
    and when a player is not in the map it returns null, and that will give you an error
     
  7. Offline

    knokko

    Also, you should use
    Code:
    if (someBooleanValue) {
        ....
    }
    instead of
    Code:
    if (someBooleanValue == true) {
        ....
    }
     
  8. Offline

    torpkev

    @knokko - While I don't disagree with you, this is really just a readability issue and does not alter functionality, right?
     
  9. Offline

    The_Spaceman

    you check 'true == true', it may be more efficient to do just 'someBoolean', but for a command you should not notice it

    it is also possible that the compiler edits it to 'someBoolean' from 'someBoolean == true', but I don't know that
     
  10. Offline

    BushGamingYT

    how do I decide if it stores as a player object or by his name?
    Code:
    package com.EggThrow.plugin;
    
    import java.util.HashMap;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    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.PlayerEggThrowEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
       
        @Override
        public void onEnable() {
            org.bukkit.Bukkit.getPluginManager().registerEvents(this, this);
        }
       
        HashMap<String, Boolean> chickenToggle = new HashMap<String, Boolean>();
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equals("ChickenToggle")) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    if(chickenToggle.getOrDefault(player.getName(), true)) {
                        chickenToggle.set(player.getName(), false);
                        player.sendMessage(ChatColor.GOLD + "Chicken has been Toggled off");
                    }else {
                        chickenToggle.set(player.getName(), true);
                        player.sendMessage(ChatColor.GOLD + "Chicken has been Toggled on");
                    }
                }else {
                    System.out.println("You can't throw eggs your not a player");
                }
            }
       
            return false;
        }
        @EventHandler
        public void onThrow(PlayerEggThrowEvent e) {
            Player player = e.getPlayer();
            if(chickenToggle.getOrDefault(player.getName(), true)) {
                player.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "" + ChatColor.BOLD + "EGG!");
            }
        }
    }
    
    
    it's either 1, I keep getting an null error in console when I type /chickentoggle and says "An internal error occured while attempting to perform this command"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 8, 2019
  11. Offline

    knokko

    @BushGamingYT

    We need to see the console of your server. The error should be in there
     
  12. Offline

    BushGamingYT

    I think i fixed it
     

Share This Page