Fixed exp splitting + now supports professions

This commit is contained in:
Indyuce 2022-06-14 09:39:58 +02:00
parent 617c2a3713
commit ba2691daf2
8 changed files with 43 additions and 30 deletions

View File

@ -4,11 +4,12 @@ import org.bukkit.event.HandlerList;
import net.Indyuce.mmocore.experience.Profession; import net.Indyuce.mmocore.experience.Profession;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import org.jetbrains.annotations.Nullable;
public class PlayerLevelUpEvent extends PlayerDataEvent { public class PlayerLevelUpEvent extends PlayerDataEvent {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
// if null, this is main level // If null, this is main level
private final Profession profession; private final Profession profession;
private final int oldLevel, newLevel; private final int oldLevel, newLevel;
@ -36,6 +37,7 @@ public class PlayerLevelUpEvent extends PlayerDataEvent {
return profession != null; return profession != null;
} }
@Nullable
public Profession getProfession() { public Profession getProfession() {
return profession; return profession;
} }

View File

@ -540,18 +540,19 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
return; return;
} }
value = MMOCore.plugin.boosterManager.calculateExp(null, value);
value *= 1 + getStats().getStat("ADDITIONAL_EXPERIENCE") / 100;
// Splitting exp through party members // Splitting exp through party members
AbstractParty party = getParty(); AbstractParty party;
if (splitExp && party != null) { if (splitExp && (party = getParty()) != null) {
List<PlayerData> onlineMembers = getParty().getOnlineMembers(); List<PlayerData> onlineMembers = party.getOnlineMembers();
value /= onlineMembers.size(); value /= onlineMembers.size();
for (PlayerData member : onlineMembers) for (PlayerData member : onlineMembers)
member.giveExperience(value, EXPSource.PARTY_SHARING, null, false); if (!equals(member))
member.giveExperience(value, source, null, false);
} }
// Apply buffs AFTER splitting exp
value *= (1 + getStats().getStat("ADDITIONAL_EXPERIENCE") / 100) * MMOCore.plugin.boosterManager.getMultiplier(null);
PlayerExperienceGainEvent event = new PlayerExperienceGainEvent(this, value, source); PlayerExperienceGainEvent event = new PlayerExperienceGainEvent(this, value, source);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) if (event.isCancelled())

View File

