Solved Help with dropping an item

Discussion in 'Plugin Development' started by mineshafter2202, Aug 4, 2014.

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

    mineshafter2202

    So I had an idea of where you throw an ore onto an iron block, with lava or fire underneath, and then the ore is turned into the ingot or dust form of that ore. I tested it, but it doesn't seem to work. No console errors either.

    Code:java
    1. package net.spideynn.bukkit.oresmelterblock;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.entity.Item;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerDropItemEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class OreSmelterBlock extends JavaPlugin implements Listener {
    15.  
    16. Logger log = Logger.getLogger("Minecraft");
    17.  
    18. public void onEnable() {
    19. log.info("[OreSmelterBlock] has been enabled.");
    20. getServer().getPluginManager().registerEvents(this, this);
    21.  
    22. }
    23.  
    24. public void onDisable() {
    25. log.info("[OreSmelterBlock] has been disabled.");
    26. }
    27.  
    28. @EventHandler
    29. public void onDrop(PlayerDropItemEvent event) {
    30. if (event.getItemDrop().isOnGround()) {
    31. Location l = event.getItemDrop().getLocation();
    32. Location d = new Location(l.getWorld(), l.getX(), l.getY() - 1,
    33. l.getZ());
    34. l.add(0.5, 0.5, 0.5);
    35. Location floc = new Location(l.getWorld(), l.getX(), l.getY() - 2,
    36. l.getZ());
    37.  
    38. if (d.getBlock().getType() == Material.IRON_BLOCK) {
    39. if (floc.getBlock().getType() == Material.FIRE || floc.getBlock().getType() == Material.LAVA) {
    40.  
    41. if (event.getItemDrop().equals(Material.IRON_ORE)) {
    42. event.getItemDrop().remove();
    43. ItemStack iron = new ItemStack(Material.IRON_INGOT, 1);
    44.  
    45. Item item = l.getWorld().dropItem(l, iron);
    46. }
    47.  
    48. if (event.getItemDrop().equals(Material.EMERALD_ORE)) {
    49. event.getItemDrop().remove();
    50. ItemStack iron = new ItemStack(Material.EMERALD, 1);
    51.  
    52. Item item = l.getWorld().dropItem(l, iron);
    53. }
    54.  
    55. if (event.getItemDrop().equals(Material.REDSTONE_ORE)) {
    56. event.getItemDrop().remove();
    57. ItemStack iron = new ItemStack(Material.REDSTONE, 5);
    58.  
    59. Item item = l.getWorld().dropItem(l, iron);
    60. }
    61.  
    62. if (event.getItemDrop().equals(Material.GOLD_ORE)) {
    63. event.getItemDrop().remove();
    64. ItemStack iron = new ItemStack(Material.GOLD_INGOT, 1);
    65.  
    66. Item item = l.getWorld().dropItem(l, iron);
    67. }
    68.  
    69. if (event.getItemDrop().equals(Material.COAL_ORE)) {
    70. event.getItemDrop().remove();
    71. ItemStack iron = new ItemStack(Material.COAL, 1);
    72.  
    73. Item item = l.getWorld().dropItem(l, iron);
    74. }
    75.  
    76. if (event.getItemDrop().equals(Material.DIAMOND_ORE)) {
    77. event.getItemDrop().remove();
    78. ItemStack iron = new ItemStack(Material.DIAMOND, 1);
    79.  
    80. Item item = l.getWorld().dropItem(l, iron);
    81. }
    82. }
    83.  
    84. }
    85.  
    86. }
    87. }
    88.  
    89. }
    90.  
     
  2. Offline

    Totom3

    My guess is that at the moment your event handler is called, the item is still falling (not on ground). Your code doesn't get executed because the condition at lign 30 returns false and skips the code.

    What you would need is to have an EntityMoveEvent handler, but unfortunately Bukkit didn't add this type of event.

    So basically our only choice is to have a repeated task in which you would keep testing if the item in on the ground, and execute your code if it is, but of course that generates a lot of lag and is harder to use + buggy if you don't use it properly.
     
  3. Offline

    hintss

    um, what?

    Edit: :confused: it apparently isn't a thing :confused:
     
  4. Offline

    ChipDev

    Maybe a loop testing if the entity's location is liquid and equals lava.
     
  5. Offline

    stormneo7

    Here I thought that if an item is in fire or lava, it disappears.
     
  6. Offline

    Totom3

    hintss Yeah that's what I said sorry if it was unclear

    ChipDev An infinite loop/loop with delays would crash the server. You have to use a repeating task for that.
     
  7. Offline

    ChipDev

    Yes.. what made you think that I wanted a infinite loop?
     
  8. Offline

    Necrodoom

    stormneo7 which is true, if its in it for more than around half a second as far as I know.
     
  9. Offline

    Totom3

    ChipDev Well you need an infinite loop to constantly check if the item has the correct blocks under it...

    Edit: Nevermind -.- Just ignore this post
     
  10. Offline

    hintss

    no...
     
  11. Offline

    xJeremyCx

    I think you need to cancel the event first and remove the item thrown from player's inventory.
     
  12. Offline

    Totom3

  13. Offline

    mineshafter2202

    I solved this by scheduling a delayed task for 1 second, thanks for your help!
     
Thread Status:
Not open for further replies.

Share This Page