Command in my bukkitplugin not working - java

Now that I have changed the 'Commands' to 'commands' in the plugin.yml I get another error in my cmd when I run my server. The error says '.Jar file does not contain plugin.yml'.
This is my plugin.yml as of now:
name: Wand
version: 1.0
main: me.Pixel.Main
commands:
wand:
And this is my Main file currently:
package me.Pixel;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.Effect;
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.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
public Main plugin;
public List<String> spells = new ArrayList<String>();
public getTargets getTargets = new getTargets();
public Spark spark = new Spark(this);
public PoisonWave poisonwave = new PoisonWave(this);
public DarkSpark darkSpark = new DarkSpark(this);
#Override
public void onEnable() {
plugin = this;
getServer().getPluginManager().registerEvents(this, this);
spells.add("Spark");
spells.add("PoisonWave");
spells.add("DarkSpark");
}
#Override
public boolean onCommand(CommandSender sender, Command command,
String label, String[] args) {
if(label.equalsIgnoreCase("wand")) {
if(!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "You need to be an in-game player to perform this action!");
} else {
Player p = (Player) sender;
if(sender.hasPermission("wand.wand")) {
ItemStack stack = new ItemStack(Material.BLAZE_ROD);
ItemMeta stackMeta = stack.getItemMeta();
stackMeta.setDisplayName(ChatColor.RED + "Empire Wand");
stack.setItemMeta(stackMeta);
p.getInventory().addItem(stack);
ChatUtilities.sendMessage(p, "You have got yourself a powerful Empire Wand!");
} else {
ChatUtilities.sendMessage(p, ChatColor.RED + "ERROR: No Permission!");
}
}
}
return false;
}
#EventHandler
public void onClick(PlayerInteractEvent e) {
if((e.getAction() == Action.RIGHT_CLICK_AIR) || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
Player p = e.getPlayer();
ItemStack stack = p.getItemInHand();
if(stack != null && stack.getType() == Material.BLAZE_ROD && stack.hasItemMeta() && stack.getItemMeta().getDisplayName().equals(ChatColor.RED + "Empire Wand")) {
int SpellSelected = stack.getDurability();
if(SpellSelected < 2) {
stack.setDurability((short) (SpellSelected + 1));
p.getWorld().playEffect(p.getLocation(), Effect.STEP_SOUND, 119, 30);
} else {
stack.setDurability((short) 0);
}
ChatUtilities.sendMessage(p, "Selected: " + spells.get(SpellSelected));
}
}
if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) {
Player p = e.getPlayer();
ItemStack stack = p.getItemInHand();
if(stack != null && stack.getType() == Material.BLAZE_ROD && stack.hasItemMeta() && stack.getItemMeta().getDisplayName().equals(ChatColor.RED + "Empire Wand")) {
int SpellSelected = stack.getDurability();
if(SpellSelected == 1) {
this.spark.onCast(p);
} else if (SpellSelected == 0) {
this.poisonwave.onCast(p);
}
}
}
}
}

name: Wand
version: 1.0
main: me.Pixel.Main
commands:
wand:
Your plugin.yml file isn't valid. 'commands' must be all lower case, and there must be 2 spaces before wand.
Also, you should not make commands that way. You should make external classes implementing the CommandExecutor Interface.
I think, if you do it this way, I think you must register the command.
Like this:
getCommand("wand").setExecutor(this);

The plugin.yml file is case senstitive, the name of the list of commands needs to be all lowercase (commands instead of Commands). Since you currently have it capitalized, Bukkit/Spigot doesn't register any commands thus resulting in an "Unknown command. Type "/help" for help." message if you test the /wand command (I'm assuming this is the error you're getting, as you didn't describe the problem nor the expected behavior, but this is what happened when I tested the code, and correcting the name of the commands list made the command execute).

You might need to update the player's inventory after adding the item with p.updateInventory(). In your case, it would be something like this:
p.getInventory().addItem(stack);
p.updateInventory();

Related

How to use a string from another method

