1
0
mirror of https://github.com/mcMMO-Dev/mcMMO.git synced 2026-02-18 17:53:00 +01:00

Compare commits

...

46 Commits

Author SHA1 Message Date
TfT_02
a38d1176cb Fix casing 2014-08-01 19:25:49 +02:00
TfT_02
7d65b9422a Make the profile loaded message optional
People will complain if we don’t
2014-07-31 23:16:43 +02:00
TfT_02
e7ee31c291 Revert "Hide loading messages when at the first attempt"
This reverts commit 216ecc465d.
2014-07-31 23:10:46 +02:00
TfT_02
23cbfc9038 Revert "Increase attempt counter after the apply"
This reverts commit c24c19f5f5.
2014-07-31 23:10:39 +02:00
TfT_02
c24c19f5f5 Increase attempt counter after the apply 2014-07-31 22:37:22 +02:00
TfT_02
216ecc465d Hide loading messages when at the first attempt 2014-07-31 22:06:42 +02:00
TfT_02
635f2bdb43 Fix formatting 2014-07-31 22:04:53 +02:00
t00thpick1
2286728308 Riking fix this or its gone. 2014-07-31 15:06:12 -04:00
t00thpick1
f809bca47b Avoid race conditions in multiple load attempts. 2014-07-31 12:59:36 -04:00
t00thpick1
71d6cb661f Actually make multiple load attempts 2014-07-31 12:59:21 -04:00
t00thpick1
2248316ed3 some of zreeds work, because too lazy to fix merge compatability 2014-07-31 12:44:41 -04:00
riking
9668bb2ee7 Abort onEnable() when driver not found 2014-07-30 12:31:56 -07:00
t00thpick1
f0503c7797 Unused 2014-07-29 16:27:54 -04:00
t00thpick1
9a12d86eac Configurable :P 2014-07-28 16:46:55 -04:00
t00thpick1
ed82a838c8 I like these to be explictly closed rather than in a new method. 2014-07-27 14:56:34 -04:00
t00thpick1
e6a7c8f5d2 We need both UUID and username to create new. 2014-07-27 12:57:12 -04:00
t00thpick1
fc393e1047 start 2014-07-27 12:01:08 -04:00
t00thpick1
f8f73a49b5 comma 2014-07-27 11:50:12 -04:00
t00thpick1
a7841adec5 Missed some usages 2014-07-27 11:14:46 -04:00
t00thpick1
7917b84eca Hopefully this works. 2014-07-27 11:03:16 -04:00
TfT_02
49f2bf5452 Fix formatting in SQLDatabaseManager 2014-07-27 12:32:48 +02:00
zreed
16b40c3eba Create the UUID index on the right table as well as on table creation 2014-07-26 01:42:27 -04:00
t00thpick1
1396d6d9ad Explicitly close every resource 2014-07-26 00:00:40 -04:00
zreed
b2015d68d1 Work by Zreed 2014-07-25 23:31:41 -04:00
t00thpick1
a3066803d0 Completely untested, needs some more work as follows:
A.  Handle reconnecting on full disconnects.
B.  refactor code to use same connection object in same task(Some tasks go through many methods that each get their own connection object)
C.  Test that it actually works :P
2014-07-24 23:27:56 -04:00
SLiPCoR
d02058ca13 Update player name in case it has changed.
TODO: Check for double names if that happens? In general?
2014-07-24 19:31:38 +02:00
zreed
7d0fd36a2a Further cleanup of SQLDatabaseManager upgrades 2014-07-24 12:12:41 +02:00
TfT_02
1c4ac1d766 Remove hidden interval option 2014-07-24 12:12:41 +02:00
TfT_02
7861e93497 Fixed formatting! 2014-07-24 12:12:41 +02:00
TfT_02
7118f8850d Also update flatfile with uuids 2014-07-24 12:12:40 +02:00
zreed
384bb6306a Add saveUserUUIDs to FlatfileDatabaseManager so mcMMO compiles
Minimal changes were made but it hasn't been tested at all and almost certainly doesn't work.
2014-07-24 01:08:00 +02:00
zreed
8fd9982f69 Optimize SQL side of UUID update 2014-07-24 01:08:00 +02:00
SLiPCoR
c5e1a735f9 Check for uuids instead of usernames when loading a player 2014-07-24 01:08:00 +02:00
SLiPCoR
80754b4426 Add a new fallback method that does the exact same thing; add it when the retry on getting a UUID player would have failed twice 2014-07-24 01:08:00 +02:00
SLiPCoR
4524a3e437 Add saving of UUIDs (used in the UUID Fetcher update task) 2014-07-24 01:08:00 +02:00
TfT_02
83c8d3cdb9 Save if uuid conversion is complete 2014-07-24 01:08:00 +02:00
TfT_02
ed95cfcbfc Update to UUIDFetcher v3 2014-07-24 01:08:00 +02:00
TfT_02
69ebde052d Load UUID with SQL 2014-07-24 01:08:00 +02:00
TfT_02
072e7c8b19 Update playername in database when a name changes 2014-07-24 01:07:59 +02:00
TfT_02
792f21bc20 Save and load profiles using UUIDs
fall back on usernames if neccesary
2014-07-24 01:07:59 +02:00
TfT_02
e619e01c23 UUID flatfile stuff 2014-07-24 01:07:59 +02:00
TfT_02
e7d5aa17bf Add hidden options for uuid conversion 2014-07-24 01:07:59 +02:00
TfT_02
8d6f151a54 Deprecate PlayerProfiles relying on names 2014-07-24 01:07:59 +02:00
TfT_02
d3faff67ad UUID Conversion stuff 2014-07-24 01:07:59 +02:00
TfT_02
dc21e18cc2 Deprecate methods in ExperienceAPI 2014-07-24 01:07:59 +02:00
TfT_02
7643edfdcd Prepare for UUID update 2014-07-24 01:07:58 +02:00
24 changed files with 2359 additions and 1209 deletions

15
pom.xml
View File

@@ -76,6 +76,8 @@
<artifactSet> <artifactSet>
<includes> <includes>
<include>com.turt2live.metrics:MetricsExtension</include> <include>com.turt2live.metrics:MetricsExtension</include>
<include>commons-logging:commons-logging</include>
<include>net.snaq:dbpool</include>
</includes> </includes>
</artifactSet> </artifactSet>
<relocations> <relocations>
@@ -83,6 +85,14 @@
<pattern>com.turt2live.metrics</pattern> <pattern>com.turt2live.metrics</pattern>
<shadedPattern>com.gmail.nossr50.metrics.mcstats</shadedPattern> <shadedPattern>com.gmail.nossr50.metrics.mcstats</shadedPattern>
</relocation> </relocation>
<relocation>
<pattern>org.apache.commons.logging</pattern>
<shadedPattern>com.gmail.nossr50.commons.logging</shadedPattern>
</relocation>
<relocation>
<pattern>net.snaq</pattern>
<shadedPattern>com.gmail.nossr50.dbpool</shadedPattern>
</relocation>
</relocations> </relocations>
</configuration> </configuration>
<executions> <executions>
@@ -136,6 +146,11 @@
<artifactId>MetricsExtension</artifactId> <artifactId>MetricsExtension</artifactId>
<version>0.0.5-SNAPSHOT</version> <version>0.0.5-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>net.snaq</groupId>
<artifactId>dbpool</artifactId>
<version>5.1</version>
</dependency>
</dependencies> </dependencies>
<distributionManagement> <distributionManagement>
<repository> <repository>

View File

