-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
183 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||
From: Lukas Planz <lukas.planz@web.de> | ||
Date: Tue, 5 Sep 2023 20:33:52 +0200 | ||
Subject: [PATCH] Add experience points API | ||
|
||
|
||
diff --git a/src/main/java/org/bukkit/entity/Player.java b/src/main/java/org/bukkit/entity/Player.java | ||
index c09f423fe2a029c933bd2697c8ff104fc4230cf0..ae61a39b25267b84fe0b8766e4b12d9b24b44ded 100644 | ||
--- a/src/main/java/org/bukkit/entity/Player.java | ||
+++ b/src/main/java/org/bukkit/entity/Player.java | ||
@@ -1765,6 +1765,45 @@ public interface Player extends HumanEntity, Conversable, OfflinePlayer, PluginM | ||
* @param exp New total experience points | ||
*/ | ||
public void setTotalExperience(int exp); | ||
+ // Paper start | ||
+ /** | ||
+ * Gets the players total amount of experience points he collected to reach the current level and level progress. | ||
+ * | ||
+ * <p>This method differs from {@link #getTotalExperience()} in that this method always returns an | ||
+ * up-to-date value that reflects the players{@link #getLevel() level} and {@link #getExp() level progress}</p> | ||
+ * | ||
+ * @return Current total experience points | ||
+ * @see #getLevel() | ||
+ * @see #getExp() | ||
+ * @see #setExperienceLevelAndProgress(int) | ||
+ */ | ||
+ @org.jetbrains.annotations.Range(from = 0, to = Integer.MAX_VALUE) int calculateTotalExperiencePoints(); | ||
+ | ||
+ /** | ||
+ * Updates the players level and level progress to that what would be reached when the total amount of experience | ||
+ * had been collected. | ||
+ * | ||
+ * <p>This method differs from {@link #setTotalExperience(int)} in that this method actually updates the | ||
+ * {@link #getLevel() level} and {@link #getExp() level progress} so that a subsequent call of | ||
+ * {@link #calculateTotalExperiencePoints()} yields the same amount of points that have been set</p> | ||
+ * | ||
+ * @param totalExperience New total experience points | ||
+ * @see #setLevel(int) | ||
+ * @see #setExp(float) | ||
+ * @see #calculateTotalExperiencePoints() | ||
+ */ | ||
+ void setExperienceLevelAndProgress(@org.jetbrains.annotations.Range(from = 0, to = Integer.MAX_VALUE) int totalExperience); | ||
+ | ||
+ /** | ||
+ * Gets the total amount of experience points that are needed to reach the next level from zero progress towards it. | ||
+ * | ||
+ * <p>Can be used with {@link #getExp()} to calculate the current points for the current level and alike</p> | ||
+ * | ||
+ * @return The required experience points | ||
+ * @see #getExp() | ||
+ */ | ||
+ int getExperiencePointsNeededForNextLevel(); | ||
+ // Paper end | ||
|
||
/** | ||
* Send an experience change. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||
From: Lukas Planz <lukas.planz@web.de> | ||
Date: Tue, 5 Sep 2023 20:34:20 +0200 | ||
Subject: [PATCH] Add experience points API | ||
|
||
|
||
diff --git a/src/main/java/net/minecraft/world/entity/player/Player.java b/src/main/java/net/minecraft/world/entity/player/Player.java | ||
index 7f3466340891b4409d1399ebeb2ca865d77841cd..3e597833b57377b855505b8a0f2744801c791f90 100644 | ||
--- a/src/main/java/net/minecraft/world/entity/player/Player.java | ||
+++ b/src/main/java/net/minecraft/world/entity/player/Player.java | ||
@@ -1833,7 +1833,7 @@ public abstract class Player extends LivingEntity { | ||
} | ||
|
||
public int getXpNeededForNextLevel() { | ||
- return this.experienceLevel >= 30 ? 112 + (this.experienceLevel - 30) * 9 : (this.experienceLevel >= 15 ? 37 + (this.experienceLevel - 15) * 5 : 7 + this.experienceLevel * 2); | ||
+ return this.experienceLevel >= 30 ? 112 + (this.experienceLevel - 30) * 9 : (this.experienceLevel >= 15 ? 37 + (this.experienceLevel - 15) * 5 : 7 + this.experienceLevel * 2); // Paper - diff on change | ||
} | ||
// Paper start - send SoundEffect to everyone who can see fromEntity | ||
private static void sendSoundEffect(Player fromEntity, double x, double y, double z, SoundEvent soundEffect, SoundSource soundCategory, float volume, float pitch) { | ||
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java | ||
index 4ae4436671b05a3535b9955af60842d4c9d1d102..2ec8b8f65661001716d1cb34dcc21cda7286e5d7 100644 | ||
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java | ||
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java | ||
@@ -1815,6 +1815,49 @@ public class CraftPlayer extends CraftHumanEntity implements Player { | ||
Preconditions.checkArgument(exp >= 0, "Total experience points must not be negative (%s)", exp); | ||
this.getHandle().totalExperience = exp; | ||
} | ||
+ // Paper start | ||
+ @Override | ||
+ public int calculateTotalExperiencePoints() { | ||
+ return calculateTotalExperiencePoints(this.getLevel()) + Math.round(this.getExperiencePointsNeededForNextLevel() * getExp()); | ||
+ } | ||
+ | ||
+ @Override | ||
+ public void setExperienceLevelAndProgress(final int totalExperience) { | ||
+ Preconditions.checkArgument(totalExperience >= 0, "Total experience points must not be negative (%s)", totalExperience); | ||
+ int level = calculateLevelsForExperiencePoints(totalExperience); | ||
+ int remainingPoints = totalExperience - calculateTotalExperiencePoints(level); | ||
+ | ||
+ this.getHandle().experienceLevel = level; | ||
+ this.getHandle().experienceProgress = (float) remainingPoints / this.getExperiencePointsNeededForNextLevel(); | ||
+ this.getHandle().lastSentExp = -1; | ||
+ } | ||
+ | ||
+ @Override | ||
+ public int getExperiencePointsNeededForNextLevel() { | ||
+ return this.getHandle().getXpNeededForNextLevel(); | ||
+ } | ||
+ | ||
+ // See https://minecraft.wiki/w/Experience#Leveling_up for reference | ||
+ private int calculateTotalExperiencePoints(int level) { | ||
+ if (level <= 16) { | ||
+ return (int) (Math.pow(level, 2) + 6 * level); | ||
+ } else if (level <= 31) { | ||
+ return (int) (2.5 * Math.pow(level, 2) - 40.5 * level + 360.0); | ||
+ } else { | ||
+ return (int) (4.5 * Math.pow(level, 2) - 162.5 * level + 2220.0); | ||
+ } | ||
+ } | ||
+ | ||
+ private int calculateLevelsForExperiencePoints(int points) { | ||
+ if (points <= 352) { // Level 0-16 | ||
+ return (int) Math.floor(Math.sqrt(points + 9) - 3); | ||
+ } else if (points <= 1507) { // Level 17-31 | ||
+ return (int) Math.floor(8.1 + Math.sqrt(0.4 * (points - (7839.0 / 40.0)))); | ||
+ } else { // 32+ | ||
+ return (int) Math.floor((325.0 / 18.0) + Math.sqrt((2.0 / 9.0) * (points - (54215.0 / 72.0)))); | ||
+ } | ||
+ } | ||
+ // Paper end | ||
|
||
@Override | ||
public void sendExperienceChange(float progress) { |