How to save XP and get XP on interact Item (BottledXP)

Discussion in 'Plugin Development' started by KevinTheBoss2810, May 1, 2014.

Thread Status:
Not open for further replies.
  1. Hello Guys i need help with my XP Bottle plugin.
    Its should do this:

    1. You craft a Empty XP Bottle.
    2. Your rightclick it and your Levels will be saved in it.
    3. You'll get XP Bottle in your hand.
    4. You can rightclick it and you'll get your XP back.

    My Code:

    Code:java
    1. package me.kevintheboss2810.main;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.inventory.ShapedRecipe;
    13. import org.bukkit.inventory.meta.ItemMeta;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class ChargedXP extends JavaPlugin implements Listener{
    17. public void onEnable(){
    18.  
    19. this.getServer().getPluginManager().registerEvents(this, this);
    20.  
    21. this.createRecipe();
    22. }
    23.  
    24.  
    25. public void createRecipe(){
    26.  
    27. ItemStack bottle = new ItemStack(Material.GLASS_BOTTLE);
    28. ItemMeta bottlem = bottle.getItemMeta();
    29. bottlem.setDisplayName("§aEmpty XPBottle!");
    30. bottle.setItemMeta(bottlem);
    31.  
    32.  
    33. ShapedRecipe r = new ShapedRecipe(bottle);
    34.  
    35. r.setIngredient('A', Material.GLOWSTONE_DUST);
    36. r.setIngredient('B', Material.GLASS_BOTTLE);
    37. r.setIngredient('X', Material.AIR);
    38.  
    39. r.shape("XAX","XBX","XXX");
    40.  
    41. this.getServer().addRecipe(r);
    42. }
    43.  
    44. @EventHandler
    45. public void onInteract(PlayerInteractEvent e){
    46.  
    47. ItemStack bottle = new ItemStack(Material.GLASS_BOTTLE);
    48. ItemMeta bottlem = bottle.getItemMeta();
    49. bottlem.setDisplayName("§aEmpty XPBottle!");
    50. bottle.setItemMeta(bottlem);
    51.  
    52. Player p = e.getPlayer();
    53.  
    54. if(p.getItemInHand().equals(bottle)){
    55. if(e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR){
    56. if(p.getExp() > 0){
    57. e.setCancelled(true);
    58. float xp = p.getExp();
    59.  
    60. ItemStack exp = new ItemStack(Material.EXP_BOTTLE);
    61. ItemMeta expm = exp.getItemMeta();
    62. expm.setDisplayName("§aXPBottle with §5" + xp + " §aXP.");
    63.  
    64. [COLOR=#000000] //HOW CAN I SAVE THE XP?[/COLOR]
    65.  
    66. ArrayList<String> lore = new ArrayList<String>();
    67. lore.add(""+exp);
    68. expm.setLore(lore);
    69. exp.setItemMeta(expm);
    70.  
    71. p.setItemInHand(exp);
    72.  
    73. p.setExp(0f);
    74.  
    75. }else{
    76. p.sendMessage("§7You need more then §c0 §7XP!");
    77. }
    78. }
    79.  
    80.  
    81. }else if(p.getItemInHand().getType() == Material.EXP_BOTTLE) {
    82. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    83. e.setCancelled(true);
    84. p.setItemInHand(new ItemStack(Material.AIR));
    85.  
    86. //HOW CAN I GET THE XP?
    87.  
    88. }
    89. }
    90. }
    91. }
    92.  


    Thanks to everyone who can help me!
     
  2. Offline

    mickedplay

    1. Save the XP in a HashMap before right click your empty bottle:
    private static HashMap<String, Integer> xplevel = new HashMap<String, Integer>();
    xplevel.put(player.getName(), player.getLevel());
    String = players name; integer = amount of xp

    2. add new XP bottle to players inventory
    p.getInventory().addItem(blah);

    3. After right click with your new xp bottle give players XP back
    player.setLevel(xplevel.get(player.getName()));
    xplevel.remove(player.getName());
    Remove full XP bottle, set empty.


    Thats all :) And sorry, I'm on my mobile phone...
     
  3. Offline

    GotRice

    mickedplay just a warning, when the server restarts, all the XP levels would be reset.
     
  4. mickedplay can i get the XP's out of the Item lore? The XP's are float's not a intager.
     
  5. Offline

    Konkz

    Instead of adding them to a hashmap give them lore like "7 levels" then when an XP bottle is dropped and has lore get the first integer of it in example it's 7 and give them that amount of levels.
     
  6. Offline

    mickedplay

    You can also save the HashMap in a file. they wouldnt reset then.
     
  7. Konkz yea but i have to do this with every level?
     
  8. Offline

    Konkz

    From what I understood from OP, what you are trying to do is if player, for example, does command
    /store if they have a water bottle in hand then it will put all their levels 'inside' the water bottle?

    If answer is yes to above then I'm not sure what you mean. Just get the item they have in hand and
    make them have an item which is an XP bottle with lore/name p.getLevel() + " XP in this bottle" - then set their
    XP to 0 etc.

    EDIT
    Also from what I can tell, on Bukkit we go by the tradition of me.your_name.plugin_name

    You did me.you_name_.main - it's northing major but just something we go along with.
     

  9. Yes but i made it with every package me.pluginname.commands or me.pluginname.listener
    :)
     
  10. Offline

    Konkz


    me.konkz.myplugin.listeners
    me.konkz.myplugin.commands
    me.konkz.myplugin.data

    [​IMG]

    It's just a minor thing, I just see 99% of developers do it and such. :p
     
Thread Status:
Not open for further replies.

Share This Page