Getting values from a config and setting them into a Hashmap

Discussion in 'Plugin Development' started by harvmaster, Jul 24, 2015.

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

    harvmaster

    Hey I have been trying to make a dungeon chest plugin for my server and I was wandering how I would check if the chest they are opening is a dungeon chest.

    This is what I have so far -

    class to set the dungeon chest

    Code:
    public static HashMap<String, String> DefineChest = new HashMap<String, String>();
        public String NameOfChest = "NameOfChest";
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (sender instanceof Player) {
                   
                    switch (label) {
                   
                        case "dungeoncreate":
                            if (args.length == 1) {
                                if (DefineChest.containsKey(sender.getName()))DefineChest.remove(sender.getName());
                            DefineChest.put( sender.getName(), args[0]);
                            sender.sendMessage(ChatColor.AQUA + "Right Click the chest you want the Dungeon loot to be in");
                           
                            } else sender.sendMessage(ChatColor.RED + "You do not have the correct amount of arguments. Try /dungeoncreate {Name}");
                            break;
                        case "dungeonremove":
                           
                          break;
                        case "dungeon":
                            sender.sendMessage(ChatColor.GOLD + "do '/help CustomEnchants' for a list of commands you can do");
                          break;
                         
                        default:
                            sender.sendMessage(ChatColor.RED + "That is not a valid command, Do /Dungeon for a list of the Commands");
                       
                    }
                }
            else {
               
                Bukkit.getLogger().info("You must be a player to do this");
               
            }
            return false;
        }
       
       
        @EventHandler
        public void SetChest(PlayerInteractEvent event) {
           
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Block block = (Block) event.getClickedBlock();
                if (block.getType() == Material.CHEST && DefineChest.containsKey(event.getPlayer().getName())){
               
                    ;
                    String path = "dungeons." + DefineChest.get(event.getPlayer().getName()) + ".";
                    //int[] Coords = new int[] {block.getX() , block.getY(), block.getZ()};
                    String dungeonName = DefineChest.get(event.getPlayer().getName());
                    DefineChest.remove(event.getPlayer());
                   
                    CustomEnchants.plugin.getConfig().set(path + "x", block.getX());
                    CustomEnchants.plugin.getConfig().set(path + "y", block.getY());
                    CustomEnchants.plugin.getConfig().set(path + "z", block.getZ());
                    CustomEnchants.plugin.saveConfig();
                    event.getPlayer().sendMessage(ChatColor.AQUA + "You have successfully created a Dungeon Chest named " + ChatColor.BOLD + ChatColor.BLUE + dungeonName);
                   
                }
            }
        }
    class I want to check if the chest = a dungeon chest
    Code:
    public static HashMap<String, Integer> DungeonsOnCooldown = new HashMap<String, Integer>();
       
       
       
       
       
       
        @EventHandler
        public void DungeonChestEvent(PlayerInteractEvent event) {
           
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Block block = (Block) event.getClickedBlock();
                if (block.getType() == Material.CHEST){
                   
                   
                   
            }
        }
           
        }
    Thank you in advanced
     
  2. Offline

    DoggyCode™

    "ey I have been trying to make a dungeon chest plugin for my server and I was wandering how I would check if the chest they are opening is a dungeon chest."
    Go on with:
    Code:
    if(event.getInventory().getName().equals(String)) {//String = chest's name. CaSe SEnsEtive.
                //do stuff
              }
    
    @harvmaster
     
  3. Offline

    Nic2555

    @harvmaster
    Simply put the coords into a list, then in the config, save the list. On load, retrieve the list and check if the interacted block is a chest and if its coords appear in the list. Then do whatever you want.
     
  4. Offline

    harvmaster

    @DoggyCode™ I don't think I would be able to use that because this plugin is being made for a survival server and then people could just make their own chests named that. But it is a good idea.

    @Nic2555 I like the sounds of this but how would I actually do this in the code if they are set by the user.?

    @DoggyCode™ I have ended up doing some of it with your concept of naming the chest, but how would I rename the chest to the name of the dungeon so I dont have to use an anvil before hand?
     
    Last edited by a moderator: Jul 25, 2015
  5. Offline

    mine-care

    Βtw please follow Java Naming Conventions
     
    bwfcwalshy likes this.
  6. mine-care likes this.
  7. Offline

    DoggyCode™

    @DoggyCode™ I have ended up doing some of it with your concept of naming the chest, but how would I rename the chest to the name of the dungeon so I dont have to use an anvil before hand?[/QUOTE]

    For the chest you have to create a Inventory right?
    So:
    Code:
    //I think you can make it static so you can access it from other classes:
    
        public static Inventory chest; {
            InventoryHolder p = null;
            chest = Bukkit.createInventory(p, InventoryType.CHEST, "§4DungeonChest"); //make sure to change "InventoryType.CHEST to 52 (am I right?) if you want it to have the space as a double chest.
        }
    
    Then after having it like this you can use:
    Code:
    if(event.getInventory().getName().equals(Class#chest.getName())) {
    //do stuff
    //fill up the chest with items and stuff:
    Class#chest.addItem(Material.DIAMOND_SWORD);
    //and more
        }
    
    If you'd wish to add random items to the chest, I'd suggest you make a new thread about that.

    @harvmaster
     
Thread Status:
Not open for further replies.

Share This Page