英文:
Minecraft Anti cheat ( java: cannot find symbol )
问题
我正在尝试使用Bukkit制作一个Minecraft反作弊插件。我的服务器版本是1.18。
但是我似乎遇到了这个错误:
符号: 方法getTitle()
位置: 类型为org.bukkit.inventory.Inventory的变量inventory```
这是错误引用的代码段;
```java
if (inventory.getTitle().equals("Flagged Players")) {
完整的代码段(不是整个代码文件):
package anticheat.anticheat;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.event.player.PlayerMoveEvent;
public class Anticheat implements Listener {
private Map<Player, Boolean> flaggedPlayers = new HashMap<>();
private Map<Player, Inventory> flaggedPlayersInventory = new HashMap<>();
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
if (isUsingFlyHack(player)) {
// 标记玩家并将其踢出服务器
flagPlayer(player);
player.kickPlayer("在本服务器上不允许使用飞行作弊");
}
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
Inventory inventory = event.getInventory();
if (inventory.getType() == InventoryType.CHEST) {
if (inventory.getTitle().equals("Flagged Players")) {
ItemStack item = event.getCurrentItem();
SkullMeta meta = (SkullMeta) item.getItemMeta();
Player flaggedPlayer = Bukkit.getPlayer(meta.getOwningPlayer().getName());
// 检查点击的物品是否是玩家头颅
if (item.getType() == Material.PLAYER_HEAD && flaggedPlayers.get(flaggedPlayer) != null) {
}
event.setCancelled(true);
}
}
}
}
我已经尝试用以下代码替换.getTitle
:
if (inventory.getTitle().equals("Flagged Players")) {
但这并没有解决问题,仍然给我相同的错误。
我还尝试了:
if (inventory.getType() == InventoryType.CHEST && inventory.getTitle().equals("Flagged Players")) {
但依然得到相同的输出。
英文:
Im trying to make a minecraft anti cheat with bukkit. my server version is 1.18.
but i seem to get this error;
java: cannot find symbol
symbol: method getTitle()
location: variable inventory of type org.bukkit.inventory.Inventory
here is the segment of code that error is referring about;
if (inventory.getTitle().equals("Flagged Players")) {
full segment of code (Not entire file of code):
package anticheat.anticheat;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.event.player.PlayerMoveEvent;
public class Anticheat implements Listener {
private Map<Player, Boolean> flaggedPlayers = new HashMap<>();
private Map<Player, Inventory> flaggedPlayersInventory = new HashMap<>();
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
if (isUsingFlyHack(player)) {
// Flag the player and kick them from the server
flagPlayer(player);
player.kickPlayer("Using fly hacks is not allowed on this server");
}
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
Inventory inventory = event.getInventory();
if (inventory.getType() == InventoryType.CHEST) {
if (inventory.getTitle().equals("Flagged Players")) {
ItemStack item = event.getCurrentItem();
SkullMeta meta = (SkullMeta) item.getItemMeta();
Player flaggedPlayer = Bukkit.getPlayer(meta.getOwningPlayer().getName());
// Check if the clicked item is a player head
if (item.getType() == Material.PLAYER_HEAD && flaggedPlayers.get(flaggedPlayer) != null) {
}
event.setCancelled(true);
}
}
}
I have tried to replace .getTitle
with .getName
using the code bellow
if (inventory.getTitle().equals("Flagged Players")) {
But that didn't fix it and gave me the same error.
**I have also tried **
if (inventory.getType() == InventoryType.CHEST && inventory.getTitle().equals("Flagged Players")) {
But also gave me the same output.
答案1
得分: 1
方法 getTitle
从 Inventory 中移除了。
您可以在您的代码中使用以下方式访问该值:
event.getView().getTitle()
英文:
The method getTitle
was removed from Inventory.
You can access that value in your code using:
event.getView().getTitle()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论