Solved Replace Wheat to Seeds

Discussion in 'Spigot Plugin Development' started by JunekCZ, Jan 20, 2017.

  1. Good day,
    I'm creating WheatGuard plugin, which blocks cannot break wheat, when user jumps on it.
    But I want to make event, when user breaks wheat, it automatically replace by seed.

    Problem is with placing seeds. When i write "for example: bedrock", it place it, but seeds doesn't placing.
    Thanks for help.

    There is my code:
    Code (Text):

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
            BlockState block = event.getBlock().getState();
            if(block.getType() == Material.WHEAT || block.getType() == Material.CROPS) {
                block.setType(Material.SEEDS);
            }
        }
     
     
    #1 JunekCZ, Jan 20, 2017
    Last edited: Jan 20, 2017
  2. @EventHandler?
     
  3. JanTuck

    Supporter

    -Snipped-
     
    #3 JanTuck, Jan 20, 2017
    Last edited: Jan 20, 2017
  4. It's actually a interact event.

    Code (Text):
        @EventHandler(ignoreCancelled = true) fun onStomp(event: PlayerInteractEvent) {
            if (event.action == Action.PHYSICAL && event.clickedBlock.type == Material.SOIL)
                     event.isCancelled = true
        }
     
    • Like Like x 1
  5. Praya

    Benefactor

    Because block for seed that already placed is "Crops" not "Seeds"
     
  6. Praya

    Benefactor

    Because block for seed that already placed is "Crops" not "Seeds"
     
  7. So i should place Crops in some state?
     
  8. Praya

    Benefactor

    You should check the block break is Crops but with durability (data) = 7
    And set the replacement block to crops with data = 0

    Because crops with data 7 = crops that already mature (wheat)
     
  9. Okay, so i tried to use setState();

    Code (Text):
            if(block.getType() == Material.WHEAT || block.getType() == Material.CROPS) {
                Crops crops = (Crops) block;
                if(crops.getState() == CropState.RIPE){
                    crops.setState(CropState.GERMINATED);
                }
            }
    But it writes a very big bug.
    _______________
    Code (Text):
    [12:00:51] [Server thread/ERROR]: Could not pass event BlockBreakEvent to WheatGuard v1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PlayerInteractManager.breakBlock(PlayerInteractManager.java:293) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PlayerInteractManager.a(PlayerInteractManager.java:194) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:831) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PacketPlayInBlockDig.a(SourceFile:40) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PacketPlayInBlockDig.a(SourceFile:10) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_111]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
        at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_10_R1.block.CraftBlockState cannot be cast to org.bukkit.material.Crops
        at me.wheatguard.occamy.MeinKleinListener.onBlockBreak(MeinKleinListener.java:63) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[Spigot.jar:git-Spigot-6016ac7-10c10b3]
        ... 17 more
    [12:02:11] [Server thread/INFO]: JunekCZ issued server command: /plugman unload WheatGuard
    [12:02:11] [Server thread/INFO]: [WheatGuard] Disabling WheatGuard v1
    [12:02:50] [Server thread/INFO]: JunekCZ issued server command: /plugman load me.wheatguard.occamy
    [12:02:50] [Server thread/INFO]: [WheatGuard] Enabling WheatGuard v1
    [12:02:54] [Server thread/ERROR]: Could not pass event BlockBreakEvent to WheatGuard v1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PlayerInteractManager.breakBlock(PlayerInteractManager.java:293) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PlayerInteractManager.a(PlayerInteractManager.java:194) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:831) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PacketPlayInBlockDig.a(SourceFile:40) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PacketPlayInBlockDig.a(SourceFile:10) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_111]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
        at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_10_R1.block.CraftBlockState cannot be cast to org.bukkit.material.Crops
        at me.wheatguard.occamy.MeinKleinListener.onBlockBreak(MeinKleinListener.java:63) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[Spigot.jar:git-Spigot-6016ac7-10c10b3]
        ... 17 more
    [12:02:56] [Async Chat Thread - #3/INFO]: <JunekCZ> a[m
    [12:02:57] [Server thread/ERROR]: Could not pass event BlockBreakEvent to WheatGuard v1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PlayerInteractManager.breakBlock(PlayerInteractManager.java:293) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PlayerInteractManager.a(PlayerInteractManager.java:194) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:831) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PacketPlayInBlockDig.a(SourceFile:40) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PacketPlayInBlockDig.a(SourceFile:10) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_111]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
        at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [Spigot.jar:git-Spigot-6016ac7-10c10b3]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_10_R1.block.CraftBlockState cannot be cast to org.bukkit.material.Crops
        at me.wheatguard.occamy.MeinKleinListener.onBlockBreak(MeinKleinListener.java:63) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[Spigot.jar:git-Spigot-6016ac7-10c10b3]
        ... 17 more
     
     
  10. Praya

    Benefactor

    why you not just check the data of type material crops?
     
  11. Do you mean getData() and setData()?

    I tried to make that, but it wrote bug in netbeans.

    Code (Text):
            BlockState block = event.getBlock().getState();
            if(block.getType() == Material.WHEAT || block.getType() == Material.CROPS) {
                Crops crops = (Crops) block;
                if(crops.getData() == (byte) 7){
                    crops.setData((byte) 0);
                }
            }
        }
     
  12. JanTuck

    Supporter

    You could easily test the block.getType() by doing

    Code (Text):

    if (block instanceof Crops)
    {
           // OMG WE HAVE A CROPS.
    }
     
     
    • Funny Funny x 1
  13. Praya

    Benefactor

    Code (Text):
    final Block block = event.getBlock();

    if(block.getType().equals(Material.Crops) && block.getData() == (byte) 7){
        block.setData((byte) 0);
    }

    for now block.getData() and block.setData() is deprecated, i think md5 still making for new method
     
    #14 Praya, Jan 20, 2017
    Last edited: Jan 20, 2017
  14. So, how can I replace wheat in last state to first when setState() doesn't working?
     
  15. Praya

    Benefactor

    look at my code above
     
  16. Sorry, but i don't understand.
    How can i check data of block, if getData() and setData() doesn't work?

    I tried to make it different than i try, so i wrote it again

    Code (Text):
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Block blok = event.getBlock();
            Material matros = blok.getType();
            if(matros == Material.CROPS) {
                event.setCancelled(true);
                Crops type = new Crops(blok.getType());
                type.setState(CropState.SEEDED);
            }
        }
    Now it didn't writes bug, its going in good way. But block still don't changing.

    Thanks for your help.
     
  17. When can i write bump?
     
  18. I've been waiting for someone to say the right answer here for a while. Nobody has gotten it right.

    Crops are a block state.

    Check if Block#getState instanceof Crops, and if they are, then cast the state to crops, not the block itself. They don't extend the block.

    From there, you can set the state of the block.