From 5208aeaf76812de3ff50595d4c4a76e5b5cd65b6 Mon Sep 17 00:00:00 2001 From: Caltinor <62700786+Caltinor@users.noreply.github.com> Date: Mon, 4 Mar 2024 17:04:11 -0500 Subject: [PATCH] Update APIUtils to parse skill groups --- CHANGELOG.md | 6 +- .../java/harmonised/pmmo/api/APIUtils.java | 98 ++++++++++--------- 2 files changed, 54 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ebc19f5..e28aecbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 \ No newline at end of file +- Fixed APIUtils#addXp not parsing skillgroups into component skills \ No newline at end of file diff --git a/src/main/java/harmonised/pmmo/api/APIUtils.java b/src/main/java/harmonised/pmmo/api/APIUtils.java index ecd6f658..b1516557 100644 --- a/src/main/java/harmonised/pmmo/api/APIUtils.java +++ b/src/main/java/harmonised/pmmo/api/APIUtils.java @@ -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; @@ -44,6 +45,7 @@ import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; +@SuppressWarnings("unused") public class APIUtils { /* NOTES * @@ -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 */ @@ -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 */ @@ -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. @@ -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. */ @@ -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 */ @@ -117,7 +119,9 @@ 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. @@ -125,7 +129,9 @@ public static void setXp(String skill, Player player, long xpRaw) { 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 @@ -211,7 +217,7 @@ public static Map getXpAwardMap(Level level, BlockPos pos, EventTy public static Map 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()); } @@ -258,7 +264,7 @@ public static Map 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 @@ -275,7 +281,7 @@ public static Map 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 @@ -320,11 +326,11 @@ public static Map 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 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); } @@ -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 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); } @@ -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 @@ -372,17 +378,17 @@ 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 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 @@ -390,11 +396,11 @@ public static void registerBonus(ObjectType oType, ResourceLocation objectID, Mo * @param asOverride should this apply after datapacks as an override */ public static void registerNegativeEffect(ObjectType oType, ResourceLocation objectID, Map 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); } @@ -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 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); } @@ -427,7 +433,7 @@ public static void registerPositiveEffect(ObjectType oType, ResourceLocation obj */ public static void registerSalvage(ResourceLocation item, Map 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 @@ -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. *

default = 0.0

* @param chance chance before skill based chances are added @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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 @@ -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 @@ -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 @@ -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 onExecute + * and onConclude, 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. *