Solved XP bar meter

Discussion in 'Plugin Development' started by XxShadow_NinjaxxX, Apr 5, 2013.

Thread Status:
Not open for further replies.
  1. For an XP bar meter, how would i set their XP? because when I do player.setExp(15); the Xp bar is duplicated, so i tried using player.setExp(0), then player.giveXp(15); but that doesnt work, it just sets their XP to 0? whats wrong?
    (This is for my TF2 plugin)
    code:
    for(Player spy: Spy.Spy){
    if(InvisWatch.meter.containsKey(spy)){
    if(InvisWatch.invisible.contains(spy)){
    if(InvisWatch.meter.get(spy)>0){
    InvisWatch.meter.put(spy, InvisWatch.meter.get(spy)-1);
    }else{
    InvisWatch.stop(spy);
    spy.sendMessage(ChatColor.GRAY+"Your Invisibility Meter is empty!");
    }
    }else{
    if(InvisWatch.meter.get(spy)>14){
    spy.sendMessage(ChatColor.GOLD+"Your Invisibility Meter is full!");
    }else{
    InvisWatch.meter.put(spy, InvisWatch.meter.get(spy)+1);
    }
    }
    }
    spy.setExp(0);
    int xp = InvisWatch.meter.get(spy).intValue();
    spy.giveExp(xp);
    }
     
  2. Offline

    chasechocolate

    The XP bar is a float from 0 to 1, that is separated into 18 different "sections". So that means that every section is about 0.056F big, because 1/18 is about 0.056.
     
  3. oh so instead of like spy.giveExp(xp);
    i should do spy.giveExp(xp/18); ?
     
  4. No, use .setExp() to a value from 0.0 to 1.0
    You could do it as percent (0 to 100) like percent / 100.0f.

    You can't use giveExp() to give decimal numbers because experience points are whole numbers, the exception for get/setExp() is that it's a progress bar, not actual experience points !
    Use player.getTotalExperience() to get the experience points, but it's unrelated to what you're using it for.
     
  5. hmm another thing that isnt working, when a a player right clicks with a clock renamed"invis watch" it runs this code, but nothing happens? ive done debugging and its all good until it reachers the if(meter.get(player)<=0)

    package me.Shadow.TF2;

    import java.util.ArrayList;
    import java.util.HashMap;

    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffectType;

    public class InvisWatch {
    public static HashMap<Player, Long> meter = new HashMap<Player, Long>();
    public static ArrayList<Player> invisible = new ArrayList<Player>();
    public static void use(Player player){
    if(invisible.contains(player)){
    stop(player);
    }else{
    start(player);
    }
    }
    public static void start(Player player){
    if(meter.get(player)<=0){
    player.sendMessage(ChatColor.DARK_GRAY+"Refill your Invisibility Meter first!");
    }else{
    invisible.add(player);
    Items.Potion(player, PotionEffectType.INVISIBILITY, 30*60*20, 1);
    player.playEffect(player.getLocation(), Effect.SMOKE, 0);
    player.playEffect(player.getLocation(), Effect.SMOKE, 0);
    player.playEffect(player.getLocation(), Effect.SMOKE, 0);
    player.playEffect(player.getLocation(), Effect.SMOKE, 0);
    player.sendMessage(ChatColor.GREEN+"*Invisibility cloak activated.");
    }
    }
    public static void stop(Player player){
    if(invisible.contains(player)){
    invisible.remove(player);
    player.removePotionEffect(PotionEffectType.INVISIBILITY);
    player.playEffect(player.getLocation(), Effect.SMOKE, 0);
    player.playEffect(player.getLocation(), Effect.SMOKE, 0);
    player.playEffect(player.getLocation(), Effect.SMOKE, 0);
    player.playEffect(player.getLocation(), Effect.SMOKE, 0);
    player.sendMessage(ChatColor.GREEN+"*Invisibility cloak deactivated.");
    }
    }
    }

    Digi damn thanks :) ill go test this out now! and btw, is it possible to fill his bar completely, BUT! not advance him to then next XP level (like from level 1 to 2 or 2 to 3)

    wait then would my "meter" hashmap need to be double?

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

    chasechocolate

    float.
     
  7. float.. but isnt float a double? or does float only go to 1 decimal point?

    hmm with float when I set it with InvisWatch.meter.put(player, float); I need to cast it right?

    sorry if I am spamming posts but would i use InvisWatch.meter.put(spy, (float) (InvisWatch.meter.get(spy)+0.1));
    and InvisWatch.meter.put(spy, (float) (InvisWatch.meter.get(spy)-0.1)); ?

    Thanks guys I can set XP but when im checking for XP I use if(meter.get(player)>0) and i get an error in the console:

    [SEVERE] Could not pass event PlayerInteractEvent to TF2 v1.1
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at org.bukkit.craftbukkit.v1_5_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:181)
    at net.minecraft.server.v1_5_R2.PlayerInteractManager.interact(PlayerInteractManager.java:376)
    at net.minecraft.server.v1_5_R2.PlayerConnection.a(PlayerConnection.java:635)
    at net.minecraft.server.v1_5_R2.Packet15Place.handle(SourceFile:58)
    at net.minecraft.server.v1_5_R2.NetworkManager.b(NetworkManager.java:292)
    at net.minecraft.server.v1_5_R2.PlayerConnection.d(PlayerConnection.java:113)
    at net.minecraft.server.v1_5_R2.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.v1_5_R2.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_5_R2.MinecraftServer.r(MinecraftServer.java:580)
    at net.minecraft.server.v1_5_R2.DedicatedServer.r(DedicatedServer.java:225)
    at net.minecraft.server.v1_5_R2.MinecraftServer.q(MinecraftServer.java:476)
    at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:409)
    at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
    ---->at me.Shadow.TF2.InvisWatch.start(InvisWatch.java:22)
    at me.Shadow.TF2.InvisWatch.use(InvisWatch.java:18)
    at me.Shadow.TF2.TF2abilities.use(TF2abilities.java:33)
    at me.Shadow.TF2.PlayerListener.onPlayerInteract(PlayerListener.java:87)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 16 more

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  8. Float is less accurate than double and uses half the size, 32bits for float, 64bits for double if I recall correctly.

    Anyway, to make it seem like it's full you could try 0.99.
     
  9. can I compare floats to integers for example:
    Float x = 1;
    if(x>0){
    x=x-0.1;
    }
     
  10. Yes. and you can also compare against float like this:
    Code:
    float f = 0.99f;
    
    if(f > 0.5f)
    {
        // ....
    }
     
  11. OHHH i only did f>0.5 , so i have to add "f" after the number?

    ... when i compare the float with (0.0f) it doesnt work?

    when i use this i get an error in the console:
    Float f = 0.1f;
    if(meter.get(player)>=f)
    ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  12. Offline

    SoThatsIt

    what error? if its a null pointer it is probably that your meter variable is null.
     
  13. but its not... i set it with InvisWatch.meter.put(player, 1.0f);
     
  14. XxShadow_NinjaxxX
    0.5f is specifying that it's a float value, 0.5 is by default a double value but you can also use 0.5d to be specific.

    And you should show the actual error because now it's all just guessing.
     
  15. i showed it in an above comment?
    here ill post it again

    [SEVERE] Could not pass event PlayerInteractEvent to TF2 v1.1
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at org.bukkit.craftbukkit.v1_5_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:181)
    at net.minecraft.server.v1_5_R2.PlayerInteractManager.interact(PlayerInteractManager.java:376)
    at net.minecraft.server.v1_5_R2.PlayerConnection.a(PlayerConnection.java:635)
    at net.minecraft.server.v1_5_R2.Packet15Place.handle(SourceFile:58)
    at net.minecraft.server.v1_5_R2.NetworkManager.b(NetworkManager.java:292)
    at net.minecraft.server.v1_5_R2.PlayerConnection.d(PlayerConnection.java:113)
    at net.minecraft.server.v1_5_R2.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.v1_5_R2.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_5_R2.MinecraftServer.r(MinecraftServer.java:580)
    at net.minecraft.server.v1_5_R2.DedicatedServer.r(DedicatedServer.java:225)
    at net.minecraft.server.v1_5_R2.MinecraftServer.q(MinecraftServer.java:476)
    at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:409)
    at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
    ---->at me.Shadow.TF2.InvisWatch.start(InvisWatch.java:22)
    at me.Shadow.TF2.InvisWatch.use(InvisWatch.java:18)
    at me.Shadow.TF2.TF2abilities.use(TF2abilities.java:33)
    at me.Shadow.TF2.PlayerListener.onPlayerInteract(PlayerListener.java:87)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 16 more

    when f=a float value
    hmm can i use:
    if(f>=0.1f){
    }
    or can i not use the >= and <= signs?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  16. XxShadow_NinjaxxX
    Ah I didn't notice the stacktrace :confused: anyway.

    The issue seems to not be because of a map, post the InvisWatch class at lines 18 and 22 and some surrounding code so we can understand what's going on.
     
  17. oh i think i solved the problem, apparently you cant use >= or <= when you re dealing with floats :p
    ya it works now, thanks for the help :)
     
  18. No, you can use comparators (==, >, >=, <, <=, etc) when dealing with floats... but if it's a Float then it can also be null and null can't be bigger or smaller than something, it can only be or not be equal to null.

    My suggestion is that you store the value to a Float object (not 'float' primitive which can't hold a 'null' value) and then check if it's not null then use it to compare against whatever you need.
     
Thread Status:
Not open for further replies.

Share This Page