RandomChanceUtil refactor part 3

This commit is contained in:
nossr50
2021-02-18 12:48:16 -08:00
parent fe889cf1c5
commit e30c7110eb
10 changed files with 65 additions and 60 deletions

View File

@@ -9,7 +9,6 @@ import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import com.gmail.nossr50.datatypes.skills.subskills.AbstractSubSkill;
import com.gmail.nossr50.events.experience.McMMOPlayerLevelChangeEvent;
import com.gmail.nossr50.events.experience.McMMOPlayerLevelDownEvent;
import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
@@ -190,8 +189,7 @@ public final class EventUtils {
* @param subSkillType target subskill
* @return the event after it has been fired
*/
@Deprecated
public static @NotNull SubSkillEvent callSubSkillEvent(Player player, SubSkillType subSkillType) {
public static @NotNull SubSkillEvent callSubSkillEvent(@NotNull Player player, @NotNull SubSkillType subSkillType) {
SubSkillEvent event = new SubSkillEvent(player, subSkillType);
mcMMO.p.getServer().getPluginManager().callEvent(event);
@@ -213,19 +211,6 @@ public final class EventUtils {
return event;
}
/**
* Calls a new SubSkillEvent for this SubSkill and then returns it
* @param player target player
* @param abstractSubSkill target subskill
* @return the event after it has been fired
*/
public static SubSkillEvent callSubSkillEvent(Player player, AbstractSubSkill abstractSubSkill) {
SubSkillEvent event = new SubSkillEvent(player, abstractSubSkill);
mcMMO.p.getServer().getPluginManager().callEvent(event);
return event;
}
public static FakePlayerAnimationEvent callFakeArmSwingEvent(Player player) {
FakePlayerAnimationEvent event = new FakePlayerAnimationEvent(player);
mcMMO.p.getServer().getPluginManager().callEvent(event);

View File

@@ -13,9 +13,7 @@ public class ProbabilityImpl implements Probability {
* @param staticProbability the value to assign to this probability
*/
public ProbabilityImpl(double staticProbability) throws ValueOutOfBoundsException {
if(staticProbability > 1) {
throw new ValueOutOfBoundsException("Value should never be above 1 for Probability! This suggests a coding mistake, contact the devs!");
} else if (staticProbability < 0) {
if (staticProbability < 0) {
throw new ValueOutOfBoundsException("Value should never be negative for Probability! This suggests a coding mistake, contact the devs!");
}

View File

@@ -10,8 +10,10 @@ import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import com.gmail.nossr50.events.skills.secondaryabilities.SubSkillEvent;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.EventUtils;
import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
@@ -348,16 +350,34 @@ public final class SkillUtils {
*
* 3) Creates a {@link Probability} and pipes it to {@link RandomChanceUtil} which processes the result and returns it
*
* This also calls a {@link SubSkillEvent} which can be cancelled, if it is cancelled this will return false
* The outcome of the probability can also be modified by this event that is called
*
* @param subSkillType target subskill
* @param player target player, can be null (null players are given odds equivalent to a player with no levels or luck)
* @return true if the Skill RNG succeeds, false if it fails
*/
public static boolean isSkillRNGSuccessful(@NotNull SubSkillType subSkillType, @Nullable Player player) {
public static boolean isSkillRNGSuccessful(@NotNull SubSkillType subSkillType, @NotNull Player player) {
try {
//Process probability
Probability probability = getSubSkillProbability(subSkillType, player);
//Player can be null
boolean isLucky = player != null && Permissions.lucky(player, subSkillType.getParentSkill());
//Send out event
SubSkillEvent subSkillEvent = EventUtils.callSubSkillEvent(player, subSkillType);
if(subSkillEvent.isCancelled()) {
return false; //Event got cancelled so this doesn't succeed
}
//Result modifier
double resultModifier = subSkillEvent.getResultModifier();
//Mutate probability
if(resultModifier != 1.0D)
probability = ProbabilityFactory.ofPercentageValue(probability.getValue() * resultModifier);
//Luck
boolean isLucky = Permissions.lucky(player, subSkillType.getParentSkill());
if(isLucky) {
return RandomChanceUtil.processProbability(probability, RandomChanceUtil.LUCKY_MODIFIER);
@@ -407,6 +427,16 @@ public final class SkillUtils {
}
}
/**
* Skills activate without RNG, this allows other plugins to prevent that activation
* @param subSkillType target subskill
* @param player target player
* @return true if the skill succeeds (wasn't cancelled by any other plugin)
*/
public static boolean isNonRNGSkillActivationSuccessful(@NotNull SubSkillType subSkillType, @NotNull Player player) {
return !EventUtils.callSubSkillEvent(player, subSkillType).isCancelled();
}
/**
* Grab the {@link Probability} for a specific {@link SubSkillType} for a specific {@link Player}
*