当前位置: 代码迷 >> java >> 我的世界 java 插件中的玩家聊天事件没有做它应该做的事情
  详细解决方案

我的世界 java 插件中的玩家聊天事件没有做它应该做的事情

热度:22   发布时间:2023-07-26 13:56:27.0

所以我编写了以下代码:

package com.ste999.firstplugin;

import org.bukkit.plugin.java.JavaPlugin;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.player.AsyncPlayerChatEvent;

public class Main extends JavaPlugin {

    public Main() {}

    @Override
    public void onEnable() {
        getLogger().info("Stefan's first plugin enabled");
    }

        private volatile boolean chatEnabled = true;

    @EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent event) {
        Player pli = event.getPlayer();
        if (!chatEnabled) {
            if (!pli.hasPermission("ste.chat.bypass")) {
                pli.sendMessage("§4Chat is disabled!");
                event.setCancelled(true);
                //return true;
            }
        }
    }

        @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) 
    {
        Player p = (Player)sender;

        if (cmd.getName().equalsIgnoreCase("mutechat")) {
            chatEnabled = !chatEnabled;
            sender.sendMessage(chatEnabled ? "§aUnmuted the chat" : "§aMuted the chat");
            return true;
        }
        return true;
    }

    @Override
    public void onDisable() {
        getLogger().info("Stefan's first plugin disabled");
    }
}

使用以下 plugin.yml:

name: Stefans_Helper
main: com.ste999.firstplugin.Main
version: 1.0
load: startup
description: this is my first plugin
commands:
  mutechat:
    description: mute the chat
    usage: /<command>

当我在我的 Minecraft 服务器中使用此插件时,它会显示,如果我执行/mutechat ,它会显示Muted the chat当我再次执行/mutechat时,它会显示Unmuted the chat

我希望这段代码做的是当聊天“静音”时,没有用户可以说话,除非他们有ste.chat.bypass权限节点。

但是没有 op 和ste.chat.bypass的用户在有人做了/mutechat并且聊天说Muted the chat聊天后仍然可以在聊天中Muted the chat

我试过把getServer().getPluginManager().registerEvents(this, this); public void onEnable但后来我在 Eclipse 中收到一个错误,提示: The method registerEvents(Listener, Plugin) in the type PluginManager is not applicable for the arguments (Main, Main)

呃请帮忙

您的事件类(我为此认真推荐一个新类)需要实现 。 只有这样你才能注册它。

所以我能够使用以下代码获得 mutechat 函数:package com.ste999.events;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
//import org.bukkit.event.EventPriority;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;

public class Main extends JavaPlugin implements Listener{
    public static boolean chatMuted = false;
    @Override
    public void onEnable()
    {
        getLogger().info("events tester enabled!");
        PluginManager pm = this.getServer().getPluginManager();
        pm.registerEvents(this, (this));
    }

    @Override
    public void onDisable()
    {
        getLogger().info("events tester disabled!");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    {
        Player player = (Player)sender;
        if (cmd.getName().equalsIgnoreCase("mutechat")) {
            if (player.hasPermission("ste999.chat.mute")) {
            if(chatMuted == false) {
                Bukkit.broadcastMessage("§aThe chat has been disabled!");
                chatMuted = true;
                }
                else {
                if(chatMuted == true) {
                Bukkit.broadcastMessage("§aThe chat has been enabled!");
                chatMuted = false;
                }
                }
            } else {
                if (!player.hasPermission("ste999.chat.mute")) {
                player.sendMessage("§4You can't mute the chat silly!");
                }
            }
            }
    return false;
    }
    @EventHandler
    public void OnChat(AsyncPlayerChatEvent event)
    {
        Player pli = event.getPlayer();
        if (chatMuted == true) {
            if (!pli.hasPermission("ste999.chat.bypass")) {
                event.setCancelled(true);
                pli.sendMessage("§4The chat has been disabled");
            } else {
                if (pli.hasPermission("ste999.chat.bypass")) {
                    event.setCancelled(false);
                }
            }
        }
    }
}

我需要注册事件,但getServer().getPluginManager().registerEvents(this, this)对我不起作用,所以我需要按照我在onEnable中的代码做的方式onEnable ,还有其他一些问题

  相关解决方案