Solved Loading an integer from a config file

Discussion in 'Plugin Development' started by Nickh90, Oct 7, 2016.

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

    Nickh90

    Hello I'm trying to load a couple of integers from my config file to use with my custom sword
    I can't get any of them to load and there's no errors in the console heres the code
    Code (open)

    Code:
    I failed and accidentally deleted this
    refer to the code under the spoiler Neater Code for reply #4 and down
    



    EDIT: Added neater code with the Fix applied by Reply #2
    Neater code (open)

    Code:
    public class Healingsword extends JavaPlugin implements Listener{
        @Override
        public void onDisable(){
            System.out.print("[Leech Sword] Leech Sword Disabled!");
        }
        @Override
        public void onEnable(){
            loadConfiguration();
            System.out.print("[Leech Sword] First Run Error");
            }
    @EventHandler
        public void loadConfiguration(){
            //Create default values in config
            String aws = "animal.witherseconds";
            String aps = "animal.poisonseconds";
            String awp = "animal.witherpower";
            String app = "animal.poisonpower";
            getConfig().addDefault(aws, 8);
            getConfig().addDefault(aps, 8);
            getConfig().addDefault(awp, 3);
            getConfig().addDefault(app, 2);
            String mws = "monster.witherseconds";
            String mps = "monster.poisonseconds";
            String mwp = "monster.witherpower";
            String mpp = "monster.poisonpower";
            getConfig().addDefault(mws, 8);
            getConfig().addDefault(mps, 8);
            getConfig().addDefault(mwp, 8);
            getConfig().addDefault(mpp, 8);
            String pws = "player.witherseconds";
            String pps = "player.poisonseconds";
            String pwp = "player.witherpower";
            String ppp = "player.poisonpower";
            String pla = "player.leechamount";
            getConfig().addDefault(pws, 8);
            getConfig().addDefault(pps, 8);
            getConfig().addDefault(pwp, 3);
            getConfig().addDefault(ppp, 2);
            getConfig().addDefault(pla, 2);
            getConfig().options().copyDefaults(true);
            saveConfig();
            }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
                  if (cmd.getName().equalsIgnoreCase("leechsword") && sender instanceof Player)
                  if (sender.hasPermission("leechsword.command.use")){
              Player player = (Player) sender;
              ItemStack sword = new ItemStack(Material.IRON_SWORD);
              ItemMeta swordMeta = sword.getItemMeta();
              swordMeta.addEnchant(Enchantment.KNOCKBACK, 8, true);
              swordMeta.addEnchant(Enchantment.LOOT_BONUS_MOBS, 50,true);
              swordMeta.addEnchant(Enchantment.DAMAGE_UNDEAD, 2,true);
              //swordMeta.addEnchant(Enchantment.MENDING, 1,true);
              swordMeta.addEnchant(Enchantment.FIRE_ASPECT, 2,true);
              swordMeta.addEnchant(Enchantment.DAMAGE_ALL, 3,true);
              swordMeta.setDisplayName(ChatColor.DARK_RED + "§2Potent §8Withering §7Iron Sword §fof §4Leeching");
              List<String> swordLore = new ArrayList<String>();
              swordLore.add(ChatColor.RED + "Leeches life from your foes.");
              swordLore.add("§8Withers away your opponents.");
              swordLore.add(ChatColor.DARK_GREEN + "Extremely Poisonus.");
              swordMeta.setLore(swordLore);
              sword.setItemMeta(swordMeta);
              net.minecraft.server.v1_10_R1.ItemStack stack = CraftItemStack.asNMSCopy(sword);
              NBTTagCompound tag = stack.hasTag() ? stack.getTag() : new NBTTagCompound();
              tag.set("Unbreakable", new NBTTagByte((byte)1));
              stack.setTag(tag);
              sword = CraftItemStack.asBukkitCopy(stack);
              player.getInventory().addItem(sword);
              return true;
              }else if(!(sender.hasPermission("leechsword.command.use"))){
                  sender.sendMessage("You can not use that command!");
              }
              return true;
          }
     
    @EventHandler
        //damage mobs and players
        public void onEntityDamage(EntityDamageByEntityEvent e) {
              int aws = getConfig().getInt("animal.witherseconds");
              int aps = getConfig().getInt("animal.poisonseconds");
              int awp = getConfig().getInt("animal.witherpower");
              int app = getConfig().getInt("animal.poisonpower");
              //get and set values to apply to Monsters
              int mws = getConfig().getInt("monster.witherseconds");
              int mps = getConfig().getInt("monster.poisonseconds");
              int mwp = getConfig().getInt("monster.witherpower");
              int mpp = getConfig().getInt("monster.poisonpower");
              //get and set values to apply to Players
              int pws = getConfig().getInt("player.witherseconds");
              int pps = getConfig().getInt("player.poisonseconds");
              int pwp = getConfig().getInt("player.witherpower");
              int ppp = getConfig().getInt("player.poisonpower");
              Player damager = (Player) e.getDamager();
         if(damager.getInventory().getItemInMainHand().getType().equals(Material.IRON_SWORD) && (damager.getInventory().getItemInMainHand().getItemMeta().getLore().contains(ChatColor.RED + "Leeches life from your foes."))){
             if ((e.getEntity() instanceof Animals && (e.getDamager() instanceof Player))){
              Animals adamaged = (Animals) e.getEntity();
              adamaged.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, aws*20, awp));
              adamaged.addPotionEffect(new PotionEffect(PotionEffectType.POISON, aps*20, app));
       }else{  
         if ((e.getEntity() instanceof Monster && (e.getDamager() instanceof Player))){
                  Monster mdamaged = (Monster) e.getEntity();           
              //load values  from config to apply to mob
                      mdamaged.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, mws*20, mwp));
                      mdamaged.addPotionEffect(new PotionEffect(PotionEffectType.POISON, mps*20, mpp));
       }else{
              if ((e.getEntity() instanceof Player && (e.getDamager() instanceof Player))){
                  Player pdamaged = (Player) e.getEntity();
                  //load values  from config to apply to other player
                      pdamaged.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, pws*20, pwp));
              
                      pdamaged.addPotionEffect(new PotionEffect(PotionEffectType.POISON, pps*20, ppp));
                          }
                      }
                   }
             }
        }
    @EventHandler
        public void onEntityDamage(EntityDamageEvent e){
            int pla = getConfig().getInt("player.leechamount");
        if(e instanceof EntityDamageByEntityEvent){
              EntityDamageByEntityEvent damageEvent = (EntityDamageByEntityEvent)e;
        if (damageEvent.getDamager() instanceof Player) {
              Player phealdamager = (Player) damageEvent.getDamager();
        if(phealdamager.getInventory().getItemInMainHand().getType().equals(Material.IRON_SWORD) && (phealdamager.getInventory().getItemInMainHand().getItemMeta().getLore().contains(ChatColor.RED + "Leeches life from your foes."))) {
                  //load values  from config to apply to player
        if(phealdamager.getMaxHealth() > (phealdamager.getHealth() + pla)){
              phealdamager.setHealth(phealdamager.getHealth() + pla);
              }else{
              phealdamager.setHealth(phealdamager.getMaxHealth());}
                                   }
                           }
                   }
          }
    }
    
     
    Last edited: Oct 7, 2016
  2. @Nickh90
    That's because they are outside any method, making them initialize when the class loads. Move it to a method so you can somehow reload the values, and you should be just fine.
     
  3. Offline

    Nickh90

    I've put them in onDamageEntity
    but when it tries to apply any of the effects nothing happens
    if i remove all of the config stuff and somthing change this
    adamaged.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, aws*20, awp));
    to
    adamaged.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 8*20, 8));
    it works fine
    Code (open)

    Code:
     @EventHandler
          //damage mobs and players
          public void onEntityDamage(EntityDamageByEntityEvent e) {
              int aws = getConfig().getInt("animal.witherseconds");
              int aps = getConfig().getInt("animal.poisonseconds");
              int awp = getConfig().getInt("animal.witherpower");
              int app = getConfig().getInt("animal.poisonpower");
              //get and set values to apply to Monsters
              int mws = getConfig().getInt("monster.witherseconds");
              int mps = getConfig().getInt("monster.poisonseconds");
              int mwp = getConfig().getInt("monster.witherpower");
              int mpp = getConfig().getInt("monster.poisonpower");
              //get and set values to apply to Players
              int pws = getConfig().getInt("player.witherseconds");
              int pps = getConfig().getInt("player.poisonseconds");
              int pwp = getConfig().getInt("player.witherpower");
              int ppp = getConfig().getInt("player.poisonpower");
          if ((e.getEntity() instanceof Animals && (e.getDamager() instanceof Player))){
              Player damager = (Player) e.getDamager();
              if(damager.getInventory().getItemInMainHand().getType().equals(Material.IRON_SWORD) && (damager.getInventory().getItemInMainHand().getItemMeta().getLore().contains(ChatColor.RED + "Leeches life from your foes."))){
                  //System.out.print("DEBUG: Got A Damager");
                  Animals adamaged = (Animals) e.getEntity();
                  //System.out.print("DEBUG:Got A Inventory");
              
              //load values  from config to apply to mob
                  adamaged.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, aws*20, awp));
                  adamaged.addPotionEffect(new PotionEffect(PotionEffectType.POISON, aps*20, app));
                  //System.out.print("DEBUG:Applied Effects");
              }
          
          }else{
              if ((e.getEntity() instanceof Monster && (e.getDamager() instanceof Player))){
                  Player damager = (Player) e.getDamager();
                  if(damager.getInventory().getItemInMainHand().getType().equals(Material.IRON_SWORD) && (damager.getInventory().getItemInMainHand().getItemMeta().getLore().contains(ChatColor.RED + "Leeches life from your foes."))){
                  //System.out.print("DEBUG: Got M Damager");
                  Monster mdamaged = (Monster) e.getEntity();
                      //System.out.print("DEBUG:Got M Inventory");
              
              //load values  from config to apply to mob
                      mdamaged.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, mws*20, mwp));
                      mdamaged.addPotionEffect(new PotionEffect(PotionEffectType.POISON, mps*20, mpp));
                     // System.out.print("DEBUG:Applied Effects");
              }
          }else{
              if ((e.getEntity() instanceof Player && (e.getDamager() instanceof Player))){
                  Player damager = (Player) e.getDamager();
                  if(damager.getInventory().getItemInMainHand().getType().equals(Material.IRON_SWORD) && (damager.getInventory().getItemInMainHand().getItemMeta().getLore().contains(ChatColor.RED + "Leeches life from your foes."))){
                  //System.out.print("DEBUG: Got P Damager");
                  Player pdamaged = (Player) e.getEntity();
                      //System.out.print("DEBUG:Got P Inventory");
            
                  //load values  from config to apply to other player
                      pdamaged.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, pws*20, pwp));
                      pdamaged.addPotionEffect(new PotionEffect(PotionEffectType.POISON, pps*20, ppp));
                      //System.out.print("DEBUG:Applied Effects");
                              }
                          }
                      }
                  }
              }
    

    the config file is as follows
    Code:
    animal:
      witherseconds: 8
      poisonseconds: 8
      witherpower: 3
      poisonpower: 2
    monster:
      witherseconds: 8
      poisonseconds: 8
      witherpower: 8
      poisonpower: 8
    player:
      witherseconds: 8
      poisonseconds: 8
      witherpower: 3
      poisonpower: 2
      leechamount: 2
    
     
    Last edited: Oct 7, 2016
  4. Offline

    Zombie_Striker

    @Nickh90
    First, your code has a lot of repeating lines. You continually create the same damager variable, do the same instanceof checks, and do the same lore checks. Make sure you only do these checks/creations once.

    For example, you should turn THIS
    Code:
    if(bla1 && sender is a player){
    ...
    }else if (bla2 && sender is a player){
    
    To This
    Code:
    if(sender is a player){
    if(bla1){
    ...
    } else if (bla2)
    See how I only needed to check once? Not only does it make your plugin run faster (albeit, a very small amount), but it reduces the amount of lines there are in your plugin.
     
  5. Offline

    Nickh90

    Alright I've cleaned the code up and did what you suggested.
    Theres still a problem reading the values from the config file however. Look to my OP for the updated code under the spolier Neater Code

    Bump I'm still having issues loading the values

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 8, 2016
  6. Offline

    Zombie_Striker

    Print out what each of these values are equal to (not what is in the config, but what the console prints out.). If these values are 0, then the potion effect will not be applied.
     
  7. Offline

    Nickh90

    Okay thats very odd
    Both of the onEntityDamage events aren't running at all
    I even reverted to the code before I started trying to use configs that worked
    Its not registering when I hit any mob
    which means thats probably why the variables were not loading from the config
    Heres the code I've made some tweaks
    Code (open)

    Code:
    public class Healingsword extends JavaPlugin implements Listener{
        @Override
        public void onEnable(){
            loadConfiguration();
            System.out.print("[Leech Sword] Leech Sword sabled! ");
            }
        public void onDisable(){
            System.out.print("[Leech Sword] Leech Sword Disabled!");
        }
    
    @EventHandler
        public void loadConfiguration(){
            //Create default values in config
            String aws = "animal.witherseconds";
            String aps = "animal.poisonseconds";
            String awp = "animal.witherpower";
            String app = "animal.poisonpower";
            getConfig().addDefault(aws, 8);
            getConfig().addDefault(aps, 8);
            getConfig().addDefault(awp, 3);
            getConfig().addDefault(app, 2);
            String mws = "monster.witherseconds";
            String mps = "monster.poisonseconds";
            String mwp = "monster.witherpower";
            String mpp = "monster.poisonpower";
            getConfig().addDefault(mws, 8);
            getConfig().addDefault(mps, 8);
            getConfig().addDefault(mwp, 8);
            getConfig().addDefault(mpp, 8);
            String pws = "player.witherseconds";
            String pps = "player.poisonseconds";
            String pwp = "player.witherpower";
            String ppp = "player.poisonpower";
            String pla = "player.leechamount";
            getConfig().addDefault(pws, 8);
            getConfig().addDefault(pps, 8);
            getConfig().addDefault(pwp, 3);
            getConfig().addDefault(ppp, 2);
            getConfig().addDefault(pla, 2);
            getConfig().options().copyDefaults(true);
            saveConfig();
            }
    
    
    //damage mobs and players
    @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent e){
            System.out.print("DEBUG: I'm WORKING ");
            LivingEntity edamaged = (LivingEntity)e.getEntity();
        if (e.getDamager() instanceof Player){
            Player damager = (Player)e.getDamager();
        if(damager.getInventory().getItemInMainHand().getType().equals(Material.IRON_SWORD) && (damager.getInventory().getItemInMainHand().getItemMeta().getLore().contains(ChatColor.RED + "Leeches life from your foes."))){
            edamaged.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 30*20, 8));
            edamaged.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 30*20, 8));
              }
        }
    }
    @EventHandler   
    public void onEntityDamage(EntityDamageEvent e){
            System.out.print("DEBUG: I'm WORKING HEAL");
        if(e instanceof EntityDamageByEntityEvent){
            EntityDamageByEntityEvent damageEvent = (EntityDamageByEntityEvent)e;
          if (damageEvent.getDamager() instanceof Player) {
              Player phealdamager = (Player) damageEvent.getDamager();
          if(phealdamager.getInventory().getItemInMainHand().getType().equals(Material.IRON_SWORD) && (phealdamager.getInventory().getItemInMainHand().getItemMeta().getLore().contains(ChatColor.RED + "Leeches life from your foes."))) {
              //load values  from config to apply to player
          if(phealdamager.getMaxHealth() > (phealdamager.getHealth() + 2)){
              phealdamager.setHealth(phealdamager.getHealth() + 2);
          }else{
              phealdamager.setHealth(phealdamager.getMaxHealth());
              System.out.print("DEBUG: Player healed. ");
                                      }
                               }
                       }
               }
      }
    @EventHandler
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
              if (cmd.getName().equalsIgnoreCase("leechsword") && sender instanceof Player)
              if (sender.hasPermission("leechsword.command.use")){
                  Player player = (Player) sender;
                  ItemStack sword = new ItemStack(Material.IRON_SWORD);
                  ItemMeta swordMeta = sword.getItemMeta();
                  swordMeta.addEnchant(Enchantment.KNOCKBACK, 8, true);
                  swordMeta.addEnchant(Enchantment.LOOT_BONUS_MOBS, 50,true);
                  swordMeta.addEnchant(Enchantment.DAMAGE_UNDEAD, 2,true);
              //swordMeta.addEnchant(Enchantment.MENDING, 1,true);
                  swordMeta.addEnchant(Enchantment.FIRE_ASPECT, 2,true);
                  swordMeta.addEnchant(Enchantment.DAMAGE_ALL, 3,true);
                  swordMeta.setDisplayName(ChatColor.DARK_RED + "§2Potent §8Withering §7Iron Sword §fof §4Leeching");
                  List<String> swordLore = new ArrayList<String>();
                  swordLore.add(ChatColor.RED + "Leeches life from your foes.");
                  swordLore.add("§8Withers away your opponents.");
                  swordLore.add(ChatColor.DARK_GREEN + "Extremely Poisonus.");
                  swordMeta.setLore(swordLore);
                  sword.setItemMeta(swordMeta);
                  net.minecraft.server.v1_10_R1.ItemStack stack = CraftItemStack.asNMSCopy(sword);
                  NBTTagCompound tag = stack.hasTag() ? stack.getTag() : new NBTTagCompound();
                  tag.set("Unbreakable", new NBTTagByte((byte)1));
                  stack.setTag(tag);
                  sword = CraftItemStack.asBukkitCopy(stack);
                  player.getInventory().addItem(sword);
                      return true;
                  }else
              if(!(sender.hasPermission("leechsword.command.use")))
              {
                  sender.sendMessage("You can not use that command!");
              }
              return true;
        }
    }
    
     
  8. Offline

    I Al Istannen

    @Nickh90
    1. You need to register the events in onEnable [Bukkit.getPluginManager().registerEvents(listener, plugin)]
    2. The @EventHandler over loadConfiguration will do nothing. Remove it. Same for the one above onCommand
    3. Are you able to fix the formatting? It is quite unpleasant to look at. Might be this forum though, which screws the code up. If not, Press CTRL+SHIFT+F in eclipse or CTRL+ALT+L in IntelliJ to format your code.
     
    Firestar311 likes this.
  9. Offline

    Nickh90

    Okay that helped me out I've fixed the problem now the Plugin is complete and functions correctly.
    Marking topic solved
     
    I Al Istannen likes this.
Thread Status:
Not open for further replies.

Share This Page