JellyLegs Toggle

Discussion in 'Plugin Development' started by GodzillaFlame42, Aug 18, 2016.

Thread Status:
Not open for further replies.
  1. So i am coding a jelly legs plugin but i cant figure out how to make it so someone can toggle it on and off. If someone could like give me an EXAMPLE of a toggle plugin to help me out that would be great thanks.

    Here is the code:
    Code:
    package me.godzilla;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
        public Permission enabled= new Permission("Core.Jelly");
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getLogger().info("Incidious Jelly Legs Enabled");
       
    }
    
        @EventHandler
        public void onCancelFallDamage(EntityDamageEvent e) {
           
            if(!(e.getEntity() instanceof Player)) return;
            Player p = (Player) e.getEntity();
            if(p.hasPermission(enabled)){
             if(e.getCause() == DamageCause.FALL){
           
              e.setDamage(0.00);
              e.setCancelled(true);
           
            p.playSound(p.getLocation(), Sound.NOTE_PLING, 10, 2);
                   
                }
            }
        }
    }
     
  2. Offline

    Zombie_Striker

    No one is going to give you an EXAMPLE here, since examples are the same as spoonfeeding.

    Here is what you need to do to make this toggle-able:
    1. Create an Arraylist of player's UUIDS (as storing the player may cause memory leaks)
    2. When a player wants "Jelly legs", put them into the array. When they don't, remove them.
    3. Inside the event, check if the player's UUID is inside the array. If they are, do what you are currently doing.
     
    DuaneTheDev_ likes this.
  3. This is what i have so far and when i do /jellylegs fall damage does not toggle, and i know i didnt put messages yet. @Zombie_Striker

    Code:
    Code:
    package me.godzilla;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Sound;
    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.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
        private static final Bukkit player = null;
        List<String> uuid = new ArrayList<String>();
        public Permission enabled= new Permission("Core.Jelly");
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
       
    }
         public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if(sender instanceof Player) {
                    Player player = (Player) sender;
                    if(cmd.getName().equalsIgnoreCase("jellylegs")) {
                    }
                }
                return false;
         }
        public boolean onCancelFallDamage(EntityDamageEvent e) {
            if(uuid.contains(player.getName())) {
            if(!(e.getEntity() instanceof Player)) return false;
            Player p = (Player) e.getEntity();
            if(p.hasPermission(enabled)){
             if(e.getCause() == DamageCause.FALL){
              e.setDamage(0.00);
              e.setCancelled(true);
            p.playSound(p.getLocation(), Sound.NOTE_PLING, 10, 2);
            uuid.remove(player.getName());
            return true;
             }
            }
            }
            return false;
           
             }
            
             public boolean onCancelFallDamage1(EntityDamageEvent e) {
                 if(uuid.contains(player.getName())) {
                 if(!(e.getEntity() instanceof Player)) return false;
                 Player p = (Player) e.getEntity();
                 if(p.hasPermission(enabled)){
                  if(e.getCause() == DamageCause.FALL){
                   e.setCancelled(false);
                 p.playSound(p.getLocation(), Sound.NOTE_PLING, 10, 2);
                 uuid.add(player.getName());
                 return true;
            }
        }
    }
                return false;
             }
            }
     
  4. Offline

    Zombie_Striker

    @GodzillaFlame42
    1. You forgot the @Eventhandler tags
    2. Your events should be void, as they are not used to return anything.
    3. You should only have one event [type] per plugin. You should merge each both events into one method.
    4. In regards to #3 above, the only difference between both events is one line (where even that line should not belong). I would recommend deleting the uuid.add/remove lines from the method and moving them into the command (meaning the command toggles it, not the actual falling.)
     
  5. @Zombie_Striker

    Sorry for being a bit nooby at this but i dont understand
    1. 3.) You should only have one event [type] per plugin. You should merge each both events into one method.
    2. 4.) In regards to #3 above, the only difference between both events is one line (where even that line should not belong). I would recommend deleting the uuid.add/remove lines from the method and moving them into the command (meaning the command toggles it, not the actual falling.)
     
  6. Offline

    Zombie_Striker

    @GodzillaFlame42
    You should only have one type of event (e.g. EntityDamageEvent) per plugin. That means there should only be one method with this event inside the parameter. I was going to recommend you merge "onCancelFallDamage" and "onCancelFallDamage1", but since most of the lines are exactly the same, you can delete the "onCancelfalldamage1" method.
    Remove these two lines. Only add /remove the player from these arrays when a player sends a command.

     
  7. Offline

    MCMastery

    also, don't use a List<UUID> - use a Set<UUID>. it's much more efficient, and it's good since ordering doesn't matter in this case
     
    Zombie_Striker likes this.
  8. Offline

    mythbusterma

    @MCMastery

    I agree that order doesn't matter, but don't change it for efficiency. The difference is so small as to be negligible.
     
  9. Offline

    ArsenArsen

    @mythbusterma

    Duplicates are also solved by using a Set<>, and also the implementation of HashSet is much faster for any size than and ArrayList
     
  10. Offline

    mythbusterma

    @ArsenArsen

    I understand what a Set is, thank you. My point is micro-optimisation of that level is pointless. You should always use the structure that makes the most sense, not whatever is "fastest," unless you can prove it's an issue. I agree a Set makes the most sense here, but don't just use it because it's "faster."

    It's also not, if you're doing an indexed lookup. But whatever. If we're splitting hairs, it likely takes more than 5 times the memory of an ArrayList, but whatever.
     
  11. Offline

    Lordloss

    @Zombie_Striker "Only one event type per plugin" I heard this multiple times from you, could you please explain this a bit further? Some time ago i read that internally they get merged together anyways, so i dont really understand the benefit of having only one. The reason i ask this because i have different eventHandler classes which handle the events of different minigames and much other stuff. Having it all in one would be absolutely horrible, or i would have to split it in many smaller plugins.
     
  12. @Lordloss
    Well, in the case of multiple classes which handle largely different things, my opinion is that it's okay to have different EventHandlers for the same Event. However, I think, and this is what I think @Zombie_Striker wanted to bring up, it should be avoided to have different handlers for the same Event in the same class.
     
  13. Offline

    Lordloss

    @AlvinB Yeah of course in the same class its not very useful. I agree to your view of it.
     
Thread Status:
Not open for further replies.

Share This Page