1.12.2 Placing Ripe Crops

Discussion in 'Spigot Plugin Development' started by Toadiboy, Mar 20, 2020.

Thread Status:
Not open for further replies.
  1. Hello, I'm currently making a plugin that contains a farming NPC, where every available Dirt/Grass Block gets turned into Farmland and try to place wheat blocks that are instantly grown up. However, everytime I try to set the block data to 7 or CropState.RIPE, it either stays in its first state or it throws out an error like this:

    Code (Java):
    org.bukkit.event.EventException: null
        at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:72) ~[patched_1.12.2.jar:git-Paper-1618]
        at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:78) ~[patched_1.12.2.jar:git-Paper-1618]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[patched_1.12.2.jar:git-Paper-1618]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:513) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:2002) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:14) ~[patched_1.12.2.jar:git-Paper-1618]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[?:?]
        at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:850) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:423) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:774) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:666) ~[patched_1.12.2.jar:git-Paper-1618]
        at java.lang.Thread.run(Thread.java:834) [?:?]
    Caused by: java.lang.IllegalArgumentException: Provided data is not of type org.bukkit.material.MaterialData, found org.bukkit.material.Crops
        at org.bukkit.craftbukkit.v1_12_R1.block.CraftBlockState.setData(CraftBlockState.java:95) ~[patched_1.12.2.jar:git-Paper-1618]
        at skyvide.de.citycraft.npc.FarmerNPC.startFarming(FarmerNPC.java:321) ~[?:?]
        at skyvide.de.citycraft.events.InvClickEvent.onInvClick(InvClickEvent.java:83) ~[?:?]
        at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor1704.execute(Unknown Source) ~[?:?]
        at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:70) ~[patched_1.12.2.jar:git-Paper-1618]
        ... 15 more
    Or a cast exception:

    Code (Java):
    org.bukkit.event.EventException: null
        at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:72) ~[patched_1.12.2.jar:git-Paper-1618]
        at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:78) ~[patched_1.12.2.jar:git-Paper-1618]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[patched_1.12.2.jar:git-Paper-1618]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:513) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:2002) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:14) ~[patched_1.12.2.jar:git-Paper-1618]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[?:?]
        at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:850) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:423) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:774) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:666) ~[patched_1.12.2.jar:git-Paper-1618]
        at java.lang.Thread.run(Thread.java:834) [?:?]
    Caused by: java.lang.ClassCastException: class org.bukkit.material.MaterialData cannot be cast to class org.bukkit.material.Crops (org.bukkit.material.MaterialData and org.bukkit.material.Crops are in unnamed module of loader 'app')
        at skyvide.de.citycraft.npc.FarmerNPC.startFarming(FarmerNPC.java:321) ~[?:?]
        at skyvide.de.citycraft.events.InvClickEvent.onInvClick(InvClickEvent.java:83) ~[?:?]
        at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor1445.execute(Unknown Source) ~[?:?]
        at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:70) ~[patched_1.12.2.jar:git-Paper-1618]
        ... 15 more
    Even though Crops extends to MaterialData.

    This is my current code:

    Code (Java):
    // Places soil and crops on every grass/dirt block
            for(Location loc : blocks) {
                if(loc.getBlock().getType().equals(Material.GRASS) || loc.getBlock().getType().equals(Material.DIRT)
                        || loc.getBlock().getType().equals(Material.SOIL)) {
                    loc.getBlock().setType(Material.SOIL);
                   
                    switch (getProducedMat()) {
                        case WHEAT:
                            loc.add(0, 1, 0).getBlock().setType(Material.CROPS);
                            break;
                        case BEETROOT:
                            loc.add(0, 1, 0).getBlock().setType(Material.BEETROOT_BLOCK);
                            break;
                        case CARROT_ITEM:
                            loc.add(0, 1, 0).getBlock().setType(Material.CARROT);
                            break;
                        case POTATO_ITEM:
                            loc.add(0, 1, 0).getBlock().setType(Material.POTATO);
                            break;
                        default:
                            loc.add(0, 1, 0).getBlock().setType(Material.CROPS);
                            break;
                    }
                   
                    Crops crop = new Crops(CropState.RIPE);
                    loc.add(0, 1, 0).getBlock().getState().setData(((MaterialData)crop));
                    loc.add(0, 1, 0).getBlock().getState().update(true);
                   
                    MAT_CACHE++;
                }
            }
    Any help is very appreciated :)
     
  2. Choco

    Moderator

    1. You're on a fork. Seek support elsewhere next time.

    2. Are you writing using Java 11 or higher? Why is this a modular project?

    3. If I'm not mistaken, the block's type has not yet been set. It will set on the next tick. What I advise you do instead is get the block's state (Block#getState() - ASSIGN IT TO A VARIABLE! Each call returns a clone of the state), call BlockState#setData(MaterialData) followed by BlockState#update(). That should change its type.
     
  3. Hey! Thanks for the quick reply :) I am using Java 9 because I couldnt find any Java 8 packages on Linux anymore and I have no idea why its a modular project. Now I changed the code so that I assigned everything with variables:


    Code (Java):

    Crops crop = new Crops(CropState.RIPE);
    MaterialData data = ((MaterialData)crop);
    BlockState state = loc.add(0, 1, 0).getBlock().getState();
    state.setData(data);
    state.update(true);
     
    but that gives me this error:

    Code (Text):
    org.bukkit.event.EventException: null
        at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:72) ~[patched_1.12.2.jar:git-Paper-1618]
        at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:78) ~[patched_1.12.2.jar:git-Paper-1618]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[patched_1.12.2.jar:git-Paper-1618]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:513) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:2002) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:14) ~[patched_1.12.2.jar:git-Paper-1618]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[?:?]
        at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:850) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:423) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:774) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:666) ~[patched_1.12.2.jar:git-Paper-1618]
        at java.lang.Thread.run(Thread.java:834) [?:?]
    Caused by: java.lang.IllegalArgumentException: Provided data is not of type org.bukkit.material.MaterialData, found org.bukkit.material.Crops
        at org.bukkit.craftbukkit.v1_12_R1.block.CraftBlockState.setData(CraftBlockState.java:95) ~[patched_1.12.2.jar:git-Paper-1618]
        at skyvide.de.citycraft.npc.FarmerNPC.startFarming(FarmerNPC.java:324) ~[?:?]
        at skyvide.de.citycraft.events.InvClickEvent.onInvClick(InvClickEvent.java:83) ~[?:?]
        at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor320.execute(Unknown Source) ~[?:?]
        at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:70) ~[patched_1.12.2.jar:git-Paper-1618]
        ... 15 more
    Should I implement a one tick timer before setting the block state?

    EDIT: Okay I just tried to execute it after one tick but it returns the same error just one tick later..
     
    #3 Toadiboy, Mar 20, 2020
    Last edited: Mar 20, 2020
  4. Choco

    Moderator

    Don't cast it to MaterialData. Crops is already an instance of MaterialData. It extends it. Just pass that instance directly. Not sure why it's throwing an exception. Perhaps that an error that Paper throws for one of their changes (and again is the reason you should be seeking support there instead)
     
  5. No, sadly this error gets also thrown in Spigot..

    Code (Java):
    Crops crop = new Crops(CropState.RIPE);
                    BlockState state = loc.add(0, 1, 0).getBlock().getState();
                    state.setData(crop);
                    state.update(true);
                   
    Code (Text):
    [03:51:55 ERROR]: Could not pass event InventoryClickEvent to CityCraft v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:72) ~[patched_1.12.2.jar:git-Paper-1618]
            at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:78) ~[patched_1.12.2.jar:git-Paper-1618]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[patched_1.12.2.jar:git-Paper-1618]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:513) ~[patched_1.12.2.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:2002) ~[patched_1.12.2.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) ~[patched_1.12.2.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[patched_1.12.2.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:14) ~[patched_1.12.2.jar:git-Paper-1618]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_241]
            at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_241]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) ~[patched_1.12.2.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:850) ~[patched_1.12.2.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:423) ~[patched_1.12.2.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:774) ~[patched_1.12.2.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:666) ~[patched_1.12.2.jar:git-Paper-1618]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.IllegalArgumentException: Provided data is not of type org.bukkit.material.MaterialData, found org.bukkit.material.Crops
            at org.bukkit.craftbukkit.v1_12_R1.block.CraftBlockState.setData(CraftBlockState.java:95) ~[patched_1.12.2.jar:git-Paper-1618]
            at skyvide.de.citycraft.npc.FarmerNPC.startFarming(FarmerNPC.java:321) ~[?:?]
            at skyvide.de.citycraft.events.InvClickEvent.onInvClick(InvClickEvent.java:83) ~[?:?]
            at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor62.execute(Unknown Source) ~[?:?]
            at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:70) ~[patched_1.12.2.jar:git-Paper-1618]
            ... 15 more
     
  6. Location#add mutates the location being operated on. What you're doing is essentially setting block at y 0, setting data of block at y 1, then updating block at y 2.
     
    • Like Like x 1
    • Agree Agree x 1
  7. Yes that worked! Thank you so much :)
     
Thread Status:
Not open for further replies.