Shooting a Projectile Straight

Discussion in 'Plugin Development' started by moo3oo3oo3, Oct 5, 2014.

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

    moo3oo3oo3

    I was wondering how to have a projectile shoot straight?
    Code:java
    1. @EventHandler
    2. public void KakeCannon(PlayerInteractEvent e) {
    3.  
    4. final Player player = e.getPlayer();
    5. Location location = player.getEyeLocation();
    6.  
    7. if (player.getItemInHand() != null && player.getItemInHand().getType() != Material.AIR) {
    8. if (player.getItemInHand().hasItemMeta() && player.getItemInHand().getItemMeta().hasDisplayName()) {
    9. if (player.getItemInHand().getType() == Material.BREWING_STAND_ITEM && player.getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§cK§fa§ck§fe§8 Cannon") && e.getAction() == e.getAction().LEFT_CLICK_BLOCK || player.getItemInHand().getType() == Material.BREWING_STAND_ITEM && player.getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§cK§fa§ck§fe§8 Cannon") && e.getAction() == e.getAction().LEFT_CLICK_AIR) {
    10. if (player.hasPermission("use.kakecannon")) {
    11.  
    12. if(System.currentTimeMillis() - (countdown3.containsKey(player.getName()) ? countdown3.get(player.getName()) : 0) >= 1500) {
    13. final Arrow arrow = player.getWorld().spawn(location, Arrow.class);
    14. arrow.setShooter(player);
    15. arrow.setCritical(false);
    16. arrow.setKnockbackStrength(0);
    17. arrow.setVelocity(location.getDirection().normalize().multiply(4));
    18. Item item = player.getWorld().dropItem(location, new ItemStack(Material.CAKE, 1));
    19. item.setPickupDelay(6020);
    20. arrow.setPassenger(item);
    21. map.put("KakeCannon", arrow);
    22. map.put("KakeCannonPlayer", player);
    23. countdown3.put(player.getName(), System.currentTimeMillis());
    24. } else player.sendMessage("§4Please wait for cooldown");
    25. } else player.sendMessage("§4You do not have permission");
    26. }
    27. }
    28. }
    29. }

    Code:java
    1. @EventHandler
    2. public void KakeCannonLanding(EntityDamageByEntityEvent e) {
    3.  
    4. Entity arrow = map.get("KakeCannon");
    5. Entity player = map.get("KakeCannonPlayer");
    6. Entity hurt = e.getEntity();
    7.  
    8. if (player instanceof Player) {
    9. Vector velocity = ((Player) player).getEyeLocation().getDirection();
    10.  
    11. if (e.getDamager() == arrow) {
    12. if (hurt != player) {
    13.  
    14. final Block block = hurt.getLocation().getBlock();
    15. Location kakeLocation = arrow.getLocation();
    16. kakeLocation.add(velocity.getX(),0,velocity.getZ());
    17. player.getWorld().createExplosion(kakeLocation.getX(), kakeLocation.getY(), kakeLocation.getZ(), 4, false, false);
    18. arrow.remove();
    19. Item item = player.getWorld().dropItem(hurt.getLocation(), new ItemStack(Material.CAKE, 1));
    20. item.setPickupDelay(6020);
    21. map.remove("KakeCannon");
    22. map.remove("KakecannonPlayer");
    23. if (hurt.isDead() == true) {
    24. if (hurt.getLocation().getBlock().getType() == Material.AIR) {
    25. hurt.getLocation().getBlock().setType(Material.CAKE_BLOCK);
    26. }
    27. }
    28.  
    29. //delay
    30. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    31. public void run() {
    32. block.setType(Material.AIR);
    33. }
    34. }, 100L);
    35. }
    36. }
    37. }
    38. }

    I tried .getDirection.multiply() (line 17)
    but when I put a high number, it starts to explode on me.
     
  2. Offline

    teej107

    AFAIK, I don't think you can change the effect that gravity has on Entities. You could try going into NMS (which I would have no idea where to start) or you could try using a scheduler and teleport the entity to the desired Y level every tick which could cause a lag.
     
  3. Offline

    Goblom

    teej107 It would require nms, All you do is make it so the y value never changes. Basically override the move method.
     
  4. Offline

    moo3oo3oo3

    So nms is the ONLY way?
     
  5. Offline

    Skionz

    moo3oo3oo3 You could use vectors although it wouldn't look that smooth.
     
  6. Offline

    moo3oo3oo3

    What is nms... :p
     
  7. Offline

    Mysticate

    moo3oo3oo3
    Basically it's using the actual craftbukkit jar (the thing that runs and loads plugins, aka the bukkit MOD) and replacing the code for your projectile with the code you want. Without looking into it further, you'd probably be editing the launch vectors of the projectile.
     
  8. Offline

    SmooshCakez

    net.minecraft.server, the Minecraft server package included in Bukkit. Using NMS, you have more control because you're changing things directly in the server source, and more things are included in most NMS classes for you to override and change.
     
Thread Status:
Not open for further replies.

Share This Page