Bukkit-使用静态变量导致问题 [英] Bukkit - Using static variables causing problems

查看:118
本文介绍了Bukkit-使用静态变量导致问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有正在使用的该插件,我将解释该功能,以便您了解该怎么做:

-/ showcps:每当告诉发件人时指定玩家的左/右键单击

So I've got this plugin I'm working on, I'll explain the feature so that you get an idea of what it's suppose to do:
-/showcps : tells the sender whenever the specified player left/right clicks

因此,假设在运行此插件的服务器上有两个玩家Bob和Billy; Bob执行命令 / showcps Billy 并查看Billy何时单击。然后Billy键入命令 / showcps Bob ,现在Billy看到Bob单击的时间。问题是鲍勃无法再看到比利何时点击。

So, let's say there are two players, Bob and Billy on a server running this plugin; Bob does the command /showcps Billy and sees whenever Billy clicks. Then Billy types the command /showcps Bob, and now Billy sees when Bob clicks. The problem is that Bob can no longer see when Billy is clicking.

那是我的问题。我想我知道为什么会这样,但是我不确定如何解决。我使用了全局变量,以便能够在单击侦听器和命令执行程序类之间互换使用它们。

That's my problem. I think I know why it happens, but I'm not sure how to fix it. I used global variables to be able to use them interchangeably between the click listener and command executor classes.

以下是侦听器的代码:

@
EventHandler
public void leftClick(PlayerInteractEvent event) {
  Player player = event.getPlayer();

  if (event.getAction().equals(Action.LEFT_CLICK_AIR) | event.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
    if (player.equals(ClickViewToggle.targetPlayer)) {
      ClickViewToggle.recivingPlayer.sendMessage(
        ChatColor.LIGHT_PURPLE + player.getName() + ChatColor.DARK_PURPLE + " left clicked.");
    }
  }
}

@
EventHandler
public void rightClick(PlayerInteractEvent event) {
  Player player = event.getPlayer();

  if (event.getAction().equals(Action.RIGHT_CLICK_AIR) | event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    if (player.equals(ClickViewToggle.targetPlayer)) {
      ClickViewToggle.recivingPlayer.sendMessage(
        ChatColor.LIGHT_PURPLE + player.getName() + ChatColor.DARK_PURPLE + " right clicked.");
    }
  }
}

以下是代码命令执行程序:

Here is the code for the command executor:

public static Player targetPlayer = null;
public static Player recivingPlayer;

public boolean onCommand(CommandSender sender, Command command, String cmd, String[] args) {
  recivingPlayer = (Player) sender;
  if (args[0].isEmpty()) {
    if (sender instanceof Player) {
      sender.sendMessage(ChatColor.DARK_PURPLE + "You will now be able to see your clicks.");
      targetPlayer = (Player) sender;
      return false;
    } else {
      sender.sendMessage(ChatColor.DARK_PURPLE + "You must specify a player.");
      return false;
    }
  } else {
    boolean playerFound = false;
    for (Player player: Bukkit.getServer().getOnlinePlayers()) {
      if (player.getName().equalsIgnoreCase(args[0])) {
        sender.sendMessage(ChatColor.DARK_PURPLE + "You are now seeing " + ChatColor.LIGHT_PURPLE + player.getName() + ChatColor.DARK_PURPLE + "\'s clicks.");
        targetPlayer = player;
        playerFound = true;
        break;
      }
    }
    if (!playerFound) {
      sender.sendMessage(ChatColor.DARK_PURPLE + "Couldn't find " + ChatColor.LIGHT_PURPLE + args[0] + ChatColor.DARK_PURPLE + ".");
      targetPlayer = null;
    }
    return false;
  }
}

如您所见,这两个类之间使用了receivePlayer targetPlayer ,但是我觉得这就是他们俩无法同时观看不同点击的原因。

As you can see, receivingPlayer and targetPlayer are used between the two classes, but I feel like that's the reason that they both can't watch different clicks at the same time.

我该如何解决?

推荐答案

我建议您会在进入Bukkit之前学习Java,因为此代码存在严重问题。

I'd advise you to learn java before diving into Bukkit, because this code has serious problems.

您会遇到此问题,因为您有 1 变量来存储接收播放器和发送播放器。这样,如果您进行更改,则旧的接收播放器将不会收到任何新消息。使用哈希映射,将发送方玩家的UUID作为键,将接收方玩家的UUID作为值,然后当玩家在地图中时,向其相应的接收方发送一条消息。

Your problem occurs, because you have 1 variable to store the receiveng player and the sending player. This way, if you change it, then the old receiving player won't get any new messages. Use a hashmap with the sending player's UUID as key and the receiving player's UUID as value, then when a player is in the map, send a message to his corresponding receiver.

yoir代码中的其他问题:

The other problems in yoir code:

不要滥用静态,这是一个坏习惯,请使用getter和setter。

Don't abuse static, it's a bad practice, use getters and setters.

用==而不是.equal比较枚举

Compare enums with == instead of .equal

插件中的事件类型应该只有1个事件处理程序。

You should have only 1 eventhandler for an event type in your plugin.

从不存储完整的播放器对象,如果您不够注意的话,它们会导致内存泄漏。

Never store full player objects, they cab cause a memory leak, if you're not careful enough.

args [ 0] .isEmpty()仅检查args [0]是否等于情绪字符串,如果播放器未指定参数,则将抛出错误。

args[0].isEmpty() only checks, if args[0] equals to an emoty string, if the player doesn't specify an argument, then this will throw an error.

这些问题与我们在 http://bukkit.org 论坛上遇到的问题相同遇到90%的时间。使用该论坛解决基于Bukkit的问题,人们可以在那里提供更多帮助。

These are the same kind of problems we at the http://bukkit.org forums encounter 90% of the time. Use that forum for Bukkit based questions, people can help more there.

这篇关于Bukkit-使用静态变量导致问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