员工聊天切换不起作用,您能看到我的代码有问题吗?

时间:2020-07-04 21:10:38

标签: java arraylist minecraft

我有这个StaffChat类,包括命令和事件。 该命令将播放器添加到StaffChat ArrayList,因此它是一个切换,但是我的问题是事件。我的注释标记该行不起作用,当该行不在代码中时,每次在游戏中进行聊天时,我都会向staffchat发送消息。

但是当我添加if(staffchat.contains(player)){}行时,该命令什么也没给我。如果你们中的任何一个可以帮助,那就太好了。

大约1星期前我才开始编码,所以我不太好。

package com.stellar.core.StellarCore;

import java.util.ArrayList;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
public class stellarcCore implements CommandExecutor, Listener {

    

    ArrayList<Player> staffchat = new ArrayList<>(); 
    
    String Prefix = ChatColor.translateAlternateColorCodes('&', "&8[&6StaffChat&8]: ");
    String Disabled = ChatColor.translateAlternateColorCodes('&', Prefix + "&cDisabled");
    String Enabled = ChatColor.translateAlternateColorCodes('&', Prefix + "&aEnabled");


    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        Player player = (Player) sender; 
        if (sender instanceof Player) {
            
            if (!player.hasPermission("stellarcore.staffchat.use")) {
                player.sendMessage(ChatColor.RED + "You do not have permission to use this command");
                System.out.println(player.getDisplayName() + " tried to use /scc, but didnt have permission");
                return false;
            }
            // When I got arguments, change '1' to '2'
            if (args.length != 0) {
                player.sendMessage(Prefix + ChatColor.RED + "Correct usage: /scc to enable/disable");
                return false;
                
            }
            
            if (staffchat.contains(player)) {
                staffchat.remove(player);
                player.sendMessage(Disabled);
                
            } else {
                staffchat.add(player);
                player.sendMessage(Enabled);
            }
            
        } else {
            System.out.println("Hello console, you cannot use this command");
        }
        return false;
    }
    
    @EventHandler
    public void onChat(AsyncPlayerChatEvent e) {
        Player player = e.getPlayer();
        //if (staffchat.contains(player)) {
            for (Player staff : Bukkit.getOnlinePlayers()) {
                if (staffchat.contains(player)) {
                    if (staff.hasPermission("stellarcore.staffchat.see")) {
                        staff.sendMessage(Prefix + e.getMessage());
                    }
                }
                
                e.setCancelled(true);
            }
        //}
        }
        
        
    } ```

0 个答案:

没有答案