Toggle an event

Discussion in 'Spigot Plugin Development' started by robotballs, May 9, 2015.

  1. So I have this command
    Code (Text):
            if (cmd.getName().equalsIgnoreCase("toggleee")) {
               
                Player player = (Player) sender;
               
                if (player.hasPermission("enderegg.toggle")){
                    return true;
                }

                if (toggleee.contains(player.getName())) {
                    player.sendMessage(ChatColor.DARK_GREEN + "You Have Toggled Off EnderEgg!");
                    toggleee.remove(player.getName());
                    return true;
                }
               
                player.sendMessage(ChatColor.GREEN + "You Have Toggled On EnderEgg!");
                toggleee.add(player.getName());
               
            }
    I can't seem to figure out how to make it toggle my event.
    Code (Text):
        @EventHandler
        public void eggThrow(PlayerEggThrowEvent e) {
       
            Player player = e.getPlayer();
            Egg egg = e.getEgg();
       
            if (player.hasPermission("enderegg.throw")) {
                Location newLoc = egg.getLocation();
                Location old = player.getLocation();
                newLoc.setPitch(old.getPitch());
                newLoc.setYaw(old.getYaw());
                player.teleport(newLoc);
            if (player.hasPermission("enderegg.unlimited")){
                player.setItemInHand(new ItemStack(Material.EGG));
            }
            }
        }
    If someone could tell me how to do this or at least give me a start I would appreciate it.
     
  2. By toggle, you mean trigger your event handler?
     
  3. In this plugin you get teleported to an egg. I want it so it is toggle-able.
     
  4. Code (Text):
                if (player.hasPermission("enderegg.toggle")){
                    return true;
                }
    This is your problem. You're returning the onCommand() method if they have permission to toggle, so it never continues to the code below.
    I would encase your code in the permission check like this:
    Code (Text):
          if (player.hasPermission("enderegg.toggle")) {
               if (toggleee.contains(player.getName())) {
                    player.sendMessage(ChatColor.DARK_GREEN + "You Have Toggled Off EnderEgg!");
                    toggleee.remove(player.getName());
                    return true;
                }
             
                player.sendMessage(ChatColor.GREEN + "You Have Toggled On EnderEgg!");
                toggleee.add(player.getName());
          }
     
  5. Alright, thanks
     
  6. That solution he has doesn't seem like it would work. You're not setting "enderegg.throw" permission to true, you're adding to a list. You could either set the permission or you could check if it's in the list instead of the permission.
     
  7. yeah, your right. I just tested it and it didn't work.
     
  8. Hmmm... Yeah, I didn't test this, and I answered it a little too fast. Now that I look at it, what exactly are you trying to do? This is what I was thinking: you type /toggleee and, provided you have the permission, it adds the player into the arraylist 'toggleee.' If the player is already in the arraylist, it will remove them from it. Sorry if I confused you, could you clarify what you wanted from it more?
     
  9. I want to make it so when I do /toggleee It stops teleporting the player to the eggs who use the command when they throw eggs. So it doesn't get annoying.
     
  10. Just use a Boolean?
     
  11. I will try that in a bit.
     
  12. In your egg throw event you never actually check whether or not they have the "teleport mode" activated by checking if the player is contained in the array list 'togleee.'
     
  13. Just set the event to false of the toggleee list contains the player
     
  14. That will stop the egg from being thrown, too. I think he wants only the teleporting to be disabled.
     
  15. I'm assuming an Ender egg is an Ender pearl. In that case, listen for the player getting teleported. I believe there's a getSource() method or something like that. You can check if it's from an ender pearl and check if they're in the toggleee list. If they are, cancel it.
     
  16. I haven't been able to try any of the suggestions because of mothers day :/ I either will tonight or tomorrow.

    It is an egg
     
  17. Wait what exactly is this plugin doing? Is it just canceling the teleportation of the Ender Egg when you hit it? What is @Flaming_Katana talking about with throwing it?
     
  18. I think the plugin is supposed to mimic an enderpearl using eggs instead.
     
  19. I think boolean will work. Set it to false.
     
  20. Code (Text):
    Bukkit.getPluginManager().callEvent(event);