Can't edit items in chest! NEED HELP!

Discussion in 'Plugin Development' started by kingBS11, Dec 13, 2012.

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

    kingBS11

    Okay, I have set up two java files so when you click a block it adds the coords to the config which works fine. But when I type /fillchests it is suppose to add a woodsword but instead it gives me an error in the console, Please Help!
    Code:
    package com.endlessshadow;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.block.*;
    import org.bukkit.block.Chest;
    import org.bukkit.inventory.*;
    import org.bukkit.Material;
     
    public class CommandRefill
      implements CommandExecutor
    {
      public EndlessHunger plugin;
      int i = 0;
     
      public CommandRefill(EndlessHunger plugin)
      {
        this.plugin = plugin;
      }
     
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        if ((sender instanceof Player)) {
    Player p = (Player)sender;
        if(cmd.getName().equalsIgnoreCase("Fillchests")){
       if(p.hasPermission("EndlessHunger.fillchests")){
       ItemStack woodsword = new ItemStack(Material.WOOD_SWORD, 1);
       String[] chest = plugin.config.getString("Chest1").split(",");
       double x = Double.parseDouble(chest[1]);
       double y = Double.parseDouble(chest[2]);
       double z = Double.parseDouble(chest[3]);
       World w = p.getWorld();
       Location chestloc = new Location(w, x, y, z);
       Block Chest = chestloc.getBlock();
       Inventory inv = ((Chest) Chest.getState()).getInventory();
       inv.clear();
       inv.addItem(woodsword);
       }
        }
        }
    return true;
    }
    }
    
    Code:
    package com.endlessshadow;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.Material;
    import org.bukkit.block.*;
     
    public class ChestListener implements Listener {
    public EndlessHunger plugin;
           
            public ChestListener(EndlessHunger plugin) {
                    this.plugin = plugin;
            }
     
            @EventHandler
            public void onPlayerInteract(PlayerInteractEvent evt){
           Player p = evt.getPlayer();
           Block b = evt.getClickedBlock();
           if(p.getItemInHand().getType()== Material.WOOD_SWORD){
           if(b.getState() instanceof Chest){
           if(p.isOp()){
    double x = b.getLocation().getX();
    double y = b.getLocation().getY();
    double z = b.getLocation().getZ();
    String w = b.getWorld().getName();
       this.plugin.config.set("Chest1", w + "," + x + "," + y + "," + z);
       plugin.saveConfig();
       p.sendMessage("Added chest 1!");
           }
           }
           }
            }
    }
    
     
  2. Offline

    fireblast709

    why do you save the world, but not use it? Also, as stated in your double thread, include the error
     
  3. Offline

    kingBS11

    I solved that problem, now I have a new one!
    Code:
    package com.endlessshadow;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.Material;
    import org.bukkit.block.*;
     
    public class ChestListener implements Listener {
    public EndlessHunger plugin;
         
            public ChestListener(EndlessHunger plugin) {
                    this.plugin = plugin;
            }
     
            @EventHandler
            public void onPlayerInteract(PlayerInteractEvent evt){
          Player p = evt.getPlayer();
          Block b = evt.getClickedBlock();
          if(p.getItemInHand().getType()== Material.WOOD_SWORD ){
          if(b.getState() instanceof Chest){
          if(p.isOp()){
          if(plugin.chest1== false){
    double x = b.getLocation().getX();
    double y = b.getLocation().getY();
    double z = b.getLocation().getZ();
    String w = b.getWorld().getName();
      this.plugin.config.set("Chest1", w + "," + x + "," + y + "," + z);
      plugin.saveConfig();
      plugin.chest1 = true;
      p.sendMessage("Added chest 1!");
          }
          }
          if(plugin.chest1== true && plugin.chest2== false){
    double x = b.getLocation().getX();
    double y = b.getLocation().getY();
    double z = b.getLocation().getZ();
    String w = b.getWorld().getName();
      this.plugin.config.set("Chest2", w + "," + x + "," + y + "," + z);
      plugin.saveConfig();
      plugin.chest2 = true;
      p.sendMessage("Added chest 2!");
          }
          }
          }
            }
    }
    
    When I click a chest with a wood sword it says Added chest 1 and added chest 2
     
  4. Offline

    ImDeJay

    from the looks of your code, its suppose to display those messages.
     
  5. Offline

    kingBS11

    I want it to say "Added chest 1!" when I click on the first chest then "Added chest 2!" When I click the second chest.
     
  6. Offline

    ImDeJay

    i dont really understand what your code is doing, even after looking at it time and time again.

    but lets try this.

    add a return statement after p.sendmessage("added chest 1");

    so something ilke this

    Code:
    p.sendMessage("Added chest 1!");
    return;
    see if that works.

    read this thread and try what i told you instead of posting another thread.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  7. Offline

    kingBS11

    Yeah that worked, thanks!
     
Thread Status:
Not open for further replies.

Share This Page