ItemSpawnEvent not working for redstone ore

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

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

    DeadlyScone

    Code:java
    1. private ArrayList<Location> breakLocations = new ArrayList<Location>();
    2.  
    3. @EventHandler
    4. public void onBlockBreak(BlockBreakEvent e){
    5. breakLocations.add(e.getBlock().getLocation());
    6. }
    7.  
    8. @EventHandler
    9. public void onItemSpawn(ItemSpawnEvent e){
    10. Location l = e.getEntity().getLocation().getBlock().getLocation();
    11. if (breakLocations.contains(l)){
    12. e.setCancelled(true);
    13. breakLocations.remove(l);
    14. }
    15. }


    When you try to break blocks that drop multiples of itself, it doesn't remove all of the items dropped.
    Any ideas?
     
  2. Offline

    97WaterPolo

  3. Offline

    DeadlyScone

    97WaterPolo

    Well, I could never get the getDrops () method to work. Could you give me a usage example that you know works?

    My assumption is that the getDrops() only returns a copy of the drops. Meaning it has no tangible link to the dropped items. This is also why the getDrops().clear method doesn't work.

    And yes, I am trying to stop the block from dropping its drops without canceling or setting the block to air.
     
  4. Offline

    ROTN

    That's correct, getDrops() won't be of any use here. Instead you have to cancel the block break event and set the type to air, that's the only way to stop the drops without doing some fancy NMS hacks.
     
  5. Offline

    97WaterPolo

    DeadlyScone
    Ah, I never knew the drops wouldn't work, my bad.

    Just curious, but why do you not want to set it to air or cancel the event?
     
  6. Offline

    DeadlyScone

    97WaterPolo

    I don't want to interfere with other plugins i'am using that need to use the blockbreakevent(or plugins that need the use of the block.)

    ROTN
    you didn't read my original post? I might have to learn how to use NMS, or use static objects :/
     
  7. Offline

    97WaterPolo

    DeadlyScone
    Well have you actually tested it to see if cancelling the break and setting it to air does conflict?
    EDIT:
    DeadlyScone
    Well I did some googling and you might be able to do it. I would say you would have to use NMS, but the two methods I think handle block dropping are at lines 458, and 475 (leaning more towards this).
    https://github.com/Wolf-in-a-bukkit...src/main/java/net/minecraft/server/Block.java
    Not sure how (maybe someone else can step in if this is a possible way), but if you overwrite the a() method, that should prevent the block from dropping an item.

    EDIT2:
    Question: Are you having the issue of it not working with just Redstone ore? If so, it has two types, redstone ore and glowing redstone ore.
    Random Thought: If you don't want any blocks to drop items, why not just cancel this event entirely?

    Just curious, but if the item is thrown with a velocity out of a block (when it is dropped), then the location of it would never be the same.

    I don't have an IDE on my laptop, so the code I entered is probably not that right. But basically if the item is within .5 block of the stored location, it will cancel the event.
    Code:java
    1. private ArrayList<Location> breakLocations = new ArrayList<Location>();
    2.  
    3. @EventHandler
    4. public void onBlockBreak(BlockBreakEvent e){
    5. breakLocations.add(e.getBlock().getLocation());
    6. new BukkitRunnable(){
    7. public void run(){
    8. breakLocations.remove(e.getBlock().getLocation());
    9. }
    10. }.runTaskLater(Main.getInstance(), 10);
    11. }
    12.  
    13. @EventHandler
    14. public void onItemSpawn(ItemSpawnEvent e){
    15. for (Location l : breakLocations)
    16. if (l.distance(e.getEntity().getLocation()) <= .5){
    17. event.setCancelled(true);
    18. break;
    19. }
    20. }
     
  8. Offline

    DeadlyScone

    97WaterPolo

    no about just the bug happening on the redstone(and yes jam aware there are two states) it was blocks that drop multiples. The real reason for this question was to see if there was an answer that didn't involve canceling or setting to air. I am making a plugin that adds the drops to the players inventory rather than dropping it(thus i would need to stop the drop event from occurring). Another issue i ran into was how do i get if a player uses fortune and get the additional drops atop that? silk touch? i came up with the idea to basically write the entire fortune random chance for every block plus add support for silk touch(also mob spawners ;D) I happened to notice making another plugin to run along side this, I asked on the same event(in the other plugin) to get the material and handle some code depending what block type it was. This is where I ran into the ISSUE, it was returning AIR; not so good :/ I believe the issue lied in the names of the plugins. The 'add item to inventory plugin' name started with a "n" and the other plugin started with a "r" so i was thinking it would always run the plugin starting closer to the letter "a" I was in trouble at this point.

    To sum it up i found a work around for my situation that involved not registering the blockbreakevent on the plugin(no drops) and calling the method from the other plugin under the right circumstances.

    Side Note: yes, i was thinking about the "get block relative" like you suggested, just never got around to doing it.

    thanks for the help, hopefully bukkit adds support to manipulate the drops(the correct drops and correct amounts) sometime soon, sure that would make life so much easier X_X

    #FixThisBukkit
     
    97WaterPolo likes this.
  9. Offline

    ROTN

    Firstly, forgive me if I repeat something someone else has said, don't have the time to read through the whole thread.
    I assume it will only ever remove one item. That is because you are removed the location from the list after cancelling the first item from spawning.
    It should be the same as it is spawned there. Additionally, he's getting the block at the location, and then getting it's location, making it the same location.
     
  10. Offline

    DeadlyScone

    ROTN
    Ah, you are correct sir. I assume I could get the amount of that item in the itemstack in the getDrops() within the blockbreakevent, add it to the list that many times; then remove each instance through a loop as long as it contains that location?

    hmm... not focused much on this method of doing it right now considering that i found a work around that worked for me. Although figuring this lack of mutable getDrops() method would be a nice touch :D
     
  11. Offline

    ROTN

    DeadlyScone, I've had to deal with the lack of mutable getDrops() before. Wasn't fun. Ended up cancelling the event, setting the block to air, then calculating the vanilla drop amounts (then using World#dropItemNaturally(Location, Item)), vanilla tool usage, etc. (all are based around a random generator).
     
Thread Status:
Not open for further replies.

Share This Page