尝试使用 GeyserMC API 时,Java lambda 出现错误。 Minecraft Bukkit

huangapple go评论72阅读模式
英文:

Java lambda error when trying to use GeyserMC API. Minecraft Bukkit

问题

I'm getting an error when trying to detect the click of a form button using GeyserMC's API. This is what it says:
在尝试使用GeyserMC的API检测表单按钮点击时,我遇到了一个错误。错误信息如下:
Inconvertible types; cannot cast '' to 'org.geysermc.cumulus.response.SimpleFormResponse'
不可转换的类型;无法将''转换为'org.geysermc.cumulus.response.SimpleFormResponse'

This is what i did/tried which is giving that error:
这是我尝试过的操作/方法,导致了这个错误:

package org.centoricraft.centoriui23;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.geysermc.cumulus.SimpleForm;
import org.geysermc.floodgate.api.FloodgateApi;
import org.geysermc.floodgate.api.player.FloodgatePlayer;
import org.geysermc.cumulus.response.CustomFormResponse;
import org.geysermc.cumulus.response.SimpleFormResponse;

public class CentoriUI23 extends JavaPlugin implements Listener {

    private FloodgateApi floodgateApi;

    @Override
    public void onEnable() {
        if (getServer().getPluginManager().getPlugin("Floodgate") != null) {
            floodgateApi = FloodgateApi.getInstance();
            getServer().getPluginManager().registerEvents(this, this);
        } else {
            getLogger().warning("Floodgate plugin not found! Disabling plugin...");
            getServer().getPluginManager().disablePlugin(this);
        }
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        if (floodgateApi.isFloodgatePlayer(player.getUniqueId())) {
            Bukkit.getScheduler().runTaskLater(this, () -> {
                FloodgatePlayer floodgatePlayer = floodgateApi.getPlayer(player.getUniqueId());
                if (floodgatePlayer != null) {
                    SimpleForm form = SimpleForm.builder()
                            .title("Centori UI")
                            .button("Play")
                            .button("Discord")
                            .build();
                    form.setResponseHandler((formPlayer, responseData) -> {
                        if (responseData instanceof SimpleFormResponse) {
                            SimpleFormResponse response = (SimpleFormResponse) responseData;
                            int buttonId = response.getClickedButtonId();
                            if (buttonId == 0) {
                                // Handle "Play" button click
                            } else if (buttonId == 1) {
                                // Handle "Discord" button click
                            }
                        } else if (responseData instanceof CustomFormResponse) {
                            CustomFormResponse response = (CustomFormResponse) responseData;
                            // Handle custom form response
                        }
                    });
                    floodgatePlayer.sendForm(form);
                }
            }, 20L); // Delay for 1 second (20 ticks)
        }
    }

}

<details>
<summary>英文:</summary>
Im getting an error when trying to detect the click of a form button using GeyserMC&#39;s API. This is what it says:
Inconvertible types; cannot cast &#39;&lt;lambda parameter&gt;&#39; to &#39;org.geysermc.cumulus.response.SimpleFormResponse&#39;
This is what i did/tried which is giving that error:

package org.centoricraft.centoriui23;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.geysermc.cumulus.SimpleForm;
import org.geysermc.floodgate.api.FloodgateApi;
import org.geysermc.floodgate.api.player.FloodgatePlayer;
import org.geysermc.cumulus.response.CustomFormResponse;
import org.geysermc.cumulus.response.SimpleFormResponse;

public class CentoriUI23 extends JavaPlugin implements Listener {

private FloodgateApi floodgateApi;
@Override
public void onEnable() {
if (getServer().getPluginManager().getPlugin(&quot;Floodgate&quot;) != null) {
floodgateApi = FloodgateApi.getInstance();
getServer().getPluginManager().registerEvents(this, this);
} else {
getLogger().warning(&quot;Floodgate plugin not found! Disabling plugin...&quot;);
getServer().getPluginManager().disablePlugin(this);
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
if (floodgateApi.isFloodgatePlayer(player.getUniqueId())) {
Bukkit.getScheduler().runTaskLater(this, () -&gt; {
FloodgatePlayer floodgatePlayer = floodgateApi.getPlayer(player.getUniqueId());
if (floodgatePlayer != null) {
SimpleForm form = SimpleForm.builder()
.title(&quot;Centori UI&quot;)
.button(&quot;Play&quot;)
.button(&quot;Discord&quot;)
.build();
form.setResponseHandler((formPlayer, responseData) -&gt; {
if (responseData instanceof SimpleFormResponse) {
SimpleFormResponse response = (SimpleFormResponse) responseData;
int buttonId = response.getClickedButtonId();
if (buttonId == 0) {
// Handle &quot;Play&quot; button click
} else if (buttonId == 1) {
// Handle &quot;Discord&quot; button click
}
} else if (responseData instanceof CustomFormResponse) {
CustomFormResponse response = (CustomFormResponse) responseData;
// Handle custom form response
}
});
floodgatePlayer.sendForm(form);
}
}, 20L); // Delay for 1 second (20 ticks)
}
}

}


</details>
# 答案1
**得分**: 0
我认为这一行`SimpleFormResponse response = (SimpleFormResponse) responseData;`可能是导致问题的原因。
在查看文档后,您设置`ResponseHandler`的方式已被弃用:https://github.com/GeyserMC/Cumulus/wiki/Updating-from-1.0-to-1.1-(and-2.0)#response-handling-changes
弃用的方式可能仍然被实现,但您使用的Lambda函数只返回一个字符串(可能是`responseData`),而不是两个(`formPlayer, responseData`):https://github.com/GeyserMC/Cumulus/blob/master/src/main/java/org/geysermc/cumulus/Form.java
<details>
<summary>英文:</summary>
I don&#39;t know which Version you are using, so I am guessing the newest.
I think the line `SimpleFormResponse response = (SimpleFormResponse) responseData;` is causing the problem. 
After looking into the documentation, the way you are setting the `ResponseHandler` is deprecated: https://github.com/GeyserMC/Cumulus/wiki/Updating-from-1.0-to-1.1-(and-2.0)#response-handling-changes
The deprecated way may still be implemented, but the lambda function you are using is only returning one String (probably `responseData`) and not two (`formPlayer, responseData`): https://github.com/GeyserMC/Cumulus/blob/master/src/main/java/org/geysermc/cumulus/Form.java
</details>

huangapple
  • 本文由 发表于 2023年5月7日 22:23:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定