Help for events and other things too. (Not a problem about my code, just explanations etc)

Discussion in 'Plugin Development' started by TheHadDad, Mar 31, 2017.

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

    TheHadDad

    Okay so i'm going to clear some things up before I completely say anything or ask, I am a begginer in java developing and somewhat decent at html, css & completely horrible at javascript since i never looked too far into it.

    I'm currently trying to code a plugin which uses custom items that a player can wear/hold in result of a cool boost.

    Heres an example



    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public static void antiFall(final EntityDamageEvent e) {
        ItemStack f = new ItemStack(Material.FEATHER);
        //f.addUnsafeEnchantment(Enchants.featherfalling, 200); I'm going to add this later. If you find any             //problems tell me!
        //ItemMeta fm = f.getItemMeta();
        //fm.setDisplayName(ChatColor.RED + ChatColor.BOLD.toString() + "The Feather"); //Best name ever.
        //f.setItemMeta(fm);
            if (e.getEntity() instanceof Player) {
            if (e.getCause() == DamageCause.FALL) {
                if (((Player) e.getEntity()).getItemInHand().equals(f)) {
                    e.setCancelled(true);
            }

    Yes, I did copy a bit of code, like the DamageCause but i understand what its used for. This works completely fine btw, if you see any errors or badly placed/used code just tell me, and the comments were intentional i just didnt want to use the enchants yet.

    The main part of this thread : If I did the exact same thing and used same code but instead of whats in their hand, its what they're wearing, how would I control the damage they take from players or mobs? I want to have the player wearing the item take less damage. Is there any certain event i need to know about? I just want to know what i'd need to create this.

    Another thing, yes i have a lot of questions built up from all my experimenting :

    I swear, i've been trying my best to find this out but im just completely stupid, how do i make it so if a player clicks an item or uses a command, they get sent up in the air? I dont understand the velocity stuff, if you could, just clear it up for me.

    Another another thing:

    I want to save someones entire inventory, including their armour into a variable or something that will store it. I'm looking forward to making a plugin that can make people switch inventories in a way. I will love you forever if you can tell me what is required and how i can do this. Will I need to use hash (which i'm pretty sure is a random mix of numbers and letters?) to store their items temporarily or something? I was thinking about a plugin for staff to go into regular mode and staff mode. Staff mode being the set items to teleport to players etc. A bit like staff+ but a nooby version of that amazing plugin.

    Thanks for reading if you havent clicked away from this thread yet.
     
    Last edited by a moderator: Mar 31, 2017
  2. Offline

    mehboss

    Whoops***
    Didn't know it wasn't a problem
    You could add protection to the item.
     
  3. Offline

    Zombie_Striker

    @TheHadDad
    Please read this:
    https://bukkit.org/threads/read-me-first-how-to-make-a-plugin-development-thread.395844/
    EntityDamageByEntityEvent.

    The best way I can think of would be to create an Itemstack array, or a List of Itemstacks. In either case, add all the contents into the array plus the armor contents.
     
  4. Offline

    mine-care

    Horray!! :D I love code reviewing ;)

    Well getItemInHand() is likely to return 'null' if the player holds nothing. Null is 'nothing' basically therefore 'asking' a non existant object if it is equal to something else will error miserably. You need to check if getItemInHand() is null before accessing types or methods from it.

    Aha, well that sounds like a violaiton of the golden rule: D.R.Y (Don't Repeat Yourself). In other words two identical or very similar pieces of code shall not exist more than once in a program. Why is that i hear you ask, well lets think of the following situation:
    You have a piece of code repeated 12 times throughout the program, and all of a suddent you need to update one part of it. What is likely to hapend is that you miss one of the 12 repetitions and you update only part of the code so it continues to function improperly or errors. But even if you manage to update all of them, it would take you longer than having this piece of code in a method which you call over and over as needed.

    Not nessesarily, in some aplications that might be the case but in Java for example, Object hashcodes are values unique (or as close to unique as possible) representing this specific Object. There are various aplications, one of which is the HashMap datatype which uses them to get the numerical representation of that object which is then mapped to an index of an array where the object which holds a LinkedList (Or Binary tree depending on the number of entities and the JRE version) is stored in.
    If you want to observe this in action, create an Object which returns a different number as it's hashcode every time. Then create a few instances of this object and add them in a map as keys. Then try retreiving them from the map which is most likely to fail spectacularly XD
     
    Zombie_Striker and TheHadDad like this.
  5. Offline

    TheHadDad

    Zombie i read it but like, i didnt really make any changes so i didnt include that... I mean i've tried changing the DamageCause..

    And I'll try the ItemStack array, i reckon it's easier than a huge list of itemstacks

    Protection to a feather? Not a bad idea but I mean, if i'm going to add some lores that will add an effect. Imagine a steve head with protection on it.. Yeah I just sorta want to learn more about what @Zombie_Striker said, EntityDamageByEntityEvent.


    Thanks for the replies :) I'm going to try ItemStack arrays wish me luck.


    Thank you SO MUCH! I honestly love these instant replies. I'm learning a lot faster now :) Thanks, like really thanks.

    Uh, Sorry for being such a nub but correct me if im wrong, would i need to do this?

    Before:
    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public static void antiFall(final EntityDamageEvent e) {
        ItemStack f = new ItemStack(Material.FEATHER);
        //f.addUnsafeEnchantment(Enchants.featherfalling, 200);
        //ItemMeta fm = f.getItemMeta();
        //fm.setDisplayName(ChatColor.RED + ChatColor.BOLD.toString() + "The Feather");
        //f.setItemMeta(fm);
            if (e.getEntity() instanceof Player) {
            if (e.getCause() == DamageCause.FALL) {
                if (((Player) e.getEntity()).getItemInHand().equals(f)) {
                    e.setCancelled(true);
            }
                else {
                    e.setCancelled(false);
                }
            }
            }
            }
    After:

    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public static void antiFall(final EntityDamageEvent e) {
        ItemStack f = new ItemStack(Material.FEATHER);
        //f.addUnsafeEnchantment(Enchants.featherfalling, 200);
        //ItemMeta fm = f.getItemMeta();
        //fm.setDisplayName(ChatColor.RED + ChatColor.BOLD.toString() + "The Feather");
        //f.setItemMeta(fm);
            if (e.getEntity() instanceof Player) {
            if (e.getCause() == DamageCause.FALL) {
              if (!(((Player) e.getEntity()).getItemInHand() == null)) { return; } //HERE IS THE MAIN CHANGE
                if (((Player) e.getEntity()).getItemInHand().equals(f)) {
                    e.setCancelled(true);
            }
                else {
                    e.setCancelled(false);
                } 
            } 
            }
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 31, 2017
  6. Offline

    mine-care

    Nope! Event handlers shall not be static :p no reason for the to be static ;)

    Mhm, close enough!
    Lets analyze this and you will spot the problem yourself.
    Code:
     IF not getItemInHand() is null then:
    return;
    endIF
    
    is logically equivalent to:
    Code:
     IF getItemInHand() is not null then:
    return;
    endIF
    
    Did you spot the problem?
    No :-(

    If not, sorry i didnt explain it correctly :p
    What this conditional statement does there is check if the item is not null, and if that is the case then stop the execution of code. If the item however is null then it continues execution just fine. That is the opositve of what you want basically, because it means that after this check the item in hand will always be null :p
    Remove the not ('!') operator from this if condition ;)

    Well that is correct however it can be simplified.
    First of all we agree that
    Code:
    ((Player) e.getEntity()).getItemInHand().equals(f)
    is a boolean, because if statements only work with booleans. You can't have an if like:
    Code:
    if("Hello"){...}
    because even logically it makes no sense. So the condition between the two parenthesies of the if must be a boolean value (true OR false);
    We can also agree that the method setCanceled() takes a boolean as a parameter in the parenthesies, which is seen in your code:
    Code:
    e.setCancelled(false);
    e.setCancelled(true);
    
    So what you do here is:
    Code:
    if(someBooleanIsTrue){
    setCanceled(true);
    }else{ //someBooleanIsFalse
    setCanceled(false);
    }
    
    in other words:
    Code:
    if(true){
    setCanceled(true);
    }else{
    setCanceled(false);
    }
    
    A simpler way to acheive the exact same thing is to remove everything appart from one e.setCanceled() and in the parameters of this e.setCanceled(HERE) you pass the condition of the if statement.
    I didn't get it :-(
    Basicaclly:
    Code:
    if (((Player) e.getEntity()).getItemInHand().equals(f)) {
      e.setCancelled(true);
    } else {
      e.setCancelled(false);
    }
    
    becomes
    Code:
    e.setCancelled(((Player) e.getEntity()).getItemInHand().equals(f));
    
    :D

    :p
     
    TheHadDad, mehboss and ipodtouch0218 like this.
  7. Offline

    TheHadDad

    Im gonna kms. You are actually the best dude i love you. I honestly just cant believe how stupid i was to not use

    Code:
    e.setCancelled(((Player) e.getEntity()).getItemInHand().equals(f));
    

    So pretty much :

    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void antiFall(final EntityDamageEvent e) {
        ItemStack f = new ItemStack(Material.FEATHER);
        f.addUnsafeEnchantment(Enchants.featherfalling, 200);
        ItemMeta fm = f.getItemMeta();
        fm.setDisplayName(ChatColor.RED + ChatColor.BOLD.toString() + "The Feather");
        f.setItemMeta(fm);
            if (e.getEntity() instanceof Player) {
            if (e.getCause() == DamageCause.FALL) {
                e.setCancelled(((Player) e.getEntity()).getItemInHand().equals(f));
                } 
            } 
            }
    ?
     
    Last edited by a moderator: Apr 4, 2017
Thread Status:
Not open for further replies.

Share This Page