Solved Killing mobs with a custom name (help)

Discussion in 'Plugin Development' started by 123ang, Jul 5, 2014.

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

    teej107

    123ang Good job! But you are also checking to see if a Player is a PlayerInteractEvent as well. Which will always be false;
     
  2. Offline

    xTigerRebornx

    123ang You've got sort of the idea, but you are still checking if 'e' isn't an instance of Player (it isn't, but you are checking the wrong thing).
    Also, you don't have a check for the name, nor do you have a check to see if its specifically a Horse
     
  3. Offline

    tommyhoogstra

    I'm not sure why you made an entire new thread, but if you only want to kill the animal someone has mounted (looks like that in your code) then you don't want to use the name of the donkey.

    You want it to kill them when you right click with a saddle, but that wont take any input (e.g. you can only supply one name of animal to kill), you're better off using a command, and having the argument as the animals you want killed.
     
    123ang likes this.
  4. Offline

    123ang

    teej107 xTigerRebornx Okay I understand that e isn't an instance of Player, so how do I fix it.

    Also xTigerRebornx Yes I do need to check for the name of the horse, because this plugin is going on a multi-player server & tones of people will be spawn horses (I've already coded that part). So if they right click the saddle it's deleting EVERY horse on the world (including other peoples).

    tommyhoogstra Yes I understand but every horse is named player.getName + "'s" + " Mount Mule" so it'll work.

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

    tommyhoogstra

    Ok, another thought that just came across my mind is that you right click with the saddle in order to initially tame the horse, does this fire the Right click air Action?
     
    123ang likes this.
  6. Offline

    xTigerRebornx

    123ang By using the proper variable for the entity you are checking rather then 'e', since 'e' is the event you are listening to, not the Entity. Also, your code will remove all LivingEntities, and its not checking if the horse has the name you are looking for. You'll need to add the checks.
     
  7. Offline

    ReggieMOL


    You are just asking for code to be handed to you.

    Code:java
    1. if (e instanceof LivingEntity && !(e instanceof Player))


    Code:java
    1. public void onDespawnDonkey(PlayerInteractEvent e) {


    You are checking if the EVENT (keyword was event if you didn't see the bold) for a living entity. You should be using the correct variable to check not e aka the event.
     
  8. Offline

    123ang

    xTigerRebornx Yes that's where I got stumped at; I'm not sure how to kill ONLY mobs with that name. Also, how can I make it so it only kills the mobs with that name not items/item frames, etc.
     
  9. Offline

    tommyhoogstra

    Rather than looping through EVERY Entity, why not loop through only LivingEntities
    e.g.

    Code:java
    1. for (LivingEntity ent : e.getPlayer().getWorld().getEntities()) {
    2. //no need for the if instanceof livingentity anymore
    3. //code here
    4. }
     
    123ang likes this.
  10. Offline

    123ang

    ReggieMOL well, if you'd been reading our comments you would've noticed that I fixed that part up.
     
  11. Offline

    xTigerRebornx

    tommyhoogstra World#getEntities() returns an Entity Collection (may be a List, not sure), not a LivingEntity collection. OP could use World#getLivingEntities() if he wanted.
    123ang LivingEntity has a getCustomName() method, use that.
     
  12. Offline

    123ang

    xTigerRebornx Sorry for the late response; I had to do some stuff.

    Here is my updated code:

    Code:java
    1. @EventHandler
    2. public void onDespawnDonkey(PlayerInteractEvent e) {
    3. Player player = e.getPlayer();
    4. Action a = e.getAction();
    5. if (a.equals(Action.RIGHT_CLICK_AIR)){
    6.  
    7. Material m = player.getItemInHand().getType();
    8. if (m == Material.SADDLE) {
    9. player.sendMessage(ChatColor.BLUE + "Mounts>" + ChatColor.GRAY + " Despawning Your Mount Donkey...");
    10. for (Entity event : e.getPlayer().getWorld().getEntities()) {
    11. if (e instanceof LivingEntity && ((LivingEntity) e).getCustomName().contains(player.getName() + "'s" + " Mount Donkey")) {
    12. event.remove();
    13. }
    14. }
    15.  
    16. }
    17. }
    18. }


    Ugh, still not doing anything.
     
  13. Offline

    tommyhoogstra

    Add some code after if(m == Material.SADDLE){
    like player.sendMessage("Test");

    then right click with your saddle and see if you get that message, if you do, then its a problem with the loop and or rest.

    As an alternative, try:
    Code:java
    1. for (LivingEntity ent : e.getPlayer().getWorld().getEntities()) {
    2. if(ent.getCustomName().contains(player.getName() + "'s" + " Mount Donkey")) {
    3. ent.setHealth(0D);
    4. }
    5. }


    Edit: just noticed you were casting Living Entity to the event, not the entity, that would cause a problem for sure.
     
    123ang likes this.
  14. Offline

    123ang

    tommyhoogstra I did that but there is an error under e.getPlayer().getWorld().getEntities()) {

    The error is: "change type of 'ent' to 'Entity'
     
  15. Offline

    tommyhoogstra

    change .getEntities to getLivingEntities();
     
    123ang likes this.
  16. Offline

    123ang

    tommyhoogstra Here is the updated code:

    Code:java
    1. @EventHandler
    2. public void onDespawnDonkey(PlayerInteractEvent e) {
    3. Player player = e.getPlayer();
    4. Action a = e.getAction();
    5. if (a.equals(Action.RIGHT_CLICK_AIR)){
    6.  
    7. Location loc = player.getLocation();
    8. Material m = player.getItemInHand().getType();
    9. if (m == Material.SADDLE) {
    10. player.playSound(loc, Sound.HORSE_SADDLE, 1, 0);
    11. player.sendMessage(ChatColor.BLUE + "Mounts>" + ChatColor.GRAY + " Despawning Your Mount Donkey...");
    12. for (LivingEntity ent : e.getPlayer().getWorld().getLivingEntities()) {
    13. if(ent.getCustomName().contains(player.getName() + "'s" + " Mount Donkey")) {
    14. ent.setHealth(0D);
    15. }
    16. }
    17.  
    18. }
    19. }
    20. }


    But whenever I right-click a saddle in-game I get this error in my console:

    Code:
    [06:16:32 ERROR]: Could not pass event PlayerInteractEvent to Gadgets v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:226) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-
    ge6cd8c0-b3096jnks]
            at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:196) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-
    ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :605) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.a(SourceFile:60)
    [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.handle(SourceFile
    :9) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
    Caused by: java.lang.NullPointerException
            at me.angtim123.gadgets.Gadget.onDespawnDonkey(Gadget.java:186) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _05]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _05]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_05]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_05]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            ... 15 more
     
  17. Offline

    tommyhoogstra

    Line 187 of the Gadget.java class,
    Post that please
     
    123ang likes this.
  18. Offline

    123ang

    tommyhoogstra here is line 187:

    Code:java
    1. ent.setHealth(0D);
     
  19. Offline

    tommyhoogstra

    try add if(!(ent instanceof Horse)) return; right before the check for the custom name.
     
    123ang likes this.
  20. Offline

    123ang

    tommyhoogstra Here is updated code:

    Code:java
    1. @EventHandler
    2. public void onDespawnDonkey(PlayerInteractEvent e) {
    3. Player player = e.getPlayer();
    4. Action a = e.getAction();
    5. if (a.equals(Action.RIGHT_CLICK_AIR)){
    6.  
    7. Location loc = player.getLocation();
    8. Material m = player.getItemInHand().getType();
    9. if (m == Material.SADDLE) {
    10. player.playSound(loc, Sound.HORSE_SADDLE, 1, 0);
    11. player.sendMessage(ChatColor.BLUE + "Mounts>" + ChatColor.GRAY + " Despawning Your Mount Donkey...");
    12. for (LivingEntity ent : e.getPlayer().getWorld().getLivingEntities()) {
    13. if(!(ent instanceof Horse)) return;
    14. if(ent.getCustomName().contains(player.getName() + "'s" + " Mount Donkey")) {
    15. ent.setHealth(0D);
    16. }
    17. }
    18.  
    19. }
    20. }
    21. }


    Getting no console errors, but nothing is happening, however, I'm getting the player.sendMessage
     
  21. Offline

    Necrodoom

    123ang Please learn java before trying to code plugins, you dont seem to know what is a variable type, which would mean you wouldnt be able to code any plugin unless you are given the full code. Some basic java tutorials will help you a long way.
     
    AoH_Ruthless likes this.
  22. Offline

    123ang

  23. Offline

    Necrodoom

    123ang You mean, being given code piece by piece? Why not make a plugin request?
     
  24. Offline

    tommyhoogstra

    123ang don't worry about him, I learnt the same way as you are now.

    Change instanceof Horse to instanceof Tameable perhaps

    Make sure you tame the horse first.

    Edit: if still no result, try manually setting the horses name.
     
    123ang likes this.
  25. Offline

    123ang

    tommyhoogstra I changed what you said to do, but no difference.

    The horse's owner is player.
     
  26. Offline

    tommyhoogstra

    Ok! Change Tameable back to Horse and add a player.sendMessage line just before after that if statement, if you get that specified message, we know its a problem with the names!
     
    123ang likes this.
  27. Offline

    Necrodoom

    tommyhoogstra but you haven't learnt Java either.
    Seriously, what's with people trying to code plugins without any Java knowledge? I'm trying to help you by telling you that you need to know Java so we can help you, currently the thread is just back and forth "copy-paste this,try again" which doesn't help anybody learn. People tried to help you before and told you you need to understand what is a variable type.

    Basically, this plugin will not ever work if you just throw lines of code at it and expect it to solve itself.
     
    xTigerRebornx and hintss like this.
  28. Offline

    tommyhoogstra

    Coding is a matter of trial and error until what you have works. There is a slim chance your code is going to work perfectly on your first debug. Drop the attitude or gtfo.
     
    123ang likes this.
  29. Offline

    Necrodoom

    tommyhoogstra coding is not just trail and error. Its also understanding what are you doing. I want to help him so he can make his own plugins, not just teach him to copy-paste solutions off someone else. When you learn a new language, you don't just copy-paste sentences and hope they make sense, you understand the words and construct sentences out of them. This is what I'm trying to say here. Copy-pasting lines of code is not the solution. The solution is understanding what exactly you wrote, and from that you will be able to solve the problem.
     
  30. Offline

    tommyhoogstra

    Me throwing snippets of code at him, sure won't teach him java, but like myself, gives me a further understanding of the bukkit API.
     
    123ang likes this.
Thread Status:
Not open for further replies.

Share This Page