I am trying to make a plugin where when a certain block in the GUI is clicked, a command is run. The problem is, I have to transfer a string from one method to another. I don't have very much java knowledge, so if someone could teach me how to do this, I would very much appreciate it.
P.S. The string I am trying to use is called custompermscommand.
Code:
package me.eliminiquated.yugengrant;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
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.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
public class Grant implements CommandExecutor, Listener {
#Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equals("grant")){
if (sender instanceof Player){
Player player = (Player) sender;
if (player.hasPermission("yugen.grant")){
if (args.length == 1){
player.sendMessage("Incorrect Usage! </grant (username)>");
}else if (args.length == 2){
Inventory gui = Bukkit.createInventory(player, 27, ChatColor.DARK_GREEN+"Grant Menu");
ItemStack custom = new ItemStack(Material.ORANGE_GLAZED_TERRACOTTA);
ItemStack playerranks = new ItemStack(Material.LAPIS_BLOCK);
ItemStack staffpositions = new ItemStack(Material.REDSTONE_BLOCK);
ItemMeta custommeta = custom.getItemMeta();
ItemMeta playerranksmeta = playerranks.getItemMeta();
ItemMeta staffpositionsmeta = staffpositions.getItemMeta();
custommeta.setDisplayName(ChatColor.GOLD+"Custom");
ArrayList<String> custom_lore = new ArrayList<>();
custom_lore.add(ChatColor.GRAY+""+ChatColor.ITALIC+"Coming soon");
custommeta.setLore(custom_lore);
custom.setItemMeta(custommeta);
playerranksmeta.setDisplayName(ChatColor.AQUA+"Player Ranks");
playerranks.setItemMeta(playerranksmeta);
staffpositionsmeta.setDisplayName(ChatColor.RED+"Staff Positions");
staffpositions.setItemMeta(staffpositionsmeta);
gui.setItem(10, custom);
gui.setItem(12, playerranks);
gui.setItem(14, staffpositions);
player.openInventory(gui);
String custompermscommand = "grantcustomperms "+args[0]+"";
}else{
player.sendMessage("Incorrect Usage! </grant (username)>");
}
}else{
player.sendMessage(ChatColor.RED+"You do not have permission to execute this command!");
}
}
}else{
System.out.println("You must be a player to execute this command");
}
return false;
}
#EventHandler
public void onInventoryClick(InventoryClickEvent event){
Player player = (Player) event.getWhoClicked();
if (event.getClickedInventory().getTitle().equalsIgnoreCase(ChatColor.DARK_GREEN+"Grant Menu")){
if (event.getCurrentItem().getType() == Material.ORANGE_GLAZED_TERRACOTTA){
Bukkit.dispatchCommand(player, custompermscommand);
}
}
}
}
For anyone that wants to do this themselves, this is the answer:
First, declare the variable as public outside of the methods:
Example:
public String command;
Next, edit the string inside of the method that you want to grab the string from:
Example:
command = args[0];
Finally, you are able to call this string in whatever method you want:
Example:
Bukkit.dispatchCommand(player, command);

Server doesn't enable my spigot plugin. (It doesn't even try)

I'm basically making a spigot plugin, for practice.
The server doesn't enable the server, no error in the console. It also no "INFO" for enabling the plugin.
This is my main class
package me.FarrosGaming.SpawnPillager;
import org.bukkit.plugin.java.JavaPlugin;
import me.FarrosGaming.SpawnPillager.commands.PillagerCommand;
public class Main extends JavaPlugin {
#Override
public void onEnable() {
new PillagerCommand(this);
}
#Override
public void onDisable() {
}
}
This is my command class
package me.FarrosGaming.SpawnPillager.commands;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import me.FarrosGaming.SpawnPillager.Main;
public class PillagerCommand implements CommandExecutor {
private Main plugin;
public PillagerCommand(Main plugin) {
this.plugin = plugin;
plugin.getCommand("chase").setExecutor(this);
}
#Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players who can send this command.");
return true;
}
Player player = (Player) sender;
if (player.hasPermission("hello.use")) {
Location location = player.getLocation();
World world = player.getWorld();
for (int i = 0; i < 10; i++) {
world.spawnEntity(location, EntityType.CREEPER);
}
return true;
} else {
player.sendMessage("You do not have a permission to use this command");
}
return false;
}
}
this is my plugin.yml file
name: SpawnPillager
version: 1.0
author: FarrosGaming
description: blabla
main: me.FarrosGaming.SpawnPillager.Main
commands:
chase:
description: Spawn pillager in your location
usage: /chase
Literally, there are no spaces in the plugin.yml
I'm using the newest version of spigot.
make sure you are dropping your plugin's Jar in the right spot, i mean the right server's plugins folder, if you multiple and then reload/ restart your server,
I m telling this coz, me myself, have done this mistake too..

Changing Horse Attributes

