/me in minecraft isnt disabled by the plugin I wrote

问题: So in my server /me is an enabled command. I wanted to disable this because I don't want people to be able to do this. I'm learning java, so I decided to code something t...

问题:

So in my server /me is an enabled command. I wanted to disable this because I don't want people to be able to do this.

I'm learning java, so I decided to code something that disabled /me myself.

So I wrote the following code:

package com.ste999.disableme;

import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
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 implements Listener 
    @Override
    public void onEnable() {
        getLogger().info("disable me enabled");
        PluginManager pm = this.getServer().getPluginManager();
        pm.registerEvents(this, (this));
    }

    @Override
    public void onDisable() {
        getLogger().info("disable me disabled");
    }

    @EventHandler
    public void OnMe(AsyncPlayerChatEvent event)
    {
        Player p = event.getPlayer();
        if(!p.hasPermission("ste999.me")) {
            if (event.getMessage().startsWith("/me")) {
                event.setCancelled(true);
                p.sendMessage("§4Dont me me!");
                }
            }
        }
}

with the following plugin.yml file:

name: Disable_Me
main: com.ste999.disableme.Main
version: 1.0
load: startup
description: this is should disable me
commands:

Now if someone without op would run /me hello it shouldn't output to the chat and the user should get a message like Dont me me!

But it doesn't. the user is still able to do /me hello without op and the code should prevent that

As I'm fairly new to java this error is probably easy to find, and any help would be much appreciated.


回答1:

The problem is that AsyncPlayerChatEvent only gets called when actually typing chat messages (not commands). For commands you have to use PlayerCommandPreprocessEvent as wonderfully explained by Mischa in the comments. Changing the event will make it work:

@EventHandler
public void disableMeCommand(PlayerCommandPreprocessEvent event) {
    Player p = event.getPlayer();
    if(!p.hasPermission("ste999.me")) {
        if(event.getMessage().startsWith("/me")) {
            event.setCancelled(true);
            p.sendMessage("§4Dont me me!");
        }
    }
}

However, note that PlayerCommandPreprocessEvent should be avoided. Luckily there is another way to disable a command completely in a bukkit server. You should have a commands.yml file located in your server folder. Simply add the "me" alias and set it to null inside the file:

aliases:
  me:
  - null
  • 发表于 2019-03-08 20:54
  • 阅读 ( 209 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除