英文:
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 '
不可转换的类型;无法将'
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's API. This is what it says:
Inconvertible types; cannot cast '<lambda parameter>' to '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>
# 答案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'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论