Solved Help needed with projectile event

Discussion in 'Plugin Development' started by TheHandfish, Jun 8, 2014.

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

    TheHandfish

    Hi,

    I'm trying to make it so when I shoot a melon with a bow, it bursts open with a bunch of melon slices. I tried using net.minecraft.server (which I hate to use D:). It's not working quite right:

    Code:javascript
    1. /*
    2.  * To change this license header, choose License Headers in Project Properties.
    3.  * To change this template file, choose Tools | Templates
    4.  * and open the template in the editor.
    5.  */
    6.  
    7. package com.Hand.WhymCraft.Events;
    8.  
    9. import com.Hand.WhymCraft.Main;
    10. import java.lang.reflect.Field;
    11. import java.util.logging.Level;
    12. import java.util.logging.Logger;
    13. import org.bukkit.Bukkit;
    14. import org.bukkit.World;
    15. import org.bukkit.block.Block;
    16. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftArrow;
    17. import org.bukkit.entity.Arrow;
    18. import org.bukkit.event.EventHandler;
    19. import org.bukkit.event.Listener;
    20. import org.bukkit.event.entity.ProjectileHitEvent;
    21.  
    22. /**
    23.  *
    24.  * @author Robert
    25.  */
    26. public class ProjectileHit implements Listener
    27. {
    28. public static Main plugin;
    29. public ProjectileHit(Main ins)
    30. {
    31. ProjectileHit.plugin = ins;
    32. }
    33.  
    34. //@EventHandler
    35. public static void onProjectileHit(final ProjectileHitEvent e)
    36. {
    37. plugin.getServer().broadcastMessage("§aArrow hit event.");
    38.  
    39. if(e.getEntity() instanceof Arrow)
    40. {
    41. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    42. @Override
    43. public void run()
    44. {
    45. plugin.getServer().broadcastMessage("§bScheduler active.");
    46.  
    47. Arrow arrow = (Arrow) e.getEntity();
    48. World world = arrow.getWorld();
    49.  
    50. net.minecraft.server.v1_7_R3.EntityArrow entityArrow = ((CraftArrow)arrow).getHandle();
    51.  
    52. Field fieldX = null;
    53. try {
    54. fieldX = net.minecraft.server.v1_7_R3.EntityArrow.class.getDeclaredField("e");
    55. } catch (NoSuchFieldException | SecurityException ex) {
    56. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    57. plugin.getServer().broadcastMessage("§4ono phail.");
    58.  
    59. }
    60. Field fieldY = null;
    61. try {
    62. fieldY = net.minecraft.server.v1_7_R3.EntityArrow.class.getDeclaredField("f");
    63. } catch (NoSuchFieldException | SecurityException ex) {
    64. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    65. plugin.getServer().broadcastMessage("§4ono phail.");
    66.  
    67. }
    68. Field fieldZ = null;
    69. try {
    70. fieldZ = net.minecraft.server.v1_7_R3.EntityArrow.class.getDeclaredField("g");
    71. } catch (NoSuchFieldException | SecurityException ex) {
    72. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    73. plugin.getServer().broadcastMessage("§4ono phail.");
    74. }
    75.  
    76. fieldX.setAccessible(true);
    77. fieldY.setAccessible(true);
    78. fieldZ.setAccessible(true);
    79.  
    80. int x = 0;
    81. try {
    82. x = fieldX.getInt(entityArrow);
    83. } catch (IllegalArgumentException | IllegalAccessException ex) {
    84. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    85. }
    86. int y = 0;
    87. try {
    88. y = fieldY.getInt(entityArrow);
    89. } catch (IllegalArgumentException | IllegalAccessException ex) {
    90. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    91. }
    92. int z = 0;
    93. try {
    94. z = fieldZ.getInt(entityArrow);
    95. } catch (IllegalArgumentException | IllegalAccessException ex) {
    96. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    97. }
    98.  
    99. Block block = world.getBlockAt(x, y, z);
    100. plugin.getServer().broadcastMessage("§aThe block type is: §b" + block.getType());
    101.  
    102. }
    103. });
    104. }
    105. }
    106. }


    The first debug statements work fine. The last one:

    plugin.getServer().broadcastMessage("§aThe block type is: §b" + block.getType());
    ...returns "AIR" as the block type, no matter what I shoot. Is there a way to accomplish what I'm trying to? One that preferably doesn't involve NMS?
     
  2. Offline

    fireblast709

  3. Offline

    TheHandfish

    fireblast709: So... uh...

    How do I use that? x_x I've never used BlockIterators before.
     
  4. Offline

    AoH_Ruthless

    TheHandfish
    Google "how to use block iterator bukkit"
     
  5. Offline

    TheHandfish

    ._.

    I kind of knew that, lol... I only ever post here if I've spent a good hour working at the problem. Nonetheless, I figured it out.

    Code:javascript
    1. Entity entity = e.getEntity();
    2.  
    3. if (entity instanceof Arrow)
    4. {
    5. BlockIterator iterator = new BlockIterator(entity.getWorld(), entity.getLocation().toVector(), entity.getVelocity().normalize(), 0, 4);
    6. Block hitBlock = null;
    7.  
    8. while(iterator.hasNext())
    9. {
    10. hitBlock = iterator.next();
    11. if(hitBlock.getTypeId()!=0)
    12. {
    13. break;
    14. }
    15. }
    16.  
    17. if (hitBlock.getType() == Material.MELON_BLOCK)
    18. {
    19. int drops = hitBlock.getDrops().size();
    20. hitBlock.breakNaturally(new ItemStack(Material.MELON, drops)); // Break block
    21. }
    22.  
    23. }
     
Thread Status:
Not open for further replies.

Share This Page