Solved Player Interact Event and Player file

Discussion in 'Plugin Development' started by CrystallFTW, Mar 16, 2015.

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

    CrystallFTW

    I just made a plugin that stores each player's data in a separate file, like Name.yml. I stored there Xp and Level data for my custome mini-game, everything works right but I have a special item and if you right click with it you should get +20 xp, but when i right click it, i get the message and sound but no xp. here is my code:

    Code:
          @EventHandler
          public void onPlayerinteract(PlayerInteractEvent e)
          {
    
            if ((e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK )) {
                Player p = e.getPlayer();
                File player_file = new File(this.getDataFolder() + File.separator + "Players" + File.separator + p.getName() + ".yml");
                FileConfiguration playerFile = YamlConfiguration.loadConfiguration(player_file);
                    if(player_file.exists()){
                           if(playerFile.contains("XP")){
                               this.xp = playerFile.getInt("XP");
                           }
                    }
                ItemStack sw1 = new ItemStack(Material.DIAMOND,1);
                ItemMeta metasw1 = sw1.getItemMeta();
                metasw1.setDisplayName("§6§lDiamond §r§6(§f§oRight Click§r§6)");
                sw1.setItemMeta(metasw1);
              if (p.getInventory().getItemInHand().isSimilar(sw1)){
                    p.playSound(p.getLocation(), Sound.LEVEL_UP, 1, 2);
                    Object obj = playerFile.getInt("XP") +10;
                  playerFile.set("XP", obj);
                p.sendMessage("§e+20 XP - " + obj);
                for(int x = 0 ; x <= 44 ; x++){
                    ItemStack im = p.getInventory().getItem(x);
                    if(im.isSimilar(sw1)){
                        p.getInventory().removeItem(sw1);
                    }
                    }
            }
            }
           
          }
    I just spawned 10 diamonds and used them all but everytime I get the same message with the same xp amount.
    thanks.
     
  2. @CrystallFTW
    U`re just updating the xp on config, but u`re never giving xp yourself on game
     
    CrystallFTW likes this.
  3. Offline

    CrystallFTW

    Well, I get xp from config.
    Code:
                    Object obj = playerFile.getInt("XP") +10;
                  playerFile.set("XP", obj);
     
  4. @CrystallFTW
    Yes, u`re just updating THE CONFIG, you xp will still being the same because u`re just modifying an int on config, but minecraft will never know about that int even you use something like Player#giveExp(Exp);
     
    CrystallFTW likes this.
  5. Offline

    RainoBoy97

    @CrystallFTW You never save the file. Also, you should try caching it.

    @Juancomaster1998 Spelling "you're" properly only cost you a total of 2 button clicks.
     
    Last edited: Mar 16, 2015
    CrystallFTW likes this.
  6. Offline

    CrystallFTW

    I tried something xD What's the issue?

    Code:
       
        public void giveExpp(Player p, int i) {
              File player_file = new File(this.getDataFolder() + File.separator + "Players" + File.separator + p.getName() + ".yml");
            FileConfiguration playerFile = YamlConfiguration.loadConfiguration(player_file);
         
               playerFile.set("XP", playerFile.getInt("XP") +i);
               saveConfig();
         
        }
    
    and

    Code:
                    giveExpp(p,10);
                p.sendMessage("§e+20 XP - " + this.xp);
                try {
                    playerFile.save(player_file);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
    Nevermind, fixed it! Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
Thread Status:
Not open for further replies.

Share This Page