Infinite Dispensers

Discussion in 'Plugin Development' started by ScottDurkin_, Nov 27, 2018.

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

    ScottDurkin_

    Hi all,

    I want to create an Infinite Dispenser listener but I notice I cant access the inventory of the dispenser to re-add the items that get dispensed in order to make it infinite.

    Does anyone have a solution for this?

    This is the event handler I am using:
    Code:
        @EventHandler
        public void onDispense(BlockDispenseEvent evt)
        {
                   
        }
     
  2. Online

    timtower Administrator Administrator Moderator

    @ScottDurkin_ You can get the block and work your way back to the inventory from there.
     
  3. Offline

    ScottDurkin_

    Hi Tim do you mean, Dispenser dis = (Dispenser)evt.GetBlock(); and work back from there? :)

    Can you share an example just to get me started? Kind of confused on how to do it..
     
  4. Online

    timtower Administrator Administrator Moderator

    Don't know the exact code and don't have an IDE on hand to make the code.
     
  5. Use
    Code:Java
    1. Block b = evt.getBlock();
    2. Dispenser d = (Dispenser) b.getState();
    3. Inventory inv = d.getInventory();


    but this event is also called when a dropper is triggered so make sure to add some checks before casting the BlockState
     
  6. Offline

    ScottDurkin_

    I am trying to add the items back into the dispenser but keep getting a console error?

    CODE::
    Code:
        @EventHandler
        public void onDispense(BlockDispenseEvent evt)
        {
            Block b = evt.getBlock();
            Dispenser d = (Dispenser) b.getState();
            Inventory inv = d.getInventory();
           
            ItemStack one = new ItemStack((ItemStack) b.getDrops());
           
            d.getInventory().addItem(one);
           
            Bukkit.broadcastMessage(inv.getName());
        }
    Console Error (open)

    [17:23:09 ERROR]: Could not pass event BlockDispenseEvent to HumbleFun v1.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:308) ~[minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) ~[minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) ~[minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.DispenseBehaviorItem.a(DispenseBehaviorItem.java:64) ~[minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.DispenseBehaviorItem.a(DispenseBehaviorItem.java:26) ~[minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.DispenseBehaviorItem.dispense(DispenseBehaviorItem.java:13) ~[minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.BlockDispenser.dispense(BlockDispenser.java:62) ~[minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.BlockDispenser.a(BlockDispenser.java:87) ~[minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.IBlockData.a(SourceFile:261) ~[minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.WorldServer.b(WorldServer.java:677) ~[minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.TickListServer.a(TickListServer.java:77) [minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.WorldServer.q(WorldServer.java:659) [minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.WorldServer.doTick(WorldServer.java:301) [minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.MinecraftServer.b(MinecraftServer.java:956) [minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.DedicatedServer.b(DedicatedServer.java:417) [minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:835) [minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:733) [minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
    Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.bukkit.inventory.ItemStack
    at org.humblefun.Listeners.LInfinteDispenser.onDispense(LInfinteDispenser.java:21) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_191]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_191]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_191]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_191]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:304) ~[minecraft_server.jar:git-Spigot-947a8e7-3a91182]
    ... 18 more
     
  7. Okay
    so the method getDrops() returns all items that would drop if the block would get destroyed - this is definitely not what we want to do. Also this method returns a collection which can't be casted into an itemstack (just as the error says) to get an item you would have to loop through the list

    but to get to your problem: you get the item which was dispensed by the event not the block. Instead use
    Code:Java
    1. evt.getItem();

    remember to check if the dispensing block really is a dispenser and not a dropper
     
  8. Offline

    ScottDurkin_

    Thank you very much for that!

    I am adding the evt.getItem() to the inventory but doesn't seem to add the block in. I am getting no error but I will keep trying different methods while you are offline, if I have no other reply than this I have not solved it :)
     
  9. Yeah try something on you own
    If you still got problems just post your code again and I'll have a look on it
     
  10. Offline

    ScottDurkin_

    Hey so I have been trying some things but the blocks dont seem to add back in?

    There are no errors in console either.

    CODE::
    Code:
            Block b = evt.getBlock();
            Dispenser d = (Dispenser) b.getState();
            Dropper dp = (Dropper)b.getState();
            Inventory inv = d.getInventory();
          
            ItemStack one = new ItemStack((ItemStack) evt.getItem());
          
            inv.addItem(one);
     
  11. With this you will get an error every single time. The block is either a dispenser or a dropper it can't be both at the same time.
    Code:Java
    1. Block b = e.getBlock();
    2. if(b.getState() instanceof Dispenser) {
    3. Dispenser d = (Dispenser) b.getState();
    4. Inventory inv = d.getInventory();
    5. inv.addItem(e.getItem());
    6. }


    EDIT: Also evt.getItem() returns an itemStack so you don't have to cast it
     
  12. Offline

    ScottDurkin_

    Okay I have seemed to notice if there is just 1 wood block, it will spit it out and maybe add it back but if there is 2 of the same type block it will use infinite?
     
  13. What do you mean? It will run infinite no matter which block or how many blocks are inside. Isn't that what you wanted?
     
  14. Offline

    ScottDurkin_

    So what is happening currently, if I put 1 wood block inside and trigger with redstone it will not be infinite but if I put 2 of the same block inside it becomes infinite which doesnt make sense because we are adding the same block back in no matter how much is originally in the dispenser?

    I will try make a quick video to showcase.

    Link to Gif: https://gyazo.com/dd92a69b60eedc8dfbf11ab65dd29f9f

    Unless I solve the issue by checking the inventory of the dispenser to make sure it creates a stack of whatever items in there like a loop of all slots and max them out?
     
  15. do this
    for me everything works just fine
     
  16. Offline

    ScottDurkin_

  17. okay looks like it depends on the minecraft version..
    I'll have a look at the 1.13 and tell you if I figured the problem out
     
  18. Offline

    ScottDurkin_

    No problem man, I do appreciate you helping :)
     
  19. Okay so I added a short delay before adding the item back to the inventory.
    Code:Java
    1. Block b = e.getBlock();
    2. if(b.getState() instanceof Dispenser) {
    3. Dispenser d = (Dispenser) b.getState();
    4. Inventory inv = d.getInventory();
    5. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    6. @Override
    7. public void run() {
    8. inv.addItem(e.getItem());
    9. }
    10. },1);
    11. }


    But i still have no clue why this works in 1.8 but not in higher versions
    Anyway, this should work :)
     
    ScottDurkin_ likes this.
  20. Offline

    ScottDurkin_

    Current problem::
    upload_2018-12-17_17-16-20.png

    Error: The method scheduleAsyncDelayedTask(Plugin, Runnable, long) in the type BukkitScheduler is not applicable for the arguments (LInfinteDispenser, new Runnable(){}, int)
     
  21. The listener is probably not in the the Main class.

    add in the Main:
    Code:java
    1. public static Main instance;
    2.  
    3. @Override
    4. public void onEnable() {
    5. instance = this;
    6. }


    and instead of 'this' in the scheduler:
    Main.instance
     
  22. Offline

    Zombie_Striker

    @DerDonut
    Actually, he should not do that. When it comes to programming with bukkit plugins, you should almost never use the static modifier. If you do, you are most likely not properly organizing your code.

    @ScottDurkin_
    If the other class needs the main instance, you will need to create a constructor for that class and have it require the main class instance. Do that by adding:
    Code:
    public MAIN_CLASS main;
    
    public YOUR_CLASS(MAIN_CLASS main){
      this.main = main;
    }
     
    Last edited by a moderator: Dec 18, 2018
  23. Offline

    ScottDurkin_

    That is how I gain access to the Main class all the time but good to know I am on the right track!
     
  24. Offline

    ScottDurkin_

    This worked perfectly!
     
  25. I'm glad to hear that :)
     
Thread Status:
Not open for further replies.

Share This Page