Solved Canceling fall will not work, no matter how hard I try

Discussion in 'Plugin Development' started by sgavster, Nov 12, 2013.

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

    sgavster

    Hi! I was playing around with something, but I can't get it to work..
    This is my code:
    PHP:
    /*************Add them to the arraylist***************/
                                
    if(!f.contains(p.getName()))
                                {
                                    
    f.add(p.getName());
                                }
    /**********************************/
     
    @EventHandler
        
    public void onFall(EntityDamageEvent e)
        {
            
    Bukkit.broadcastMessage("test");
            if(
    e.getEntity() instanceof Player)
            {
                
    Bukkit.broadcastMessage("test2");
                if(
    e.getCause() == DamageCause.FALL)
                {
                    
    Bukkit.broadcastMessage("test3");
                    
    Player p = (Playere.getEntity();
                    if(
    f.contains(p.getName()))
                    {
                        
    Bukkit.broadcastMessage("test4");
                        
    f.remove(p.getName());
                        
    Bukkit.broadcastMessage("test5");
                        
    e.setDamage(0);
                        
    Bukkit.broadcastMessage("test6");
                    }
                }
            }
        }
    It says up to test3. I don't know why it doesn't say I'm in it..
    (f is an arraylist<string>)

    Thanks!
     
  2. Offline

    Amgis

    Here is my code for blocking fall damage. Hope it helps.

    Code:
        @EventHandler
        public void onEntityDamage(EntityDamageEvent event) {
            if(!(event instanceof EntityDamageByEntityEvent)) {
                if(event.getCause() == DamageCause.FALL) {
                    if(event.getEntity() instanceof Player) {
                        event.setCancelled(true);
                    }
                }
            }
        }
     
  3. Offline

    sgavster

    Amgis .. I don't see how that helps my problem? It works if I don't have the arraylist check, but with it it doesn't work.

    Sorry for double post, but I added/changed this:

    PHP:
                                if(!f.contains(p.getName()))
                                {
                                    
    f.add(p.getName());
                                    
    Bukkit.broadcastMessage("added");
                                }
    And this is the outcome in game!
    [​IMG]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    Amgis

    In that case it it means (!f.contains(p.getName())) is returning false. In which case, I don't know how I can help you without knowing how you are using f or what f is, or how you are adding names to f.

    contains() is case sensitive. It may be that you are trying to see if f contains "Amgis" when it may contain "amgis".
     
  5. Offline

    sgavster

    Amgis I said what it was.. an ArrayList<String>.
    Code:java
    1. private ArrayList<String> f = new ArrayList<String>();

    and it's not !f.contains(..) it's if(f.contains(...)) {
     
  6. Offline

    Amgis

    sgavster

    I need to know how/where you are adding to f. It's possible that your method that adds a String to f may not be doing its job properly.
     
  7. Offline

    sgavster

    I said how I added it..

    PHP:
                              if(!f.contains(p.getName()))
                                {
                                    
    f.add(p.getName());
                                    
    Bukkit.broadcastMessage("added");
                                }
    p is a player.
     
  8. Offline

    valon750

    sgavster

    Well for a start you've got it set at setting the damage to 0, not simply cancelling the event.

    What broadcasting message does it get to before it stops working?
     
  9. Offline

    MrSparkzz

    sgavster
    Well try a little harder! :D no just kidding

    Code:java
    1. if(!f.contains(p.getName()))
    2. {
    3. f.add(p.getName());
    4. Bukkit.broadcastMessage("added");
    5. event.setCancelled(true);
    6. }
     
  10. Offline

    sgavster

    MrSparkzz umm.. why would I do that? I'm canceling it when they fall, and removing them, I add them onCommand :p
    valon750 test3, as said in the OP
     
  11. Offline

    MrSparkzz

    sgavster
    I don't know. I thought you wanted to cancel the fall damage if they were in that list... :oops:
     
  12. Offline

    sgavster

    MrSparkzz I do..?
    Your code is checking if they're not in it, adding them to it, and canceling nothing :p
    My code *should* cancel it if they're in it, but for some reason it's not returning when they're in it
     
  13. Offline

    MrSparkzz

    sgavster
    What I like to do is add something like System.out.println("Working #"); at the end or beginning of every bracketed statement and see where it stops.
     
  14. Offline

    sgavster

    MrSparkzz I am, with Bukkit.broadcastMessage(); It stops at number 3 (as seen in the OP)
     
  15. Offline

    MrSparkzz

    sgavster
    Oh duh, maybe I should pay a little more attention.

    the f list or whatever it is doesn't contain the player's name... What is your f variable?
     
  16. Offline

    sgavster

    MrSparkzz an ArrayList<String>, as said in the OP xDD
     
  17. Offline

    MrSparkzz

    sgavster
    Maybe I should start reading these posts .-.

    Well how are you adding players to the list?
     
  18. Offline

    sgavster

    MrSparkzz with this:

    PHP:
                            if(!f.contains(p.getName()))
                                {
                                    
    f.add(p.getName());
                                    
    Bukkit.broadcastMessage("added");
                                }
    Again, as said in the OP :p
     
  19. Offline

    MrSparkzz

    sgavster
    I saw that one ;) but what's surrounding that? You can't just have that sitting out in the open.
     
  20. Offline

    sgavster

    MrSparkzz
    PHP:
    public boolean onCommand(CommandSender senderCommand cmdString commandLabelString[] args)
        {
            if(
    cmd.getName().equalsIgnoreCase("g2p"))
            {
                if(
    sender instanceof Player)
                {
                    if(
    Math.random() < 0.50)
                    {
                        
    Player p = (Playersender;
                        
    Bukkit.broadcastMessage("§9 * §2" p.getName() + " §8pees");
                    }
                    else
                    {
                        
    Bukkit.getScheduler().runTaskTimer(QuadularMC.getInstance(), new Poop(), 0L0L);
                        final 
    Player p = (Playersender;
                        
    Bukkit.broadcastMessage("§9 * §2" p.getName() + " §9poops!");
                        
    QuadularMC.getInstance().poop.add(p.getName());
                        
    Bukkit.getScheduler().scheduleSyncDelayedTask(QuadularMC.getInstance(), new Runnable()
                        {
                            public 
    void run()
                            {
                                
    p.getWorld().createExplosion(p.getLocation(), 0L);
                                
    p.getWorld().createExplosion(p.getLocation(), 0L);
                                
    p.getWorld().createExplosion(p.getLocation(), 0L);
                                
    p.getWorld().createExplosion(p.getLocation(), 0L);
                                
    Vector v p.getVelocity();
                                if(!
    f.contains(p.getName()))
                                {
                                    
    f.add(p.getName());
                                    
    Bukkit.broadcastMessage("added");
                                }
                                
    p.setVelocity(new Vector(v.getX(), v.getY() + 10v.getZ()));
                                
    QuadularMC.getInstance().poop.remove(p.getName());
                            }
                        }, 
    500L);
                    }
                }
            }
            return 
    false;
        }
    //yes. I know it's childish, but it looks so funny in game xDD
     
  21. Offline

    MrSparkzz

    sgavster
    Sorry for the late reply. Maybe it's because when you enter the command it takes 500 (ticks or seconds don't remember much about Bukkit Runnables), and when you try to take fall damage the player isn't in the ArrayList yet.

    ... also you created three instances of QuadularMC... And it looks like you're using some sort of list or something in that class. so you'd only want to have one instance of it.
     
  22. Offline

    sgavster

    MrSparkzz They are in it before though, it says "added" then it waits like 3 seconds, I fall, and die.

    QuadularMC is my plugin;
    it's a method in that class, getting an arraylist that it in there, which doesn't seem to do much, atleast never has effect anything
    this plugin will never be public, so if it has something to do with preformance issues, it doesn't matter all that much
     
  23. Offline

    MrSparkzz

    sgavster
    When you create a new instance of a class it doesn't remember anything stored in variables or lists.

    For example if in your class you're creating an instance of has an ArrayList in it and in another class you add a variable to it, then in another class you create another instance, and try to get that variable, it wont have it.

    Or if you create a second instance in a class that already has an instance of that and try to do the same thing, the same stuff will happen.
     
  24. Offline

    sgavster

    MrSparkzz It does have it though? It works for me, if I add it through one class, then check for it in another it all seems to work? I'm confused :O
     
  25. Offline

    MrSparkzz

    sgavster
    I'm not quite sure, that's weird.
     
  26. Offline

    maxben34

    You can also do this differently. Use a PlayerMoveEvent. Use e.getFallDistance() then use e.setFallDistance(0); and e.getPlayer().setHealth(20.0); or whatever.
     
  27. Offline

    rbrick

    i can get the damaged cancelled to work for me by casting a double (0.0) to a int, the only problem i am having with my code is it doesnt remove the player from the arraylist after the event is cancelled >.>
     
  28. Offline

    sgavster

    MrSparkzz I've just tried to add them to the arraylist when they do the command, and it still wont work :( any ideas?

    bump :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  29. Offline

    jimbo8

    You don't need to bump it more than once a day ;)

    Just sayin'
     
  30. Offline

    McMhz

    sgavster
    You could do it with HashMaps like so?
    Code:java
    1. HashMap<String, Boolean> map = new HashMap<String, Boolean>();


    Then to add the player to it do so?
    Code:java
    1. map.put(p.getName(), true);

    Make sure to put it to false onJoin.
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent e){
    3. map.put(e.getPlayer().getName(), false);
    4. }

    Then check for a fall event like so:
    Code:java
    1. @EventHandler
    2. public void onFallDamage(EntityDamageEvent e){
    3. if(e.getEntity() instanceof Player){
    4. Player p = (Player) e.getEntity();
    5. e.setCancelled(true);
    6.  
    7. }
    8. }

    If you want to check if he has the fall protection on:
    Code:java
    1. public boolean getFallProtected(Player p){
    2. return map.get(p.getName()) != null;
    3. }
     
Thread Status:
Not open for further replies.

Share This Page