Skip to content

Commit

Permalink
major optimizations to getting xp values for specific blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
nossr50 committed Nov 3, 2024
1 parent 03c9dfd commit ce18f3d
Show file tree
Hide file tree
Showing 28 changed files with 236 additions and 243 deletions.
1 change: 1 addition & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version 2.2.024
Significant optimizations to the way mcMMO handles grabbing XP values for blocks

Version 2.2.023
Compatibility with Minecraft 1.21.3
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/gmail/nossr50/api/ExperienceAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1154,15 +1154,19 @@ public static void addXpFromBlocks(ArrayList<BlockState> blockStates, McMMOPlaye
}

/**
* Will add the appropriate type of XP from the block to the player based on the material of the blocks given if it matches the given skillType
* Will add the appropriate type of XP from the block to the player based on the material of the blocks given
* if it matches the given skillType
* @param blockStates the blocks to reward XP for
* @param mcMMOPlayer the target player
* @param skillType target primary skill
*/
public static void addXpFromBlocksBySkill(ArrayList<BlockState> blockStates, McMMOPlayer mcMMOPlayer, PrimarySkillType skillType) {
public static void addXpFromBlocksBySkill(ArrayList<BlockState> blockStates, McMMOPlayer mcMMOPlayer,
PrimarySkillType skillType) {
for(BlockState bs : blockStates) {
if (ExperienceConfig.getInstance().getXp(skillType, bs.getType()) > 0) {
mcMMOPlayer.applyXpGain(skillType, ExperienceConfig.getInstance().getXp(skillType, bs.getType()), XPGainReason.PVE, XPGainSource.SELF);
mcMMOPlayer.applyXpGain(skillType,
ExperienceConfig.getInstance().getXp(skillType, bs.getType()),
XPGainReason.PVE, XPGainSource.SELF);
}
}
}
Expand All @@ -1175,7 +1179,8 @@ public static void addXpFromBlocksBySkill(ArrayList<BlockState> blockStates, McM
public static void addXpFromBlock(BlockState blockState, McMMOPlayer mcMMOPlayer) {
for(PrimarySkillType skillType : PrimarySkillType.values()) {
if (ExperienceConfig.getInstance().getXp(skillType, blockState.getType()) > 0) {
mcMMOPlayer.applyXpGain(skillType, ExperienceConfig.getInstance().getXp(skillType, blockState.getType()), XPGainReason.PVE, XPGainSource.SELF);
mcMMOPlayer.applyXpGain(skillType, ExperienceConfig.getInstance().getXp(
skillType, blockState.getType()), XPGainReason.PVE, XPGainSource.SELF);
}
}
}
Expand All @@ -1188,7 +1193,8 @@ public static void addXpFromBlock(BlockState blockState, McMMOPlayer mcMMOPlayer
*/
public static void addXpFromBlockBySkill(BlockState blockState, McMMOPlayer mcMMOPlayer, PrimarySkillType skillType) {
if (ExperienceConfig.getInstance().getXp(skillType, blockState.getType()) > 0) {
mcMMOPlayer.applyXpGain(skillType, ExperienceConfig.getInstance().getXp(skillType, blockState.getType()), XPGainReason.PVE, XPGainSource.SELF);
mcMMOPlayer.applyXpGain(skillType, ExperienceConfig.getInstance().getXp(skillType, blockState.getType()),
XPGainReason.PVE, XPGainSource.SELF);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/gmail/nossr50/chat/ChatManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.text.StringUtils;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.chat.SignedMessage;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.command.ConsoleCommandSender;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.gmail.nossr50.util.skills.RankUtils;
import com.gmail.nossr50.util.text.TextComponentFactory;
import net.kyori.adventure.text.Component;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.gmail.nossr50.util.skills.SkillUtils;
import com.gmail.nossr50.util.text.TextComponentFactory;
import net.kyori.adventure.text.Component;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.gmail.nossr50.util.skills.SkillUtils;
import com.gmail.nossr50.util.text.TextComponentFactory;
import net.kyori.adventure.text.Component;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import java.util.ArrayList;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/gmail/nossr50/config/GeneralConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public boolean getDoubleDropsEnabled(PrimarySkillType skill, Material material)
if (material.toString().equalsIgnoreCase("LILY_PAD"))
return false;

return config.getBoolean("Bonus_Drops." + StringUtils.getCapitalized(skill.toString()) + "." + StringUtils.getPrettyItemString(material).replace(" ", "_"));
return config.getBoolean("Bonus_Drops." + StringUtils.getCapitalized(skill.toString()) + "." + StringUtils.getFormattedMaterialString(material).replace(" ", "_"));
}

public boolean getDoubleDropsDisabled(PrimarySkillType skill) {
Expand Down Expand Up @@ -890,8 +890,9 @@ public int getTamingCOTWMaxAmount(String cotwEntity) {
}

/* Woodcutting */
public boolean getWoodcuttingDoubleDropsEnabled(BlockData material) {
return config.getBoolean("Bonus_Drops.Woodcutting." + StringUtils.getFriendlyConfigBlockDataString(material));
public boolean getWoodcuttingDoubleDropsEnabled(BlockData blockData) {
return config.getBoolean("Bonus_Drops.Woodcutting."
+ StringUtils.getFormattedMaterialString(blockData.getMaterial()));
}

public boolean getTreeFellerSoundsEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
import org.bukkit.entity.EntityType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.gmail.nossr50.util.skills.SkillTools.isChildSkill;

public class ExperienceConfig extends BukkitConfig {
private static ExperienceConfig instance;
final private Map<PrimarySkillType, Map<Material, Integer>> blockExperienceMap = new HashMap<>();

private ExperienceConfig() {
super("experience.yml");
Expand All @@ -30,6 +35,23 @@ public static ExperienceConfig getInstance() {
instance = new ExperienceConfig();
}

for (PrimarySkillType skill : PrimarySkillType.values()) {
// Skip child skills
if (isChildSkill(skill)) {
continue;
}

final Map<Material, Integer> experienceMap = new HashMap<>();
for (Material material : Material.values()) {
int xp = instance.getConfigXp(skill, material);

if (xp > 0) {
experienceMap.put(material, xp);
}
}
instance.blockExperienceMap.put(skill, experienceMap);
}

return instance;
}

Expand Down Expand Up @@ -321,15 +343,22 @@ public boolean hasCombatXP(EntityType entity) {
}

/* Materials */
private int getConfigXp(PrimarySkillType skill, Material material) {
// prevents exploit
if (material == Material.LILY_PAD)
return 0;

final String baseString = "Experience_Values." + StringUtils.getCapitalized(skill.toString()) + ".";
final String configPath = baseString + StringUtils.getFormattedMaterialString(material);
return config.getInt(configPath, 0);
}

public int getXp(PrimarySkillType skill, Material material) {
return getXpHelper(skill, StringUtils.getExplicitConfigMaterialString(material),
StringUtils.getFriendlyConfigMaterialString(material),
StringUtils.getWildcardConfigMaterialString(material));
return blockExperienceMap.get(skill).getOrDefault(material, 0);
}

public int getXp(PrimarySkillType skill, BlockState blockState) {
Material material = blockState.getType();
return getXp(skill, material);
return getXp(skill, blockState.getType());
}

public int getXp(PrimarySkillType skill, Block block) {
Expand All @@ -338,57 +367,17 @@ public int getXp(PrimarySkillType skill, Block block) {
}

public int getXp(PrimarySkillType skill, BlockData data) {
return getXpHelper(skill, StringUtils.getExplicitConfigBlockDataString(data),
StringUtils.getFriendlyConfigBlockDataString(data),
StringUtils.getWildcardConfigBlockDataString(data));
return getXp(skill, data.getMaterial());
}

private int getXpHelper(PrimarySkillType skill, String explicitString, String friendlyString, String wildcardString) {
if (explicitString.equalsIgnoreCase("LILY_PAD")) {
return 0;
}

String baseString = "Experience_Values." + StringUtils.getCapitalized(skill.toString()) + ".";
String[] configStrings = {explicitString, friendlyString, wildcardString};

for (String configString : configStrings) {
String fullPath = baseString + configString;
if (config.contains(fullPath)) {
return config.getInt(fullPath);
}
}

return 0;
}


public boolean doesBlockGiveSkillXP(PrimarySkillType skill, Material material) {
return doesBlockGiveSkillXPHelper(skill, StringUtils.getExplicitConfigMaterialString(material),
StringUtils.getFriendlyConfigMaterialString(material),
StringUtils.getWildcardConfigMaterialString(material));
return getXp(skill, material) > 0;
}

public boolean doesBlockGiveSkillXP(PrimarySkillType skill, BlockData data) {
return doesBlockGiveSkillXPHelper(skill, StringUtils.getExplicitConfigBlockDataString(data),
StringUtils.getFriendlyConfigBlockDataString(data),
StringUtils.getWildcardConfigBlockDataString(data));
return getXp(skill, data) > 0;
}

private boolean doesBlockGiveSkillXPHelper(PrimarySkillType skill, String explicitString, String friendlyString, String wildcardString) {
String baseString = "Experience_Values." + StringUtils.getCapitalized(skill.toString()) + ".";
String[] configStrings = {explicitString, friendlyString, wildcardString};

for (String configString : configStrings) {
String fullPath = baseString + configString;
if (config.contains(fullPath)) {
return true;
}
}

return false;
}


/*
* Experience Bar Stuff
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static ItemWeightConfig getInstance() {
}

public int getItemWeight(Material material) {
return config.getInt("Item_Weights." + StringUtils.getPrettyItemString(material).replace(" ", "_"), config.getInt("Item_Weights.Default"));
return config.getInt("Item_Weights." + StringUtils.getFormattedMaterialString(material).replace(" ", "_"), config.getInt("Item_Weights.Default"));
}

public HashSet<Material> getMiscItems() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.gmail.nossr50.util.ItemUtils.setItemName;
import static com.gmail.nossr50.util.PotionUtil.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.gmail.nossr50.skills.salvage.salvageables.SalvageableFactory;
import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.LogUtils;
import com.gmail.nossr50.util.skills.SkillUtils;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/gmail/nossr50/config/treasure/TreasureConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,29 +239,29 @@ private void loadTreasures(String type) {

for (String dropper : dropList) {
if (dropper.equals("Bushes")) {
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(Material.FERN), hylianTreasure);
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(BlockUtils.getShortGrass()), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(Material.FERN), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(BlockUtils.getShortGrass()), hylianTreasure);
for (Material species : Tag.SAPLINGS.getValues())
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(species), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(species), hylianTreasure);

AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(Material.DEAD_BUSH), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(Material.DEAD_BUSH), hylianTreasure);
continue;
}
if (dropper.equals("Flowers")) {
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(Material.POPPY), hylianTreasure);
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(Material.DANDELION), hylianTreasure);
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(Material.BLUE_ORCHID), hylianTreasure);
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(Material.ALLIUM), hylianTreasure);
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(Material.AZURE_BLUET), hylianTreasure);
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(Material.ORANGE_TULIP), hylianTreasure);
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(Material.PINK_TULIP), hylianTreasure);
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(Material.RED_TULIP), hylianTreasure);
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(Material.WHITE_TULIP), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(Material.POPPY), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(Material.DANDELION), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(Material.BLUE_ORCHID), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(Material.ALLIUM), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(Material.AZURE_BLUET), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(Material.ORANGE_TULIP), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(Material.PINK_TULIP), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(Material.RED_TULIP), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(Material.WHITE_TULIP), hylianTreasure);
continue;
}
if (dropper.equals("Pots")) {
for (Material species : Tag.FLOWER_POTS.getValues())
AddHylianTreasure(StringUtils.getFriendlyConfigMaterialString(species), hylianTreasure);
AddHylianTreasure(StringUtils.getFormattedMaterialString(species), hylianTreasure);
continue;
}
AddHylianTreasure(dropper, hylianTreasure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.gmail.nossr50.api.exceptions.InvalidSkillException;
import com.gmail.nossr50.chat.author.PlayerAuthor;
import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.ChatConfig;
import com.gmail.nossr50.config.WorldBlacklist;
import com.gmail.nossr50.config.experience.ExperienceConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public String getAbilityRefresh() {
}

public String getName() {
return StringUtils.getPrettyAbilityString(this);
return StringUtils.getPrettySuperAbilityString(this);
}

public String getLocalizedName() {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/gmail/nossr50/listeners/EntityListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.*;
Expand All @@ -45,8 +44,6 @@
import org.bukkit.potion.PotionEffectType;
import org.bukkit.projectiles.ProjectileSource;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import static com.gmail.nossr50.util.AttributeMapper.MAPPED_MAX_HEALTH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String getIngredientList() {
StringBuilder list = new StringBuilder();

for (ItemStack ingredient : getIngredients()) {
String string = StringUtils.getPrettyItemString(ingredient.getType());
String string = StringUtils.getFormattedMaterialString(ingredient.getType());

list.append(", ").append(string);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.gmail.nossr50.util.skills.CombatUtils;
import com.gmail.nossr50.util.skills.ParticleEffectUtils;
import com.gmail.nossr50.util.skills.RankUtils;
import com.gmail.nossr50.util.skills.SkillUtils;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Excavation {
* @return the list of treasures that could be found
*/
protected static List<ExcavationTreasure> getTreasures(BlockState blockState) {
String friendly = StringUtils.getFriendlyConfigBlockDataString(blockState.getBlockData());
String friendly = StringUtils.getFormattedMaterialString(blockState.getBlockData().getMaterial());
if (TreasureConfig.getInstance().excavationMap.containsKey(friendly))
return TreasureConfig.getInstance().excavationMap.get(friendly);
return new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public void processBerryBushHarvesting(@NotNull BlockState blockState) {
mmoPlayer.getPlayer().sendMessage("Bush Reward Multiplier: " + rewardByAge);
}

int xpReward = ExperienceConfig.getInstance().getXp(PrimarySkillType.HERBALISM, blockState) * rewardByAge;
int xpReward = ExperienceConfig.getInstance().getXp(PrimarySkillType.HERBALISM, blockState.getType())
* rewardByAge;

if (mmoPlayer.isDebugMode()) {
mmoPlayer.getPlayer().sendMessage("Bush XP: " + xpReward);
Expand Down Expand Up @@ -713,12 +714,11 @@ public boolean processHylianLuck(BlockState blockState) {
return false;
}

String friendly = StringUtils.getFriendlyConfigBlockDataString(blockState.getBlockData());
final String friendly = StringUtils.getFormattedMaterialString(blockState.getBlockData().getMaterial());
if (!TreasureConfig.getInstance().hylianMap.containsKey(friendly))
return false;
List<HylianTreasure> treasures = TreasureConfig.getInstance().hylianMap.get(friendly);


if (treasures.isEmpty()) {
return false;
}
Expand Down Expand Up @@ -750,12 +750,12 @@ public boolean processShroomThumb(BlockState blockState) {
PlayerInventory playerInventory = getPlayer().getInventory();

if (!playerInventory.contains(Material.BROWN_MUSHROOM, 1)) {
NotificationManager.sendPlayerInformation(getPlayer(), NotificationType.REQUIREMENTS_NOT_MET, "Skills.NeedMore", StringUtils.getPrettyItemString(Material.BROWN_MUSHROOM));
NotificationManager.sendPlayerInformation(getPlayer(), NotificationType.REQUIREMENTS_NOT_MET, "Skills.NeedMore", StringUtils.getFormattedMaterialString(Material.BROWN_MUSHROOM));
return false;
}

if (!playerInventory.contains(Material.RED_MUSHROOM, 1)) {
NotificationManager.sendPlayerInformation(getPlayer(), NotificationType.REQUIREMENTS_NOT_MET, "Skills.NeedMore", StringUtils.getPrettyItemString(Material.RED_MUSHROOM));
NotificationManager.sendPlayerInformation(getPlayer(), NotificationType.REQUIREMENTS_NOT_MET, "Skills.NeedMore", StringUtils.getFormattedMaterialString(Material.RED_MUSHROOM));
return false;
}

Expand Down
Loading

0 comments on commit ce18f3d

Please sign in to comment.