I have been making a plugin for a "portable horse" recently and I am very close to being done, my horses spawn in and despawn as I want them. However, the only thing that I am having a considerable amount of trouble with is changing Horse attributes like speed, color, jump height and Variant.
Here is my Code:
package io.github.bxnie.events;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Donkey;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.vehicle.VehicleExitEvent;
import org.bukkit.inventory.ItemStack;
public class HorseSpawn implements Listener {
#SuppressWarnings({ "deprecation" })
#EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
Player p = e.getPlayer();
ItemStack item = e.getItem();
if(item.getItemMeta().getDisplayName().equals(ChatColor.GRAY + "Donkey")) {
Donkey donkey = (Donkey) p.getWorld().spawn(p.getLocation(), Donkey.class);
donkey.setAdult();
donkey.setTamed(true);
donkey.setOwner(p);
donkey.getInventory().setSaddle(new ItemStack(Material.SADDLE));
donkey.setCustomName(ChatColor.GRAY + "Donkey");
donkey.setPassenger(p);
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.RED + "Brown Horse")) {
Horse horsebrown = (Horse) p.getWorld().spawn(p.getLocation(), Horse.class);
horsebrown.setAdult();
horsebrown.setTamed(true);
horsebrown.setOwner(p);
horsebrown.getInventory().setSaddle(new ItemStack(Material.SADDLE));
horsebrown.setCustomName(ChatColor.RED + "Horse");
horsebrown.setPassenger(p);
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.DARK_GRAY + "Black Horse")) {
Horse horseblack = (Horse) p.getWorld().spawn(p.getLocation(), Horse.class);
horseblack.setAdult();
horseblack.setTamed(true);
horseblack.setOwner(p);
horseblack.getInventory().setSaddle(new ItemStack(Material.SADDLE));
horseblack.setCustomName(ChatColor.DARK_GRAY + "Horse");
horseblack.setPassenger(p);
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.WHITE + "White Horse")) {
Horse horsewhite = (Horse) p.getWorld().spawn(p.getLocation(), Horse.class);
horsewhite.setAdult();
horsewhite.setTamed(true);
horsewhite.setOwner(p);
horsewhite.getInventory().setSaddle(new ItemStack(Material.SADDLE));
horsewhite.setCustomName(ChatColor.WHITE + "Horse");
horsewhite.setPassenger(p);
}
}
}
#EventHandler
public void onPLayerDismount(VehicleExitEvent e) {
if(e.getExited() instanceof Player) {
if(e.getVehicle() instanceof Donkey) {
Donkey donkey = (Donkey) e.getVehicle();
if(donkey.getCustomName() != null) {
if(donkey.getCustomName().equals(ChatColor.GRAY + "Donkey")) {
donkey.remove();
}
}
}
if(e.getVehicle() instanceof Horse) {
Horse horse = (Horse) e.getVehicle();
if(horse.getCustomName() != null) {
if(horse.getCustomName().equals(ChatColor.RED + "Horse")) {
horse.remove();
}
if(horse.getCustomName().equals(ChatColor.DARK_GRAY + "Horse")) {
horse.remove();
}
if(horse.getCustomName().equals(ChatColor.WHITE + "Horse")) {
horse.remove();
}
}
}
}
}
}
I've done a ton of research but nothing really seems to help my case, is there any way that any of you know of to spawn in a horse with the custom attributes I mentioned above?
When you believe you've researched enough, research a little more, I searched for "bukkit change horse speed" and one of the top answers works perfectly. With that being said, let's try to solve your issue.
Everything you need is right in the horse API, more specifically #setColor(), #setStyle(), #setJumpStrength and #setVariant(). Changing variant may not work using that method due to deprecation, instead you need to create a different entity, such as a skeletonHorse
These methods are really straightforward:
Horse horseblack = (Horse) p.getWorld().spawn(p.getLocation(), Horse.class);
horseblack.setJumpStrength(2.0);
horseblack.setColor(Color.BLACK);
SkeletonHorse skeletonHorse = (SkeletonHorse) p.getWorld().spawn(p.getLocation(), SkeletonHorse.class);
Changing speed is a little different, but the method works for any mobs.
entity.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED).setBaseValue(yourValue);

Bukkit plugin not working?

