Error in Mob Spawning In Bukkit - java

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?

Related

I have a minecraft plugin that detects swear words. How do I execute commands when the player curses?

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");
});

How do I test if the player in an onPlayerRenameItem event has a certain permission?

I made an anvil Color rename plugin but i want to restrict the feature to users with a specific permission. The current code throws this error: The method getPlayer() is undefined for the type PrepareAnvilEvent
My listener:
package com.delight.anvilcolorrename;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class AnvilColorRenameListener implements Listener {
//Constructor
public AnvilColorRenameListener(Main plugin) {
}
//EventHandler
#EventHandler
public void onPlayerRenameItem(PrepareAnvilEvent event){
if (event.getPlayer().hasPermission("AnvilColorRename.use")) {
if(event.getResult() != null && event.getResult().hasItemMeta() && event.getInventory().getRenameText() != ""){
ItemStack result = event.getResult();
ItemMeta resultMeta = result.getItemMeta();
String nameColored = ChatColor.translateAlternateColorCodes('&', event.getInventory().getRenameText());
resultMeta.setDisplayName(nameColored);
result.setItemMeta(resultMeta);
}
}// if(!event.hasPermission("AnvilColorRename.use")) {
// event.sendMessage("You can not use color codes in an anvil!");
// }
}
//}
Please refer to the official Spigot javadocs: https://hub.spigotmc.org/javadocs/spigot
The PrepareAnvilEvent class does not have a method that returns a player.
The problem is that the PrepareAnvilEvent doesn't come with a Player field that you can access. However, you can get the Player via the InventoryView:
event.getInventoryView().getPlayer()

string sends command name instead of text after command

so I'm trying to do broadcast command but it sends out the command name I don't know why? if you have any idea why and how to solve this problem
package ml.harrytubestudios.helloworld.commands;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import ml.harrytubestudios.helloworld.main;
public class bro implements CommandExecutor {
private main plugins;
#Override
public boolean onCommand(CommandSender sender, Command cmd, String no, String[] args) {
Bukkit.broadcastMessage(no);
return false;
}
}
Your problems here are twofold. If you look at the documentation for CommandExecutor; it's the same in Spigot as in Bukkit, you'll see that it says for onCommand:
If false is returned, then the "usage" plugin.yml entry for this command (if defined) will be sent to the player.
Because you're returning false, you're saying that the command wasn't entered correctly and the usage string should be sent to the CommandSender. You should return true if the command executed successfully.
However, you should still see your broadcastMessage, which you are. This is explained again in the documentation as it says for the 3rd argument (label):
Alias of the command which was used
This means that you are broadcasting the alias of the command (your no parameter) that the CommandSender used, rather than their arguments, which is what I presume you're after.
In order to get the arguments of the command used, you'll want the args parameter which is an array of Strings. You'll probably want to format this into one string for use with your broadcast; for which there are different solutions.
I found out the answer its that string is the command and the args is the arguments after the command so
package ga.harrytubestudios.helloworld.commands;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import ga.harrytubestudios.helloworld.main;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
public class shopcommand implements CommandExecutor {
private main pluign;
#Override
public boolean onCommand(CommandSender Sender, Command smd, String label, String[] args) {
Player p = (Player)Sender;
p.sendmessgae(args[0])
}

/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 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

Why is getItemInHand being crossed out (see image below)

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

Categories