Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GiveCommand with Bukkit
public boolean onCommand(CommandSender sender, Command arg1, String arg2, String[] args) {
if (args.length != 3) {
return false;
}
Player p = Bukkit.getPlayer(args[0]);
if (p == null) {
sender.sendMessage("Player is offline!");
return true;
}
ItemStack is = null;
String itm = args[1];
if (!isInt(args[2])) {
sender.sendMessage("Amount has to be a positive number or zero!");
return true;
}
int amount = Integer.parseInt(args[2]);
if (amount <= 0) {
sender.sendMessage("Amount has to be a positive number or zero!");
return true;
}
if (itm.contains(":")) {
String a = itm.split(":")[0];
String b = itm.split(":")[1];
if (!isInt(a) || !isInt(b)) {
sender.sendMessage("Enter Item in format ID:Data!");
return true;
}
int id = Integer.parseInt(a);
int meta = Integer.parseInt(b);
if (meta < 0) {
sender.sendMessage("Item-Data has to be a positive number or zero!");
return true;
}
if (id < 0) {
sender.sendMessage("Item-Meta has to be a positive number or zero!");
return true;
}
is = new ItemStack(Material.getMaterial(id), 1, (short) meta);
} else if (isInt(itm)) {
int i = Integer.parseInt(itm);
if (i < 0) {
sender.sendMessage("Item-Data has to be a positive number or zero!");
return true;
}
is = new ItemStack(i);
}
if (is == null) {
sender.sendMessage("Item doesn't exist!");
return true;
}
is.setAmount(amount);
return true;
}
private boolean isInt(String val) {
try {
Integer.parseInt(val);
return true;
} catch (Exception e) {}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.