Solved Trying to enable an event with Commands

Discussion in 'Spigot Plugin Development' started by NeverFlame42, Jun 25, 2021.

  1. You are still lacking the
    Code (Java):
    implements CommandExecutor, Listener
    part in your class declaration
     
    • Like Like x 1
  2. Strahan

    Benefactor

    Ahhh OK, thanks :)
     

  3. So, while testing this out, I got it to work! However, there is a down side. I made the boolean false and whenever /vampireenable is typed in, it becomes true. The Event Handler then activates giving the effects. Now for the down side. I also made a /vampiredisable command to set the boolean back to false to turn off the effects, but it doesn't work. In game, it just enables it again.
     
  4. Post your DarkEnable code.
     
  5. here ya go:

    Code (Java):
    package me.flame.darkness;

    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    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.PlayerMoveEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class DarkEnable implements CommandExecutor, Listener {
        private Main main;
        private boolean check = false;

        public DarkEnable(){
            this.main = main;
        }


        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(sender instanceof Player){
                Player player = (Player) sender;

                if(player.isOp() || player.hasPermission("vampireenable")){

                    check = true;

                    player.sendMessage(ChatColor.GREEN + "You are now a reverse vampire!");

                    return true;
                }
                else if ((player.isOp() || player.hasPermission("vampiredisable"))){

                    check = !true;

                    player.sendMessage(ChatColor.GREEN + "You are no longer a reverse vampire!");
                    return false;
                }else {
                    player.sendMessage(ChatColor.RED + "You don't have permission to use this command!");
                    return true;
                }

            }
            else {
                main.getLogger().info("You have to be a player to become a reverse vampire!");
                return true;
            }
        }



        @EventHandler
        public void onEnterDark(PlayerMoveEvent event) {
            Block block = event.getTo().getBlock();
            int lightLevel = block.getLightLevel();

            if (check == !false) {
                if (lightLevel < 8) {
                    event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 60, 2));
                    event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 60, 1));
                    event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 60, 1));
                    event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 60, 1));
                } else if (lightLevel == 15) {
                    event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SATURATION, 120, 4));
                    event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 120, 1));
                }
            }
        }

    }
     
     
  6. Your if else statement only executes the first branch where the conditions are met. If a player has the permission vampireenable, then the first branch of the statement is the only one that will run, even if they have the vampiredisable permission. I can think of two options to fix this.
    1. Make the command toggle the boolean rather than set it specifically (do the command, if it's false it becomes true, if it's true it becomes false, then you print a message to the sender so they know what the current state of the boolean is).
    2. Have the command be /vampire and check the first argument in the argument array for "enable" or "disable".
     
  7. Ok, so I see more than one serious programming mistake, I'd suggest you go study some java/programming, there are tons of videos/books/tutorials/documentation out there that can help you learn programming and help you prevent these mistakes in the future
     
    • Agree Agree x 1
  8. So, i got the toggle to work and it's work fine, but now the potion effects won't activate:

    Code (Java):
    public class DarkEnable implements CommandExecutor, Listener {
        private Main main;
        private boolean check = false;

        public DarkEnable(){
            this.main = this.main;
        }


        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(sender instanceof Player){
                Player player = (Player) sender;
                //check = false;//remove after working

                if(player.hasPermission("vampire")){

                    if (check == false){
                        player.sendMessage(ChatColor.GREEN + "You are now a reverse vampire!");
                    }else{
                        player.sendMessage(ChatColor.GREEN + "You are no longer a reverse vampire!");
                    }

                    if (check == false){
                        check = true;
                    }else {
                        check = false;
                    }

                    return true;

                }else {
                    player.sendMessage(ChatColor.RED + "You don't have permission to use this command!");
                    return true;
                }

            }
            else {
                main.getLogger().info("You have to be a player to become a reverse vampire!");
                return true;
            }
        }


        @EventHandler
        public void onEnterDark(PlayerMoveEvent event) {
            Block block = event.getTo().getBlock();
            int lightLevel = block.getLightLevel();

            if (check == true) {           **code**
                if (lightLevel < 8) {
                    event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 60, 2));
    The command is working fine but the if statement at the bottom isn't wanting to change. If its check == true, the potion effects won't do anything while if its check == false, they stay on regardless of the command.
     
  9. Yeah after this I probably should
     
  10. Strahan

    Benefactor

    Code (Text):
    private boolean check = false;
    This is not going to work if you intend to have more than just one player. That is a single value variable. So say you start the server and have five players on. Nobody is currently a vampire. Player 1 does whatever command and makes themselves a vampire and the check boolean is set. However, now everyone gets that effect because when anyone moves, it checks the status of the check boolean and it is now as player 1 set. So player 3 gets irritated and turns it back off. Now player 1 loses their vampire status too.

    You have to think globally when designing things. Remember, this code is not compartmentalized for each player. To do that with the vampire status, you need to track status for each player. Look into the Map interface.

    Also booleans represent a true/false value themselves, do you don't need to do == true or == false. Just check itself. So like:
    Code (Text):
    if (check == false){
        player.sendMessage(ChatColor.GREEN + "You are now a reverse vampire!");
    }else{
        player.sendMessage(ChatColor.GREEN + "You are no longer a reverse vampire!");
    }
    You are also checking the status backwards there since you haven't set it yet. It'd be more logical to set the var then do the messaging. Speaking of setting the var, ! means not/opposite. So if you set a bool to ! itself, it effectively toggles it. So check = !check means that if check was false, it's now true and vice versa. So you could write that as:
    Code (Text):
    check = !check;
    if (check){
        player.sendMessage(ChatColor.GREEN + "You are now a reverse vampire!");
    }else{
        player.sendMessage(ChatColor.GREEN + "You are no longer a reverse vampire!");
    }
    That makes a lot more sense. That could actually be distilled more, but as you don't understand booleans yet ternary operators may be a bit much.
     

  11. Here is the same code, just looking a little better:
    Code (Java):
        public boolean check = false;

        public DarkEnable(){
            this.main = this.main;
        }


        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(sender instanceof Player){
                Player player = (Player) sender;

                if(player.hasPermission("vampire")){

                    if (check == false){
                        player.sendMessage(ChatColor.GREEN + "You are now a reverse vampire!");
                    }else{
                        player.sendMessage(ChatColor.GREEN + "You are no longer a reverse vampire!");
                    }

                    if (!this.check){
                        check = true;
                    }else {
                        check = false;
                    }

                    return true;

                }else {
                    player.sendMessage(ChatColor.RED + "You don't have permission to use this command!");
                    return true;
                }

            }
            else {
                main.getLogger().info("You have to be a player to become a reverse vampire!");
                return true;
            }
        }


        @EventHandler
        public void onEnterDark(PlayerMoveEvent event) {
            Block block = event.getTo().getBlock();
            int lightLevel = block.getLightLevel();

            if (!check) {       // code
                if (lightLevel < 8) {
    Also, I'm pretty sure that the issue is that the boolean is changing is the commandSender, but going but to false for the EventHandler, which would explain why the effects don't change with the command
     
  12. The global toggle thing might be on purpose, it was one of the things I suggested (also suggested a way to do it per player).

    It looks like you're testing !check instead of check in your event handler, is that on purpose?
     
  13. That was from me testing it to see if changing that affected it, which it did. !check leaves the effects on all the time and check never toggles them on
     
  14. That would mean check is always false, which would suggest that your command isn't toggling it properly. Are you getting the messages in the command method? I'd also suggest simplifying your code according to @Strahan 's suggestion, it will make it easier to debug.
     
  15. Yes, when I use the command ingame, get the messages "You are now a reverse vampire" and "You are no longer a reverse vampire"
     
  16. Log a debug message to console in each branch of this if statement and see which branches are run when you do the command (this is a basic debugging step, remember it for the future):
    Code (Java):
    if (!this.check){
        check = true;
    }else {
        check = false;
    }
    Again, I recommend doing what Strahan suggested to simplify your command method checks.
     
  17. Quick question, how do you debug?
     
  18. Debugging means finding and removing bugs (mistakes or unwanted behavior) in your code. By debug message, I just meant send a message, like use the logger or Bukkit.getConsoleSender().sendMessage() or Player.sendMessage().
     
  19. I think I understand. So like send myself a message in game each time it should change? At first I thought you meant to use cmd to debug each line of code with in the if statement.
     
  20. Yeah, just send a message to yourself in each branch of the if statement I pasted a few posts ago, then run the command and see what messages you get (my hunch is that you won't ever get the message in the first branch, where check is set to true).