@@ -1,6 +1,7 @@
package com.gmail.nossr50.api; package com.gmail.nossr50.api;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -86,6 +87,14 @@ public final class ExperienceAPI {
UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason));
} }
/**
* Adds raw XP to an offline player.
* </br>
* This function is designed for API usage.
*
* @deprecated We're using float for our XP values now
* replaced by {@link #addRawXPOffline(String playerName, String skillType, float XP)}
*/
@Deprecated @Deprecated
public static void addRawXPOffline(String playerName, String skillType, int XP) { public static void addRawXPOffline(String playerName, String skillType, int XP) {
addRawXPOffline(playerName, skillType, (float) XP); addRawXPOffline(playerName, skillType, (float) XP);
@@ -96,6 +105,9 @@ public final class ExperienceAPI {
* </br> * </br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @deprecated We're using uuids to get an offline player
* replaced by {@link #addRawXPOffline(UUID uuid, String skillType, float XP)}
*
* @param playerName The player to add XP to * @param playerName The player to add XP to
* @param skillType The skill to add XP to * @param skillType The skill to add XP to
* @param XP The amount of XP to add * @param XP The amount of XP to add
@@ -103,10 +115,27 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid * @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
*/ */
@Deprecated
public static void addRawXPOffline(String playerName, String skillType, float XP) { public static void addRawXPOffline(String playerName, String skillType, float XP) {
addOfflineXP(playerName, getSkillType(skillType), (int) Math.floor(XP)); addOfflineXP(playerName, getSkillType(skillType), (int) Math.floor(XP));
} }
/**
* Adds raw XP to an offline player.
* </br>
* This function is designed for API usage.
*
* @param uuid The UUID of player to add XP to
* @param skillType The skill to add XP to
* @param XP The amount of XP to add
*
* @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database
*/
public static void addRawXPOffline(UUID uuid, String skillType, float XP) {
addOfflineXP(uuid, getSkillType(skillType), (int) Math.floor(XP));
}
/** /**
* Adds XP to the player, calculates for XP Rate only. * Adds XP to the player, calculates for XP Rate only.
* </br> * </br>
@@ -152,6 +181,7 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid * @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
*/ */
@Deprecated
public static void addMultipliedXPOffline(String playerName, String skillType, int XP) { public static void addMultipliedXPOffline(String playerName, String skillType, int XP) {
addOfflineXP(playerName, getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier())); addOfflineXP(playerName, getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()));
} }
@@ -203,6 +233,7 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid * @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
*/ */
@Deprecated
public static void addModifiedXPOffline(String playerName, String skillType, int XP) { public static void addModifiedXPOffline(String playerName, String skillType, int XP) {
SkillType skill = getSkillType(skillType); SkillType skill = getSkillType(skillType);
@@ -273,10 +304,28 @@ public final class ExperienceAPI {
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
@Deprecated
public static int getOfflineXP(String playerName, String skillType) { public static int getOfflineXP(String playerName, String skillType) {
return getOfflineProfile(playerName).getSkillXpLevel(getNonChildSkillType(skillType)); return getOfflineProfile(playerName).getSkillXpLevel(getNonChildSkillType(skillType));
} }
/**
* Get the amount of XP an offline player has in a specific skill.
* </br>
* This function is designed for API usage.
*
* @param uuid The player to get XP for
* @param skillType The skill to get XP for
* @return the amount of XP in a given skill
*
* @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static int getOfflineXP(UUID uuid, String skillType) {
return getOfflineProfile(uuid).getSkillXpLevel(getNonChildSkillType(skillType));
}
/** /**
* Get the raw amount of XP a player has in a specific skill. * Get the raw amount of XP a player has in a specific skill.
* </br> * </br>
@@ -306,10 +355,28 @@ public final class ExperienceAPI {
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
@Deprecated
public static float getOfflineXPRaw(String playerName, String skillType) { public static float getOfflineXPRaw(String playerName, String skillType) {
return getOfflineProfile(playerName).getSkillXpLevelRaw(getNonChildSkillType(skillType)); return getOfflineProfile(playerName).getSkillXpLevelRaw(getNonChildSkillType(skillType));
} }
/**
* Get the raw amount of XP an offline player has in a specific skill.
* </br>
* This function is designed for API usage.
*
* @param uuid The player to get XP for
* @param skillType The skill to get XP for
* @return the amount of XP in a given skill
*
* @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static float getOfflineXPRaw(UUID uuid, String skillType) {
return getOfflineProfile(uuid).getSkillXpLevelRaw(getNonChildSkillType(skillType));
}
/** /**
* Get the total amount of XP needed to reach the next level. * Get the total amount of XP needed to reach the next level.
* </br> * </br>
@@ -339,10 +406,28 @@ public final class ExperienceAPI {
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
@Deprecated
public static int getOfflineXPToNextLevel(String playerName, String skillType) { public static int getOfflineXPToNextLevel(String playerName, String skillType) {
return getOfflineProfile(playerName).getXpToLevel(getNonChildSkillType(skillType)); return getOfflineProfile(playerName).getXpToLevel(getNonChildSkillType(skillType));
} }
/**
* Get the total amount of XP an offline player needs to reach the next level.
* </br>
* This function is designed for API usage.
*
* @param uuid The player to get XP for
* @param skillType The skill to get XP for
* @return the total amount of XP needed to reach the next level
*
* @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static int getOfflineXPToNextLevel(UUID uuid, String skillType) {
return getOfflineProfile(uuid).getXpToLevel(getNonChildSkillType(skillType));
}
/** /**
* Get the amount of XP remaining until the next level. * Get the amount of XP remaining until the next level.
* </br> * </br>
@@ -376,14 +461,34 @@ public final class ExperienceAPI {
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
@Deprecated
public static int getOfflineXPRemaining(String playerName, String skillType) { public static int getOfflineXPRemaining(String playerName, String skillType) {
SkillType skill = getNonChildSkillType(skillType); SkillType skill = getNonChildSkillType(skillType);
PlayerProfile profile = getOfflineProfile(playerName); PlayerProfile profile = getOfflineProfile(playerName);
return profile.getXpToLevel(skill) - profile.getSkillXpLevel(skill); return profile.getXpToLevel(skill) - profile.getSkillXpLevel(skill);
} }
/**
* Get the amount of XP an offline player has left before leveling up.
* </br>
* This function is designed for API usage.
*
* @param uuid The player to get XP for
* @param skillType The skill to get XP for
* @return the amount of XP needed to reach the next level
*
* @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static float getOfflineXPRemaining(UUID uuid, String skillType) {
SkillType skill = getNonChildSkillType(skillType);
PlayerProfile profile = getOfflineProfile(uuid);
return profile.getXpToLevel(skill) - profile.getSkillXpLevelRaw(skill);
}
/** /**
* Add levels to a skill. * Add levels to a skill.
* </br> * </br>
@@ -411,6 +516,7 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid * @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
*/ */
@Deprecated
public static void addLevelOffline(String playerName, String skillType, int levels) { public static void addLevelOffline(String playerName, String skillType, int levels) {
PlayerProfile profile = getOfflineProfile(playerName); PlayerProfile profile = getOfflineProfile(playerName);
SkillType skill = getSkillType(skillType); SkillType skill = getSkillType(skillType);
@@ -430,6 +536,37 @@ public final class ExperienceAPI {
profile.scheduleAsyncSave(); profile.scheduleAsyncSave();
} }
/**
* Add levels to a skill for an offline player.
* </br>
* This function is designed for API usage.
*
* @param uuid The player to add levels to
* @param skillType Type of skill to add levels to
* @param levels Number of levels to add
*
* @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database
*/
public static void addLevelOffline(UUID uuid, String skillType, int levels) {
PlayerProfile profile = getOfflineProfile(uuid);
SkillType skill = getSkillType(skillType);
if (skill.isChildSkill()) {
Set<SkillType> parentSkills = FamilyTree.getParents(skill);
for (SkillType parentSkill : parentSkills) {
profile.addLevels(parentSkill, (levels / parentSkills.size()));
}
profile.scheduleAsyncSave();
return;
}
profile.addLevels(skill, levels);
profile.scheduleAsyncSave();
}
/** /**
* Get the level a player has in a specific skill. * Get the level a player has in a specific skill.
* </br> * </br>
@@ -457,10 +594,27 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid * @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
*/ */
@Deprecated
public static int getLevelOffline(String playerName, String skillType) { public static int getLevelOffline(String playerName, String skillType) {
return getOfflineProfile(playerName).getSkillLevel(getSkillType(skillType)); return getOfflineProfile(playerName).getSkillLevel(getSkillType(skillType));
} }
/**
* Get the level an offline player has in a specific skill.
* </br>
* This function is designed for API usage.
*
* @param uuid The player to get the level for
* @param skillType The skill to get the level for
* @return the level of a given skill
*
* @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database
*/
public static int getLevelOffline(UUID uuid, String skillType) {
return getOfflineProfile(uuid).getSkillLevel(getSkillType(skillType));
}
/** /**
* Gets the power level of a player. * Gets the power level of a player.
* </br> * </br>
@@ -483,6 +637,7 @@ public final class ExperienceAPI {
* *
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
*/ */
@Deprecated
public static int getPowerLevelOffline(String playerName) { public static int getPowerLevelOffline(String playerName) {
int powerLevel = 0; int powerLevel = 0;
PlayerProfile profile = getOfflineProfile(playerName); PlayerProfile profile = getOfflineProfile(playerName);
@@ -494,6 +649,27 @@ public final class ExperienceAPI {
return powerLevel; return powerLevel;
} }
/**
* Gets the power level of an offline player.
* </br>
* This function is designed for API usage.
*
* @param uuid The player to get the power level for
* @return the power level of the player
*
* @throws InvalidPlayerException if the given player does not exist in the database
*/
public static int getPowerLevelOffline(UUID uuid) {
int powerLevel = 0;
PlayerProfile profile = getOfflineProfile(uuid);
for (SkillType type : SkillType.NON_CHILD_SKILLS) {
powerLevel += profile.getSkillLevel(type);
}
return powerLevel;
}
/** /**
* Get the level cap of a specific skill. * Get the level cap of a specific skill.
* </br> * </br>
@@ -533,10 +709,28 @@ public final class ExperienceAPI {
* *
* @return the position on the leaderboard * @return the position on the leaderboard
*/ */
@Deprecated
public static int getPlayerRankSkill(String playerName, String skillType) { public static int getPlayerRankSkill(String playerName, String skillType) {
return mcMMO.getDatabaseManager().readRank(getOfflineProfile(playerName).getPlayerName()).get(getNonChildSkillType(skillType)); return mcMMO.getDatabaseManager().readRank(getOfflineProfile(playerName).getPlayerName()).get(getNonChildSkillType(skillType));
} }
/**
* Get the position on the leaderboard of a player.
* </br>
* This function is designed for API usage.
*
* @param uuid The name of the player to check
* @param skillType The skill to check
*
* @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill
*
* @return the position on the leaderboard
*/
public static int getPlayerRankSkill(UUID uuid, String skillType) {
return mcMMO.getDatabaseManager().readRank(getOfflineProfile(uuid).getPlayerName()).get(getNonChildSkillType(skillType));
}
/** /**
* Get the position on the power level leaderboard of a player. * Get the position on the power level leaderboard of a player.
@@ -549,10 +743,26 @@ public final class ExperienceAPI {
* *
* @return the position on the power level leaderboard * @return the position on the power level leaderboard
*/ */
@Deprecated
public static int getPlayerRankOverall(String playerName) { public static int getPlayerRankOverall(String playerName) {
return mcMMO.getDatabaseManager().readRank(getOfflineProfile(playerName).getPlayerName()).get(null); return mcMMO.getDatabaseManager().readRank(getOfflineProfile(playerName).getPlayerName()).get(null);
} }
/**
* Get the position on the power level leaderboard of a player.
* </br>
* This function is designed for API usage.
*
* @param uuid The name of the player to check
*
* @throws InvalidPlayerException if the given player does not exist in the database
*
* @return the position on the power level leaderboard
*/
public static int getPlayerRankOverall(UUID uuid) {
return mcMMO.getDatabaseManager().readRank(getOfflineProfile(uuid).getPlayerName()).get(null);
}
/** /**
* Sets the level of a player in a specific skill type. * Sets the level of a player in a specific skill type.
* </br> * </br>
@@ -580,10 +790,27 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid * @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
*/ */
@Deprecated
public static void setLevelOffline(String playerName, String skillType, int skillLevel) { public static void setLevelOffline(String playerName, String skillType, int skillLevel) {
getOfflineProfile(playerName).modifySkill(getSkillType(skillType), skillLevel); getOfflineProfile(playerName).modifySkill(getSkillType(skillType), skillLevel);
} }
/**
* Sets the level of an offline player in a specific skill type.
* </br>
* This function is designed for API usage.
*
* @param uuid The player to set the level of
* @param skillType The skill to set the level for
* @param skillLevel The value to set the level to
*
* @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database
*/
public static void setLevelOffline(UUID uuid, String skillType, int skillLevel) {
getOfflineProfile(uuid).modifySkill(getSkillType(skillType), skillLevel);
}
/** /**
* Sets the XP of a player in a specific skill type. * Sets the XP of a player in a specific skill type.
* </br> * </br>
@@ -613,10 +840,28 @@ public final class ExperienceAPI {
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
@Deprecated
public static void setXPOffline(String playerName, String skillType, int newValue) { public static void setXPOffline(String playerName, String skillType, int newValue) {
getOfflineProfile(playerName).setSkillXpLevel(getNonChildSkillType(skillType), newValue); getOfflineProfile(playerName).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
} }
/**
* Sets the XP of an offline player in a specific skill type.
* </br>
* This function is designed for API usage.
*
* @param uuid The player to set the XP of
* @param skillType The skill to set the XP for
* @param newValue The value to set the XP to
*
* @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static void setXPOffline(UUID uuid, String skillType, int newValue) {
getOfflineProfile(uuid).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
}
/** /**
* Removes XP from a player in a specific skill type. * Removes XP from a player in a specific skill type.
* </br> * </br>
@@ -646,12 +891,37 @@ public final class ExperienceAPI {
* @throws InvalidPlayerException if the given player does not exist in the database * @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
@Deprecated
public static void removeXPOffline(String playerName, String skillType, int xp) { public static void removeXPOffline(String playerName, String skillType, int xp) {
getOfflineProfile(playerName).removeXp(getNonChildSkillType(skillType), xp); getOfflineProfile(playerName).removeXp(getNonChildSkillType(skillType), xp);
} }
// Utility methods follow. /**
* Removes XP from an offline player in a specific skill type.
* </br>
* This function is designed for API usage.
*
* @param uuid The player to change the XP of
* @param skillType The skill to change the XP for
* @param xp The amount of XP to remove
*
* @throws InvalidSkillException if the given skill is not valid
* @throws InvalidPlayerException if the given player does not exist in the database
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static void removeXPOffline(UUID uuid, String skillType, int xp) {
getOfflineProfile(uuid).removeXp(getNonChildSkillType(skillType), xp);
}
// Utility methods follow.
private static void addOfflineXP(UUID playerUniqueId, SkillType skill, int XP) {
PlayerProfile profile = getOfflineProfile(playerUniqueId);
profile.addXp(skill, XP);
profile.save();
}
@Deprecated
private static void addOfflineXP(String playerName, SkillType skill, int XP) { private static void addOfflineXP(String playerName, SkillType skill, int XP) {
PlayerProfile profile = getOfflineProfile(playerName); PlayerProfile profile = getOfflineProfile(playerName);
@@ -659,8 +929,20 @@ public final class ExperienceAPI {
profile.scheduleAsyncSave(); profile.scheduleAsyncSave();
} }
private static PlayerProfile getOfflineProfile(UUID uuid) {
PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(uuid);
if (!profile.isLoaded()) {
throw new InvalidPlayerException();
}
return profile;
}
@Deprecated
private static PlayerProfile getOfflineProfile(String playerName) { private static PlayerProfile getOfflineProfile(String playerName) {
PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, false); UUID uuid = mcMMO.p.getServer().getOfflinePlayer(playerName).getUniqueId();
PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(uuid);
if (!profile.isLoaded()) { if (!profile.isLoaded()) {
throw new InvalidPlayerException(); throw new InvalidPlayerException();

View File

@@ -12,6 +12,7 @@ import com.gmail.nossr50.datatypes.database.DatabaseType;
import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.runnables.database.DatabaseConversionTask; import com.gmail.nossr50.runnables.database.DatabaseConversionTask;
import com.gmail.nossr50.runnables.player.PlayerProfileLoadingTask;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
public class ConvertDatabaseCommand implements CommandExecutor { public class ConvertDatabaseCommand implements CommandExecutor {
@@ -55,13 +56,13 @@ public class ConvertDatabaseCommand implements CommandExecutor {
UserManager.clearAll(); UserManager.clearAll();
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) { for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
PlayerProfile profile = oldDatabase.loadPlayerProfile(player.getName(), false); PlayerProfile profile = oldDatabase.loadPlayerProfile(player.getUniqueId());
if (profile.isLoaded()) { if (profile.isLoaded()) {
mcMMO.getDatabaseManager().saveUser(profile); mcMMO.getDatabaseManager().saveUser(profile);
} }
UserManager.addUser(player); new PlayerProfileLoadingTask(player).runTaskTimerAsynchronously(mcMMO.p, 1, 20); // 1 Tick delay to ensure the player is marked as online before we begin loading
} }
new DatabaseConversionTask(oldDatabase, sender, previousType.toString(), newType.toString()).runTaskAsynchronously(mcMMO.p); new DatabaseConversionTask(oldDatabase, sender, previousType.toString(), newType.toString()).runTaskAsynchronously(mcMMO.p);

View File

@@ -9,6 +9,7 @@ import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.experience.FormulaType; import com.gmail.nossr50.datatypes.experience.FormulaType;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.runnables.database.FormulaConversionTask; import com.gmail.nossr50.runnables.database.FormulaConversionTask;
import com.gmail.nossr50.runnables.player.PlayerProfileLoadingTask;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
public class ConvertExperienceCommand implements CommandExecutor { public class ConvertExperienceCommand implements CommandExecutor {
@@ -37,7 +38,7 @@ public class ConvertExperienceCommand implements CommandExecutor {
new FormulaConversionTask(sender, newType).runTaskLater(mcMMO.p, 1); new FormulaConversionTask(sender, newType).runTaskLater(mcMMO.p, 1);
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) { for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
UserManager.addUser(player); new PlayerProfileLoadingTask(player).runTaskTimerAsynchronously(mcMMO.p, 1, 20); // 1 Tick delay to ensure the player is marked as online before we begin loading
} }
return true; return true;

View File

@@ -232,6 +232,7 @@ public class Config extends AutoUpdateConfigLoader {
/* General Settings */ /* General Settings */
public String getLocale() { return config.getString("General.Locale", "en_us"); } public String getLocale() { return config.getString("General.Locale", "en_us"); }
public boolean getMOTDEnabled() { return config.getBoolean("General.MOTD_Enabled", true); } public boolean getMOTDEnabled() { return config.getBoolean("General.MOTD_Enabled", true); }
public boolean getShowProfileLoadedMessage() { return config.getBoolean("General.Show_Profile_Loaded", true); }
public boolean getDonateMessageEnabled() { return config.getBoolean("Commands.mcmmo.Donate_Message", true); } public boolean getDonateMessageEnabled() { return config.getBoolean("Commands.mcmmo.Donate_Message", true); }
public int getSaveInterval() { return config.getInt("General.Save_Interval", 10); } public int getSaveInterval() { return config.getInt("General.Save_Interval", 10); }
public boolean getStatsTrackingEnabled() { return config.getBoolean("General.Stats_Tracking", true); } public boolean getStatsTrackingEnabled() { return config.getBoolean("General.Stats_Tracking", true); }
@@ -313,6 +314,8 @@ public class Config extends AutoUpdateConfigLoader {
public int getMySQLServerPort() { return config.getInt("MySQL.Server.Port", 3306); } public int getMySQLServerPort() { return config.getInt("MySQL.Server.Port", 3306); }
public String getMySQLServerName() { return config.getString("MySQL.Server.Address", "localhost"); } public String getMySQLServerName() { return config.getString("MySQL.Server.Address", "localhost"); }
public String getMySQLUserPassword() { return getStringIncludingInts("MySQL.Database.User_Password"); } public String getMySQLUserPassword() { return getStringIncludingInts("MySQL.Database.User_Password"); }
public int getMySQLMaxConnections() { return config.getInt("MySQL.Database.MaxConnections"); }
public int getMySQLMaxPoolSize() { return config.getInt("MySQL.Database.MaxPoolSize"); }
private String getStringIncludingInts(String key) { private String getStringIncludingInts(String key) {
String str = config.getString(key); String str = config.getString(key);

View File

@@ -12,6 +12,7 @@ public class HiddenConfig {
private static int conversionRate; private static int conversionRate;
private static boolean useEnchantmentBuffs; private static boolean useEnchantmentBuffs;
private static boolean resendChunksAfterBlockAbility; private static boolean resendChunksAfterBlockAbility;
private static int uuidConvertAmount;
public HiddenConfig(String fileName) { public HiddenConfig(String fileName) {
HiddenConfig.fileName = fileName; HiddenConfig.fileName = fileName;
@@ -33,6 +34,7 @@ public class HiddenConfig {
conversionRate = config.getInt("Options.ConversionRate", 1); conversionRate = config.getInt("Options.ConversionRate", 1);
useEnchantmentBuffs = config.getBoolean("Options.EnchantmentBuffs", true); useEnchantmentBuffs = config.getBoolean("Options.EnchantmentBuffs", true);
resendChunksAfterBlockAbility = config.getBoolean("Options.RefreshChunks", false); resendChunksAfterBlockAbility = config.getBoolean("Options.RefreshChunks", false);
uuidConvertAmount = config.getInt("Options.UUIDConvertAmount", 5);
} }
} }
@@ -51,4 +53,8 @@ public class HiddenConfig {
public boolean resendChunksAfterBlockAbility() { public boolean resendChunksAfterBlockAbility() {
return resendChunksAfterBlockAbility; return resendChunksAfterBlockAbility;
} }
public int getUUIDConvertAmount() {
return uuidConvertAmount;
}
} }

View File

@@ -2,6 +2,7 @@ package com.gmail.nossr50.database;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.database.DatabaseType; import com.gmail.nossr50.datatypes.database.DatabaseType;
@@ -67,19 +68,42 @@ public interface DatabaseManager {
* *
* @param playerName The name of the player to be added to the database * @param playerName The name of the player to be added to the database
*/ */
public void newUser(String playerName); public void newUser(String playerName, String uuid);
/** /**
* Load a player from the database. * Load a player from the database.
* *
* @deprecated replaced by {@link #loadPlayerProfile(String playerName, UUID uuid, boolean createNew)}
*
* @param playerName The name of the player to load from the database * @param playerName The name of the player to load from the database
* @param createNew Whether to create a new record if the player is not * @param createNew Whether to create a new record if the player is not
* found * found
* @return The player's data, or an unloaded PlayerProfile if not found * @return The player's data, or an unloaded PlayerProfile if not found
* and createNew is false * and createNew is false
*/ */
@Deprecated
public PlayerProfile loadPlayerProfile(String playerName, boolean createNew); public PlayerProfile loadPlayerProfile(String playerName, boolean createNew);
/**
* Load a player from the database.
*
* @param uuid The uuid of the player to load from the database
* @return The player's data, or an unloaded PlayerProfile if not found
*/
public PlayerProfile loadPlayerProfile(UUID uuid);
/**
* Load a player from the database. Attempt to use uuid, fall back on playername
*
* @param playerName The name of the player to load from the database
* @param uuid The uuid of the player to load from the database
* @param createNew Whether to create a new record if the player is not
* found
* @return The player's data, or an unloaded PlayerProfile if not found
* and createNew is false
*/
public PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean createNew);
/** /**
* Get all users currently stored in the database. * Get all users currently stored in the database.
* *
@@ -95,10 +119,19 @@ public interface DatabaseManager {
*/ */
public void convertUsers(DatabaseManager destination); public void convertUsers(DatabaseManager destination);
public boolean saveUserUUID(String userName, UUID uuid);
public boolean saveUserUUIDs(Map<String, UUID> fetchedUUIDs);
/** /**
* Retrieve the type of database in use. Custom databases should return CUSTOM. * Retrieve the type of database in use. Custom databases should return CUSTOM.
* *
* @return The type of database * @return The type of database
*/ */
public DatabaseType getDatabaseType(); public DatabaseType getDatabaseType();
/**
* Called when the plugin disables
*/
public void onDisable();
} }

View File

@@ -2,7 +2,6 @@ package com.gmail.nossr50.database;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
@@ -10,10 +9,12 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@@ -26,6 +27,7 @@ import com.gmail.nossr50.datatypes.database.UpgradeType;
import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.AbilityType;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.runnables.database.UUIDUpdateAsyncTask;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.StringUtils; import com.gmail.nossr50.util.StringUtils;
@@ -44,6 +46,10 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
usersFile = new File(mcMMO.getUsersFilePath()); usersFile = new File(mcMMO.getUsersFilePath());
checkStructure(); checkStructure();
updateLeaderboards(); updateLeaderboards();
if (mcMMO.getUpgradeManager().shouldUpgrade(UpgradeType.ADD_UUIDS)) {
new UUIDUpdateAsyncTask(mcMMO.p, getStoredUsers()).runTaskAsynchronously(mcMMO.p);
}
} }
public void purgePowerlessUsers() { public void purgePowerlessUsers() {
@@ -92,8 +98,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString()); mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
} }
finally { finally {
tryClose(in); if (in != null) {
tryClose(out); try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
} }
} }
@@ -158,8 +178,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString()); mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
} }
finally { finally {
tryClose(in); if (in != null) {
tryClose(out); try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
} }
} }
@@ -197,8 +231,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString()); mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
} }
finally { finally {
tryClose(in); if (in != null) {
tryClose(out); try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
} }
} }
@@ -209,6 +257,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
public boolean saveUser(PlayerProfile profile) { public boolean saveUser(PlayerProfile profile) {
String playerName = profile.getPlayerName(); String playerName = profile.getPlayerName();
UUID uuid = profile.getUniqueId();
BufferedReader in = null; BufferedReader in = null;
FileWriter out = null; FileWriter out = null;
@@ -223,8 +272,9 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// While not at the end of the file // While not at the end of the file
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
// Read the line in and copy it to the output it's not the player we want to edit // Read the line in and copy it to the output if it's not the player we want to edit
if (!line.split(":")[0].equalsIgnoreCase(playerName)) { String[] character = line.split(":");
if (!character[41].equalsIgnoreCase(uuid.toString()) && !character[0].equalsIgnoreCase(playerName)) {
writer.append(line).append("\r\n"); writer.append(line).append("\r\n");
} }
else { else {
@@ -271,6 +321,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
writer.append(mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString()).append(":"); writer.append(mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString()).append(":");
writer.append(profile.getSkillLevel(SkillType.ALCHEMY)).append(":"); writer.append(profile.getSkillLevel(SkillType.ALCHEMY)).append(":");
writer.append(profile.getSkillXpLevel(SkillType.ALCHEMY)).append(":"); writer.append(profile.getSkillXpLevel(SkillType.ALCHEMY)).append(":");
writer.append(uuid.toString()).append(":");
writer.append("\r\n"); writer.append("\r\n");
} }
} }
@@ -285,8 +336,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
return false; return false;
} }
finally { finally {
tryClose(in); if (in != null) {
tryClose(out); try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
} }
} }
} }
@@ -302,7 +367,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
public Map<SkillType, Integer> readRank(String playerName) { public Map<SkillType, Integer> readRank(String playerName) {
updateLeaderboards(); updateLeaderboards();
Map<SkillType, Integer> skills = new HashMap<SkillType, Integer>(); Map<SkillType, Integer> skills = new EnumMap<SkillType, Integer>(SkillType.class);
for (SkillType skill : SkillType.NON_CHILD_SKILLS) { for (SkillType skill : SkillType.NON_CHILD_SKILLS) {
skills.put(skill, getPlayerRank(playerName, playerStatHash.get(skill))); skills.put(skill, getPlayerRank(playerName, playerStatHash.get(skill)));
@@ -313,7 +378,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
return skills; return skills;
} }
public void newUser(String playerName) { public void newUser(String playerName, String uuid) {
BufferedWriter out = null; BufferedWriter out = null;
synchronized (fileWritingLock) { synchronized (fileWritingLock) {
try { try {
@@ -362,6 +427,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out.append(Config.getInstance().getMobHealthbarDefault().toString()).append(":"); // Mob Healthbar HUD out.append(Config.getInstance().getMobHealthbarDefault().toString()).append(":"); // Mob Healthbar HUD
out.append("0:"); // Alchemy out.append("0:"); // Alchemy
out.append("0:"); // AlchemyXp out.append("0:"); // AlchemyXp
out.append(uuid).append(":"); // UUID
// Add more in the same format as the line above // Add more in the same format as the line above
@@ -371,12 +437,32 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
e.printStackTrace(); e.printStackTrace();
} }
finally { finally {
tryClose(out); if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
} }
} }
} }
@Deprecated
public PlayerProfile loadPlayerProfile(String playerName, boolean create) { public PlayerProfile loadPlayerProfile(String playerName, boolean create) {
return loadPlayerProfile(playerName, "", false);
}
public PlayerProfile loadPlayerProfile(UUID uuid) {
return loadPlayerProfile("", uuid.toString(), false);
}
public PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean create) {
return loadPlayerProfile(playerName, uuid.toString(), create);
}
private PlayerProfile loadPlayerProfile(String playerName, String uuid, boolean create) {
BufferedReader in = null; BufferedReader in = null;
String usersFilePath = mcMMO.getUsersFilePath(); String usersFilePath = mcMMO.getUsersFilePath();
@@ -390,17 +476,28 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// Find if the line contains the player we want. // Find if the line contains the player we want.
String[] character = line.split(":"); String[] character = line.split(":");
if (!character[0].equalsIgnoreCase(playerName)) { if (!character[41].equalsIgnoreCase(uuid) && !character[0].equalsIgnoreCase(playerName)) {
continue; continue;
} }
// Update playerName in database after name change
if (!character[0].equalsIgnoreCase(playerName)) {
mcMMO.p.debug("Name change detected: " + character[0] + " => " + playerName);
character[0] = playerName;
}
return loadFromLine(character); return loadFromLine(character);
} }
// Didn't find the player, create a new one // Didn't find the player, create a new one
if (create) { if (create) {
newUser(playerName); if (uuid.isEmpty()) {
return new PlayerProfile(playerName, true); newUser(playerName, uuid);
return new PlayerProfile(playerName, true);
}
newUser(playerName, uuid);
return new PlayerProfile(playerName, UUID.fromString(uuid), true);
} }
} }
catch (Exception e) { catch (Exception e) {
@@ -414,14 +511,18 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
in.close(); in.close();
} }
catch (IOException e) { catch (IOException e) {
e.printStackTrace(); // Ignore
} }
} }
} }
} }
// Return unloaded profile // Return unloaded profile
return new PlayerProfile(playerName); if (uuid.isEmpty()) {
return new PlayerProfile(playerName);
}
return new PlayerProfile(playerName, UUID.fromString(uuid));
} }
public void convertUsers(DatabaseManager destination) { public void convertUsers(DatabaseManager destination) {
@@ -453,11 +554,131 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
e.printStackTrace(); e.printStackTrace();
} }
finally { finally {
tryClose(in); if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
} }
} }
} }
public boolean saveUserUUID(String userName, UUID uuid) {
boolean worked = false;
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
synchronized (fileWritingLock) {
try {
in = new BufferedReader(new FileReader(usersFilePath));
StringBuilder writer = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
String[] character = line.split(":");
if (!worked && character[0].equalsIgnoreCase(userName)) {
if (character.length < 42) {
mcMMO.p.getLogger().severe("Could not update UUID for " + userName + "!");
mcMMO.p.getLogger().severe("Database entry is invalid.");
break;
}
line = line.replace(character[41], uuid.toString());
worked = true;
}
writer.append(line).append("\r\n");
}
out = new FileWriter(usersFilePath); // Write out the new file
out.write(writer.toString());
}
catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
return worked;
}
public boolean saveUserUUIDs(Map<String, UUID> fetchedUUIDs) {
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
synchronized (fileWritingLock) {
try {
in = new BufferedReader(new FileReader(usersFilePath));
StringBuilder writer = new StringBuilder();
String line;
while (((line = in.readLine()) != null) && !fetchedUUIDs.isEmpty()) {
String[] character = line.split(":");
if (fetchedUUIDs.containsKey(character[0])) {
if (character.length < 42) {
mcMMO.p.getLogger().severe("Could not update UUID for " + character[0] + "!");
mcMMO.p.getLogger().severe("Database entry is invalid.");
return false;
}
line = line.replace(character[41], fetchedUUIDs.remove(character[0]).toString());
}
writer.append(line).append("\r\n");
}
out = new FileWriter(usersFilePath); // Write out the new file
out.write(writer.toString());
}
catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
return true;
}
public List<String> getStoredUsers() { public List<String> getStoredUsers() {
ArrayList<String> users = new ArrayList<String>(); ArrayList<String> users = new ArrayList<String>();
BufferedReader in = null; BufferedReader in = null;
@@ -478,7 +699,14 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
e.printStackTrace(); e.printStackTrace();
} }
finally { finally {
tryClose(in); if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
} }
} }
return users; return users;
@@ -548,7 +776,14 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " during user " + playerName + " (Are you sure you formatted it correctly?) " + e.toString()); mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " during user " + playerName + " (Are you sure you formatted it correctly?) " + e.toString());
} }
finally { finally {
tryClose(in); if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
} }
} }
@@ -598,6 +833,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
in = new BufferedReader(new FileReader(usersFilePath)); in = new BufferedReader(new FileReader(usersFilePath));
StringBuilder writer = new StringBuilder(); StringBuilder writer = new StringBuilder();
String line; String line;
HashSet<String> usernames = new HashSet<String>();
HashSet<String> players = new HashSet<String>(); HashSet<String> players = new HashSet<String>();
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
@@ -612,8 +848,13 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
} }
String[] character = line.split(":"); String[] character = line.split(":");
// Prevent the same username from being present multiple times
if (!usernames.add(character[0])) {
continue;
}
// Prevent the same player from being present multiple times // Prevent the same player from being present multiple times
if (!players.add(character[0])) { if (character.length == 42 && (!character[41].isEmpty() && !players.add(character[41]))) {
continue; continue;
} }
@@ -645,7 +886,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
} }
// If they're valid, rewrite them to the file. // If they're valid, rewrite them to the file.
if (character.length == 41) { if (character.length == 42) {
writer.append(line).append("\r\n"); writer.append(line).append("\r\n");
continue; continue;
} }
@@ -699,16 +940,25 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
oldVersion = "1.4.08"; oldVersion = "1.4.08";
} }
} }
if (character.length <= 41) {
// Addition of UUIDs
// Version 1.5.01
// Add a space because otherwise it gets removed
newLine.append(" :");
if (oldVersion == null) {
oldVersion = "1.5.01";
}
}
// Remove any blanks that shouldn't be there, and validate the other fields // Remove any blanks that shouldn't be there, and validate the other fields
String[] newCharacter = newLine.toString().split(":"); String[] newCharacter = newLine.toString().split(":");
boolean corrupted = false; boolean corrupted = false;
for (int i = 0; i < newCharacter.length; i++) { for (int i = 0; i < newCharacter.length; i++) {
if (newCharacter[i].isEmpty() && !(i == 2 || i == 3 || i == 23 || i == 33)) { if (newCharacter[i].isEmpty() && !(i == 2 || i == 3 || i == 23 || i == 33 || i == 41)) {
corrupted = true; corrupted = true;
if (newCharacter.length != 41) { if (newCharacter.length != 42) {
newCharacter = (String[]) ArrayUtils.remove(newCharacter, i); newCharacter = (String[]) ArrayUtils.remove(newCharacter, i);
} }
else { else {
@@ -729,7 +979,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
newCharacter[i] = Config.getInstance().getMobHealthbarDefault().toString(); newCharacter[i] = Config.getInstance().getMobHealthbarDefault().toString();
} }
if (!StringUtils.isInt(newCharacter[i]) && !(i == 0 || i == 2 || i == 3 || i == 23 || i == 33 || i == 38)) { if (!StringUtils.isInt(newCharacter[i]) && !(i == 0 || i == 2 || i == 3 || i == 23 || i == 33 || i == 38 || i == 41)) {
corrupted = true; corrupted = true;
newCharacter[i] = "0"; newCharacter[i] = "0";
} }
@@ -740,7 +990,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
} }
if (oldVersion != null) { if (oldVersion != null) {
mcMMO.p.debug("Updating database line for player " + character[0] + " from before version " + oldVersion); mcMMO.p.debug("Updating database line from before version " + oldVersion + " for player " + character[0]);
} }
if (corrupted || oldVersion != null) { if (corrupted || oldVersion != null) {
@@ -759,8 +1009,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString()); mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
} }
finally { finally {
tryClose(in); if (in != null) {
tryClose(out); try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
} }
} }
@@ -785,18 +1049,6 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
} }
} }
private void tryClose(Closeable c) {
if (c == null) {
return;
}
try {
c.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
private Integer getPlayerRank(String playerName, List<PlayerStat> statsList) { private Integer getPlayerRank(String playerName, List<PlayerStat> statsList) {
if (statsList == null) { if (statsList == null) {
return null; return null;
@@ -829,8 +1081,8 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
private PlayerProfile loadFromLine(String[] character) { private PlayerProfile loadFromLine(String[] character) {
Map<SkillType, Integer> skills = getSkillMapFromLine(character); // Skill levels Map<SkillType, Integer> skills = getSkillMapFromLine(character); // Skill levels
Map<SkillType, Float> skillsXp = new HashMap<SkillType, Float>(); // Skill & XP Map<SkillType, Float> skillsXp = new EnumMap<SkillType, Float>(SkillType.class); // Skill & XP
Map<AbilityType, Integer> skillsDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown Map<AbilityType, Integer> skillsDATS = new EnumMap<AbilityType, Integer>(AbilityType.class); // Ability & Cooldown
MobHealthbarType mobHealthbarType; MobHealthbarType mobHealthbarType;
// TODO on updates, put new values in a try{} ? // TODO on updates, put new values in a try{} ?
@@ -869,11 +1121,19 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mobHealthbarType = Config.getInstance().getMobHealthbarDefault(); mobHealthbarType = Config.getInstance().getMobHealthbarDefault();
} }
return new PlayerProfile(character[0], skills, skillsXp, skillsDATS, mobHealthbarType); UUID uuid;
try {
uuid = UUID.fromString(character[41]);
}
catch (Exception e) {
uuid = null;
}
return new PlayerProfile(character[0], uuid, skills, skillsXp, skillsDATS, mobHealthbarType);
} }
private Map<SkillType, Integer> getSkillMapFromLine(String[] character) { private Map<SkillType, Integer> getSkillMapFromLine(String[] character) {
Map<SkillType, Integer> skills = new HashMap<SkillType, Integer>(); // Skill & Level Map<SkillType, Integer> skills = new EnumMap<SkillType, Integer>(SkillType.class); // Skill & Level
skills.put(SkillType.TAMING, Integer.valueOf(character[24])); skills.put(SkillType.TAMING, Integer.valueOf(character[24]));
skills.put(SkillType.MINING, Integer.valueOf(character[1])); skills.put(SkillType.MINING, Integer.valueOf(character[1]));
@@ -895,4 +1155,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
public DatabaseType getDatabaseType() { public DatabaseType getDatabaseType() {
return DatabaseType.FLATFILE; return DatabaseType.FLATFILE;
} }
@Override
public void onDisable() { }
} }

View File

@@ -7,5 +7,6 @@ public enum UpgradeType {
ADD_MOB_HEALTHBARS, ADD_MOB_HEALTHBARS,
DROP_SQL_PARTY_NAMES, DROP_SQL_PARTY_NAMES,
DROP_SPOUT, DROP_SPOUT,
ADD_ALCHEMY; ADD_ALCHEMY,
ADD_UUIDS;
} }

View File

@@ -3,6 +3,7 @@ package com.gmail.nossr50.datatypes.player;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
@@ -92,15 +93,20 @@ public class McMMOPlayer {
private boolean isUsingUnarmed; private boolean isUsingUnarmed;
private final FixedMetadataValue playerMetadata; private final FixedMetadataValue playerMetadata;
public McMMOPlayer(Player player) { public McMMOPlayer(Player player, PlayerProfile profile) {
String playerName = player.getName(); String playerName = player.getName();
UUID uuid = player.getUniqueId();
this.player = player; this.player = player;
playerMetadata = new FixedMetadataValue(mcMMO.p, playerName); playerMetadata = new FixedMetadataValue(mcMMO.p, playerName);
profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, true); this.profile = profile;
party = PartyManager.getPlayerParty(playerName); party = PartyManager.getPlayerParty(playerName);
ptpRecord = new PartyTeleportRecord(); ptpRecord = new PartyTeleportRecord();
if (profile.getUniqueId() == null) {
profile.setUniqueId(uuid);
}
/* /*
* I'm using this method because it makes code shorter and safer (we don't have to add all SkillTypes manually), * I'm using this method because it makes code shorter and safer (we don't have to add all SkillTypes manually),
* but I actually have no idea about the performance impact, if there is any. * but I actually have no idea about the performance impact, if there is any.
@@ -124,69 +130,6 @@ public class McMMOPlayer {
for (ToolType toolType : ToolType.values()) { for (ToolType toolType : ToolType.values()) {
toolMode.put(toolType, false); toolMode.put(toolType, false);
} }
if (!profile.isLoaded()) {
mcMMO.p.getLogger().warning("Unable to load the PlayerProfile for " + playerName + ". Will retry over the next several seconds.");
new RetryProfileLoadingTask().runTaskTimerAsynchronously(mcMMO.p, 11L, 31L);
}
}
private class RetryProfileLoadingTask extends BukkitRunnable {
private static final int MAX_TRIES = 5;
private final String playerName = McMMOPlayer.this.player.getName();
private int attempt = 0;
// WARNING: ASYNC TASK
// DO NOT MODIFY THE McMMOPLAYER FROM THIS CODE
@Override
public void run() {
// Quit if they logged out
if (!player.isOnline()) {
mcMMO.p.getLogger().info("Aborting profile loading recovery for " + playerName + " - player logged out");
this.cancel();
return;
}
// Send the message that we're doing the recovery
if (attempt == 0) {
player.sendMessage(LocaleLoader.getString("Recovery.Notice"));
}
// Increment attempt counter and try
attempt++;
PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, true);
// If successful, schedule the apply
if (profile.isLoaded()) {
new ApplySuccessfulProfile(profile).runTask(mcMMO.p);
player.sendMessage(LocaleLoader.getString("Recovery.Success"));
this.cancel();
return;
}
// If we've failed five times, give up
if (attempt >= MAX_TRIES) {
mcMMO.p.getLogger().severe("Giving up on attempting to load the PlayerProfile for " + playerName);
mcMMO.p.getServer().broadcast(LocaleLoader.getString("Recovery.AdminFailureNotice", playerName), Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
player.sendMessage(LocaleLoader.getString("Recovery.Failure").split("\n"));
this.cancel();
return;
}
}
}
private class ApplySuccessfulProfile extends BukkitRunnable {
private final PlayerProfile profile;
private ApplySuccessfulProfile(PlayerProfile profile) {
this.profile = profile;
}
// Synchronized task
// No database access permitted
@Override
public void run() {
McMMOPlayer.this.profile = profile;
}
} }
public AcrobaticsManager getAcrobaticsManager() { public AcrobaticsManager getAcrobaticsManager() {

View File

@@ -3,6 +3,7 @@ package com.gmail.nossr50.datatypes.player;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
@@ -19,6 +20,7 @@ import com.google.common.collect.ImmutableMap;
public class PlayerProfile { public class PlayerProfile {
private final String playerName; private final String playerName;
private UUID uuid;
private boolean loaded; private boolean loaded;
private volatile boolean changed; private volatile boolean changed;
@@ -30,7 +32,13 @@ public class PlayerProfile {
private final Map<SkillType, Float> skillsXp = new HashMap<SkillType, Float>(); // Skill & XP private final Map<SkillType, Float> skillsXp = new HashMap<SkillType, Float>(); // Skill & XP
private final Map<AbilityType, Integer> abilityDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown private final Map<AbilityType, Integer> abilityDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown
@Deprecated
public PlayerProfile(String playerName) { public PlayerProfile(String playerName) {
this(playerName, null);
}
public PlayerProfile(String playerName, UUID uuid) {
this.uuid = uuid;
this.playerName = playerName; this.playerName = playerName;
mobHealthbarType = Config.getInstance().getMobHealthbarDefault(); mobHealthbarType = Config.getInstance().getMobHealthbarDefault();
@@ -45,13 +53,20 @@ public class PlayerProfile {
} }
} }
@Deprecated
public PlayerProfile(String playerName, boolean isLoaded) { public PlayerProfile(String playerName, boolean isLoaded) {
this(playerName); this(playerName);
this.loaded = isLoaded; this.loaded = isLoaded;
} }
public PlayerProfile(String playerName, Map<SkillType, Integer> levelData, Map<SkillType, Float> xpData, Map<AbilityType, Integer> cooldownData, MobHealthbarType mobHealthbarType) { public PlayerProfile(String playerName, UUID uuid, boolean isLoaded) {
this(playerName, uuid);
this.loaded = isLoaded;
}
public PlayerProfile(String playerName, UUID uuid, Map<SkillType, Integer> levelData, Map<SkillType, Float> xpData, Map<AbilityType, Integer> cooldownData, MobHealthbarType mobHealthbarType) {
this.playerName = playerName; this.playerName = playerName;
this.uuid = uuid;
this.mobHealthbarType = mobHealthbarType; this.mobHealthbarType = mobHealthbarType;
skills.putAll(levelData); skills.putAll(levelData);
@@ -71,11 +86,11 @@ public class PlayerProfile {
} }
// TODO should this part be synchronized? // TODO should this part be synchronized?
PlayerProfile profileCopy = new PlayerProfile(playerName, ImmutableMap.copyOf(skills), ImmutableMap.copyOf(skillsXp), ImmutableMap.copyOf(abilityDATS), mobHealthbarType); PlayerProfile profileCopy = new PlayerProfile(playerName, uuid, ImmutableMap.copyOf(skills), ImmutableMap.copyOf(skillsXp), ImmutableMap.copyOf(abilityDATS), mobHealthbarType);
changed = !mcMMO.getDatabaseManager().saveUser(profileCopy); changed = !mcMMO.getDatabaseManager().saveUser(profileCopy);
if (changed) { if (changed) {
mcMMO.p.getLogger().warning("PlayerProfile for " + playerName + " failed to save"); mcMMO.p.getLogger().warning("PlayerProfile saving failed for player: " + playerName + " " + uuid);
} }
} }
@@ -83,6 +98,16 @@ public class PlayerProfile {
return playerName; return playerName;
} }
public UUID getUniqueId() {
return uuid;
}
public void setUniqueId(UUID uuid) {
changed = true;
this.uuid = uuid;
}
public boolean isLoaded() { public boolean isLoaded() {
return loaded; return loaded;
} }

View File

@@ -41,7 +41,7 @@ import com.gmail.nossr50.datatypes.skills.AbilityType;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.ShareHandler; import com.gmail.nossr50.party.ShareHandler;
import com.gmail.nossr50.runnables.commands.McScoreboardKeepTask; import com.gmail.nossr50.runnables.player.PlayerProfileLoadingTask;
import com.gmail.nossr50.runnables.skills.BleedTimerTask; import com.gmail.nossr50.runnables.skills.BleedTimerTask;
import com.gmail.nossr50.skills.fishing.FishingManager; import com.gmail.nossr50.skills.fishing.FishingManager;
import com.gmail.nossr50.skills.herbalism.HerbalismManager; import com.gmail.nossr50.skills.herbalism.HerbalismManager;
@@ -387,9 +387,7 @@ public class PlayerListener implements Listener {
return; return;
} }
McMMOPlayer mcMMOPlayer = UserManager.addUser(player); new PlayerProfileLoadingTask(player).runTaskTimerAsynchronously(mcMMO.p, 1, 20); // 1 Tick delay to ensure the player is marked as online before we begin loading
mcMMOPlayer.actualizeRespawnATS();
ScoreboardManager.setupPlayer(player);
if (Config.getInstance().getMOTDEnabled() && Permissions.motd(player)) { if (Config.getInstance().getMOTDEnabled() && Permissions.motd(player)) {
Motd.displayAll(player); Motd.displayAll(player);
@@ -403,11 +401,6 @@ public class PlayerListener implements Listener {
player.sendMessage(LocaleLoader.getString("UpdateChecker.Outdated")); player.sendMessage(LocaleLoader.getString("UpdateChecker.Outdated"));
player.sendMessage(LocaleLoader.getString("UpdateChecker.NewAvailable")); player.sendMessage(LocaleLoader.getString("UpdateChecker.NewAvailable"));
} }
if (Config.getInstance().getShowStatsAfterLogin()) {
ScoreboardManager.enablePlayerStatsScoreboard(player);
new McScoreboardKeepTask(player).runTaskLater(mcMMO.p, 1 * Misc.TICK_CONVERSION_FACTOR);
}
} }
/** /**

View File

@@ -38,6 +38,7 @@ import com.gmail.nossr50.runnables.UpdaterResultAsyncTask;
import com.gmail.nossr50.runnables.backups.CleanBackupsTask; import com.gmail.nossr50.runnables.backups.CleanBackupsTask;
import com.gmail.nossr50.runnables.database.UserPurgeTask; import com.gmail.nossr50.runnables.database.UserPurgeTask;
import com.gmail.nossr50.runnables.party.PartyAutoKickTask; import com.gmail.nossr50.runnables.party.PartyAutoKickTask;
import com.gmail.nossr50.runnables.player.PlayerProfileLoadingTask;
import com.gmail.nossr50.runnables.player.PowerLevelUpdatingTask; import com.gmail.nossr50.runnables.player.PowerLevelUpdatingTask;
import com.gmail.nossr50.runnables.skills.BleedTimerTask; import com.gmail.nossr50.runnables.skills.BleedTimerTask;
import com.gmail.nossr50.skills.alchemy.Alchemy; import com.gmail.nossr50.skills.alchemy.Alchemy;
@@ -167,8 +168,7 @@ public class mcMMO extends JavaPlugin {
holidayManager = new HolidayManager(); holidayManager = new HolidayManager();
for (Player player : getServer().getOnlinePlayers()) { for (Player player : getServer().getOnlinePlayers()) {
UserManager.addUser(player); // In case of reload add all users back into UserManager new PlayerProfileLoadingTask(player).runTaskTimerAsynchronously(mcMMO.p, 1, 20); // 1 Tick delay to ensure the player is marked as online before we begin loading
ScoreboardManager.setupPlayer(player);
} }
debug("Version " + getDescription().getVersion() + " is enabled!"); debug("Version " + getDescription().getVersion() + " is enabled!");
@@ -242,6 +242,7 @@ public class mcMMO extends JavaPlugin {
} }
} }
databaseManager.onDisable();
debug("Was disabled."); // How informative! debug("Was disabled."); // How informative!
} }

View File

@@ -1,39 +0,0 @@
package com.gmail.nossr50.runnables.database;
import java.lang.ref.WeakReference;
import org.bukkit.scheduler.BukkitRunnable;
import com.gmail.nossr50.database.SQLDatabaseManager;
/**
* This task is in charge of sending a MySQL ping over the MySQL connection
* every hour to prevent the connection from timing out and losing players'
* data when they join.
* <p/>
* A WeakReference is used to keep the database instance, because
* {@link com.gmail.nossr50.commands.database.ConvertDatabaseCommand database
* conversion} may create a SQLDatabaseManager that will be thrown out. If a
* normal reference was used, the conversion would cause a combined data and
* resource leak through this task.
*/
public class SQLDatabaseKeepaliveTask extends BukkitRunnable {
WeakReference<SQLDatabaseManager> databaseInstance;
public SQLDatabaseKeepaliveTask(SQLDatabaseManager dbman) {
databaseInstance = new WeakReference<SQLDatabaseManager>(dbman);
}
public void run() {
SQLDatabaseManager dbman = databaseInstance.get();
if (dbman != null) {
dbman.checkConnected();
}
else {
// This happens when the database was started for a conversion,
// or discarded by its creator for any other reason. If this code
// was not present, we would leak the connection resources.
this.cancel();
}
}
}

View File

@@ -1,22 +0,0 @@
package com.gmail.nossr50.runnables.database;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.database.SQLDatabaseManager;
import com.gmail.nossr50.util.player.UserManager;
public class SQLReconnectTask extends BukkitRunnable {
@Override
public void run() {
if (((SQLDatabaseManager) mcMMO.getDatabaseManager()).checkConnected()) {
UserManager.saveAll(); // Save all profiles
UserManager.clearAll(); // Clear the profiles
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
UserManager.addUser(player); // Add in new profiles, forcing them to 'load' again from MySQL
}
}
}
}

View File

@@ -0,0 +1,73 @@
package com.gmail.nossr50.runnables.database;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.scheduler.BukkitRunnable;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.HiddenConfig;
import com.gmail.nossr50.database.DatabaseManager;
import com.gmail.nossr50.datatypes.database.UpgradeType;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.uuid.UUIDFetcher;
public class UUIDUpdateAsyncTask extends BukkitRunnable {
private mcMMO plugin;
private static final int MAX_LOOKUP = Math.max(HiddenConfig.getInstance().getUUIDConvertAmount(), 100);
private List<String> userNames;
private int size;
private int checkedUsers;
private long startMillis;
public UUIDUpdateAsyncTask(mcMMO plugin, List<String> userNames) {
this.plugin = plugin;
this.userNames = userNames;
this.checkedUsers = 0;
this.startMillis = System.currentTimeMillis();
}
@Override
public void run() {
size = userNames.size();
plugin.getLogger().info("Starting to check and update UUIDs, total amount of users: " + size);
List<String> userNamesSection;
Map<String, UUID> fetchedUUIDs = new HashMap<String, UUID>();
while (size != 0) {
if (size > MAX_LOOKUP) {
userNamesSection = userNames.subList(size - MAX_LOOKUP, size);
size -= MAX_LOOKUP;
}
else {
userNamesSection = userNames.subList(0, size);
size = 0;
}
try {
fetchedUUIDs.putAll(new UUIDFetcher(userNamesSection).call());
}
catch (Exception e) {
plugin.getLogger().severe("Unable to fetch UUIDs!");
return;
}
checkedUsers += userNamesSection.size();
userNamesSection.clear();
size = userNames.size();
Misc.printProgress(checkedUsers, DatabaseManager.progressInterval, startMillis);
}
if (mcMMO.getDatabaseManager().saveUserUUIDs(fetchedUUIDs)) {
mcMMO.getUpgradeManager().setUpgradeCompleted(UpgradeType.ADD_UUIDS);
plugin.getLogger().info("UUID upgrade completed!");
}
}
}

View File

@@ -0,0 +1,108 @@
package com.gmail.nossr50.runnables.player;
import java.util.concurrent.locks.ReentrantLock;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.runnables.commands.McScoreboardKeepTask;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.scoreboards.ScoreboardManager;
public class PlayerProfileLoadingTask extends BukkitRunnable {
private static final int MAX_TRIES = 5;
private final Player player;
private int attempt = 0;
private ReentrantLock lock = new ReentrantLock();
private boolean cancelled = false;
public PlayerProfileLoadingTask(Player player) {
this.player = player;
}
// WARNING: ASYNC TASK
// DO NOT MODIFY THE McMMOPLAYER FROM THIS CODE
@Override
public void run() {
lock.lock();
if (this.cancelled) {
return;
}
// Quit if they logged out
if (!player.isOnline()) {
mcMMO.p.getLogger().info("Aborting profile loading recovery for " + player.getName() + " - player logged out");
this.cancel();
cancelled = true;
lock.unlock();
return;
}
// Increment attempt counter and try
attempt++;
PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(player.getName(), player.getUniqueId(), true);
// If successful, schedule the apply
if (profile.isLoaded()) {
new ApplySuccessfulProfile(profile).runTask(mcMMO.p);
this.cancel();
cancelled = true;
lock.unlock();
return;
}
// If we've failed five times, give up
if (attempt >= MAX_TRIES) {
mcMMO.p.getLogger().severe("Giving up on attempting to load the PlayerProfile for " + player.getName());
mcMMO.p.getServer().broadcast(LocaleLoader.getString("Profile.Loading.AdminFailureNotice", player.getName()), Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
player.sendMessage(LocaleLoader.getString("Profile.Loading.Failure").split("\n"));
this.cancel();
cancelled = true;
lock.unlock();
return;
}
lock.unlock();
}
private class ApplySuccessfulProfile extends BukkitRunnable {
private final PlayerProfile profile;
private ApplySuccessfulProfile(PlayerProfile profile) {
this.profile = profile;
}
// Synchronized task
// No database access permitted
@Override
public void run() {
if (!player.isOnline()) {
mcMMO.p.getLogger().info("Aborting profile loading recovery for " + player.getName() + " - player logged out");
return;
}
McMMOPlayer mcMMOPlayer = new McMMOPlayer(player, profile);
UserManager.track(mcMMOPlayer);
mcMMOPlayer.actualizeRespawnATS();
ScoreboardManager.setupPlayer(player);
if (Config.getInstance().getShowProfileLoadedMessage()) {
player.sendMessage(LocaleLoader.getString("Profile.Loading.Success"));
}
if (Config.getInstance().getShowStatsAfterLogin()) {
ScoreboardManager.enablePlayerStatsScoreboard(player);
new McScoreboardKeepTask(player).runTaskLater(mcMMO.p, 1 * Misc.TICK_CONVERSION_FACTOR);
}
}
}
}

View File

@@ -13,6 +13,7 @@ import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.events.items.McMMOItemSpawnEvent; import com.gmail.nossr50.events.items.McMMOItemSpawnEvent;
import com.gmail.nossr50.runnables.player.PlayerProfileLoadingTask;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
public final class Misc { public final class Misc {
@@ -112,7 +113,7 @@ public final class Misc {
if (player != null) { if (player != null) {
UserManager.remove(player); UserManager.remove(player);
UserManager.addUser(player); new PlayerProfileLoadingTask(player).runTaskTimerAsynchronously(mcMMO.p, 1, 20); // 1 Tick delay to ensure the player is marked as online before we begin loading
} }
} }

View File

@@ -18,16 +18,12 @@ public final class UserManager {
private UserManager() {} private UserManager() {}
/** /**
* Add a new user. * Track a new user.
* *
* @param player The player to create a user record for * @param mcMMOPlayer the player profile to start tracking
* @return the player's {@link McMMOPlayer} object
*/ */
public static McMMOPlayer addUser(Player player) { public static void track(McMMOPlayer mcMMOPlayer) {
McMMOPlayer mcMMOPlayer = new McMMOPlayer(player); mcMMOPlayer.getPlayer().setMetadata(mcMMO.playerDataKey, new FixedMetadataValue(mcMMO.p, mcMMOPlayer));
player.setMetadata(mcMMO.playerDataKey, new FixedMetadataValue(mcMMO.p, mcMMOPlayer));
return mcMMOPlayer;
} }
/** /**

View File

@@ -0,0 +1,100 @@
package com.gmail.nossr50.util.uuid;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import com.google.common.collect.ImmutableList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class UUIDFetcher implements Callable<Map<String, UUID>> {
private static final double PROFILES_PER_REQUEST = 100;
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
private final JSONParser jsonParser = new JSONParser();
private final List<String> names;
private final boolean rateLimiting;
public UUIDFetcher(List<String> names, boolean rateLimiting) {
this.names = ImmutableList.copyOf(names);
this.rateLimiting = rateLimiting;
}
public UUIDFetcher(List<String> names) {
this(names, true);
}
public Map<String, UUID> call() throws Exception {
Map<String, UUID> uuidMap = new HashMap<String, UUID>();
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
for (int i = 0; i < requests; i++) {
HttpURLConnection connection = createConnection();
String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
writeBody(connection, body);
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
for (Object profile : array) {
JSONObject jsonProfile = (JSONObject) profile;
String id = (String) jsonProfile.get("id");
String name = (String) jsonProfile.get("name");
UUID uuid = UUIDFetcher.getUUID(id);
uuidMap.put(name, uuid);
}
if (rateLimiting && i != requests - 1) {
Thread.sleep(100L);
}
}
return uuidMap;
}
private static void writeBody(HttpURLConnection connection, String body) throws Exception {
OutputStream stream = connection.getOutputStream();
stream.write(body.getBytes());
stream.flush();
stream.close();
}
private static HttpURLConnection createConnection() throws Exception {
URL url = new URL(PROFILE_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
return connection;
}
private static UUID getUUID(String id) {
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32));
}
public static byte[] toBytes(UUID uuid) {
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits());
return byteBuffer.array();
}
public static UUID fromBytes(byte[] array) {
if (array.length != 16) {
throw new IllegalArgumentException("Illegal byte array length: " + array.length);
}
ByteBuffer byteBuffer = ByteBuffer.wrap(array);
long mostSignificant = byteBuffer.getLong();
long leastSignificant = byteBuffer.getLong();
return new UUID(mostSignificant, leastSignificant);
}
public static UUID getUUIDOf(String name) throws Exception {
return new UUIDFetcher(Arrays.asList(name)).call().get(name);
}
}

View File

@@ -10,6 +10,8 @@
General: General:
Locale: en_US Locale: en_US
MOTD_Enabled: true MOTD_Enabled: true
# Send a message to the player when his profile was successfully loaded
Show_Profile_Loaded: false
# Amount of time (in minutes) to wait between saves of player information # Amount of time (in minutes) to wait between saves of player information
Save_Interval: 10 Save_Interval: 10
# Allow mcMMO to report on basic anonymous usage # Allow mcMMO to report on basic anonymous usage
@@ -122,6 +124,12 @@ MySQL:
User_Password: UserPassword User_Password: UserPassword
Name: DataBaseName Name: DataBaseName
TablePrefix: mcmmo_ TablePrefix: mcmmo_
# This setting is the max simultaneous mysql connections allowed at a time, needs to be
# high enough to support multiple player logins in quick succession
MaxConnections: 30
# This setting is the max size of the pool of cached connections that we hold available
# at any given time
MaxPoolSize: 20
Server: Server:
Port: 3306 Port: 3306
Address: localhost Address: localhost

View File

@@ -11,3 +11,6 @@ Options:
EnchantmentBuffs: true EnchantmentBuffs: true
# true to enable refreshing of chunks around a player at the end of Super Breaker, Giga Drill Breaker, and Berserk. This should fix blocks being broken client side, but not server-side # true to enable refreshing of chunks around a player at the end of Super Breaker, Giga Drill Breaker, and Berserk. This should fix blocks being broken client side, but not server-side
RefreshChunks: false RefreshChunks: false
# Amount of users to convert every interval
UUIDConvertAmount: 100

View File

@@ -960,7 +960,6 @@ Scoreboard.Misc.Cooldown=[[LIGHT_PURPLE]]Cooldown
Scoreboard.Misc.Overall=[[GOLD]]Overall Scoreboard.Misc.Overall=[[GOLD]]Overall
#DATABASE RECOVERY #DATABASE RECOVERY
Recovery.Notice=[[RED]]Notice: mcMMO was [[DARK_RED]]unable to load your data.[[RED]] Retrying 5 times... Profile.Loading.Success=[[GREEN]]Your mcMMO profile has been loaded.
Recovery.Success=[[GREEN]]Success! Your mcMMO data was loaded. Profile.Loading.Failure=[[RED]]mcMMO still cannot load your data. You may want to [[AQUA]]contact the server owner.\n[[YELLOW]]You can still play on the server, but you will have [[BOLD]]no mcMMO levels[[YELLOW]] and any XP you get [[BOLD]]will not be saved[[YELLOW]].
Recovery.Failure=[[RED]]mcMMO still cannot load your data. You may want to [[AQUA]]contact the server owner.\n[[YELLOW]]You can still play on the server, but you will have [[BOLD]]no mcMMO levels[[YELLOW]] and any XP you get [[BOLD]]will not be saved[[YELLOW]]. Profile.Loading.AdminFailureNotice=[[DARK_RED]][A][[RED]] mcMMO was unable to load the player data for [[YELLOW]]{0}[[RED]]. [[LIGHT_PURPLE]]Please inspect your database setup.
Recovery.AdminFailureNotice=[[DARK_RED]][A][[RED]] mcMMO was unable to load the player data for [[YELLOW]]{0}[[RED]]. [[LIGHT_PURPLE]]Please inspect your database setup.