diff --git a/Changelog.txt b/Changelog.txt index 8056f3361..ef4d0c5be 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -7,6 +7,9 @@ Key: ! Change - Removal +Version 1.5.01-api + + Added skill api! + Version 1.5.01-dev + Added new child skill; Salvage + Added UUID support! @@ -21,6 +24,7 @@ Version 1.5.01-dev + Added API to ExperienceAPI to get the amount of XP needed for a level + Added API class SkillAPI used to get a list of valid skill names + Added API events for hardcore features, McMMOPlayerPreDeathPenaltyEvent, McMMOPlayerStatLossEvent and McMMOPlayerVampirismEvent + + Added API to ExperienceAPI to specify if XP can be shared + Added options to tools.yml and armor.yml config files to set a pretty repair material name + Added full support for repairables in tools.yml and armor.yml config files + Added magical mod config file import command, for Cauldron 1.7+. Check wiki for usage diff --git a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java index bf0f448ab..ab4944666 100644 --- a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java +++ b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java @@ -10,9 +10,11 @@ import com.gmail.nossr50.api.exceptions.InvalidFormulaTypeException; import com.gmail.nossr50.api.exceptions.InvalidPlayerException; import com.gmail.nossr50.api.exceptions.InvalidSkillException; import com.gmail.nossr50.api.exceptions.InvalidXPGainReasonException; +import com.gmail.nossr50.api.exceptions.McMMOPlayerNotFoundException; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.experience.ExperienceConfig; import com.gmail.nossr50.datatypes.experience.FormulaType; +import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.XPGainReason; @@ -86,7 +88,30 @@ public final class ExperienceAPI { * @throws InvalidXPGainReasonException if the given xpGainReason is not valid */ public static void addRawXP(Player player, String skillType, float XP, String xpGainReason) { - UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); + addRawXP(player, skillType, XP, xpGainReason, false); + } + + /** + * Adds raw XP to the player. + *
+ * This function is designed for API usage. + * + * @param player The player to add XP to + * @param skillType The skill to add XP to + * @param XP The amount of XP to add + * @param xpGainReason The reason to gain XP + * @param isUnshared true if the XP cannot be shared with party members + * + * @throws InvalidSkillException if the given skill is not valid + * @throws InvalidXPGainReasonException if the given xpGainReason is not valid + */ + public static void addRawXP(Player player, String skillType, float XP, String xpGainReason, boolean isUnshared) { + if (isUnshared) { + getPlayer(player).beginUnsharedXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); + return; + } + + getPlayer(player).applyXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); } /** @@ -168,7 +193,7 @@ public final class ExperienceAPI { * @throws InvalidXPGainReasonException if the given xpGainReason is not valid */ public static void addMultipliedXP(Player player, String skillType, int XP, String xpGainReason) { - UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason)); + getPlayer(player).applyXpGain(getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason)); } /** @@ -218,9 +243,32 @@ public final class ExperienceAPI { * @throws InvalidXPGainReasonException if the given xpGainReason is not valid */ public static void addModifiedXP(Player player, String skillType, int XP, String xpGainReason) { + addModifiedXP(player, skillType, XP, xpGainReason, false); + } + + /** + * Adds XP to the player, calculates for XP Rate and skill modifier. + *
+ * This function is designed for API usage. + * + * @param player The player to add XP to + * @param skillType The skill to add XP to + * @param XP The amount of XP to add + * @param xpGainReason The reason to gain XP + * @param isUnshared true if the XP cannot be shared with party members + * + * @throws InvalidSkillException if the given skill is not valid + * @throws InvalidXPGainReasonException if the given xpGainReason is not valid + */ + public static void addModifiedXP(Player player, String skillType, int XP, String xpGainReason, boolean isUnshared) { SkillType skill = getSkillType(skillType); - UserManager.getPlayer(player).applyXpGain(skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason)); + if (isUnshared) { + getPlayer(player).beginUnsharedXpGain(skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason)); + return; + } + + getPlayer(player).applyXpGain(skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason)); } /** @@ -274,7 +322,31 @@ public final class ExperienceAPI { * @throws InvalidXPGainReasonException if the given xpGainReason is not valid */ public static void addXP(Player player, String skillType, int XP, String xpGainReason) { - UserManager.getPlayer(player).beginXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); + addXP(player, skillType, XP, xpGainReason, false); + } + + /** + * Adds XP to the player, calculates for XP Rate, skill modifiers, perks, child skills, + * and party sharing. + *
+ * This function is designed for API usage. + * + * @param player The player to add XP to + * @param skillType The skill to add XP to + * @param XP The amount of XP to add + * @param xpGainReason The reason to gain XP + * @param isUnshared true if the XP cannot be shared with party members + * + * @throws InvalidSkillException if the given skill is not valid + * @throws InvalidXPGainReasonException if the given xpGainReason is not valid + */ + public static void addXP(Player player, String skillType, int XP, String xpGainReason, boolean isUnshared) { + if (isUnshared) { + getPlayer(player).beginUnsharedXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); + return; + } + + getPlayer(player).beginXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); } /** @@ -290,7 +362,7 @@ public final class ExperienceAPI { * @throws UnsupportedOperationException if the given skill is a child skill */ public static int getXP(Player player, String skillType) { - return UserManager.getPlayer(player).getSkillXpLevel(getNonChildSkillType(skillType)); + return getPlayer(player).getSkillXpLevel(getNonChildSkillType(skillType)); } /** @@ -341,7 +413,7 @@ public final class ExperienceAPI { * @throws UnsupportedOperationException if the given skill is a child skill */ public static float getXPRaw(Player player, String skillType) { - return UserManager.getPlayer(player).getSkillXpLevelRaw(getNonChildSkillType(skillType)); + return getPlayer(player).getSkillXpLevelRaw(getNonChildSkillType(skillType)); } /** @@ -392,7 +464,7 @@ public final class ExperienceAPI { * @throws UnsupportedOperationException if the given skill is a child skill */ public static int getXPToNextLevel(Player player, String skillType) { - return UserManager.getPlayer(player).getXpToLevel(getNonChildSkillType(skillType)); + return getPlayer(player).getXpToLevel(getNonChildSkillType(skillType)); } /** @@ -445,7 +517,7 @@ public final class ExperienceAPI { public static int getXPRemaining(Player player, String skillType) { SkillType skill = getNonChildSkillType(skillType); - PlayerProfile profile = UserManager.getPlayer(player).getProfile(); + PlayerProfile profile = getPlayer(player).getProfile(); return profile.getXpToLevel(skill) - profile.getSkillXpLevel(skill); } @@ -503,7 +575,7 @@ public final class ExperienceAPI { * @throws InvalidSkillException if the given skill is not valid */ public static void addLevel(Player player, String skillType, int levels) { - UserManager.getPlayer(player).addLevels(getSkillType(skillType), levels); + getPlayer(player).addLevels(getSkillType(skillType), levels); } /** @@ -581,7 +653,7 @@ public final class ExperienceAPI { * @throws InvalidSkillException if the given skill is not valid */ public static int getLevel(Player player, String skillType) { - return UserManager.getPlayer(player).getSkillLevel(getSkillType(skillType)); + return getPlayer(player).getSkillLevel(getSkillType(skillType)); } /** @@ -626,7 +698,7 @@ public final class ExperienceAPI { * @return the power level of the player */ public static int getPowerLevel(Player player) { - return UserManager.getPlayer(player).getPowerLevel(); + return getPlayer(player).getPowerLevel(); } /** @@ -777,7 +849,7 @@ public final class ExperienceAPI { * @throws InvalidSkillException if the given skill is not valid */ public static void setLevel(Player player, String skillType, int skillLevel) { - UserManager.getPlayer(player).modifySkill(getSkillType(skillType), skillLevel); + getPlayer(player).modifySkill(getSkillType(skillType), skillLevel); } /** @@ -826,7 +898,7 @@ public final class ExperienceAPI { * @throws UnsupportedOperationException if the given skill is a child skill */ public static void setXP(Player player, String skillType, int newValue) { - UserManager.getPlayer(player).setSkillXpLevel(getNonChildSkillType(skillType), newValue); + getPlayer(player).setSkillXpLevel(getNonChildSkillType(skillType), newValue); } /** @@ -877,7 +949,7 @@ public final class ExperienceAPI { * @throws UnsupportedOperationException if the given skill is a child skill */ public static void removeXP(Player player, String skillType, int xp) { - UserManager.getPlayer(player).removeXp(getNonChildSkillType(skillType), xp); + getPlayer(player).removeXp(getNonChildSkillType(skillType), xp); } /** @@ -1019,4 +1091,12 @@ public final class ExperienceAPI { return formulaType; } + + private static McMMOPlayer getPlayer(Player player) throws McMMOPlayerNotFoundException { + if (!UserManager.hasPlayerDataKey(player)) { + throw new McMMOPlayerNotFoundException(player); + } + + return UserManager.getPlayer(player); + } } diff --git a/src/main/java/com/gmail/nossr50/api/SkillAPI.java b/src/main/java/com/gmail/nossr50/api/SkillAPI.java index 4ef6cffa6..b948f3115 100644 --- a/src/main/java/com/gmail/nossr50/api/SkillAPI.java +++ b/src/main/java/com/gmail/nossr50/api/SkillAPI.java @@ -16,7 +16,6 @@ import com.gmail.nossr50.datatypes.skills.ToolType; import com.gmail.nossr50.skills.SkillAbilityManager; import com.gmail.nossr50.skills.SkillManager; import com.gmail.nossr50.skills.child.ChildConfig; -import com.gmail.nossr50.skills.child.FamilyTree; import com.gmail.nossr50.util.StringUtils; import com.gmail.nossr50.util.commands.CommandRegistrationManager; import com.gmail.nossr50.util.player.UserManager; diff --git a/src/main/java/com/gmail/nossr50/api/exceptions/McMMOPlayerNotFoundException.java b/src/main/java/com/gmail/nossr50/api/exceptions/McMMOPlayerNotFoundException.java new file mode 100644 index 000000000..d087631cd --- /dev/null +++ b/src/main/java/com/gmail/nossr50/api/exceptions/McMMOPlayerNotFoundException.java @@ -0,0 +1,11 @@ +package com.gmail.nossr50.api.exceptions; + +import org.bukkit.entity.Player; + +public class McMMOPlayerNotFoundException extends RuntimeException { + private static final long serialVersionUID = 761917904993202836L; + + public McMMOPlayerNotFoundException(Player player) { + super("McMMOPlayer object was not found for: " + player.getName() + " " + player.getUniqueId()); + } +} diff --git a/src/main/java/com/gmail/nossr50/commands/McImportCommand.java b/src/main/java/com/gmail/nossr50/commands/McImportCommand.java index a372dca0d..17a755048 100644 --- a/src/main/java/com/gmail/nossr50/commands/McImportCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/McImportCommand.java @@ -17,6 +17,7 @@ import org.bukkit.command.CommandSender; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.skills.ModConfigType; +import com.gmail.nossr50.util.Misc; public class McImportCommand implements CommandExecutor { int fileAmount; @@ -49,6 +50,7 @@ public class McImportCommand implements CommandExecutor { String line; String materialName; + String modName; // While not at the end of the file while ((line = in.readLine()) != null) { @@ -65,19 +67,16 @@ public class McImportCommand implements CommandExecutor { } materialName = split2[0]; - String[] materialSplit = materialName.split("_"); - if (materialSplit.length > 1) { - // Categorise each material under a mod config type - ModConfigType type = ModConfigType.getModConfigType(materialName); + // Categorise each material under a mod config type + ModConfigType type = ModConfigType.getModConfigType(materialName); - if (!materialNames.containsKey(type)) { - materialNames.put(type, new ArrayList()); - } - - materialNames.get(type).add(materialName); - continue; + if (!materialNames.containsKey(type)) { + materialNames.put(type, new ArrayList()); } + + materialNames.get(type).add(materialName); + continue; } } catch (FileNotFoundException e) { @@ -104,7 +103,7 @@ public class McImportCommand implements CommandExecutor { HashMap> materialNamesType = new HashMap>(); for (String materialName : materialNames.get(modConfigType)) { - String modName = materialName.split("_")[0].toLowerCase(); + String modName = Misc.getModName(materialName); if (!materialNamesType.containsKey(modName)) { materialNamesType.put(modName, new ArrayList()); diff --git a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java index bce3fe11d..bae8686de 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java @@ -408,15 +408,21 @@ public final class FlatfileDatabaseManager implements DatabaseManager { String[] character = line.split(":"); int uuidIndex = getUUIDIndexFromLine(character); int nameIndex = getNameIndexFromLine(character); + boolean shouldCheckName = false; if (uuidIndex == -1 && nameIndex == -1) { continue; } - else if(uuidIndex != -1) { - if ((uuid != null && (!character[uuidIndex].split(",")[1].equalsIgnoreCase(uuid.toString()) && !character[uuidIndex].split(",")[1].equalsIgnoreCase("NULL")))) { - continue; + else if(uuidIndex != -1 && uuid != null) { + if (((!character[uuidIndex].split(",")[1].equalsIgnoreCase(uuid.toString())))) { + if(!character[uuidIndex].split(",")[1].equalsIgnoreCase("NULL")) { + continue; + } + else { + shouldCheckName = true; + } } } - else if(nameIndex != -1) { + if(shouldCheckName && nameIndex != -1) { if (uuid == null && !character[nameIndex].split(",")[1].equalsIgnoreCase(playerName)) { continue; } diff --git a/src/main/java/com/gmail/nossr50/runnables/commands/McrankCommandDisplayTask.java b/src/main/java/com/gmail/nossr50/runnables/commands/McrankCommandDisplayTask.java index e7544a19c..572037a5d 100644 --- a/src/main/java/com/gmail/nossr50/runnables/commands/McrankCommandDisplayTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/commands/McrankCommandDisplayTask.java @@ -37,9 +37,7 @@ public class McrankCommandDisplayTask extends BukkitRunnable { if (useChat) { displayChat(); } - if(sender instanceof Player) { - mcMMO.p.getServer().getPlayerExact(playerName).removeMetadata(mcMMO.databaseCommandKey, mcMMO.p); - } + ((Player) sender).removeMetadata(mcMMO.databaseCommandKey, mcMMO.p); } private void displayChat() { diff --git a/src/main/java/com/gmail/nossr50/runnables/commands/MctopCommandDisplayTask.java b/src/main/java/com/gmail/nossr50/runnables/commands/MctopCommandDisplayTask.java index 91fb8753e..ae5603f9e 100644 --- a/src/main/java/com/gmail/nossr50/runnables/commands/MctopCommandDisplayTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/commands/MctopCommandDisplayTask.java @@ -42,6 +42,9 @@ public class MctopCommandDisplayTask extends BukkitRunnable { displayChat(); } + if (sender instanceof Player) { + ((Player) sender).removeMetadata(mcMMO.databaseCommandKey, mcMMO.p); + } sender.sendMessage(LocaleLoader.getString("Commands.mctop.Tip")); } @@ -62,9 +65,6 @@ public class MctopCommandDisplayTask extends BukkitRunnable { sender.sendMessage(String.format("%2d. %s%s - %s%s", place, ChatColor.GREEN, stat.name, ChatColor.WHITE, stat.statVal)); place++; } - if (sender instanceof Player) { - ((Player) sender).removeMetadata(mcMMO.databaseCommandKey, mcMMO.p); - } } private void displayBoard() { diff --git a/src/main/java/com/gmail/nossr50/util/HardcoreManager.java b/src/main/java/com/gmail/nossr50/util/HardcoreManager.java index ca869bb86..85e2234cf 100644 --- a/src/main/java/com/gmail/nossr50/util/HardcoreManager.java +++ b/src/main/java/com/gmail/nossr50/util/HardcoreManager.java @@ -25,7 +25,9 @@ public final class HardcoreManager { for (SkillType skillType : SkillType.getNonChildSkills()) { if (!skillType.getHardcoreStatLossEnabled()) { - break; + levelChanged.put(skillType.toString(), 0); + experienceChanged.put(skillType.toString(), 0F); + continue; } int playerSkillLevel = playerProfile.getSkillLevel(skillType); @@ -64,7 +66,9 @@ public final class HardcoreManager { for (SkillType skillType : SkillType.getNonChildSkills()) { if (!skillType.getHardcoreVampirismEnabled()) { - break; + levelChanged.put(skillType.toString(), 0); + experienceChanged.put(skillType.toString(), 0F); + continue; } int killerSkillLevel = killerProfile.getSkillLevel(skillType); diff --git a/src/main/java/com/gmail/nossr50/util/Misc.java b/src/main/java/com/gmail/nossr50/util/Misc.java index 6a21339cc..d472d7426 100644 --- a/src/main/java/com/gmail/nossr50/util/Misc.java +++ b/src/main/java/com/gmail/nossr50/util/Misc.java @@ -2,6 +2,7 @@ package com.gmail.nossr50.util; import java.util.Collection; import java.util.Random; +import java.util.Set; import org.bukkit.Location; import org.bukkit.Material; @@ -15,6 +16,7 @@ import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.events.items.McMMOItemSpawnEvent; import com.gmail.nossr50.runnables.player.PlayerProfileLoadingTask; import com.gmail.nossr50.util.player.UserManager; +import com.google.common.collect.ImmutableSet; public final class Misc { private static Random random = new Random(); @@ -36,6 +38,8 @@ public final class Misc { public static final float LEVELUP_PITCH = 0.5F; // Reduced to differentiate between vanilla level-up public static final float LEVELUP_VOLUME = 0.75F; // Use max volume always + public static final Set modNames = ImmutableSet.of("LOTR", "BUILDCRAFT", "ENDERIO", "ENHANCEDBIOMES", "IC2", "METALLURGY", "FORESTRY", "GALACTICRAFT", "RAILCRAFT", "TWILIGHTFOREST", "THAUMCRAFT", "GRAVESTONEMOD", "GROWTHCRAFT", "ARCTICMOBS", "DEMONMOBS", "INFERNOMOBS", "SWAMPMOBS", "MARICULTURE", "MINESTRAPPOLATION"); + private Misc() {}; public static float getFizzPitch() { @@ -123,6 +127,22 @@ public final class Misc { } } + public static String getModName(String materialName) { + for (String mod : modNames) { + if (materialName.contains(mod)) { + return mod; + } + } + + String[] materialSplit = materialName.split("_"); + + if (materialSplit.length > 1) { + return materialSplit[0].toLowerCase(); + } + + return "UnknownMods"; + } + public static Random getRandom() { return random; }