giveExp not adding exp

Discussion in 'Spigot Plugin Development' started by SamB440, Apr 16, 2018.

  1. Hello,

    I am trying to add exp to a player using:

    Code (Text):
    p.giveExp((int) 0.5 * multiplier);
    However, it does not add exp. Multiplier is just the current exp multiplier they have - it's set to 1 at the moment.

    But if I set the value '0.5' to 1, it works fine. But this is not good as it adds too much EXP.

    Thanks
     
  2. Benz56

    Junior Mod Supporter

    Either extract the amount calculation to an int variable or add parentheses around the calculation.

    I am not sure it is going to work but it is worth a shot.
     
  3. (int) 0.5 * multiplier, if you use (int), it makes 'rounding'
    use double or flat
     
  4. Benz56

    Junior Mod Supporter

    Well the method takes an int as the amount parameter as seen in the docs.

    Passing any other data type is incorrect.
     
  5. #setExp(float exp)*

    setExp((float)getExp + 0.5 * multip);
     
  6. #setExp causes issues, since it isn't adding it on or whatever... You won't get what I mean, but try it yourself. It doesn't work well at all since you're setting it, not giving it.

    What do you mean by this?
     
  7. Benz56

    Junior Mod Supporter

    int amount = (int) (0.5 * multiplier)

    Then use the variable in the method.
     
  8. Casting to int doesn't do any rounding IIRC, it just cuts off decimals. You should use Math.round(float).
     
  9. ok, i tried just now then i got u
    it doesn't work, i tried again*

    so, maybe you can create getNeedLvlUpExp()* then again you player.setExp method but now with create a new algrorithm or calcutor
     
  10. I have tried both of these and neither worked. Math#round just makes it 1, which is not what I want.

    Not sure how I'd go about this, I'll try later...
     
  11. p.setExp(p.getExp() + ((float) 0.5 + multiplier));
    his should work, it get the exp then add it.
     
  12. I already tried that and mentioned it doesn't work when the player needs to level up.
     
  13. int are whole numbers.

    use:

    Code (Text):
    p.giveExp((double) 0.5 * multiplier);
     
  14. Benz56

    Junior Mod Supporter

    The giveExp() method still takes an int as the amount parameter. Not a double.
     
  15. What are you trying to do? Do you mean xp rather then xp level?

    The amount of xp required to get from one level to another depends on the current xp level of the player the chances of a player having 1 xp (as in the return of .getTotalExperience() is very low).
    If you are trying to give the player a 50% xp boost then what you are doing is correct (assuming multiplier is obtained from .getTotalExperience()) and you test is just broken and you need to test with say at least 10 xp.
     
  16. Code (Text):
        public boolean needsLevelUp(Player player)
        {
            if(player.getExpToLevel() <= player.getExp()) return true;
            else return false;
        }
    Shouldn't this work? I tried it but keep getting this error:

    Code (Text):
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:72) ~[pa
    tched_1.12.2.jar:git-Paper-1380]
            at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:7
    8) ~[patched_1.12.2.jar:git-Paper-1380]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[patched_1.12.2.jar:git-Paper-1380]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:515) [patched_1.12.2.jar:git-Paper-1380]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:500) [patched_1.12.2.jar:git-Paper-1380]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callBlockPlac
    eEvent(CraftEventFactory.java:151) [patched_1.12.2.jar:git-Paper-1380]
            at net.minecraft.server.v1_12_R1.ItemStack.placeItem(ItemStack.java:230)
     [patched_1.12.2.jar:git-Paper-1380]
            at net.minecraft.server.v1_12_R1.PlayerInteractManager.a(PlayerInteractM
    anager.java:513) [patched_1.12.2.jar:git-Paper-1380]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.jav
    a:991) [patched_1.12.2.jar:git-Paper-1380]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseIt
    em.java:37) [patched_1.12.2.jar:git-Paper-1380]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseIt
    em.java:5) [patched_1.12.2.jar:git-Paper-1380]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils.lambda$ensureMain
    Thread$0(PlayerConnectionUtils.java:14) [patched_1.12.2.jar:git-Paper-1380]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$$Lambda$340/59315
    9960.run(Unknown Source) [patched_1.12.2.jar:git-Paper-1380]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_51]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_51]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [patched_1
    .12.2.jar:git-Paper-1380]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:
    843) [patched_1.12.2.jar:git-Paper-1380]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:
    426) [patched_1.12.2.jar:git-Paper-1380]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:
    767) [patched_1.12.2.jar:git-Paper-1380]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.jav
    a:665) [patched_1.12.2.jar:git-Paper-1380]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    Caused by: java.lang.IllegalArgumentException: Experience progress must be betwe
    en 0.0 and 1.0 (1.0000001)
            at com.google.common.base.Preconditions.checkArgument(Preconditions.java
    :191) ~[patched_1.12.2.jar:git-Paper-1380]
            at org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer.setExp(CraftPlayer
    .java:977) ~[patched_1.12.2.jar:git-Paper-1380]
            at com.SamB440.RPGWorld.listeners.BlockListener.onPlace(BlockListener.ja
    va:34) ~[?:?]
            at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventEx
    ecutor442.execute(Unknown Source) ~[?:?]
            at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:70) ~[pa
    tched_1.12.2.jar:git-Paper-1380]
            ... 20 more
    Line 34 is:
    Code (Text):
    player.setExp(player.getExp() + (float) 0.1 * (float) multiplier);
    I am trying to add EXP to the level bar.
     
  17. This can be simplified to:
    Code (Text):
    return player.getExpToLevel() <= player.getExp()
    Since the value needs to be 0-1, you can use Ints.constrainToRange(xp, 0, 1). Your issue is that your value is going just over 1.0 (floating point error?)
     

Share This Page