this count doesn't add 1 why ...

Discussion in 'Plugin Development' started by ahuby09, Feb 6, 2014.

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

    ahuby09

    why does this code not just add one instead it adds a random number
    Code:
       }else if((p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.GOLD_BLOCK)){
       if(getConfig().getString("Player." + p.getName()) == null) {
       getConfig().set("Player." + p.getName(), 0);
       saveConfig();
       }else{
        
       int a = getConfig().getInt("Player." + p.getName());
       getConfig().set("Player." + p.getName(), a + 1);
       saveConfig();
       }
     
  2. Offline

    L33m4n123

    what is a?
     
  3. Offline

    Bikkel007

    You are doing something strange...
    First you say: 'getConfig().getString("Player." + p.getName()) == null'
    That would suggest the 'Player." + p.getName()) is a string, but later you read it and save it as an Integer.
     
  4. Offline

    ahuby09

    here is the full code
    Code:
    package me.blueninjn.templerun;
     
    import java.util.Collection;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.BlockFace;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class templerun extends JavaPlugin implements Listener{
     
    String name ="TempleRun";
    String version = "0.1";
    public void onDisable() {
    System.out.print(name + version + "disabled");
    }
     
     
    public void onEnable() {
    System.out.print(name + version + "enabled");
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    getConfig();
    saveConfig();
    }
     
    //Remove
    public void removePoints(Player p, int amount) {
     
    if(getConfig().getString("Player." + p.getName()) == null) {
    getConfig().set("Player." + p.getName(), 0);
    saveConfig();
    }else{
    int a = getConfig().getInt("Player." + p.getName());
    getConfig().set("Player." + p.getName(), amount - amount);
    saveConfig();
    }
     
    }
    @SuppressWarnings("deprecation")
    @EventHandler
    public void playermoveevent(PlayerMoveEvent event){
       Player p = event.getPlayer();
       if(p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.COBBLESTONE){
       p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 300, 3));
       }else if(p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.SPONGE){
       p.setHealth(0);
       }else if((p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.REDSTONE_BLOCK)){
       p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 300, 3));
       }else if((p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.GLASS)){
       p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 300, 3));
       }else if((p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.BEDROCK)){
       p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 300, 3));
       }else if((p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.ANVIL)){
       p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 300, 5));
       }else if((p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.WATER)){
       p.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 20, 1));
       }else if((p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.ICE)){
       event.setCancelled(true);
       }else if((p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.GOLD_BLOCK)){
       
       if(getConfig().getString("Player." + p.getName()) == null) {
       getConfig().set("Player." + p.getName(), 0);
       saveConfig();
       }else{
        
       int a = getConfig().getInt("Player." + p.getName());
       getConfig().set("Player." + p.getName(), a + 1);
       saveConfig();
       }
       }
        
       }
         
     
     
    }
    
     
  5. Offline

    1Rogue

    Try getting the int regardless and incrementing it:

    Code:java
    1. String path = "Player." + p.getName();
    2. getConfig().set(path, ++getConfig().getInt(path, 0));
     
  6. Offline

    Alshain01

    That's not strange. Ints can be read as strings from SnakeYML and if the string is null, getInt() would have thrown an exception because there is nothing there. It's not the easiest way, but it works.
     
Thread Status:
Not open for further replies.

Share This Page