RandomChanceUtil refactor part 4

This commit is contained in:
nossr50
2021-02-18 16:41:57 -08:00
parent e30c7110eb
commit 6a3671a190
26 changed files with 232 additions and 226 deletions

View File

@@ -8,7 +8,7 @@ import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.skills.repair.Repair;
import com.gmail.nossr50.skills.salvage.Salvage;
import com.gmail.nossr50.util.random.RandomChanceUtil;
import com.gmail.nossr50.util.skills.SkillUtils;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
@@ -52,9 +52,9 @@ public final class BlockUtils {
* @param blockState the blockstate
* @return true if the player succeeded in the check
*/
public static boolean checkDoubleDrops(Player player, BlockState blockState, PrimarySkillType skillType, SubSkillType subSkillType) {
public static boolean checkDoubleDrops(@NotNull Player player, @NotNull BlockState blockState, @NotNull PrimarySkillType skillType, @NotNull SubSkillType subSkillType) {
if (Config.getInstance().getDoubleDropsEnabled(skillType, blockState.getType()) && Permissions.isSubSkillEnabled(player, subSkillType)) {
return RandomChanceUtil.processProbabilityResults(new SkillProbabilityWrapper(player, subSkillType, true));
return SkillUtils.isSkillRNGSuccessful(subSkillType, player);
}
return false;

View File

@@ -16,7 +16,7 @@ public class ProbabilityFactory {
public static @NotNull Probability ofSubSkill(@Nullable Player player,
@NotNull SubSkillType subSkillType,
@NotNull SkillProbabilityType skillProbabilityType) throws InvalidStaticChance, RuntimeException {
@NotNull SkillProbabilityType skillProbabilityType) {
switch (skillProbabilityType) {
case DYNAMIC_CONFIGURABLE:
@@ -40,7 +40,11 @@ public class ProbabilityFactory {
xCeiling = AdvancedConfig.getInstance().getMaxBonusLevel(subSkillType);
return new ProbabilityImpl(xPos, xCeiling, probabilityCeiling);
case STATIC_CONFIGURABLE:
return ofPercentageValue(getStaticRandomChance(subSkillType));
try {
return ofPercentageValue(getStaticRandomChance(subSkillType));
} catch (InvalidStaticChance invalidStaticChance) {
invalidStaticChance.printStackTrace();
}
default:
throw new RuntimeException("No case in switch statement for Skill Probability Type!");
}

View File

@@ -9,7 +9,6 @@ import java.text.DecimalFormat;
import java.util.concurrent.ThreadLocalRandom;
//TODO: Normalize chance values
//TODO: Test the 2 types of SkillProbabilityTypes
//TODO: Update calls to this class and its members
public class RandomChanceUtil {
public static final @NotNull DecimalFormat percent = new DecimalFormat("##0.00%");
@@ -57,22 +56,29 @@ public class RandomChanceUtil {
*
* @return "percentage" representation of success
*/
private static double chanceOfSuccessPercentage(@NotNull Player player, @NotNull SubSkillType subSkillType, boolean isLucky) {
try {
Probability probability = SkillUtils.getSubSkillProbability(subSkillType, player);
//Probability values are on a 0-1 scale and need to be "transformed" into a 1-100 scale
double percentageValue = probability.getValue() * 100;
public static double chanceOfSuccessPercentage(@NotNull Player player, @NotNull SubSkillType subSkillType, boolean isLucky) {
Probability probability = SkillUtils.getSubSkillProbability(subSkillType, player);
//Probability values are on a 0-1 scale and need to be "transformed" into a 1-100 scale
double percentageValue = probability.getValue() * 100;
//Apply lucky modifier
if(isLucky) {
percentageValue *= LUCKY_MODIFIER;
}
return percentageValue;
} catch (InvalidStaticChance invalidStaticChance) {
invalidStaticChance.printStackTrace();
return 0;
//Apply lucky modifier
if(isLucky) {
percentageValue *= LUCKY_MODIFIER;
}
return percentageValue;
}
public static double chanceOfSuccessPercentage(@NotNull Probability probability, boolean isLucky) {
//Probability values are on a 0-1 scale and need to be "transformed" into a 1-100 scale
double percentageValue = probability.getValue() * 100;
//Apply lucky modifier
if(isLucky) {
percentageValue *= LUCKY_MODIFIER;
}
return percentageValue;
}
}

View File

@@ -358,38 +358,31 @@ public final class SkillUtils {
* @return true if the Skill RNG succeeds, false if it fails
*/
public static boolean isSkillRNGSuccessful(@NotNull SubSkillType subSkillType, @NotNull Player player) {
try {
//Process probability
Probability probability = getSubSkillProbability(subSkillType, player);
//Process probability
Probability probability = getSubSkillProbability(subSkillType, player);
//Send out event
SubSkillEvent subSkillEvent = EventUtils.callSubSkillEvent(player, subSkillType);
//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);
} else {
return RandomChanceUtil.processProbability(probability);
}
} catch (RuntimeException | InvalidStaticChance e) {
e.printStackTrace();
if(subSkillEvent.isCancelled()) {
return false; //Event got cancelled so this doesn't succeed
}
return false;
//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);
} else {
return RandomChanceUtil.processProbability(probability);
}
}
/**
@@ -446,7 +439,7 @@ public final class SkillUtils {
* @throws InvalidStaticChance when a skill that does not have a hard coded static chance and it is asked for
* @throws RuntimeException
*/
public static @NotNull Probability getSubSkillProbability(@NotNull SubSkillType subSkillType, @Nullable Player player) throws InvalidStaticChance, RuntimeException {
public static @NotNull Probability getSubSkillProbability(@NotNull SubSkillType subSkillType, @Nullable Player player) {
SkillProbabilityType skillProbabilityType = SkillProbabilityType.DYNAMIC_CONFIGURABLE;
if(subSkillType == SubSkillType.TAMING_FAST_FOOD_SERVICE || subSkillType == SubSkillType.AXES_ARMOR_IMPACT || subSkillType == SubSkillType.AXES_GREATER_IMPACT)
@@ -454,4 +447,18 @@ public final class SkillUtils {
return ProbabilityFactory.ofSubSkill(player, subSkillType, skillProbabilityType);
}
public static @NotNull String[] getRNGDisplayValues(@NotNull Player player, @NotNull SubSkillType subSkill) {
double firstValue = RandomChanceUtil.chanceOfSuccessPercentage(player, subSkill, false);
double secondValue = RandomChanceUtil.chanceOfSuccessPercentage(player, subSkill, true);
return new String[]{RandomChanceUtil.percent.format(firstValue), RandomChanceUtil.percent.format(secondValue)};
}
public static @NotNull String[] getRNGDisplayValues(@NotNull Probability probability) {
double firstValue = RandomChanceUtil.chanceOfSuccessPercentage(probability, false);
double secondValue = RandomChanceUtil.chanceOfSuccessPercentage(probability, true);
return new String[]{RandomChanceUtil.percent.format(firstValue), RandomChanceUtil.percent.format(secondValue)};
}
}