Solved add the amount

Discussion in 'Plugin Development' started by WHQ, Nov 6, 2014.

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

    WHQ

    Hello guys,

    i am making a plugin where a player gets a certain reward when he kills another player. Right now there are 2 rewards: Exp and Item.

    I want to add an amount to the item i give to the player but i haven't really figured out how yet.

    Any ideas (sorry if my code is bad but i am learning java :))
    Code:java
    1.  
    2. public class ExpKills extends JavaPlugin implements Listener {
    3.  
    4. public void onEnable(){
    5. PluginManager pm = getServer().getPluginManager();
    6. pm.registerEvents(this, this);
    7. saveDefaultConfig();
    8. }
    9.  
    10. @EventHandler
    11. public void PlayerDeath(PlayerDeathEvent e){
    12. Player killer = e.getEntity().getKiller();
    13. Player killed = e.getEntity();
    14. ItemStack item = new ItemStack(Material.getMaterial(this.getConfig().getString("item-per-kill")));
    15. int amount = item.getAmount() * this.getConfig().getInt("amount-item") ;
    16.  
    17. if(killer instanceof Player) {
    18. }else{
    19. return;
    20. }
    21.  
    22. if(this.getConfig().getBoolean("Exp") == true) {
    23. killer.giveExpLevels(this.getConfig().getInt("exp-per-kill"));
    24. killer.sendMessage(ChatColor.GOLD + "You killed " + killed.getDisplayName() + ChatColor.GOLD + " and gained " + this.getConfig().getInt("exp-per-kill") + ChatColor.GOLD + " level(s)!");
    25. }
    26.  
    27. if(this.getConfig().getBoolean("item") == true);
    28. killer.getInventory().addItem(item); //how can i add the amount? (sorry new to java)
    29. killer.sendMessage(ChatColor.GOLD + "You killed " + killed.getDisplayName() +ChatColor.GOLD+ " and earned " + this.getConfig().getInt("amount-item") + ChatColor.GOLD + " " + this.getConfig().getString("item-per-kill") + ChatColor.GOLD + " !");
    30.  
    31.  
    32. }
    33. }
    34.  
    35.  
     
  2. Offline

    zhshero

  3. Offline

    WHQ

    zhshero

    Thanks haha!

    I didn't really know how to set the amount but now i do :)
     
  4. Offline

    mine-care

    Hello, since you found the solution please mark thread as solved.
    Also some efficiency and time saving tips :)
    1. You could have a boolean called whatever that on enable loads from config so on death event you don't have to do do getconfig.betboolean("") that also makes it easier for the server too,
    2. To save you some time you don't have to add boolean == true/false as if condition justt use if(boolean){
    //this will work only of boolean is true
    If you want a if for false boolean use the ! Operator
    }
     
    WHQ likes this.
Thread Status:
Not open for further replies.

Share This Page