Solved Spawning a mob and custom drops, Help!

Discussion in 'Plugin Development' started by DominicGamesHD, Jul 28, 2017.

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

    DominicGamesHD

    Okay so I am trying to make it so that when I do "pinata" it will spawn a Donkey that will drop diamonds etc, so far I have this code,

    Code:
    if (args[0].equalsIgnoreCase("pinata")) {
                sender.sendMessage(ChatColor.GREEN + "Pinatas have been deployed behind all players!");
                for (Player p : Bukkit.getOnlinePlayers()) {
                    p.getLocation().getYaw();
                    Location spawnLoc = p.getLocation();
                    p.getWorld().spawnEntity(spawnLoc, EntityType.DONKEY);
                }
    
            }
    So I have spawned the donkeys, now how do I add custom drops to just the donkeys that where spawned.

    DominicGamesHD
     
  2. Offline

    Machine Maker

    @DominicGamesHD
    One way you could do this is check spawn the donkeys with a name (preferably a colored one so people can't use name tags to fool the plugin), then check EntityDeathEvent and see if the donkey has the name, and change the drops.

    You could also change the drops a much more complicated way using NMS code but I think it's unnecessary for this issue.
     
  3. Offline

    DominicGamesHD

    @X1machinemaker1X
    Could you explain how I could set a name on the donkeys?
     
  4. Offline

    Machine Maker

  5. @X1machinemaker1X @DominicGamesHD
    I don't think using the names of the donkey's is the best approach here. What if someone figured out your special name and made their own donkeys? Then they could get free diamonds.

    A better approach is to save the UUID's of the donkey's you've spawned to a list, and then check on the DeathEvent if the list contains the UUID.
     
  6. Offline

    DominicGamesHD

    @X1machinemaker1X Thanks for the help, I will now mark this as solved :).

    @AlvinB I will have a look at that, thanks for pointing it out :)
     
  7. Offline

    Machine Maker

    @AlvinB
    That's why I said color the name.
     
  8. @X1machinemaker1X
    Well, what if you have another plugin which lets you name animals with colour? Or what if another plugin uses the same name with the same colour?

    The ways this name identification method can go wrong are endless, just like with guessing. If there is a proper way to do it which you know will work, why not use it?
     
  9. Offline

    Machine Maker

    @AlvinB
    Well then using a list of UUID's is not the best option. That requires the plugin to keep a list of UUIDs which takes up memory. A better way would be to add a custom NBTTag to the donkey on spawn and then check the Entity for the tag on death.
     
  10. @X1machinemaker1X
    And the NBTTag doesn't take up memory? Each UUID is 16 bytes. The strings required to set NBT Tags are far more than that. But this is totally irrelevant, because micro-optimization at this level is pointless. It's not even close to worth the effort thinking about how much memory things occupy at this small scale. What matters is whether it makes sense from a coding perspective.

    And from that coding perspective, an NBTTag would've made just as much sense at the UUID list (maybe even more, if you're reasoning from the Object Oriented perspective), but there exists no support for NBTTags in the Bukkit API, so any proper implementation of this would require reflection, which is not worth the hassle. If you really wanted to do something like that, I'd use Entity Metadata, which would help with restarts, but I thought was maybe a bit out of scope for the OP.

    EDIT: Actually, metadata does not save through restarts, I misremembered.
     
    Last edited: Jul 28, 2017
  11. Offline

    Machine Maker

    @AlvinB
    Yeah you're probably right. Lol, and the thread was already marked solved:oops:.
     
  12. @X1machinemaker1X
    Yeah, well the thread's purpose is not only to serve OP, but also to serve future people. Someone in the future might find this discussion very useful :p
     
Thread Status:
Not open for further replies.

Share This Page