Monster spawning!

Discussion in 'Plugin Development' started by phantomcut3, Jan 8, 2014.

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

    phantomcut3

    Hi if you didn't get from the title i am new and what i am basically trying to do is the following:

    When this events happens spawn mob and x amount of them, then when mob(s) die do the following.
     
  2. Offline

    HeavyMine13

    1. for (int i = 0; i <= 4; i ++) {
    2. world.spawn(loc, Cow.class);
    3. }
    spawns 5 cows
     
  3. Offline

    phantomcut3

    HeavyMine13

    How do i do it so they spawn at the players and how do I tell how many cows I am spawning?
     
  4. Offline

    Alshain01

    The loop tells you how many your spawning. start at 0, keep doing it till you reach 5 then stop.

    to spawn at a players location (assuming you have retrieved the player).
    Code:java
    1.  
    2. for (int i = 0; i <= 4; i ++) {
    3. world.spawn(player.getLocation(), Cow.class);
    4. }
    5.  
     
  5. Offline

    phantomcut3

    Alshain01

    Also how do i make it when they die do the following or does the second line do that?

    Anyone know if :
    Code:
     for (int i = 0; i <= 4; i ++) {
            world.spawn(player.getLocation(), Cow.class);
            }
    activates something when the mobs die?

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

    WhosDaMan

    I'm still learning, so please correct me if I'm wrong.

    I believe you have to add the spawned mobs to a HashMap and use an EntityDeath event to check if the mob that died is one of the spawned mobs and then remove it from the HashMap. Then you can do what you want when the right mob dies.

    In pseudo code:
    Code:java
    1. public void entityDeath (EntityDeathEvent enDeath) {
    2. if (entity in hashmap) {
    3. // remove from hashmap
    4. // do something
    5. }
    6. }
     
  7. Offline

    phantomcut3

    WhosDaMan

    I see what you mean I will try that

    WhosDaMan

    I have this

    Code:
        public HashMap<Entity, Integer> events1 = new HashMap<Entity, Integer>();
    Code:
    @EventHandler
    public void onBlockBreak(BlockBreakEvent e) {
    Player p = e.getPlayer();
    Block b = e.getBlock();
    World world = p.getLocation().getWorld();
    PlayerInventory pi = p.getInventory();
     
    if(b.getType() == Material.DIAMOND_ORE){
          blockBroke++;
        if(blockBroke >= 10){
        blockBroke = 0;
        for (int i = 0; i <= 4; i ++) {
        world.spawn(p.getLocation(), Cow.class);
        p.sendMessage("Test");
        }
        }
        }
     
     
     
     
    {
    }
    }
     
    public void entityDeath (EntityDeathEvent enDeath) {
        if (events1.containsKey(key);
     
     
        }
    }
        
    But i am stuck on how to add the cow value, because i need the value in the first event so i can use it with hashmaps but that event dose not use entities so i am confused, how would i go about doing it?

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

    WhosDaMan

    You should be adding the cow value inside your for loop that spawns the cows. Also I'm just about at my limit of knowledge on this subject so take what I say with a grain of salt.
     
  9. Offline

    phantomcut3

    WhosDaMan

    Is there any knowledge left to tell me the code to add inside the for loop?

    Anyone have any Ideas that can help?

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

    Windy Day

    You would have to do hashmapname.put(entity, int);
    However, instead of using a hashmap and storing entity objects. What I would recommend you should use an arraylist because you don't need to put any values in the hashmap for one. Also saving an entity's UUID instead of the entity object would perhaps be better in this case and resistant after restarts/reloads. So save the UUID in the arraylist, then in the event check if the arraylist contains that ID. If it does do whatever then remove the ID.
    UUID = Unique Id
     
  11. Offline

    phantomcut3

    Windy Day

    ik the function of the array list but is the coding element in a similar style as the Hashamps?
     
  12. Offline

    Windy Day

    What do you mean by coding element? Like how they are used to add/remove objects?
     
  13. Offline

    phantomcut3

  14. Offline

    Windy Day

  15. Offline

    phantomcut3

    Windy Day

    @EventHandler
    public void onBlockBreak(BlockBreakEvent e) {
    Player p = e.getPlayer();
    Block b = e.getBlock();
    World world = p.getLocation().getWorld();
    PlayerInventory pi = p.getInventory();

    if(b.getType() == Material.GOLD_ORE){


    e.getBlock().getLocation().getWorld().playEffect(e.getBlock().getLocation(), Effect.STEP_SOUND, 49);
    blockBroke++;
    if(blockBroke >= 128){
    blockBroke = 0;
    pi.addItem(new ItemStack(Material.));
    for (int i = 0; i <= 4; i ++) {
    world.spawn(p.getLocation(), Cow.class);
    if (!events1.contains(p));
    events1.add(p);
    }
    }
    }

    }
    Ok so I worked this out but what do i replace "p" with so it apples to the mobs being spawned in this case a cow?

    Does anyone know who to replace "p" in the code above with 5 cow's UUID so I can make an event where they die they will activate an event?

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

    Windy Day

    You need to have a cow object. So to spawn a cow in and initialize it you can use:
    Code:
    Entity cow = world.spawnEntity(location, Cow.class);
    This way we are still spawning in the cow but now we also initialized it as an entity called cow. Now this is where you can add the UUID that you get with entity.getUniqueId() to the arraylist.
     
Thread Status:
Not open for further replies.

Share This Page