Static Abuse Removal - RandomChanceUtil -> RandomChanceTools

This commit is contained in:
nossr50
2019-07-04 06:13:29 -07:00
parent 8f540796ff
commit c5a7c88e0c
24 changed files with 128 additions and 121 deletions

View File

@@ -6,7 +6,6 @@ import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.skills.repair.Repair;
import com.gmail.nossr50.skills.salvage.Salvage;
import com.gmail.nossr50.util.random.RandomChanceSkill;
import com.gmail.nossr50.util.random.RandomChanceUtil;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
@@ -52,7 +51,7 @@ public final class BlockUtils {
*/
public static boolean checkDoubleDrops(Player player, BlockState blockState, SubSkillType subSkillType) {
if (pluginRef.getDynamicSettingsManager().isBonusDropsEnabled(blockState.getType()) && Permissions.isSubSkillEnabled(player, subSkillType)) {
return RandomChanceUtil.checkRandomChanceExecutionSuccess(new RandomChanceSkill(player, subSkillType, true));
return pluginRef.getRandomChanceTools().checkRandomChanceExecutionSuccess(new RandomChanceSkill(player, subSkillType, true));
}
return false;

View File

@@ -2,6 +2,7 @@ package com.gmail.nossr50.util.random;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.Permissions;
import org.bukkit.entity.Player;
@@ -11,12 +12,15 @@ public class RandomChanceSkill implements RandomChanceExecution {
protected final SubSkillType subSkillType;
protected final double probabilityCap;
protected final boolean isLucky;
private int skillLevel;
protected int skillLevel;
protected final mcMMO pluginRef;
public RandomChanceSkill(Player player, SubSkillType subSkillType) {
this.primarySkillType = subSkillType.getParentSkill();
public RandomChanceSkill(mcMMO pluginRef, Player player, SubSkillType subSkillType) {
this.pluginRef = pluginRef;
this.primarySkillType = subSkillType.getParentSkill(pluginRef);
this.subSkillType = subSkillType;
this.probabilityCap = RandomChanceUtil.LINEAR_CURVE_VAR;
this.probabilityCap = pluginRef.getRandomChanceTools().LINEAR_CURVE_VAR;
if (player != null)
this.skillLevel = pluginRef.getUserManager().getPlayer(player).getSkillLevel(primarySkillType);
@@ -29,13 +33,15 @@ public class RandomChanceSkill implements RandomChanceExecution {
isLucky = false;
}
public RandomChanceSkill(Player player, SubSkillType subSkillType, boolean hasCap) {
public RandomChanceSkill(mcMMO pluginRef, Player player, SubSkillType subSkillType, boolean hasCap) {
this.pluginRef = pluginRef;
if (hasCap)
this.probabilityCap = pluginRef.getDynamicSettingsManager().getSkillMaxChance(subSkillType);
else
this.probabilityCap = RandomChanceUtil.LINEAR_CURVE_VAR;
this.probabilityCap = pluginRef.getRandomChanceTools().LINEAR_CURVE_VAR;
this.primarySkillType = subSkillType.getParentSkill();
this.primarySkillType = subSkillType.getParentSkill(pluginRef);
this.subSkillType = subSkillType;
if (player != null)

View File

@@ -1,13 +1,14 @@
package com.gmail.nossr50.util.random;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.mcMMO;
import org.bukkit.entity.Player;
public class RandomChanceSkillStatic extends RandomChanceSkill {
private final double xPos;
public RandomChanceSkillStatic(double xPos, Player player, SubSkillType subSkillType) {
super(player, subSkillType);
public RandomChanceSkillStatic(mcMMO pluginRef, double xPos, Player player, SubSkillType subSkillType) {
super(pluginRef, player, subSkillType);
this.xPos = xPos;
}

View File

@@ -5,6 +5,7 @@ import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.datatypes.skills.subskills.AbstractSubSkill;
import com.gmail.nossr50.events.skills.secondaryabilities.SubSkillEvent;
import com.gmail.nossr50.events.skills.secondaryabilities.SubSkillRandomCheckEvent;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.skills.SkillActivationType;
import org.bukkit.entity.Player;
@@ -12,10 +13,19 @@ import org.bukkit.entity.Player;
import java.text.DecimalFormat;
import java.util.Random;
public class RandomChanceUtil {
public static final DecimalFormat percent = new DecimalFormat("##0.00%");
//public static final DecimalFormat decimal = new DecimalFormat("##0.00");
public static final double LINEAR_CURVE_VAR = 100.0D;
public class RandomChanceTools {
private final mcMMO pluginRef;
public final DecimalFormat percent;
public final double LINEAR_CURVE_VAR;
public RandomChanceTools(mcMMO pluginRef) {
this.pluginRef = pluginRef;
percent = new DecimalFormat("##0.00%");
LINEAR_CURVE_VAR = 100.0D;
}
/**
* This method is the final step in determining if a Sub-Skill / Secondary Skill in mcMMO successfully activates either from chance or otherwise
@@ -27,7 +37,7 @@ public class RandomChanceUtil {
* @param player The owner of this sub-skill
* @return returns true if all conditions are met and the event is not cancelled
*/
public static boolean isActivationSuccessful(SkillActivationType skillActivationType, SubSkillType subSkillType, Player player) {
public boolean isActivationSuccessful(SkillActivationType skillActivationType, SubSkillType subSkillType, Player player) {
switch (skillActivationType) {
case RANDOM_LINEAR_100_SCALE_WITH_CAP:
return checkRandomChanceExecutionSuccess(player, subSkillType);
@@ -41,7 +51,7 @@ public class RandomChanceUtil {
}
}
public static double getActivationChance(SkillActivationType skillActivationType, SubSkillType subSkillType, Player player) {
public double getActivationChance(SkillActivationType skillActivationType, SubSkillType subSkillType, Player player) {
switch (skillActivationType) {
case RANDOM_LINEAR_100_SCALE_WITH_CAP:
return getRandomChanceExecutionSuccess(player, subSkillType, true);
@@ -57,7 +67,7 @@ public class RandomChanceUtil {
*
* @return true if the random chance succeeds
*/
public static boolean checkRandomChanceExecutionSuccess(Player player, PrimarySkillType primarySkillType, double chance) {
public boolean checkRandomChanceExecutionSuccess(Player player, PrimarySkillType primarySkillType, double chance) {
//Check the odds
chance *= 100;
@@ -70,7 +80,7 @@ public class RandomChanceUtil {
return rollDice(chance, 10000);
}
public static boolean rollDice(double chanceOfSuccess, int bound) {
public boolean rollDice(double chanceOfSuccess, int bound) {
Random random = new Random();
if (chanceOfSuccess > random.nextInt(bound))
@@ -85,14 +95,14 @@ public class RandomChanceUtil {
* @param randomChance
* @return
*/
public static boolean checkRandomChanceExecutionSuccess(RandomChanceSkillStatic randomChance) {
public boolean checkRandomChanceExecutionSuccess(RandomChanceSkillStatic randomChance) {
double chanceOfSuccess = calculateChanceOfSuccess(randomChance);
//Check the odds
return rollDice(chanceOfSuccess, 100);
}
public static boolean checkRandomChanceExecutionSuccess(RandomChanceSkill randomChance) {
public boolean checkRandomChanceExecutionSuccess(RandomChanceSkill randomChance) {
double chanceOfSuccess = calculateChanceOfSuccess(randomChance);
//Check the odds
@@ -100,25 +110,25 @@ public class RandomChanceUtil {
}
/*public static double getRandomChanceExecutionChance(RandomChanceSkill randomChance)
/*public double getRandomChanceExecutionChance(RandomChanceSkill randomChance)
{
double chanceOfSuccess = calculateChanceOfSuccess(randomChance);
return chanceOfSuccess;
}*/
/**
* Gets the Static Chance for something to activate
* Gets the Chance for something to activate
*
* @param randomChance
* @return
*/
public static double getRandomChanceExecutionChance(RandomChanceExecution randomChance) {
public double getRandomChanceExecutionChance(RandomChanceExecution randomChance) {
double chanceOfSuccess = getChanceOfSuccess(randomChance.getXPos(), randomChance.getProbabilityCap(), LINEAR_CURVE_VAR);
return chanceOfSuccess;
}
public static double getRandomChanceExecutionChance(RandomChanceStatic randomChance) {
public double getRandomChanceExecutionChance(RandomChanceStatic randomChance) {
double chanceOfSuccess = getChanceOfSuccess(randomChance.getXPos(), randomChance.getProbabilityCap(), LINEAR_CURVE_VAR);
chanceOfSuccess = addLuck(randomChance.isLucky(), chanceOfSuccess);
@@ -126,12 +136,12 @@ public class RandomChanceUtil {
return chanceOfSuccess;
}
/*private static double calculateChanceOfSuccess(RandomChanceStatic randomChance) {
/*private double calculateChanceOfSuccess(RandomChancerandomChance) {
double chanceOfSuccess = getChanceOfSuccess(randomChance.getXPos(), randomChance.getProbabilityCap());
return chanceOfSuccess;
}*/
private static double calculateChanceOfSuccess(RandomChanceSkill randomChanceSkill) {
private double calculateChanceOfSuccess(RandomChanceSkill randomChanceSkill) {
double skillLevel = randomChanceSkill.getSkillLevel();
double maximumProbability = randomChanceSkill.getProbabilityCap();
double maximumBonusLevel = randomChanceSkill.getMaximumBonusLevelCap();
@@ -152,7 +162,7 @@ public class RandomChanceUtil {
return chanceOfSuccess;
}
private static double calculateChanceOfSuccess(RandomChanceSkillStatic randomChance) {
private double calculateChanceOfSuccess(RandomChanceSkillStatic randomChance) {
double chanceOfSuccess = getChanceOfSuccess(randomChance.getXPos(), 100, 100);
//Add Luck
@@ -167,42 +177,42 @@ public class RandomChanceUtil {
*
* @return the chance of success from 0-100 (100 = guaranteed)
*/
private static int getChanceOfSuccess(double skillLevel, double maxProbability, double maxLevel) {
private int getChanceOfSuccess(double skillLevel, double maxProbability, double maxLevel) {
//return (int) (x / (y / LINEAR_CURVE_VAR));
return (int) (maxProbability * (skillLevel / maxLevel));
// max probability * (weight/maxlevel) = chance of success
}
private static int getChanceOfSuccess(double x, double y) {
private int getChanceOfSuccess(double x, double y) {
return (int) (x / (y / LINEAR_CURVE_VAR));
// max probability * (weight/maxlevel) = chance of success
}
public static double getRandomChanceExecutionSuccess(Player player, SubSkillType subSkillType, boolean hasCap) {
RandomChanceSkill rcs = new RandomChanceSkill(player, subSkillType, hasCap);
public double getRandomChanceExecutionSuccess(Player player, SubSkillType subSkillType, boolean hasCap) {
RandomChanceSkill rcs = new RandomChanceSkill(pluginRef, player, subSkillType, hasCap);
return calculateChanceOfSuccess(rcs);
}
public static double getRandomStaticChanceExecutionSuccess(Player player, SubSkillType subSkillType) {
public double getRandomStaticChanceExecutionSuccess(Player player, SubSkillType subSkillType) {
try {
return getRandomChanceExecutionChance(new RandomChanceSkillStatic(getStaticRandomChance(subSkillType), player, subSkillType));
return getRandomChanceExecutionChance(new RandomChanceSkillStatic(pluginRef, getStaticRandomChance(subSkillType), player, subSkillType));
} catch (InvalidStaticChance invalidStaticChance) {
//Catch invalid static skills
//Catch invalid skills
invalidStaticChance.printStackTrace();
}
return 0.1337; //Puts on shades
}
public static boolean checkRandomChanceExecutionSuccess(Player player, SubSkillType subSkillType) {
return checkRandomChanceExecutionSuccess(new RandomChanceSkill(player, subSkillType));
public boolean checkRandomChanceExecutionSuccess(Player player, SubSkillType subSkillType) {
return checkRandomChanceExecutionSuccess(new RandomChanceSkill(pluginRef, player, subSkillType));
}
public static boolean checkRandomStaticChanceExecutionSuccess(Player player, SubSkillType subSkillType) {
public boolean checkRandomStaticChanceExecutionSuccess(Player player, SubSkillType subSkillType) {
try {
return checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(getStaticRandomChance(subSkillType), player, subSkillType));
return checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(pluginRef, getStaticRandomChance(subSkillType), player, subSkillType));
} catch (InvalidStaticChance invalidStaticChance) {
//Catch invalid static skills
//Catch invalid skills
invalidStaticChance.printStackTrace();
}
@@ -210,36 +220,36 @@ public class RandomChanceUtil {
}
/**
* Grabs static activation rolls for Secondary Abilities
* Grabs activation rolls for Secondary Abilities
*
* @param subSkillType The secondary ability to grab properties of
* @return The static activation roll involved in the RNG calculation
* @throws InvalidStaticChance if the skill has no defined static chance this exception will be thrown and you should know you're a naughty boy
* @return The activation roll involved in the RNG calculation
* @throws InvalidStaticChance if the skill has no defined chance this exception will be thrown and you should know you're a naughty boy
*/
public static double getStaticRandomChance(SubSkillType subSkillType) throws InvalidStaticChance {
public double getStaticRandomChance(SubSkillType subSkillType) throws InvalidStaticChance {
return pluginRef.getDynamicSettingsManager().getSkillPropertiesManager().getStaticChanceProperty(subSkillType);
}
public static boolean sendSkillEvent(Player player, SubSkillType subSkillType, double activationChance) {
public boolean sendSkillEvent(Player player, SubSkillType subSkillType, double activationChance) {
SubSkillRandomCheckEvent event = new SubSkillRandomCheckEvent(player, subSkillType, activationChance);
return !event.isCancelled();
}
/*public static boolean treasureDropSuccessful(Player player, double dropChance, int activationChance) {
/*public boolean treasureDropSuccessful(Player player, double dropChance, int activationChance) {
SubSkillRandomCheckEvent event = new SubSkillRandomCheckEvent(player, SubSkillType.EXCAVATION_ARCHAEOLOGY, dropChance / activationChance);
mcMMO.p.getServer().getPluginManager().callEvent(event);
return (event.getChance() * activationChance) > (Misc.getRandom().nextDouble() * activationChance) && !event.isCancelled();
}*/
public static boolean isActivationSuccessful(SkillActivationType skillActivationType, AbstractSubSkill abstractSubSkill, Player player) {
public boolean isActivationSuccessful(SkillActivationType skillActivationType, AbstractSubSkill abstractSubSkill, Player player) {
return isActivationSuccessful(skillActivationType, abstractSubSkill.getSubSkillType(), player);
}
public static String[] calculateAbilityDisplayValues(SkillActivationType skillActivationType, Player player, SubSkillType subSkillType) {
public String[] calculateAbilityDisplayValues(SkillActivationType skillActivationType, Player player, SubSkillType subSkillType) {
double successChance = getActivationChance(skillActivationType, subSkillType, player);
String[] displayValues = new String[2];
boolean isLucky = Permissions.lucky(player, subSkillType.getParentSkill());
boolean isLucky = Permissions.lucky(player, subSkillType.getParentSkill(pluginRef));
displayValues[0] = percent.format(Math.min(successChance, 100.0D) / 100.0D);
displayValues[1] = isLucky ? percent.format(Math.min(successChance * 1.3333D, 100.0D) / 100.0D) : null;
@@ -247,7 +257,7 @@ public class RandomChanceUtil {
return displayValues;
}
public static String[] calculateAbilityDisplayValuesStatic(Player player, PrimarySkillType primarySkillType, double chance) {
public String[] calculateAbilityDisplayValuesStatic(Player player, PrimarySkillType primarySkillType, double chance) {
RandomChanceStatic rcs = new RandomChanceStatic(chance, false);
double successChance = getRandomChanceExecutionChance(rcs);
@@ -264,14 +274,14 @@ public class RandomChanceUtil {
return displayValues;
}
public static String[] calculateAbilityDisplayValuesCustom(SkillActivationType skillActivationType, Player player, SubSkillType subSkillType, double multiplier) {
public String[] calculateAbilityDisplayValuesCustom(SkillActivationType skillActivationType, Player player, SubSkillType subSkillType, double multiplier) {
double successChance = getActivationChance(skillActivationType, subSkillType, player);
successChance *= multiplier; //Currently only used for graceful roll
String[] displayValues = new String[2];
//TODO: Account for lucky in this
boolean isLucky = Permissions.lucky(player, subSkillType.getParentSkill());
boolean isLucky = Permissions.lucky(player, subSkillType.getParentSkill(pluginRef));
displayValues[0] = percent.format(Math.min(successChance, 100.0D) / 100.0D);
displayValues[1] = isLucky ? percent.format(Math.min(successChance * 1.3333D, 100.0D) / 100.0D) : null;
@@ -279,14 +289,14 @@ public class RandomChanceUtil {
return displayValues;
}
public static double addLuck(Player player, PrimarySkillType primarySkillType, double chance) {
public double addLuck(Player player, PrimarySkillType primarySkillType, double chance) {
if (Permissions.lucky(player, primarySkillType))
return chance * 1.333D;
else
return chance;
}
public static double addLuck(boolean isLucky, double chance) {
public double addLuck(boolean isLucky, double chance) {
if (isLucky)
return chance * 1.333D;
else