Solved Throw Items?

Discussion in 'Plugin Development' started by StaticJava, Nov 30, 2014.

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

    StaticJava

    Hey Guys,
    I am trying to spawn a FallingBlock Entity, which works fine. However, I want to remove it after a for loop is done executing.
    My code:
    Code:java
    1. public class FireTask extends BukkitRunnable {
    2. private final FallingBlock fire;
    3. public FireTask(FallingBlock fire) {
    4. this.fire = fire;
    5. }
    6.  
    7. @Override
    8. public void run() {
    9. if (fire.isOnGround()) {
    10. List<Entity> nearby = fire.getNearbyEntities(0.5, 0.5, 0.5);
    11. for (Entity e : nearby) {
    12. if (e instanceof Player) {
    13. e.setFireTicks(100);
    14. }
    15. }
    16. fire.remove();
    17. } else {
    18. cancel();
    19. }
    20. }
    21. }

    I call the Scheduler in this way:
    Code:java
    1. final FallingBlock fire = player.getWorld().spawnFallingBlock(player.getEyeLocation().add(player.getLocation().getDirection().normalize()), Material.FIRE, (byte) 0);
    2. fire.setVelocity(player.getLocation().getDirection().multiply(4));
    3. BukkitTask task = new FireTask(fire).runTaskTimer(this, 0, 1);

    The fire entity spawns, but does not get removed. Any help is appreciated!
    Thanks,
    ~StaticJava
     
  2. Offline

    Skionz

    StaticJava If the falling block is on the ground it will turn into a Block and will no longer be an Entity.
     
  3. Offline

    StaticJava

    So what should I do? :eek: Skionz
     
  4. Offline

    Skionz

    StaticJava I believe you would use the EntityBlockFormEvent.
     
  5. Offline

    StaticJava

    Ah, that makes sense. I will get back to you on whether or not this works.

    Ok, so the plugin somewhat works.
    Here is my corresponding code:
    Code:java
    1. @EventHandler
    2. public void onItemUse(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4.  
    5. if (kitMap.containsKey(player.getUniqueId().toString())) {
    6. SugarKit kit = kitMap.get(player.getUniqueId().toString());
    7. if (kit == SugarKit.PYRO) {
    8. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    9. if (player.getItemInHand().getType() == Material.DIAMOND_SWORD) {
    10. if (player.getItemInHand().getItemMeta().getDisplayName().equals("§c§lPyro's Blade")) {
    11. final FallingBlock fire = player.getWorld().spawnFallingBlock(player.getEyeLocation().add(player.getLocation().getDirection().normalize()), Material.FIRE, (byte) 0);
    12. fire.setVelocity(player.getLocation().getDirection().multiply(4));
    13. BukkitTask task = new FireTask(fire).runTaskTimer(this, 0, 1);
    14.  
    15. short durability = player.getItemInHand().getDurability();
    16. short maxDurability = Material.DIAMOND_SWORD.getMaxDurability();
    17.  
    18. if (durability == maxDurability) {
    19. player.getInventory().remove(player.getItemInHand());
    20. } else {
    21. player.getItemInHand().setDurability((short) (durability + 5));
    22. }
    23. }
    24. }
    25. }
    26. }
    27. }
    28. }


    Here is the FireTask Class:
    Code:java
    1. package me.staticjava;
    2.  
    3. import org.bukkit.entity.Entity;
    4. import org.bukkit.entity.FallingBlock;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.scheduler.BukkitRunnable;
    7.  
    8. import java.util.List;
    9.  
    10. /**
    11. * Created by StaticJava.
    12. */
    13. public class FireTask extends BukkitRunnable {
    14. private final FallingBlock fire;
    15.  
    16. public FireTask(FallingBlock fire) {
    17. this.fire = fire;
    18. }
    19.  
    20. @Override
    21. public void run() {
    22. if (!fire.isOnGround()) {
    23. List<Entity> nearby = fire.getNearbyEntities(0.5, 0.5, 0.5);
    24. for (Entity e : nearby) {
    25. if (e instanceof Player) {
    26. e.setFireTicks(100);
    27. fire.remove();
    28. }
    29. }
    30. } else {
    31. cancel();
    32. }
    33. }
    34. }
    35.  

    Here is a video of what is currently happening:
    https://www.dropbox.com/s/jwzbh9rqmkk7p2h/Produce_1.mp4?dl=0

    As you can see, the flames are somewhat bugged; sometimes they disappear instantly. Additionally, if aimed towards a half block, they drop in the form of a Flame Item (http://jd.bukkit.org/rb/doxygen/dc/dfc/interfaceorg_1_1bukkit_1_1entity_1_1Item.html).
    All help is appreciated!
    ~StaticJava [@Skionz]

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

    Skionz

    StaticJava Cancel the ItemSpawnEvent when necessary.
     
  7. Offline

    StaticJava

    Ok, do you know how I can fix the other issues? (Flames disappearing?) Skionz
     
  8. Offline

    Skionz

    StaticJava Can you elaborate? I can't watch the video right now.
     
  9. Offline

    StaticJava

    Skionz It's kinda hard to explain in words. When are you able to watch the video? I can wait. :)

    Additionally, the fire sometimes turns into a block, even though I cancelled it in the EntityBlockFormEvent. Skionz

    Here is an updated video with words to explain the situations: https://www.dropbox.com/s/jwzbh9rqmkk7p2h/Produce_1.mp4?dl=0

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

    Skionz

    StaticJava I will watch it in 4 hours. I am at school right now.
     
  11. Offline

    StaticJava

    Ok, thanks.
     
  12. Offline

    Skionz

    StaticJava Did you use the EntityBlockFormEvent? The code you used above uses a scheduler.
     
  13. Offline

    StaticJava

    Yes:
    Code:java
    1. @EventHandler
    2. public void onEntityForm(EntityBlockFormEvent event) {
    3. Block block = event.getBlock();
    4. if (block.getType() == Material.FIRE) {
    5. block.setType(Material.AIR);
    6. }
    7. }


    Those events + scheduler cause the video result above. Skionz
     
  14. Offline

    Skionz

  15. Offline

    StaticJava

    Ok, would that fix all of the problems? Skionz
     
  16. Offline

    Skionz

  17. Offline

    StaticJava

    Skionz Ok, the items are no longer spawning on the ground. But the actual fire is still glitched as shown in the video.
     
  18. Offline

    Skionz

  19. Offline

    StaticJava

  20. Offline

    Skionz

    StaticJava I still don't get it. In the video it makes it to the shrub?
     
  21. Offline

    StaticJava

    [quote uid=90861229 name="Skionz" post=2915502]StaticJava I still don't get it. In the video it makes it to the shrub?[/quote]


    It should act like this: <Edit by Moderator: Redacted bit url>

    Skionz But as you can see, in my video the fire is removed instantly.
     
    Last edited by a moderator: Feb 11, 2017
  22. Offline

    Skionz

    StaticJava They are using Items in the video, not FallingBlocks.
     
  23. Offline

    [b]Blake[/b]

    I can help with the cause itself, you don't need to spawn an entity. What do you need done? Something with fire?
     
  24. Offline

    StaticJava

    <Edit by Moderator: Redacted bit url>

    I need that. My code is located above Blake

    Skionz Ok, I will try using this with Items.

    This is my updated event, but the Fire doesn't even spawn o.o Skionz Blake

    Code:java
    1. @EventHandler
    2. public void onItemUse(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4.  
    5. if (kitMap.containsKey(player.getUniqueId().toString())) {
    6. SugarKit kit = kitMap.get(player.getUniqueId().toString());
    7. if (kit == SugarKit.PYRO) {
    8. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    9. if (player.getItemInHand().getType() == Material.DIAMOND_SWORD) {
    10. if (player.getItemInHand().getItemMeta().getDisplayName().equals("§c§lPyro's Blade")) {
    11. Item fireItem = player.getWorld().dropItem(player.getLocation(), new ItemStack(Material.FIRE));
    12. fireItem.setVelocity(player.getLocation().getDirection().multiply(4));
    13. BukkitTask task = new FireTask(fireItem).runTaskTimer(this, 0, 1);
    14.  
    15. short durability = player.getItemInHand().getDurability();
    16. short maxDurability = Material.DIAMOND_SWORD.getMaxDurability();
    17.  
    18. if (durability == maxDurability) {
    19. player.getInventory().remove(player.getItemInHand());
    20. } else {
    21. player.getItemInHand().setDurability((short) (durability + 5));
    22. }
    23. }
    24. }
    25. }
    26. }
    27. }
    28. }


    Here is the updated FireTask:

    Code:java
    1. package me.staticjava;
    2.  
    3. import org.bukkit.entity.Entity;
    4. import org.bukkit.entity.Item;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.scheduler.BukkitRunnable;
    7.  
    8. import java.util.List;
    9.  
    10. /**
    11. * Created by StaticJava.
    12. */
    13. public class FireTask extends BukkitRunnable {
    14. private final Item fireItem;
    15.  
    16. public FireTask(Item fireItem) {
    17. this.fireItem = fireItem;
    18. }
    19.  
    20. @Override
    21. public void run() {
    22. if (!fireItem.isOnGround()) {
    23. List<Entity> nearby = fireItem.getNearbyEntities(0.5, 0.5, 0.5);
    24. for (Entity e : nearby) {
    25. if (e instanceof Player) {
    26. e.setFireTicks(100);
    27. fireItem.remove();
    28. }
    29. }
    30. } else {
    31. //fireItem.remove();
    32. cancel();
    33. }
    34. }
    35. }
    36.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 11, 2017
  25. Offline

    Skionz

  26. Offline

    StaticJava

  27. Offline

    Skionz

  28. Offline

    StaticJava

  29. Offline

    Gater12

    StaticJava
    The task is being ran a tick later. What happens in that task? You remove the fire item. Boom that's why it's gone in an instant.
     
    StaticJava likes this.
  30. Offline

    StaticJava

    Gater12 Makes sense. If I remove this, how can I make sure the fire item is removed when it hits the ground, as well as burn all Player entities in its way?

    Never mind, fixed this. However, whenever the fire is launched, it always launches far to the left of the player.
     
Thread Status:
Not open for further replies.

Share This Page