Solved Making mob shoot arrow at player, code not working

Discussion in 'Plugin Development' started by ice374, Nov 26, 2013.

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

    ice374

    my broadcasters work, so i know the event is firing, but i cant get the giant to shoot an arrow at the player, can someone point out what i am doing wrong please :)

    Code:java
    1. @EventHandler
    2. public void onGiantTarget(PlayerInteractEntityEvent e) {
    3. Bukkit.broadcastMessage("Event Fired");
    4. if ((e.getRightClicked().getType() == EntityType.GIANT)) {
    5. Bukkit.broadcastMessage("Hit Giant");
    6. final Arrow s = ((LivingEntity) e.getRightClicked()).launchProjectile(Arrow.class);
    7. s.setShooter(((LivingEntity) e.getRightClicked()));
    8. s.setVelocity((Vector) ((LivingEntity) e.getPlayer().getLocation().getDirection().multiply(2)));
    9. }
    10. }
     
  2. Offline

    adam753

    You're setting the velocity of the arrow to the player's direction. That means the arrow will be launched in the direction the player's facing - in other words, the exact opposite direction from where it needs to go.
    Try seeing if Vector has a reverse method, or you could just multiply it by -2 instead of 2.
     
  3. Offline

    ice374

    it doesn't fire (or create as far as i can see) an arrow at all, does anyone know of a method to do what i want?
     
  4. Offline

    xTrollxDudex

    ice374
    Does the debug message show?
     
  5. Offline

    calebbfmv

    Why are you casting a LivingEntity to an Arrow?Just do a check if the hit entity instanceof Living Entity...
     
  6. Offline

    ice374

    Im not following, please do explain :)
     
  7. Offline

    Jalau

    He is just casting the rightclicked monster to living: ((LivingEntity) e.getRightClicked()).

    But do the Debug Message show? Maybe the arrow does spawn inside the Giant?
     
  8. Offline

    ice374

    Jalau calebbfmv xTrollxDudex adam753

    For some reason i wasnt getting errors b4, but now i am, here is what im doing, and the error im getting

    Error:
    Code:java
    1. 2013-11-27 21:25:11 [INFO] Event Fired
    2. 2013-11-27 21:25:11 [INFO] Hit Giant
    3. 2013-11-27 21:25:11 [SEVERE] Could not pass event PlayerInteractEntityEvent to Christmas v1.0
    4. org.bukkit.event.EventException
    5. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    6. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    7. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    8. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    9. at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:1097)
    10. at net.minecraft.server.v1_6_R3.Packet7UseEntity.handle(SourceFile:36)
    11. at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
    12. at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    13. at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    14. at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
    15. at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
    16. at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    17. at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    18. at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    19. at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    20. Caused by: java.lang.ClassCastException: org.bukkit.util.Vector cannot be cast to org.bukkit.entity.LivingEntity
    21. at info.tregmine.christmas.listeners.TargetingListener.onGiantTarget(TargetingListener.java:57)
    22. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    23. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    24. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    25. at java.lang.reflect.Method.invoke(Unknown Source)
    26. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    27. ... 14 more


    Code:
    Code:java
    1. //Line below this is Line 50, just for reference
    2. @EventHandler
    3. public void onGiantTarget(PlayerInteractEntityEvent e) {
    4. Bukkit.broadcastMessage("Event Fired");
    5. if ((e.getRightClicked().getType() == EntityType.GIANT)) {
    6. Bukkit.broadcastMessage("Hit Giant");
    7. final Arrow s = ((LivingEntity) e.getRightClicked()).launchProjectile(Arrow.class);
    8. s.setShooter(((LivingEntity) e.getRightClicked()));
    9. s.setVelocity((Vector) ((LivingEntity) e.getPlayer().getLocation().getDirection().multiply(2)));
    10. Bukkit.broadcastMessage("Final Check");
    11. }
    12. }
     
  9. Offline

    jimbo8

    As mentioned earlier, just check if the entity is a instanceof Living Entity.

    The error in the console pretty much explains it;

    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.util.Vector cannot be cast to org.bukkit.entity.LivingEntity
    
     
  10. Offline

    Jalau

    Didn't saw that, yeah s.setVelocity((Vector) ((LivingEntity) e.getPlayer().getLocation().getDirection().multiply(2)));
    is wrong remove the livignentity
     
  11. Offline

    ice374

    Jalau jimbo8

    ok, ive done that, now i have got no errors, but the arrow still isnt showing up, it may be spawning inside of the giant but im not sure

    edit: yes, all 3 debug messages show up

    current method:
    Code:java
    1. @EventHandler
    2. public void onGiantTarget(PlayerInteractEntityEvent e) {
    3. Bukkit.broadcastMessage("Event Fired");
    4. if ((e.getRightClicked().getType() == EntityType.GIANT)) {
    5. Bukkit.broadcastMessage("Hit Giant");
    6. final Arrow s = ((LivingEntity) e.getRightClicked()).launchProjectile(Arrow.class);
    7. s.setShooter(((LivingEntity) e.getRightClicked()));
    8. s.setVelocity((Vector) (e.getPlayer().getLocation().toVector().multiply(20)));
    9. Bukkit.broadcastMessage("Final Check");
    10. }
    11. }


    Ping

    Solved@!!!!!!!@##@#!@#!@#!@#!@

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page