Need help with this error....

Discussion in 'Plugin Development' started by Repasa, Feb 12, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Repasa

    Hey so whats wrong with my code here?
    ths idea is that when the player types /exchange if they have a stack of cobble you get how ever many diamonds you define in the config file.

    example this code...
    the material is defined as "id" and "amount" so in the config i have those defined but its not working

    help please?


    Code:
    Material material = Material.getMaterial(this.getConfig().getInt("id"));
                int amount = this.getConfig().getInt("amount");
                ItemStack item = new ItemStack(material, amount);

    Code:
    package me.repasa.ex;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    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.inventory.PlayerInventory;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class exchange extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static exchange 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" + "Has Been Enabled!");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("exchange")){ // If the player typed /exchange then do the following...
                PlayerInventory inventory = player.getInventory(); // The player's inventory
                Material material = Material.getMaterial(this.getConfig().getInt("id"));
                int amount = this.getConfig().getInt("amount");
                ItemStack item = new ItemStack(material, amount);
                Material material2 = Material.getMaterial(this.getConfig().getInt("id2"));
                int amount2 = this.getConfig().getInt("amount2");
                ItemStack item2 = new ItemStack(material2, amount2);
                ItemStack diamond = new ItemStack(Material.DIAMOND, this.getConfig().getInt("diamond.amount")); // A stack of whats exchanged
                ItemStack gold = new ItemStack(Material.GOLD_INGOT, this.getConfig().getInt("gold.amount"));
                Material material3 = Material.getMaterial(this.getConfig().getInt("id3"));
                int amount3  = this.getConfig().getInt("amount3");
                ItemStack item3 = new ItemStack(material3, amount3);
                ItemStack iron = new ItemStack(Material.IRON_INGOT, this.getConfig().getInt("iron.amount"));
            if (inventory.contains(item)){
                inventory.addItem(diamond); // Adds diamonds to the player's inventory
                inventory.remove(item); // removes item from inventory
                player.sendMessage(ChatColor.GOLD + "Exchanged Items for a diamond!");
                return true;
            }else {
               
           
            if (inventory.contains(item2)){
                inventory.addItem(gold); // Adds diamonds to the player's inventory
                inventory.remove(item2); // removes item from inventory
                player.sendMessage(ChatColor.GOLD + "Exchanged Items for Gold!");
                return true;
            }else {
               
            }
            if (inventory.contains(item3)){
                inventory.addItem(iron); // Adds diamonds to the player's inventory
                inventory.remove(item3); // removes item from inventory
                player.sendMessage(ChatColor.GOLD + "Exchanged Items for Iron!");
                return true;
               
            }else {
                player.sendMessage(ChatColor.GOLD + "You dont have that item!");
               
               
                return true;
            } //If this has happened the function will return true.
                // If this hasn't happened the a value of false will be returned.
        }
            }
            return false;
        }
    }
     
     
       
     
       
       
        
    Code:
    diamond.amount 5
    id Cobblestone
    amount 64
    gold.amount 5
    id2 Stone
    amount2 64
    iron.amount 5
    id3 Dirt
    amount3 64
     
     
     
     
    
     
  2. Offline

    xmarinusx

    Is this your config.yml? Then you did something wrong, you have to use these ":" so for example if you want to get the amount of diamonds you have to get for cobble type this in your config: coblestone-amount: 4. If you now type getConfig().getInt("coblestone-amount") it will return 4.
     
  3. Offline

    mb01

    Your piece of code is extremely difficult to understand.

    Anyway, you're creating new item stacks and then you check if they're present in the inventory of the player. Even if it can work (depending on what Craftbukkit does in .contains()), you shouldn't rely on it. And I suppose his is where the issue is. It's not only about materials, amounts or durability but also about references.
     
Thread Status:
Not open for further replies.

Share This Page