0

I have just recently started to program Bukkit plugins for personal use and for learning purposes. I have decided to make a command-line global shop, since I'd rather not have to build a shop in-game. Here is the code i have currently:

package me.rougelong.globalshop;

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.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.Material;

import java.util.logging.Logger;

public class globalshop extends JavaPlugin{
public final Logger logger = Logger.getLogger("Minecraft");
public static globalshop plugin;

@Override
public void onDisable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + "  Has Been Disabled!");
}

@Override
public void onEnable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + "  Has Been Enabled!");
}


public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    String itemName = args[0];
    String amount = args[1];
    if (args.length > 4 && commandLabel.equalsIgnoreCase("shopcheck")) {
           sender.sendMessage(ChatColor.RED + "Too many arguments! /shopcheck [item_name] [quantity]");
           return false;
        } 
    if (args.length < 2 && commandLabel.equalsIgnoreCase("shopcheck")) {
           sender.sendMessage(ChatColor.RED + "Not enough arguments! /shopcheck [item_name] [quantity]");
           return false;
        }
    if (args.length > 4 && commandLabel.equalsIgnoreCase("shopbuy")) {
           sender.sendMessage(ChatColor.RED + "Too many arguments! /shopbuy [item_name] [quantity]");
           return false;
        } 
    if (args.length < 2 && commandLabel.equalsIgnoreCase("shopbuy")) {
           sender.sendMessage(ChatColor.RED + "Not enough arguments! /shopbuy [item_name] [quantity]");
           return false;
        }
    if (args.length > 4 && commandLabel.equalsIgnoreCase("shopsell")) {
           sender.sendMessage(ChatColor.RED + "Too many arguments! /shopsell [item_name] [quantity]");
           return false;
        } 
    if (args.length < 2 && commandLabel.equalsIgnoreCase("shopsell")) {
           sender.sendMessage(ChatColor.RED + "Not enough arguments! /shopsell [item_name] [quantity]");
           return false;
        }
    if (commandLabel.equalsIgnoreCase("shopcheck")){
            //I need to get it to compare HERE. :P
        return false;
    }

    return false;
}

}

I need it to compare itemName, or the args[0] (would be a String. Example: "cobblestone") to all the possible items, and I would prefer not to have to write in all the items into this plugin, I was wondering how to reference the lists of items within Bukkit. I am not sure if it would be blocks, or materials, or whatever.

My question(s) is: Which would I need to import to correctly compare the arg to? What code would I use to compare that singular arg to all the items so it can identify it?

Once that code is figured out and works, I'll be moving to have it then identify a price listed either in that program(hopefully not), or in a separate file. AND then having it go to check the balance of the sending player to see if they can buy it, and then have the money subtracted and the item added to their inventory Without removing something else they have in their hand.

2 Answers2

0

One way to do this would be to use an existing enum from Bukkit's API as the list of things that you can shop for. Each enum class supports a valueOf(String) method. Calling this method returns the specific enum value, or throws an Exception (if the string is not a valid enum constant).

String materialName = arg[n]..toUpperCase();
Material theMaterial = null;
try
{
  theMaterial = Material.valueOf(materialName );
}
catch (Exception e)
{
  //Not a valid material
}

However, if you're going to have some file or list which has all the possible materials, with their price, then you can just as easily look up that list.

Darius X.
  • 2,886
  • 4
  • 24
  • 51
0

Use the ID/Name of the material to get it. I have done it before. It is easy

user3029101
  • 317
  • 1
  • 3
  • 13