2019-01-13 08:54:53 +01:00
package com.gmail.nossr50.config ;
2019-01-15 11:43:44 +01:00
import com.gmail.nossr50.datatypes.skills.SubSkillType ;
import com.gmail.nossr50.datatypes.skills.subskills.AbstractSubSkill ;
2020-11-10 21:05:42 +01:00
import org.jetbrains.annotations.NotNull ;
2019-01-15 11:43:44 +01:00
import java.util.ArrayList ;
2020-11-10 21:05:42 +01:00
import java.util.HashSet ;
2019-01-15 11:43:44 +01:00
import java.util.List ;
2019-01-13 08:54:53 +01:00
public class RankConfig extends AutoUpdateConfigLoader {
private static RankConfig instance ;
public RankConfig ( )
{
super ( " skillranks.yml " ) ;
validate ( ) ;
2020-07-13 20:39:03 +02:00
instance = this ;
2019-01-13 08:54:53 +01:00
}
@Override
protected void loadKeys ( ) {
}
public static RankConfig getInstance ( )
{
if ( instance = = null )
return new RankConfig ( ) ;
return instance ;
}
2019-01-15 11:43:44 +01:00
@Override
protected boolean validateKeys ( ) {
2020-07-13 21:31:30 +02:00
List < String > reason = new ArrayList < > ( ) ;
2019-01-15 11:43:44 +01:00
/ *
* In the future this method will check keys for all skills , but for now it only checks overhauled skills
* /
checkKeys ( reason ) ;
return noErrorsInConfig ( reason ) ;
}
/ * *
* Returns the unlock level for a subskill depending on the gamemode
* @param subSkillType target subskill
* @param rank the rank we are checking
* @return the level requirement for a subskill at this particular rank
* /
public int getSubSkillUnlockLevel ( SubSkillType subSkillType , int rank )
{
String key = subSkillType . getRankConfigAddress ( ) ;
return findRankByRootAddress ( rank , key ) ;
}
2020-11-10 21:05:42 +01:00
/ * *
* Returns the unlock level for a subskill depending on the gamemode
* @param subSkillType target subskill
* @param rank the rank we are checking
* @return the level requirement for a subskill at this particular rank
* /
public int getSubSkillUnlockLevel ( SubSkillType subSkillType , int rank , boolean retroMode )
{
String key = getRankAddressKey ( subSkillType , rank , retroMode ) ;
return config . getInt ( key , getInternalConfig ( ) . getInt ( key ) ) ;
}
2019-01-15 11:43:44 +01:00
/ * *
* Returns the unlock level for a subskill depending on the gamemode
* @param abstractSubSkill target subskill
* @param rank the rank we are checking
* @return the level requirement for a subskill at this particular rank
* /
public int getSubSkillUnlockLevel ( AbstractSubSkill abstractSubSkill , int rank )
{
String key = abstractSubSkill . getPrimaryKeyName ( ) + " . " + abstractSubSkill . getConfigKeyName ( ) ;
return findRankByRootAddress ( rank , key ) ;
}
/ * *
* Returns the unlock level for a subskill depending on the gamemode
* @param key root address of the subskill in the rankskills . yml file
* @param rank the rank we are checking
* @return the level requirement for a subskill at this particular rank
* /
private int findRankByRootAddress ( int rank , String key ) {
String scalingKey = Config . getInstance ( ) . getIsRetroMode ( ) ? " .RetroMode. " : " .Standard. " ;
String targetRank = " Rank_ " + rank ;
key + = scalingKey ;
key + = targetRank ;
return config . getInt ( key ) ;
}
2020-11-10 21:05:42 +01:00
public String getRankAddressKey ( SubSkillType subSkillType , int rank , boolean retroMode ) {
String key = subSkillType . getRankConfigAddress ( ) ;
String scalingKey = retroMode ? " .RetroMode. " : " .Standard. " ;
String targetRank = " Rank_ " + rank ;
key + = scalingKey ;
key + = targetRank ;
return key ;
}
public String getRankAddressKey ( AbstractSubSkill subSkillType , int rank , boolean retroMode ) {
String key = subSkillType . getPrimaryKeyName ( ) + " . " + subSkillType . getConfigKeyName ( ) ;
String scalingKey = retroMode ? " .RetroMode. " : " .Standard. " ;
String targetRank = " Rank_ " + rank ;
key + = scalingKey ;
key + = targetRank ;
return key ;
}
private void resetRankValue ( @NotNull SubSkillType subSkillType , int rank , boolean retroMode ) {
String key = getRankAddressKey ( subSkillType , rank , retroMode ) ;
int defaultValue = getInternalConfig ( ) . getInt ( key ) ;
config . set ( key , defaultValue ) ;
2020-11-10 21:19:23 +01:00
plugin . getLogger ( ) . info ( key + " SET -> " + defaultValue ) ;
2020-11-10 21:05:42 +01:00
}
2019-01-15 11:43:44 +01:00
/ * *
* Checks for valid keys for subskill ranks
* /
2020-11-10 21:05:42 +01:00
private void checkKeys ( @NotNull List < String > reasons )
2019-01-15 11:43:44 +01:00
{
2020-11-10 21:05:42 +01:00
HashSet < SubSkillType > badSkillSetup = new HashSet < > ( ) ;
2019-01-15 11:43:44 +01:00
//For now we will only check ranks of stuff I've overhauled
2020-11-10 21:05:42 +01:00
checkConfig ( reasons , badSkillSetup , true ) ;
checkConfig ( reasons , badSkillSetup , false ) ;
//Fix bad entries
if ( badSkillSetup . isEmpty ( ) )
return ;
plugin . getLogger ( ) . info ( " (FIXING CONFIG) mcMMO is correcting a few mistakes found in your skill rank config setup " ) ;
for ( SubSkillType subSkillType : badSkillSetup ) {
plugin . getLogger ( ) . info ( " (FIXING CONFIG) Resetting rank config settings for skill named - " + subSkillType . toString ( ) ) ;
fixBadEntries ( subSkillType ) ;
}
}
private void checkConfig ( @NotNull List < String > reasons , @NotNull HashSet < SubSkillType > badSkillSetup , boolean retroMode ) {
2019-01-15 11:43:44 +01:00
for ( SubSkillType subSkillType : SubSkillType . values ( ) )
{
//Keeping track of the rank requirements and making sure there are no logical errors
int curRank = 0 ;
int prevRank = 0 ;
for ( int x = 0 ; x < subSkillType . getNumRanks ( ) ; x + + )
{
2020-11-10 21:05:42 +01:00
int index = x + 1 ;
2019-01-15 11:43:44 +01:00
if ( curRank > 0 )
prevRank = curRank ;
2020-11-10 21:05:42 +01:00
curRank = getSubSkillUnlockLevel ( subSkillType , index , retroMode ) ;
2019-01-15 11:43:44 +01:00
//Do we really care if its below 0? Probably not
if ( curRank < 0 )
{
2020-11-10 21:05:42 +01:00
reasons . add ( " (CONFIG ISSUE) " + subSkillType . toString ( ) + " should not have any ranks that require a negative level! " ) ;
badSkillSetup . add ( subSkillType ) ;
continue ;
2019-01-15 11:43:44 +01:00
}
if ( prevRank > curRank )
{
//We're going to allow this but we're going to warn them
2020-11-10 21:05:42 +01:00
plugin . getLogger ( ) . info ( " (CONFIG ISSUE) You have the ranks for the subskill " + subSkillType . toString ( ) + " set up poorly, sequential ranks should have ascending requirements " ) ;
badSkillSetup . add ( subSkillType ) ;
2019-01-15 11:43:44 +01:00
}
}
}
}
2020-11-10 21:05:42 +01:00
private void fixBadEntries ( @NotNull SubSkillType subSkillType ) {
for ( int x = 0 ; x < subSkillType . getNumRanks ( ) ; x + + )
{
int index = x + 1 ;
//Reset Retromode entries
resetRankValue ( subSkillType , index , true ) ;
//Reset Standard Entries
resetRankValue ( subSkillType , index , false ) ;
}
saveConfig ( ) ;
}
2019-01-13 08:54:53 +01:00
}