Solved Help with arraylist

Discussion in 'Plugin Development' started by GetReadyForBart, Aug 27, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    GetReadyForBart

    I am currently working on a fly plugin and don't want people to take fall damage after disable. I made this code and it is not working. I don't get any errors but when you fall you still get fall damage. any fixes?

    Code:
    package me.getreadyforbart.fly.commands;
    
    import java.util.ArrayList;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    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 me.getreadyforbart.fly.main;
    
    public class togglefly implements CommandExecutor, Listener {
       
        ArrayList<Player> falldamage = new ArrayList<>();
       
        private main plugin;
    
        public togglefly(main pl) {
            plugin = pl; 
    
    }
        public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
            final String NoPerms = plugin.getConfig().getString("NoPerms").replaceAll("&", "§").replaceAll("%player%", sender.getName());
            final String FlyOn = plugin.getConfig().getString("FlyOn").replaceAll("&", "§").replaceAll("%player%", sender.getName());
            final String FlyOff = plugin.getConfig().getString("FlyOff").replaceAll("&", "§").replaceAll("%player%", sender.getName());
            final String ConsoleSender = plugin.getConfig().getString("ConsoleSender").replaceAll("&", "§").replaceAll("%player%", sender.getName());
            if (cmd.getName().equalsIgnoreCase("fly")) 
            if (!(sender instanceof Player)) {
                sender.sendMessage(String.valueOf(ConsoleSender));
                return true;
            }
            final Player p = (Player)sender;
            if (!p.hasPermission("fly.toggle")) {
                p.sendMessage(String.valueOf(NoPerms));
                return true;
            }
            if (p.getAllowFlight()) {
                p.setAllowFlight(false);
                p.setFlying(false);
                p.sendMessage(String.valueOf(FlyOff));
                falldamage.add(p);
            }
            else {
                p.setAllowFlight(true);
                p.sendMessage(String.valueOf(FlyOn));  
            }
    
        return false;}
    
        @EventHandler
        public void onFallDamage(EntityDamageEvent event) {
            Entity entity = event.getEntity();
            if(entity instanceof Player) {
            Player p = (Player) event.getEntity();
    
            if (entity.getType() == EntityType.PLAYER) {
            if(falldamage.contains(p)) {
                if(event.getCause() == DamageCause.FALL) {
                //code
                }
                }
        event.setCancelled(true);
        //to remove
        falldamage.remove(p);
    }}}}
    
    
     
    Last edited: Aug 27, 2017
  2. Offline

    FireBuster12

    Hello GetReadyForBart,

    First of all, I'm sorry for my bad English, I hope you can understand.

    1. Replace:
    Code:java
    1.  
    2. if(event.getCause() == DamageCause.FALL) {
    3.  

    With:
    Code:java
    1.  
    2. if (event.getCause().equals(EntityDamageEvent.DamageCause.FALL)){
    3.  

    2. Add:
    Code:java
    1.  
    2. else {
    3. p.setAllowFlight(true);
    4. p.sendMessage(String.valueOf(FlyOn));
    5. falldamage.remove(p); //added
    6. }
    7.  





    I hope this helped you, otherwise I will hear from you.
    FireBuster12/Jeroen
     
  3. Offline

    GetReadyForBart

    with this won't you remove the player of the arraylist when they enable fly?

    Edit: Tested and doesn't work yet.

    Fixed already! Thanks for the help though!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 27, 2017
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    GetReadyForBart

    Instead of using an arraylist I set this at on disable:

    p.setfalldistance(-100000)
     
  6. Offline

    FireBuster12

    No, you can also do it with a equals, it works
     
    Last edited by a moderator: Sep 27, 2017
  7. Offline

    timtower Administrator Administrator Moderator

Thread Status:
Not open for further replies.

Share This Page