So I recently attempted to make a bukkit plugin for minecraft.
The idea is that this would just be a test plugin to see if I could do it, and apparently I cant. This is the code I came up with
package me.glowhoo.BlockChanger; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class BlockChanger extends JavaPlugin
{
#Override public void onEnable() {
Bukkit.getLogger().info(this.getDescription().getName() +
" has been enabled");
}
#Override public void onDisable() {
Bukkit.getLogger().info(this.getDescription().getName() + " has been disabled");
}
#Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
/*command: /tpa Glowhoo
* args.length = 1
* args[0] = Glowhoo
* command.getName() = tpa
*/
if (cmd.getName().equalsIgnoreCase("message")){
if(sender instanceof Player)
{
/*ComandSender sender-who sent the command
* Command cmd- the command that was executed
* String commandLabel-the command alias that was used
* String[] args-array of additional arguments
*/
sender.sendMessage(ChatColor.RED +"Hello player!");
}else
{
sender.sendMessage(ChatColor.AQUA +"Hello console!");
}
}
return false;
}
}
So the problem with this, is that whenever I load up the server, it says the "BlockChanger has been enabled", but then when I try to type the only command it has (message) or /message, Nothing happens and it says it is an unrecognized command and to type /help for info. The problem doesn't end there though, whenever I actually try to get the plugin going, 90% of the time i have a plugin.yml error, which I don't completely understand, but if any of you know anything about this issue, please respond
My plugin.yml file is here (NOTE: this plugin.yml copy actually works, but the command itsself doesn't.) :
name: BlockChanger
version: 1.0
main: me.glowhoo.BlockChanger.BlockChanger
description:
commands:
message:
description: -no desc-
usage: /message
Your plugin.yml is incorrect. You need to indent message under commands to define a node. Currently commands is a key value for nothing. Change the relevant part to this:
commands:
message:
usage: /message
This is some time in the future now, but in case you use this again, when you have a command, you need to return true, else it will tell you it is an incorrect command, even if your .yml/.yaml file is set up right. So here is what you would need to do to make it register your command when called:
package me.glowhoo.BlockChanger; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class BlockChanger extends JavaPlugin
{
#Override public void onEnable() {
Bukkit.getLogger().info(this.getDescription().getName() +
" has been enabled");
}
#Override public void onDisable() {
Bukkit.getLogger().info(this.getDescription().getName() + " has been disabled");
}
#Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
/*command: /tpa Glowhoo
* args.length = 1
* args[0] = Glowhoo
* command.getName() = tpa
*/
if (cmd.getName().equalsIgnoreCase("message")){
if(sender instanceof Player)
{
/*ComandSender sender-who sent the command
* Command cmd- the command that was executed
* String commandLabel-the command alias that was used
* String[] args-array of additional arguments
*/
sender.sendMessage(ChatColor.RED +"Hello player!");
}else
{
sender.sendMessage(ChatColor.AQUA +"Hello console!");
}
return true;
}
return false;
}
}
U need to register this class as command executor in onEnable method.

I need to know how to test if a value is not equal to multiple strings java

How do i do this?
i want to test if the sign line 2 (1) is equal to "Closed" or "Open" and if not i want to say to please specify if its open or closed it says that if i type open or closed
where the || is does not work...
package me.mcmatt.shops;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
public class Signs implements Listener {
#EventHandler
public void onSignChange(SignChangeEvent e) {
if (e.getLine(0).equalsIgnoreCase("[shop]")) {
Block attached = e.getBlock().getRelative(0, -1, 0);
String name = e.getPlayer().getDisplayName();
if (!(attached.getType() == Material.CHEST))
e.getPlayer().sendMessage(ChatColor.RED + "Please place the shop on a chest!");
else {
if (!e.getPlayer().hasPermission("shops.create"))
e.getPlayer().sendMessage(ChatColor.RED + "You don't have permission to create a shop! (shops.create)");
else {
if (!e.getLine(1).equalsIgnoreCase("open") || (!e.getLine(1).equalsIgnoreCase("closed"))) {
e.getPlayer().sendMessage(ChatColor.RED + "You must specify if the shop is open or closed on the second line!");
} else {
Sign o = (Sign) e.getBlock().getState();
String p = o.getLine(1);
e.setLine(0, "§9[Shop]");
e.setLine(1, "§4" + name + "'s");
e.setLine(2, "§4Shop");
e.setLine(3, p);
e.getPlayer().sendMessage(ChatColor.GREEN + "Shop Created!");
e.getPlayer().playSound(e.getPlayer().getLocation(), Sound.LEVEL_UP, 10, 10);
}
}
}
}
}
#EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
Player p = e.getPlayer();
Block b = e.getClickedBlock();
Material m = b.getType();
if (!(m == Material.SIGN_POST)) {
return;
} else {
Sign sign = (Sign) e.getClickedBlock().getState();
if ((sign.getLine(0).equalsIgnoreCase("§9[Shop]"))) {
p.sendMessage("I right clicked the sign!");
}
}
}
}
}
You can create a list of strings you want to compare with, and then use the List.contains method:
if (!Arrays.asList("open", "closed").contains(e.getLine(1).toLowerCase()) {...
The reason that your code is not working is because of De Morgan's law
Given two statements, A and B. If you do not want A or B to true
(!A) or (!B) is not correct
(!A) and (!B) is correct or !(A or B) is logically equivalent.

Categories

Resources