Block Decay

Discussion in 'Plugin Development' started by DJCowGaming, Jun 6, 2019.

  1. Offline

    DJCowGaming

    I was wondering if there is any good way to add block decay. I initially tried to use the event BlockPlaceEvent, add delay, then setCancelled(true); which didn't work. I am new to bukkit, though not new to Java, and any help is appreciated.
     
  2. Offline

    KarimAKL

    @DJCowGaming Instead of using 'setCancelled(true)', try setting the block back by saving the material. (and in case of version < 1.13, also data)
    Something like this:
    Code:Java
    1. public void onPlace(BlockPlaceEvent event) {
    2. // check if block should decay
    3. Block block = event.getBlock();
    4. Material mat = block.getType();
    5. //short data = block.getData(); <--- If version is < 1.13
    6. /*Delay*/ {
    7. block.setType(mat);
    8. //block.setData(data); <--- If version is < 1.13
    9. }
    10. }

    Hope this helps. :)
     
  3. Offline

    DJCowGaming

    Thank you for the help. block.setType(mat) sets the block to the block it already was, so I changed it from block.getType(); to Material.AIR;. Also, I don't think scheduled tasks work with BlockPlaceEvent, so I used a for loop counting an integer down from the total decay time before the block is set to air. It works well enough, and the help you gave me was very useful.
     
  4. Offline

    Kars

    @DJCowGaming All good and well that it works, but that is a terrible idea. You need to schedule a task with delay, not hold up the main thread with a loop.
     
  5. Offline

    KarimAKL

    What makes you think that? Why wouldn't it work? The reason the 'setCancelled(true)' didn't work was because you can't cancel the event after a delay, by that time the event has already gone through as not cancelled; that's the reason i said to try and set the block back to the one you want instead of cancelling.
    If you have any other questions, let me know.
     
  6. Offline

    DJCowGaming

    Thanks for letting me know. I asked my AP computer science teacher what I should do, and he told me to use the java timer and schedule a timer task, then run the code.
     
    Last edited: Jun 24, 2019 at 11:11 AM
  7. Offline

    timtower Ninja on the waves Moderator

    What error message?
     
  8. Offline

    DJCowGaming

    [22:22:10 ERROR]: Could not pass event BlockPlaceEvent to TowerWarsPlugin v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:127) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.ItemStack.placeItem(ItemStack.java:158) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:503) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_201]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_201]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]
    Caused by: java.lang.ClassCastException: com.DJCowGaming.towerwars.event.block.Decay cannot be cast to org.bukkit.plugin.Plugin
    at com.DJCowGaming.towerwars.event.block.Decay.onDecay(Decay.java:16) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_201]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_201]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_201]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    ... 18 more

    If I remove the bukkit scheduled task, and just get the block, then set to air, it works fine.
     
  9. Offline

    timtower Ninja on the waves Moderator

    @DJCowGaming Caused by: java.lang.ClassCastException: com.DJCowGaming.towerwars.event.block.Decay cannot be cast to org.bukkit.plugin.Plugin
    You need an instance of your main thread, not of a Decay instance.
     

Share This Page