Minecraft反作弊(Java:找不到符号)

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

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(&quot;Flagged Players&quot;)) { 

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&lt;Player, Boolean&gt; flaggedPlayers = new HashMap&lt;&gt;();
private Map&lt;Player, Inventory&gt; flaggedPlayersInventory = new HashMap&lt;&gt;();
@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(&quot;Using fly hacks is not allowed on this server&quot;);
}
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
Inventory inventory = event.getInventory();
if (inventory.getType() == InventoryType.CHEST) {
if (inventory.getTitle().equals(&quot;Flagged Players&quot;)) {
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 &amp;&amp; flaggedPlayers.get(flaggedPlayer) != null) {
}
event.setCancelled(true);
}
}
}

I have tried to replace .getTitle with .getName using the code bellow

if (inventory.getTitle().equals(&quot;Flagged Players&quot;)) {

But that didn't fix it and gave me the same error.

**I have also tried **

if (inventory.getType() == InventoryType.CHEST &amp;&amp; inventory.getTitle().equals(&quot;Flagged Players&quot;)) {

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()

huangapple
  • 本文由 发表于 2023年2月14日 05:15:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441257.html
匿名

发表评论

匿名网友

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

确定