mirror of
https://github.com/TotalFreedomMC/TF-Marriage.git
synced 2025-02-05 14:22:45 +00:00
fix chat status & null names
This commit is contained in:
parent
103255bb9b
commit
38344a94ef
9 changed files with 137 additions and 19 deletions
2
pom.xml
2
pom.xml
|
@ -4,7 +4,7 @@
|
|||
|
||||
<groupId>com.lenis0012.bukkit</groupId>
|
||||
<artifactId>marriage2</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<version>2.0.3-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>Marriage</name>
|
||||
<url>http://dev.bukkit.org/server-mods/marriage-reloaded/</url>
|
||||
|
|
|
@ -49,6 +49,13 @@ public interface MPlayer {
|
|||
*/
|
||||
@Nullable
|
||||
MData getMarriage();
|
||||
|
||||
/**
|
||||
* Get the last name the player logged on with.
|
||||
*
|
||||
* @return Last name, can be null
|
||||
*/
|
||||
String getLastName();
|
||||
|
||||
/**
|
||||
* Check if the player is married.
|
||||
|
@ -71,6 +78,13 @@ public interface MPlayer {
|
|||
*/
|
||||
boolean isPriest();
|
||||
|
||||
/**
|
||||
* Set the last name the player logged on with.
|
||||
*
|
||||
* @param name of player
|
||||
*/
|
||||
void setLastName(String name);
|
||||
|
||||
/**
|
||||
* Set whether or not this player is a priest.
|
||||
*
|
||||
|
|
|
@ -1,9 +1,22 @@
|
|||
package com.lenis0012.bukkit.marriage2.internal.data;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class DBUpgrade {
|
||||
private static final int VERSION_ID = 1;
|
||||
private static final int VERSION_ID = 2;
|
||||
|
||||
public int getVersionId() {
|
||||
return VERSION_ID;
|
||||
}
|
||||
|
||||
public void run(Statement statement, int currentVersion, String prefix) throws SQLException {
|
||||
switch(currentVersion) {
|
||||
case 1:
|
||||
statement.execute("ALTER TABLE " + prefix + "players ADD last_name VARCHAR(16);");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import org.bukkit.configuration.file.FileConfiguration;
|
|||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
@ -32,7 +33,7 @@ public class DataConverter {
|
|||
|
||||
public void convert() {
|
||||
long lastMessage = 0;
|
||||
String[] files = dir.list();
|
||||
File[] files = dir.listFiles();
|
||||
int totalFiles = files.length;
|
||||
core.getLogger().log(Level.INFO, "Converting " + totalFiles + " old database entries...");
|
||||
core.getLogger().log(Level.INFO, "Retrieving UUIDs...");
|
||||
|
@ -41,7 +42,20 @@ public class DataConverter {
|
|||
Map<String, UUID> uuidMap = Maps.newHashMap();
|
||||
UUIDFetcher uuidFetcher = new UUIDFetcher(new ArrayList<String>());
|
||||
for(int completed = 0; completed < totalFiles; completed++) {
|
||||
String name = files[completed].replace(".yml", "");
|
||||
File file = files[completed];
|
||||
String name = file.getName().replace(".yml", "");
|
||||
if(files.length > 50000) {
|
||||
// Over 500 requests, check for marriage
|
||||
try {
|
||||
FileConfiguration cnf = YamlConfiguration.loadConfiguration(file);
|
||||
cnf.load(file);
|
||||
String partner = cnf.getString("partner");
|
||||
if(partner == null) continue;
|
||||
} catch(Exception e) {
|
||||
continue; // skip
|
||||
}
|
||||
}
|
||||
|
||||
uuidFetcher.addName(name);
|
||||
if(uuidFetcher.size() >= 100 || completed >= totalFiles - 1) {
|
||||
try {
|
||||
|
|
|
@ -107,7 +107,11 @@ public class DataManager {
|
|||
if(result.next()) {
|
||||
int dbVersion = result.getInt("version_id");
|
||||
if(dbVersion < upgrade.getVersionId()) {
|
||||
// TODO: Apply database upgrade.
|
||||
upgrade.run(statement, dbVersion, prefix);
|
||||
PreparedStatement ps = connection.prepareStatement("UPDATE " + prefix + "version SET version_id=? WHERE version_id=?;");
|
||||
ps.setInt(1, upgrade.getVersionId());
|
||||
ps.setInt(2, dbVersion);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
} else {
|
||||
statement.executeUpdate(String.format("INSERT INTO %sversion (version_id) VALUES(%s);", prefix, upgrade.getVersionId()));
|
||||
|
@ -208,7 +212,8 @@ public class DataManager {
|
|||
if(result.next()) {
|
||||
// Already in database (update)
|
||||
PreparedStatement ps2 = connection.prepareStatement(String.format(
|
||||
"UPDATE %splayers SET gender=?,priest=?,lastlogin=? WHERE unique_user_id=?;", prefix));
|
||||
"UPDATE %splayers SET last_name=?,gender=?,priest=?,lastlogin=? WHERE unique_user_id=?;", prefix));
|
||||
ps2.setString(1, player.getLastName());
|
||||
ps2.setString(1, player.getGender().toString());
|
||||
ps2.setBoolean(2, player.isPriest());
|
||||
ps2.setLong(3, System.currentTimeMillis());
|
||||
|
@ -218,7 +223,7 @@ public class DataManager {
|
|||
} else {
|
||||
// Not in database yet
|
||||
PreparedStatement ps2 = connection.prepareStatement(String.format(
|
||||
"INSERT INTO %splayers (unique_user_id,gender,priest,lastlogin) VALUES(?,?,?,?);", prefix));
|
||||
"INSERT INTO %splayers (unique_user_id,last_name,gender,priest,lastlogin) VALUES(?,?,?,?,?);", prefix));
|
||||
player.save(ps2);
|
||||
ps2.executeUpdate();
|
||||
ps2.close();
|
||||
|
@ -323,10 +328,10 @@ public class DataManager {
|
|||
}
|
||||
ps.close();
|
||||
|
||||
return new ListQuery(pages, page, list);
|
||||
return new ListQuery(this, pages, page, list);
|
||||
} catch (SQLException e) {
|
||||
core.getLogger().log(Level.WARNING, "Failed to load marriage list", e);
|
||||
return new ListQuery(0, 0, new ArrayList<MData>());
|
||||
return new ListQuery(this, 0, 0, new ArrayList<MData>());
|
||||
} finally {
|
||||
supplier.finish();
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ import com.lenis0012.bukkit.marriage2.misc.Cooldown;
|
|||
public class MarriagePlayer implements MPlayer {
|
||||
private final Cooldown<UUID> requests;
|
||||
private final UUID uuid;
|
||||
private String lastName;
|
||||
private MData marriage;
|
||||
private Gender gender = Gender.UNKNOWN;
|
||||
private boolean inChat;
|
||||
|
@ -28,6 +29,7 @@ public class MarriagePlayer implements MPlayer {
|
|||
public MarriagePlayer(UUID uuid, ResultSet data) throws SQLException {
|
||||
this.uuid = uuid;
|
||||
if(data.next()) {
|
||||
this.lastName = data.getString("last_name");
|
||||
this.gender = Gender.valueOf(data.getString("gender"));
|
||||
this.priest = data.getBoolean("priest");
|
||||
this.lastLogout = data.getLong("lastlogin");
|
||||
|
@ -42,9 +44,18 @@ public class MarriagePlayer implements MPlayer {
|
|||
|
||||
void save(PreparedStatement ps) throws SQLException {
|
||||
ps.setString(1, uuid.toString());
|
||||
ps.setString(2, gender.toString());
|
||||
ps.setBoolean(3, priest);
|
||||
ps.setLong(4, System.currentTimeMillis());
|
||||
ps.setString(2, lastName);
|
||||
ps.setString(3, gender.toString());
|
||||
ps.setBoolean(4, priest);
|
||||
ps.setLong(5, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String name) {
|
||||
this.lastName = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -48,7 +48,9 @@ public class ChatListener implements Listener {
|
|||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onPlayerChatLate(AsyncPlayerChatEvent event) {
|
||||
final String format = event.getFormat();
|
||||
if(format.contains("{marriage_status}")) {
|
||||
final Player player = event.getPlayer();
|
||||
final MPlayer mplayer = core.getMPlayer(player.getUniqueId());
|
||||
if(format.contains("{marriage_status}") && mplayer.isMarried()) {
|
||||
String status = Settings.CHAT_FORMAT.value().replace("{heart}", "\u2764");
|
||||
status = ChatColor.translateAlternateColorCodes('&', status);
|
||||
event.setFormat(format.replace("{marriage_status}", status));
|
||||
|
|
|
@ -32,7 +32,9 @@ public class DatabaseListener implements Listener {
|
|||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onPlayerLogin(AsyncPlayerPreLoginEvent event) {
|
||||
if(event.getLoginResult() == Result.ALLOWED) {
|
||||
cache.put(event.getUniqueId(), core.getDataManager().loadPlayer(event.getUniqueId()));
|
||||
MarriagePlayer player = core.getDataManager().loadPlayer(event.getUniqueId());
|
||||
player.setLastName(event.getName());
|
||||
cache.put(event.getUniqueId(), player);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,7 +50,9 @@ public class DatabaseListener implements Listener {
|
|||
// Something went wrong (unusually long login?)
|
||||
core.getLogger().log(Level.WARNING, "Player " + event.getPlayer().getName() + " was not in cache");
|
||||
core.getLogger().log(Level.INFO, "If this message shows often, report to dev");
|
||||
core.setMPlayer(userId, core.getDataManager().loadPlayer(userId));
|
||||
player = core.getDataManager().loadPlayer(userId);
|
||||
player.setLastName(event.getPlayer().getName());
|
||||
core.setMPlayer(userId, player);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
|
|
@ -1,7 +1,19 @@
|
|||
package com.lenis0012.bukkit.marriage2.misc;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.lenis0012.bukkit.marriage2.internal.data.DataManager;
|
||||
import com.lenis0012.bukkit.marriage2.internal.data.MarriagePlayer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
@ -12,14 +24,21 @@ import com.lenis0012.bukkit.marriage2.MData;
|
|||
import com.lenis0012.bukkit.marriage2.internal.MarriagePlugin;
|
||||
|
||||
public class ListQuery {
|
||||
private static final JsonParser JSON_PARSER = new JsonParser();
|
||||
|
||||
private final int pages;
|
||||
private final int page;
|
||||
private final List<MData> marriages;
|
||||
private final Map<UUID, String> names = Maps.newHashMap();
|
||||
|
||||
public ListQuery(int pages, int page, List<MData> marriages) {
|
||||
public ListQuery(DataManager db, int pages, int page, List<MData> marriages) {
|
||||
this.pages = pages;
|
||||
this.page = page;
|
||||
this.marriages = marriages;
|
||||
for(MData marriage : marriages) {
|
||||
names.put(marriage.getPlayer1Id(), getName(db, marriage.getPlayer1Id()));
|
||||
names.put(marriage.getPllayer2Id(), getName(db, marriage.getPllayer2Id()));
|
||||
}
|
||||
}
|
||||
|
||||
public void send(final CommandSender to) {
|
||||
|
@ -29,9 +48,7 @@ public class ListQuery {
|
|||
to.sendMessage(ChatColor.GOLD + ChatColor.BOLD.toString() + "Married players:");
|
||||
to.sendMessage(ChatColor.GOLD + "Page " + (page + 1) + "/" + pages);
|
||||
for(MData data : marriages) {
|
||||
OfflinePlayer player1 = Bukkit.getOfflinePlayer(data.getPlayer1Id());
|
||||
OfflinePlayer player2 = Bukkit.getOfflinePlayer(data.getPllayer2Id());
|
||||
to.sendMessage(ChatColor.GREEN + player1.getName() + " + " + player2.getName());
|
||||
to.sendMessage(ChatColor.GREEN + names.get(data.getPlayer1Id()) + " + " + names.get(data.getPllayer2Id()));
|
||||
}
|
||||
}
|
||||
}.runTask(MarriagePlugin.getInstance().getPlugin());
|
||||
|
@ -48,4 +65,42 @@ public class ListQuery {
|
|||
public List<MData> getMarriages() {
|
||||
return marriages;
|
||||
}
|
||||
|
||||
private String getName(DataManager db, UUID userId) {
|
||||
// local uuid cache
|
||||
OfflinePlayer op = Bukkit.getOfflinePlayer(userId);
|
||||
if(op != null && op.getName() != null) {
|
||||
return op.getName();
|
||||
}
|
||||
|
||||
// local database
|
||||
MarriagePlayer mp = db.loadPlayer(userId);
|
||||
if(mp.getLastName() != null) {
|
||||
return mp.getLastName();
|
||||
}
|
||||
|
||||
// Last attempt, fetch from mojang.
|
||||
return nameFromMojang(userId);
|
||||
}
|
||||
|
||||
private String nameFromMojang(UUID uuid) {
|
||||
try {
|
||||
URL url = new URL(" https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "") + "/names");
|
||||
URLConnection connection = url.openConnection();
|
||||
connection.addRequestProperty("User-Agent", "Mozilla/4.0");
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
while((line = reader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
|
||||
JsonArray entries = JSON_PARSER.parse(builder.toString()).getAsJsonArray();
|
||||
if(entries.size() == 0) return null; // Fail
|
||||
JsonObject lastEntry = entries.get(entries.size() - 1).getAsJsonObject();
|
||||
return lastEntry.get("name").getAsString();
|
||||
} catch(Exception e) {
|
||||
return null; // Complete failure
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue