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
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.
Well the method takes an int as the amount parameter as seen in the docs. Passing any other data type is incorrect.
#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?
Casting to int doesn't do any rounding IIRC, it just cuts off decimals. You should use Math.round(float).
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
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...
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.
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.
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?)