Solved CustomName for entity and entity equipment

Discussion in 'Plugin Development' started by dkfredebang, Jul 16, 2015.

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

    dkfredebang

    So I am trying to make an entity (in this case a Zombie) have a custom name when they spawn, and a chance 1 out of 5 to spawn with equipment such as Sword leather helmet etc. Though for some reason it doesn't work when I test it out. Could anyone help me with why this is not working? Thank you!

    Code:
        @EventHandler
        public void onCreatureSpawn(CreatureSpawnEvent e) {
            if (e.getEntity() instanceof LivingEntity) {
                LivingEntity le = e.getEntity();
                le.setCustomName(ChatColor.RED + "Zombie");
            }
           
           
           
           
            if(e.getEntity() instanceof Zombie){
               
            }
                Zombie z = (Zombie) e.getEntity();
                Random r = new Random();
                int per = r.nextInt(5);
                if(per == 5){
                z.getEquipment().setHelmet(new ItemStack(Material.LEATHER_HELMET));
                z.getEquipment().setBoots(new ItemStack(Material.IRON_BOOTS));
                z.getEquipment().setItemInHand(new ItemStack(Material.STONE_SWORD));
                z.getEquipment().setItemInHandDropChance(10);
            }
        }
     
  2. the r.nextInt(5) will return a random number from 0 to 4. the 5 means that it will choose from 5 numbers starting from 0.
     
  3. Offline

    dkfredebang

    That still doesn't help me with my problem... First of all no entities have the name Zombie and second even if I put the nextInt(1) it will never do it. I am looking for an answer on why it doesn't give the equipment and the custom name. This is just a test of what I want to do, therefore I am sharing my code to see if anyone knows why.
     
  4. He gave you the answer. You are getting a random number from 0 to 4. If you check if its 5 it will NEVER be true
     
  5. @dkfredebang
    Indeed what was said above is also true. But if it's also not setting the name then I think you haven't registered your events
     
  6. Offline

    dkfredebang

    So I have to do (per == 4), if so that still doesn't work :/ (Sorry if I am sounding hostile, I have just been trying to figure this out for so long now lol)

    EDIT: Nvm, I just fixed the CustomName, apparently my registering events was wrong after googling a bit, though the zombies still don't get equipment, I don't really get the random system though they do get equipment without this part
    Code:
                    Random r = new Random();
                    int per = r.nextInt(5);
                    if(per == 4){
    Pretty sure I am registering my events...

    Code:
    package com.froddeb.custommobs;
    
    import java.util.Random;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener{
    
    
        @Override
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
     
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
     
            logger.info(pdfFile.getName() + " has been enabled!" + "(V." + pdfFile.getVersion() + ")" );
        }
    
            public void onDisable(){
                PluginDescriptionFile pdfFile = getDescription();
                Logger logger = getLogger();
         
                logger.info(pdfFile.getName() + " has been disabled" + "(V." + pdfFile.getVersion() + ")" );
            }
     
            @EventHandler
            public void onCreatureSpawn(CreatureSpawnEvent e) {
                if (e.getEntity() instanceof LivingEntity) {
                    LivingEntity le = e.getEntity();
                    le.setCustomName(ChatColor.RED + "OOOOOOOOOO");
                }
         
         
                if(e.getEntity() instanceof Zombie){
             
                }
                    Zombie z = (Zombie) e.getEntity();
                    Random r = new Random();
                    int per = r.nextInt(5);
                    if(per == 4){
                    z.getEquipment().setHelmet(new ItemStack(Material.LEATHER_HELMET));
                    z.getEquipment().setBoots(new ItemStack(Material.IRON_BOOTS));
                    z.getEquipment().setItemInHand(new ItemStack(Material.STONE_SWORD));
                    z.getEquipment().setItemInHandDropChance(10);
                }
            }
     
    }
    
    Thank you for your help! Though I found a new way of making a randomizer which makes more sense to me, this here works like I wanted it to. Thank you though for trying to help. :)

    Code:
        }
                if(e.getEntity() instanceof Zombie){
                    Zombie z = (Zombie) e.getEntity();
                    Random object = new Random();
                    int zombieEquip;
                   
                    for(int counter =1; counter <=1;counter++){
                        zombieEquip = 1+object.nextInt(2);
                       
                        if(zombieEquip == 1){
                            z.getEquipment().setHelmet(new ItemStack(Material.LEATHER_HELMET));
                            z.getEquipment().setBoots(new ItemStack(Material.IRON_BOOTS));
                            z.getEquipment().setItemInHand(new ItemStack(Material.STONE_SWORD));
                        }
                    }
    
                    }
                }
            }
           
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  7. Offline

    nj2miami

    Counter will only ever run once, so it is useless to even have this in a For-Loop
    zombieEquip will either be 1 or 2, so this will only affect roughly 1/2 of your spawns
     
  8. Offline

    dkfredebang

    Yes I know, it's not useless for what I need it for...
     
  9. Offline

    nj2miami

    Er, it is useless (and cannot possibly be useful in any situation you are using it for) to have a For-Loop which is hard-coded to only ever be 1.
     
  10. Offline

    dkfredebang

    What do you mean?... I have tested it out, it's acting like a 50% chance of getting a zombie without equipment and a zombie with equipment. It works for me right now, it might not be the most effecient way of doing it though it is actually funtional.

    Try it yourself and you will see what I mean.
     
  11. Offline

    nj2miami

    I don't think you quite get it, or understand Java entirely honestly. There is absolutely NO NEED for you to have it in a For-Loop where the counter is ALWAYS 1. You are literally saying do this exactly ONE TIME.
     
  12. For 50% chance you could simply use:
    Code:
    Random r = new Random();
    if (r.nextBoolean()) {
    //code
    }
     
Thread Status:
Not open for further replies.

Share This Page