Skip to content

Commit

Permalink
Update APIUtils to parse skill groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Caltinor committed Mar 4, 2024
1 parent ccb2b70 commit 5208aea
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 50 deletions.
6 changes: 2 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Changelog

## [1.20.1-1.3.19] - 2024-03-02
## [1.20.1-1.3.20] - 2024-03-04
### Bugfixes / Tweaks
- Fixed treasure config toggle disabling conditions instead of loot
- Fixed Tetra configs using REPLACE instead of HIGHER.
- Added per_level option to treasure and rare drop configurations
- Fixed APIUtils#addXp not parsing skillgroups into component skills
98 changes: 52 additions & 46 deletions src/main/java/harmonised/pmmo/api/APIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import javax.annotation.Nullable;

import harmonised.pmmo.core.CoreUtils;
import harmonised.pmmo.core.IDataStorage;
import org.checkerframework.checker.nullness.qual.NonNull;

Expand Down Expand Up @@ -44,6 +45,7 @@
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;

@SuppressWarnings("unused")
public class APIUtils {
/* NOTES
*
Expand All @@ -53,7 +55,7 @@ public class APIUtils {
//===============CORE HOOKS======================================
/**get the player's current level in the skill provided
*
* @param skill skill name. Skills are case sensitive and usually all lowercase
* @param skill skill name. Skills are case-sensitive and usually all lowercase
* @param player the player whose skills are being obtained.
* @return the current skill level of the player
*/
Expand All @@ -65,7 +67,7 @@ public static int getLevel(String skill, Player player) {

/**Sets the player's current level in the skill provided
*
* @param skill skill's name. skills are case sensitive and usually all lowercase
* @param skill skill's name. skills are case-sensitive and usually all lowercase
* @param player the player whose skills are being set
* @param level the new level being applied to the skill
*/
Expand All @@ -79,7 +81,7 @@ public static void setLevel(String skill, Player player, int level) {
* providing a negative value in @{link levelChange} will reduce the player's
* level.
*
* @param skill skill name. Skills are case sensitive and usually lowercase.
* @param skill skill name. Skills are case-sensitive and usually lowercase.
* @param player the player whose level is being changed
* @param levelChange the number of levels being changed by. negative values will reduce the player level.
* @return true if the level was in fact changed.
Expand All @@ -92,7 +94,7 @@ public static boolean addLevel(String skill, Player player, int levelChange) {

/**Gets the raw xp value associated with the specified skill and player.
*
* @param skill skill name. Skills are case sensitive and usually lowercase.
* @param skill skill name. Skills are case-sensitive and usually lowercase.
* @param player the player whose experience is being sought.
* @return the raw experience earned in the specified skill.
*/
Expand All @@ -104,7 +106,7 @@ public static long getXp(String skill, Player player) {

/**Sets the raw XP value for the player in the skill specified.
*
* @param skill skill name. Skills are case sensitive and usually lowercase.
* @param skill skill name. Skills are case-sensitive and usually lowercase.
* @param player the player whose skill is being set.
* @param xpRaw the new experience amount to be set for this skill
*/
Expand All @@ -117,15 +119,19 @@ public static void setXp(String skill, Player player, long xpRaw) {
/**Changes the player's current experience in the specified skill by the amount.
* Negative values will reduce current experience.
*
* @param skill skill name. Skills are case sensitive and usually lowercase.
* @param skill skill name. Skills are case-sensitive and usually lowercase. if a
* skill group is passed into this method, it will be parsed according
* to its component skills and the xp distributed accordingly.
* @param player the player whose experience is being changed.
* @param change the amount being changed by. Negative values reduce experience.
* @return true if the modification was successful.
*/
public static boolean addXp(String skill, Player player, long change) {
Preconditions.checkNotNull(skill);
Preconditions.checkNotNull(player);
return Core.get(player.level()).getData().setXpDiff(player.getUUID(), skill, change);
IDataStorage data = Core.get(player.level()).getData();
return CoreUtils.processSkillGroupXP(Map.of(skill, change)).entrySet().stream()
.allMatch(entry -> data.setXpDiff(player.getUUID(), entry.getKey(), entry.getValue()));
}

/**Supplies the player's entire skill map with raw xp
Expand Down Expand Up @@ -211,7 +217,7 @@ public static Map<String, Long> getXpAwardMap(Level level, BlockPos pos, EventTy
public static Map<String, Long> getXpAwardMap(Entity entity, EventType type, LogicalSide side, @Nullable Player player) {
Preconditions.checkNotNull(entity);
Preconditions.checkNotNull(type);
Preconditions.checkNotNull(side);;
Preconditions.checkNotNull(side);
return Core.get(side).getExperienceAwards(type, entity, player, new CompoundTag());
}

Expand Down Expand Up @@ -258,7 +264,7 @@ public static Map<String, Integer> getRequirementMap(ItemStack item, ReqType typ

/**Returns a skill-level map for the requirements of the block and the requirement type passed.
* Note that registered block predicates do not have to conform to the level system in determining
* whether an block action is permitted or not. Because of this, a missing or inaccurate tooltip
* whether a block action is permitted or not. Because of this, a missing or inaccurate tooltip
* registration may not reflect the outcome of a predicate check during gameplay.
*
* @param pos the location of the block or block entity being queried
Expand All @@ -275,7 +281,7 @@ public static Map<String, Integer> getRequirementMap(BlockPos pos, Level level,

/**Returns a skill-level map for the requirements of the entity and the requirement type passed.
* Note that registered entity predicates do not have to conform to the level system in determining
* whether an block action is permitted or not. Because of this, a missing or inaccurate tooltip
* whether a block action is permitted or not. Because of this, a missing or inaccurate tooltip
* registration may not reflect the outcome of a predicate check during gameplay.
*
* @param entity the entity being queried
Expand Down Expand Up @@ -320,11 +326,11 @@ public static Map<String, Integer> getRequirementMap(ObjectType oType, ResourceL
* @param asOverride should this apply after datapacks as an override
*/
public static void registerRequirement(ObjectType oType, ResourceLocation objectID, ReqType type, Map<String, Integer> requirements, boolean asOverride) {
DataSource<?> raw = null;
DataSource<?> raw;
switch (oType) {
case BIOME, DIMENSION -> {raw = new LocationData();}
case ITEM, BLOCK, ENTITY -> {raw = new ObjectData();}
default -> {}}
case BIOME, DIMENSION -> raw = new LocationData();
case ITEM, BLOCK, ENTITY -> raw = new ObjectData();
default -> {return;}}
raw.setReqs(type, requirements);
registerConfiguration(asOverride, oType, objectID, raw);
}
Expand All @@ -337,11 +343,11 @@ public static void registerRequirement(ObjectType oType, ResourceLocation object
* @param asOverride should this apply after datapacks as an override
*/
public static void registerXpAward(ObjectType oType, ResourceLocation objectID, EventType type, Map<String, Long> award, boolean asOverride) {
DataSource<?> raw = null;
DataSource<?> raw;
switch (oType) {
case BIOME, DIMENSION -> {raw = new LocationData();}
case ITEM, BLOCK, ENTITY -> {raw = new ObjectData();}
default -> {}}
case BIOME, DIMENSION -> raw = new LocationData();
case ITEM, BLOCK, ENTITY -> raw = new ObjectData();
default -> {return;}}
raw.setXpValues(type, award);
registerConfiguration(asOverride, oType, objectID, raw);
}
Expand All @@ -352,7 +358,7 @@ public static void registerXpAward(ObjectType oType, ResourceLocation objectID,
*
* @param oType use only ITEM or ENTITY
* @param objectID the key for the object being configured
* @param isDealt is Dealt Daamge config else if false will be received damage
* @param isDealt is Dealt Damage config else if false will be received damage
* @param damageType the id or tag string for damage type
* @param award a map of skills and experience values to be awarded
* @param asOverride should this apply after datapacks as an override
Expand All @@ -372,29 +378,29 @@ public static void registerDamageXpAward(ObjectType oType, ResourceLocation obje
* @param asOverride should this apply after datapacks as an override
*/
public static void registerBonus(ObjectType oType, ResourceLocation objectID, ModifierDataType type, Map<String, Double> bonus, boolean asOverride) {
DataSource<?> raw = null;
DataSource<?> raw;
switch (oType) {
case BIOME, DIMENSION -> {raw = new LocationData();}
case ITEM -> {raw = new ObjectData();}
case PLAYER -> {raw = new PlayerData();}
default -> {}}
case BIOME, DIMENSION -> raw = new LocationData();
case ITEM -> raw = new ObjectData();
case PLAYER -> raw = new PlayerData();
default -> {return;}}
raw.setBonuses(type, bonus);
registerConfiguration(asOverride, oType, objectID, raw);
}
/**registers a configuration setting for what status effects should be applied to the player
* if they attempt to wear/hold/travel and they are not skilled enough to do so.
* if they attempt to wear/hold/travel, and they are not skilled enough to do so.
*
* @param oType the object type this effect is being stored on
* @param objectID the key for the item being configured
* @param effects a map of effect ids and levels
* @param asOverride should this apply after datapacks as an override
*/
public static void registerNegativeEffect(ObjectType oType, ResourceLocation objectID, Map<ResourceLocation, Integer> effects, boolean asOverride) {
DataSource<?> raw = null;
DataSource<?> raw;
switch (oType) {
case BIOME, DIMENSION -> {raw = new LocationData();}
case ITEM -> {raw = new ObjectData();}
default -> {}}
case BIOME, DIMENSION -> raw = new LocationData();
case ITEM -> raw = new ObjectData();
default -> {return;}}
raw.setNegativeEffects(effects);
registerConfiguration(asOverride, oType, objectID, raw);
}
Expand All @@ -408,11 +414,11 @@ public static void registerNegativeEffect(ObjectType oType, ResourceLocation obj
* @param asOverride should this apply after datapacks as an override
*/
public static void registerPositiveEffect(ObjectType oType, ResourceLocation objectID, Map<ResourceLocation, Integer> effects, boolean asOverride) {
DataSource<?> raw = null;
DataSource<?> raw;
switch (oType) {
case BIOME, DIMENSION -> {raw = new LocationData();}
case ITEM -> {raw = new ObjectData();}
default -> {}}
case BIOME, DIMENSION -> raw = new LocationData();
case ITEM -> raw = new ObjectData();
default -> {return;}}
raw.setPositiveEffects(effects);
registerConfiguration(asOverride, oType, objectID, raw);
}
Expand All @@ -427,7 +433,7 @@ public static void registerPositiveEffect(ObjectType oType, ResourceLocation obj
*/
public static void registerSalvage(ResourceLocation item, Map<ResourceLocation, SalvageBuilder> salvage, boolean asOverride) {
ObjectData raw = new ObjectData();
raw.salvage().putAll(salvage.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue().build())));
raw.salvage().putAll(salvage.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().build())));
registerConfiguration(asOverride, ObjectType.ITEM, item, raw);
}
/**registers vein information for the specified block or item. Items
Expand Down Expand Up @@ -537,7 +543,7 @@ public SalvageBuilder setSalvageMax(int max) {
this.salvageMax = max;
return this;
}
/**The default chance irregardless of skills that this
/**The default chance regardless of skills that this
* salvage will be obtained.
* <p>default = 0.0</p>
* @param chance chance before skill based chances are added
Expand Down Expand Up @@ -567,7 +573,7 @@ public SalvageData build() {
//===============REQ AND TOOLTIP REFERENCES======================
/** registers a predicate to be used in determining if a given player is permitted
* to perform a particular action. [Except for break action. see {@link APIUtils#registerBreakPredicate registerBreakPredicate}.
* The ResouceLocation and ReqType parameters are
* The ResourceLocation and ReqType parameters are
* conditions for when this check should be applied and are used by PMMO to know
* which predicates apply in which contexts.
*
Expand All @@ -580,7 +586,7 @@ public static void registerActionPredicate(ResourceLocation res, ReqType reqType
}

/** registers a predicate to be used in determining if a given player is permitted
* to break a block. The ResouceLocation and ReqType parameters are
* to break a block. The ResourceLocation and ReqType parameters are
* conditions for when this check should be applied and are used by PMMO to know
* which predicates apply in which contexts.
*
Expand All @@ -594,7 +600,7 @@ public static void registerBreakPredicate(ResourceLocation res, ReqType reqType,

/** registers a predicate to be used in determining if a given player is permitted
* to perform a particular action related to an entity.
* The ResouceLocation and ReqType parameters are
* The ResourceLocation and ReqType parameters are
* conditions for when this check should be applied and are used by PMMO to know
* which predicates apply in which contexts.
*
Expand All @@ -608,7 +614,7 @@ public static void registerEntityPredicate(ResourceLocation res, ReqType reqType

/**registers a Function to be used in providing the requirements for specific item
* skill requirements. The map consists of skill name and skill value pairs.
* The ResouceLocation and ReqType parameters are conditions for when this check
* The ResourceLocation and ReqType parameters are conditions for when this check
* should be applied and are used by PMMO to know which functions apply in which
* contexts.
*
Expand All @@ -622,7 +628,7 @@ public static void registerItemRequirementTooltipData(ResourceLocation res, ReqT

/**registers a Function to be used in providing the requirements for specific block
* skill requirements. The map consists of skill name and skill value pairs.
* The ResouceLocation and ReqType parameters are conditions for when this check
* The ResourceLocation and ReqType parameters are conditions for when this check
* should be applied and are used by PMMO to know which functions apply in which
* contexts.
*
Expand All @@ -636,7 +642,7 @@ public static void registerBlockRequirementTooltipData(ResourceLocation res, Req

/**registers a Function to be used in providing the requirements for specific entity
* skill requirements. The map consists of skill name and skill value pairs.
* The ResouceLocation and ReqType parameters are conditions for when this check
* The ResourceLocation and ReqType parameters are conditions for when this check
* should be applied and are used by PMMO to know which functions apply in which
* contexts.
*
Expand All @@ -650,7 +656,7 @@ public static void registerEntityRequirementTooltipData(ResourceLocation res, Re

/**registers a Function to be used in providing the experience gains for specific item
* and event. The map consists of skill name and experience value pairs.
* The ResouceLocation and EventType parameters are conditions for when the function
* The ResourceLocation and EventType parameters are conditions for when the function
* should be applied.
*
* @param res the item registrykey
Expand All @@ -663,7 +669,7 @@ public static void registerItemXpGainTooltipData(ResourceLocation res, EventType

/**registers a Function to be used in providing the experience gains for specific block
* and event. The map consists of skill name and experience value pairs.
* The ResouceLocation and EventType parameters are conditions for when the function
* The ResourceLocation and EventType parameters are conditions for when the function
* should be applied.
*
* @param res the block registrykey
Expand All @@ -676,7 +682,7 @@ public static void registerBlockXpGainTooltipData(ResourceLocation res, EventTyp

/**registers a Function to be used in providing the experience gains for specific entity
* and event. The map consists of skill name and experience value pairs.
* The ResouceLocation and EventType parameters are conditions for when the function
* The ResourceLocation and EventType parameters are conditions for when the function
* should be applied.
*
* @param res the entity registrykey
Expand Down Expand Up @@ -816,8 +822,8 @@ public static void registerPerk(
public static final String SERIALIZED_AWARD_MAP = "serialized_award_map";

/** Both Perks and Event Triggers can be used to provide custom XP award maps
* to events. When returning the {@link net.minecraft.nbt.CompoundTag CompoundTag} in {@link onExecute}
* and {@link onConclude}, use the key {@link SERIALIZED_AWARD_MAP} and use
* to events. When returning the {@link net.minecraft.nbt.CompoundTag CompoundTag} in <code>onExecute</code>
* and <code>onConclude</code>, use the key {@link #SERIALIZED_AWARD_MAP} and use
* this method to convert your award map into a universally serializable
* object that PMMO can understand and utilize when processing rewards.
*
Expand Down

0 comments on commit 5208aea

Please sign in to comment.