我正在为一个朋友制作插件,他想要一个用于杂草生长的插件。
我希望如果我右键单击带有种子的FlowerPot,则在FlowerPot中会生成一个橡树树苗,但我遇到了错误。我尝试了很多事情来纠正此错误,但我不知道该怎么做。

    @EventHandler
    public void onPlace(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        //----------------------------------------------------//
        ItemStack amnesiaHaze = new ItemStack (Material.BEETROOT_SEEDS, 1);
        ItemMeta amnesiaMeta = amnesiaHaze.getItemMeta();
        amnesiaMeta.setDisplayName("§2Amnesia-Haze Seeds");
        amnesiaHaze.setItemMeta(amnesiaMeta);
        //----------------------------------------------------//


        Block block = e.getClickedBlock();
        if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if(block.getType() == Material.FLOWER_POT) {
                if(p.getInventory().getItemInMainHand().equals(amnesiaHaze)) {
                    ((FlowerPot) block.getState().getData()).setContents(new MaterialData(Material.OAK_SAPLING));
                    block.getState().update();
                    p.getInventory().getItemInHand().setAmount(p.getItemInHand().getAmount()-1);
                }
            }
        }
    }


错误:

[14:25:20] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to DeniPlugin v1.0
org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:442) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.PlayerInteractManager.a(PlayerInteractManager.java:441) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1250) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:27) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:918) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:911) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:895) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:828) [spigot.jar:git-Spigot-800b93f-8160e29]
        at java.lang.Thread.run(Unknown Source) [?:?]
**Caused by: java.lang.ClassCastException: org.bukkit.material.MaterialData cannot be cast to org.bukkit.material.FlowerPot**
        at de.deni.survival.weedplugin.classes.L_CreatePlant.onPlace(L_CreatePlant.java:33) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        ... 19 more

最佳答案

FlowerPot类已被弃用,使用该类将在运行时导致错误。

https://www.spigotmc.org/threads/328883


之所以不赞成使用FlowerPot BlockState,是因为每个盆栽花卉现在都是其自己的材质,而不是图块实体。


https://www.spigotmc.org/threads/407454


成就,FlowerPot(BlockState)和NoteBlock(BlockState)类已被删除。尝试在1.13.x或1.14.x上使用这些类的插件将无法按预期工作,但是现在它们将出错。


更改花盆内容的唯一方法是更改​​块的材质。因此,您将不用在块上使用setContents来将材质更改为setType,而不是执行Material.POTTED_OAK_SAPLING

block.setType(Material.POTTED_OAK_SAPLING);

10-08 13:53