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.
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
Related
Sa as the title says, I have a plugin that detects swear words. What it does now is that it sends a message to the player. But I also want it to execute a command that is already in the game or from another plugin. I'm not sure how to do this. How is that done?
Here is my current code:
package
import com.beam.sweardetector.SwearDetector;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
public class EventsClass implements Listener {
SwearDetector plugin = SwearDetector.getPlugin(SwearDetector.class);
#EventHandler
public void onPlayerJoinEvent(PlayerJoinEvent event) {
Player player = event.getPlayer();
player.sendMessage(ChatColor.GOLD + "This server is running AntiSwear v1.0 by BeamCRASH");
}
#EventHandler
public void chatevent (AsyncPlayerChatEvent event) {
for(String s: event.getMessage().split(" ")) {
if(plugin.getConfig().getStringList("swears").contains(s)) {
event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.DARK_RED + "§lSwearing is not allowed on this server!");
}
}
}
}
Any help will be appreciated.
Thank you!
You can execute command as player :
event.getPlayer().performCommand("mycommand");
But, it will not if the player don't have enough permission.
To run command as console, use this :
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mycommand");
Also, you don't need the / at the beginning of command because it's only to say that it's a command and it can change between players.
Warn: you should run those code in sync. If you are async (specially from AsyncEvent), you should use this code :
Bukkit.getScheduler().runTask(plugin, () -> {
// my code
player.performCommand("kill");
});
I am trying to make a plugin and for some reason my onBlockPlace event does not work. Here is my code:
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class test implements Listener {
#EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
Player player = event.getPlayer();
System.out.println("Test!");
player.sendMessage("Test");
}
}
Am I missing something? Please help.
It looks like you already have all the stuff necessary in the listener class. So in your plugin class (the class that extends JavaPlugin) you'll want to use this function. I'll explain the steps but it'll be a mess so you can understand what the code is doing.
Create a new instance of your listener using Listener listener = new test();
Get the server's plugin manager
PluginManager manager = getServer.getPluginManager();
Register the listener with the plugin manager manager.registerEvents(listener, this);
Alright, So I had this problem on a project I've started in bukkit recently. As you will see I defined the head and everything, I set the owner of the head and applied it. But when I load in-game it shows normal Steve head! What i want it to do is when i execute the command "spawnmnz" It will spawn a minion with the sender/player's (In this case) head!
package me.frostgamersa;
import net.minecraft.server.v1_8_R3.ItemSkull;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.SkullType;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.java.JavaPlugin;
public class NewMinion extends JavaPlugin {
String minion_name = "§3Minion §bSpawn §fEgg §8- §a[Spawned]";
#Override
public void onEnable() {
}
#Override
public void onDisable() {
}
#SuppressWarnings("deprecation")
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label,
String[] args) {
Player player = (Player) sender;
if (cmd.getName().equalsIgnoreCase("spawnmnz")){
World world = player.getWorld();
Location loc = player.getLocation();
ItemStack p_skull = new ItemStack(Material.SKULL_ITEM, 1, (short)
SkullType.PLAYER.ordinal());
SkullMeta sm = (SkullMeta) p_skull.getItemMeta();
sm.setOwner(player.getName());
p_skull.setItemMeta(sm);
Zombie minion = (Zombie) world.spawn(loc, Zombie.class);
minion.setBaby(true);
minion.setCustomName(minion_name);
minion.setCustomNameVisible(true);
minion.getEquipment().setHelmet(p_skull);
return true;
}
return false;
}
}
Get the skinvalue from the players profile and then set it on the skull item usin reflection, if it is not clear to you how to do this i will provide you a api for that ;)
Does the server have an internet connection? It has to download the skin from the Mojang servers. Because of that, it might also just take a moment for the skin to appear on the head.
The import for the ItemSkull also seems weird, what server version do you use?
I am new to creating minecraft plugins, however not new to programming, I am following a tutorial very thoroughly, the video has good ratings so it is trusted, when watching the video the guy has no problems what so ever (Youtube video on developing minecraft plugins) , so I did some research into solutions but always the line through the code.
Eclipse gives me the option for: #SuppressWarnings("deprecation") which allows the code to be used still but I would rather have no need of that usage.
Basically my question is why is there the need of the line going through the code and how do I find a solution to get rid of it.
Main class:
package com.jc1;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class Core extends JavaPlugin
{
public Permission pPermission = new Permission("playerAbilities.allowed");
#Override
public void onEnable()
{
new BlockListener(this);
PluginManager pm = getServer().getPluginManager();
pm.addPermission(pPermission);
}
#Override
public void onDisable()
{
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(cmd.getName().equalsIgnoreCase("giveitems") && sender instanceof Player)
{
Player p = (Player) sender;
if(p.hasPermission("playerAbilities.allowed"))
{
p.setItemInHand(new ItemStack(Material.DIAMOND_BOOTS));
}
return true;
}
return false;
}
}
Secondary class:
package com.jc1;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class BlockListener implements Listener
{
public BlockListener(Core plugin)
{
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
#EventHandler
public void onBlockPlace(BlockPlaceEvent e)
{
Player p = e.getPlayer();
if(!p.hasPermission("playerAbilities.allowed"))
{
e.setCancelled(true);
}
}
}
The method is deprecated, meaning that it is not recommended to be used anymore and is most likely replaced with another method.
Methods that are deprecated may still work as intended.
A simple search for the method reveals (this) documentation, stating:
players can duel wield now use the methods for the specific hand instead
which referes to the #see references:
getItemInMainHand() and getItemInOffHand().
Use this:
player.getInventory().getItemInMainHand()
Instead of:
player.getItemInHand()
Hope this helps! :D
I'm relatively new to making bukkit plugins, and i have a basic understanding of java. My plugin won't work. From what i see on other forums, this is a common error but none of the solutions have worked.
Here is my error:
[16:18:19 ERROR]: Could not load 'plugins/MtgCraft.jar' in folder 'plugins'
org.bukkit.plugin.InvalidPluginException: Cannot find main class `me.sporech.MagictgCraft'
My plugin.yml:
name: MtgCraft
main: me.sporech.MagictgCraft
version: 1.8
author: Sporech
description: A basic plugin
My code is:
package me.sporech;
import java.util.Set;
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.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class MagictgCraft extends JavaPlugin {
public static MagictgCraft plugin;
#Override
public void onEnable(){
getLogger().info("this is the plugin doing it");
}
#Override
public void onDisable(){
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("hello") && sender instanceof Player) {
Player player = (Player) sender;
player.sendMessage("Hello, " + player.getName() + "!");
return true;
}
return false;
}
#EventHandler
public void onPlayerInteractBlock(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (player.getItemInHand().getType() == Material.STICK) {
player.getWorld().strikeLightning(player.getTargetBlock((Set<Material>) null, 200).getLocation());
}
}
}
The error is with your plugin.yml, not your code. Ensure that the plugin.yml is included in the default package and is inside your jar after exporting/zipping it.
It says that your description is invalid ("InvalidDescriptionException"); it may be too short, but that is just a guess. If lengthening your description does not work, try following the description with a ">" and a line break, then writing the description on the next line preceded by at least 8 spaces as shown in the example below from one of my plugins:
description: >
This super simple plugin has so many features your head may just implode.
The above works in my plugins, though honestly it should not be necessary. Still, it's worth a try.
EDIT:
For future readers who don't want to sift through comments, the problem here was that the plugin.yml was not included in the "src" folder or in the default package of the exported jar. Always make sure your plugin.yml is in your exported jar in the default package!