@ -23,9 +23,9 @@ public class ExperienceCommandTreeNode extends CommandTreeNode {
addChild(new ActionCommandTreeNode(this, "set", PlayerData::setExperience, PlayerProfessions::setExperience)); addChild(new ActionCommandTreeNode(this, "set", PlayerData::setExperience, PlayerProfessions::setExperience));
addChild(new ActionCommandTreeNode(this, "give", (data, value) -> data.giveExperience(value, EXPSource.COMMAND), (professions, profession, addChild(new ActionCommandTreeNode(this, "give", (data, value) -> data.giveExperience(value, EXPSource.COMMAND), (professions, profession,
value) -> professions.giveExperience(profession, value, EXPSource.COMMAND, professions.getPlayerData().getPlayer().getLocation()))); value) -> professions.giveExperience(profession, value, EXPSource.COMMAND)));
addChild(new ActionCommandTreeNode(this, "take", (data, value) -> data.giveExperience(-value, EXPSource.COMMAND), (professions, profession, addChild(new ActionCommandTreeNode(this, "take", (data, value) -> data.giveExperience(-value, EXPSource.COMMAND), (professions, profession,
value) -> professions.giveExperience(profession, -value, EXPSource.COMMAND, professions.getPlayerData().getPlayer().getLocation()))); value) -> professions.giveExperience(profession, -value, EXPSource.COMMAND)));
} }
public static class ActionCommandTreeNode extends CommandTreeNode { public static class ActionCommandTreeNode extends CommandTreeNode {

View File

@ -19,7 +19,10 @@ public enum EXPSource {
/** /**
* When party members share exp * When party members share exp
*
* @deprecated Not used anymore
*/ */
@Deprecated
PARTY_SHARING, PARTY_SHARING,
/** /**

View File

@ -13,6 +13,7 @@ import net.Indyuce.mmocore.api.event.PlayerLevelUpEvent;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.util.MMOCoreUtils; import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.Indyuce.mmocore.loot.chest.particle.SmallParticleEffect; import net.Indyuce.mmocore.loot.chest.particle.SmallParticleEffect;
import net.Indyuce.mmocore.party.AbstractParty;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -22,6 +23,7 @@ import org.bukkit.configuration.ConfigurationSection;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -147,15 +149,15 @@ public class PlayerProfessions {
giveExperience(profession, total, source); giveExperience(profession, total, source);
} }
public void giveExperience(Profession profession, double value, EXPSource source) {
giveExperience(profession, value, source, null);
}
public boolean hasReachedMaxLevel(Profession profession) { public boolean hasReachedMaxLevel(Profession profession) {
return profession.hasMaxLevel() && getLevel(profession) >= profession.getMaxLevel(); return profession.hasMaxLevel() && getLevel(profession) >= profession.getMaxLevel();
} }
public void giveExperience(Profession profession, double value, EXPSource source, @Nullable Location hologramLocation) { public void giveExperience(Profession profession, double value, EXPSource source) {
giveExperience(profession, value, source, null, true);
}
public void giveExperience(Profession profession, double value, EXPSource source, @Nullable Location hologramLocation, boolean splitExp) {
Validate.isTrue(playerData.isOnline(), "Cannot give experience to offline player"); Validate.isTrue(playerData.isOnline(), "Cannot give experience to offline player");
if (value <= 0) if (value <= 0)
return; return;
@ -165,23 +167,31 @@ public class PlayerProfessions {
return; return;
} }
value = MMOCore.plugin.boosterManager.calculateExp(profession, value); // Splitting exp through party members
AbstractParty party;
if (splitExp && (party = playerData.getParty()) != null) {
List<PlayerData> onlineMembers = party.getOnlineMembers();
value /= onlineMembers.size();
for (PlayerData member : onlineMembers)
if (!member.equals(playerData))
member.getCollectionSkills().giveExperience(profession, value, source, null, false);
}
// Adds functionality for additional experience per profession. // Apply buffs AFTER splitting exp
value *= 1 + playerData.getStats().getInstance("ADDITIONAL_EXPERIENCE_" + UtilityMethods.enumName(profession.getId())).getTotal() / 100; value *= (1 + playerData.getStats().getStat("ADDITIONAL_EXPERIENCE_" + UtilityMethods.enumName(profession.getId())) / 100) * MMOCore.plugin.boosterManager.getMultiplier(profession);
// Display hologram
if (hologramLocation != null)
MMOCoreUtils.displayIndicator(hologramLocation.add(.5, 1.5, .5), MMOCore.plugin.configManager.getSimpleMessage("exp-hologram", "exp", "" + value).message());
PlayerExperienceGainEvent event = new PlayerExperienceGainEvent(playerData, profession, value, source); PlayerExperienceGainEvent event = new PlayerExperienceGainEvent(playerData, profession, value, source);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) if (event.isCancelled())
return; return;
// Display hologram
if (hologramLocation != null)
MMOCoreUtils.displayIndicator(hologramLocation.add(.5, 1.5, .5), MMOCore.plugin.configManager.getSimpleMessage("exp-hologram", "exp", "" + value).message());
exp.put(profession.getId(), Math.max(0, exp.getOrDefault(profession.getId(), 0.) + event.getExperience())); exp.put(profession.getId(), Math.max(0, exp.getOrDefault(profession.getId(), 0.) + event.getExperience()));
int level, oldLevel = getLevel(profession); int level, oldLevel = getLevel(profession);
double needed,exp; double needed, exp;
/* /*
* Loop for exp overload when leveling up, will continue * Loop for exp overload when leveling up, will continue

View File

@ -127,7 +127,7 @@ public class Profession extends PostLoadObject implements ExperienceObject {
public void giveExperience(PlayerData playerData, double experience, @Nullable Location hologramLocation, EXPSource source) { public void giveExperience(PlayerData playerData, double experience, @Nullable Location hologramLocation, EXPSource source) {
hologramLocation = !getOption(Profession.ProfessionOption.EXP_HOLOGRAMS) ? null hologramLocation = !getOption(Profession.ProfessionOption.EXP_HOLOGRAMS) ? null
: hologramLocation; : hologramLocation;
playerData.getCollectionSkills().giveExperience(this, experience, EXPSource.SOURCE, hologramLocation); playerData.getCollectionSkills().giveExperience(this, experience, EXPSource.SOURCE, hologramLocation, true);
} }
@Override @Override

View File

@ -197,7 +197,7 @@ public class FishingListener implements Listener {
location.getWorld().spawnParticle(Particle.FIREWORKS_SPARK, location, 0, 4 * (RANDOM.nextDouble() - .5), RANDOM.nextDouble() + 1, 4 * (RANDOM.nextDouble() - .5), .08); location.getWorld().spawnParticle(Particle.FIREWORKS_SPARK, location, 0, 4 * (RANDOM.nextDouble() - .5), RANDOM.nextDouble() + 1, 4 * (RANDOM.nextDouble() - .5), .08);
if (MMOCore.plugin.fishingManager.hasLinkedProfession()) if (MMOCore.plugin.fishingManager.hasLinkedProfession())
playerData.getCollectionSkills().giveExperience(MMOCore.plugin.fishingManager.getLinkedProfession(), experienceDropped, EXPSource.FISHING, location); playerData.getCollectionSkills().giveExperience(MMOCore.plugin.fishingManager.getLinkedProfession(), experienceDropped, EXPSource.FISHING, location, true);
} }
} }
} }

View File

@ -2,6 +2,7 @@ package net.Indyuce.mmocore.manager.social;
import net.Indyuce.mmocore.experience.Booster; import net.Indyuce.mmocore.experience.Booster;
import net.Indyuce.mmocore.experience.Profession; import net.Indyuce.mmocore.experience.Profession;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -48,7 +49,7 @@ public class BoosterManager {
/** /**
* @return Sums all current experience boosters values * @return Sums all current experience boosters values
*/ */
public double getMultiplier(Profession profession) { public double getMultiplier(@Nullable Profession profession) {
double d = 1; double d = 1;
for (Booster booster : map) for (Booster booster : map)
@ -58,10 +59,6 @@ public class BoosterManager {
return d; return d;
} }
public double calculateExp(Profession profession, double exp) {
return (exp * getMultiplier(profession));
}
/** /**
* @return Collection of currently registered boosters. Some of them can be * @return Collection of currently registered boosters. Some of them can be
* expired but are not unregistered yet! * expired but are not unregistered yet!