Update to V1.7

Bugfix for closed connection with MySQL database
Replacing stacktrace through an message when unable to get an online
UUID
Replacing error on not found name for an UUID through an attempt to get
the players name through Bukkit offline players
Adding files as storage option
This commit is contained in:
GeorgH93 2015-03-31 00:05:06 +02:00
parent e1449a4bfc
commit 57e7382be1
6 changed files with 267 additions and 102 deletions

80
pom.xml
View File

@ -2,54 +2,54 @@
<modelVersion>4.0.0</modelVersion>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>MinePacks</artifactId>
<version>1.6.3</version>
<version>1.7</version>
<repositories>
<repository>
<id>in-project</id>
<name>In Project Repo</name>
<url>file://${project.basedir}/jar_libs</url>
</repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>in-project</id>
<name>In Project Repo</name>
<url>file://${project.basedir}/jar_libs</url>
</repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<build>
<defaultGoal>clean install</defaultGoal>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<defaultGoal>clean install</defaultGoal>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>LATEST</version>
<scope>provided</scope>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>LATEST</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.minecraft</groupId>
<artifactId>Minecraft</artifactId>
<version>1</version>
<scope>system</scope>
<systemPath>${project.basedir}/jar_libs/bukkit.jar</systemPath>
<groupId>net.minecraft</groupId>
<artifactId>Minecraft</artifactId>
<version>1</version>
<scope>system</scope>
<systemPath>${project.basedir}/jar_libs/bukkit.jar</systemPath>
</dependency>
</dependencies>
</project>

View File

@ -42,11 +42,32 @@ public Database(MinePacks mp)
UseUUIDs = plugin.config.UseUUIDs();
}
protected String GetPlayerNameOrUUID(OfflinePlayer player)
{
if(UseUUIDs)
{
if(UseUUIDSeparators)
{
return player.getUniqueId().toString();
}
else
{
return player.getUniqueId().toString().replace("-", "");
}
}
else
{
return player.getName();
}
}
public static Database getDatabase(MinePacks Plugin)
{
switch(Plugin.config.GetDatabaseType().toLowerCase())
{
case "mysql": return new MySQL(Plugin);
case "file":
case "files": return new Files(Plugin);
case "sqlite":
default: return new SQLite(Plugin);
}

View File

@ -0,0 +1,177 @@
/*
* Copyright (C) 2014-2015 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.georgh.MinePacks.Database;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import javax.swing.filechooser.FileFilter;
import org.bukkit.OfflinePlayer;
import at.pcgamingfreaks.georgh.MinePacks.Backpack;
import at.pcgamingfreaks.georgh.MinePacks.MinePacks;
public class Files extends Database
{
private File saveFolder;
private final String ext = ".backpack";
public Files(MinePacks mp)
{
super(mp);
saveFolder = new File(plugin.getDataFolder(), "backpacks");
if(!saveFolder.exists())
{
saveFolder.mkdirs();
}
else
{
CheckFileNames();
}
}
private void CheckFileNames()
{
File[] allFiles = saveFolder.listFiles(new BackpackFileFilter());
int len;
for (File file : allFiles)
{
len = file.getName().length() - ext.length();
if(UseUUIDs) // Use UUID-based saving
{
if(len <= 16) // It's a player name
{
file.renameTo(new File(saveFolder, UUIDConverter.getUUIDFromName(file.getName().substring(0, len), true, UseUUIDSeparators) + ext));
}
else // It's an UUID
{
if(file.getName().contains("-"))
{
if(!UseUUIDSeparators)
{
file.renameTo(new File(saveFolder, file.getName().replaceAll("-", "")));
}
}
else
{
if(UseUUIDSeparators)
{
file.renameTo(new File(saveFolder, file.getName().replaceAll("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})" + ext, "$1-$2-$3-$4-$5" + ext)));
}
}
}
}
else // Use name-based saving
{
if(len > 16) // We only have to rename it if it's name is more than 16 chars (minecraft max player name length)
{
file.renameTo(new File(saveFolder, UUIDConverter.getNameFromUUID(file.getName().substring(0, len)) + ext));
}
}
}
}
private String getFileName(OfflinePlayer player)
{
return GetPlayerNameOrUUID(player) + ext;
}
// DB Functions
public void SaveBackpack(Backpack backpack)
{
File save = new File(saveFolder, getFileName(backpack.getOwner()));
try
{
FileOutputStream fos = new FileOutputStream(save);
fos.write(itsSerializer.getUsedVersion());
fos.write(itsSerializer.Serialize(backpack.getBackpack()));
fos.flush();
fos.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public Backpack LoadBackpack(OfflinePlayer player)
{
File save = new File(saveFolder, getFileName(player));
try
{
if(save.exists())
{
FileInputStream fis = new FileInputStream(save);
int v = fis.read();
byte[] out = new byte[(int)(save.length()-1)];
fis.read(out);
fis.close();
return new Backpack(player, itsSerializer.Deserialize(out, v), -1);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
class BackpackFileFilter extends FileFilter implements FilenameFilter
{
String description, extension;
public BackpackFileFilter()
{
description = "Filters for Minepack backpack files.";
extension = "backpack";
}
@Override
public String getDescription()
{
return description;
}
@Override
public boolean accept(File file)
{
if (!file.isDirectory())
{
String path = file.getAbsolutePath().toLowerCase();
if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.'))
{
return true;
}
}
return false;
}
@Override
public boolean accept(File dir, String name)
{
if ((name.endsWith(extension) && (name.charAt(name.length() - extension.length() - 1)) == '.'))
{
return true;
}
return false;
}
}
}

View File

@ -27,6 +27,7 @@
import java.util.List;
import org.bukkit.entity.Player;
import at.pcgamingfreaks.georgh.MinePacks.MinePacks;
public class MySQL extends SQL
@ -41,6 +42,10 @@ public MySQL(MinePacks mp)
{
CheckUUIDs(); // Check if there are user accounts without UUID
}
// Fire DB request every 10 minutes to keep database connection alive
plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){ @Override public void run() { try { GetConnection().createStatement().execute("SELECT 1"); }
catch(Exception e) { } }}, 600*20, 600*20);
}
protected void CheckUUIDs()
@ -122,7 +127,7 @@ protected Connection GetConnection()
{
if(conn == null || conn.isClosed())
{
conn = DriverManager.getConnection("jdbc:mysql://" + plugin.config.GetMySQLHost() + "/" + plugin.config.GetMySQLDatabase(), plugin.config.GetMySQLUser(), plugin.config.GetMySQLPassword());
conn = DriverManager.getConnection("jdbc:mysql://" + plugin.config.GetMySQLHost() + "/" + plugin.config.GetMySQLDatabase() + "?autoReconnect=true&timeBetweenEvictionRunsMillis=300000&testWhileIdle=true", plugin.config.GetMySQLUser(), plugin.config.GetMySQLPassword());
}
}
catch (SQLException e)
@ -188,21 +193,7 @@ public void run()
PreparedStatement ps;
Connection con = DriverManager.getConnection("jdbc:mysql://" + plugin.config.GetMySQLHost() + "/" + plugin.config.GetMySQLDatabase(), plugin.config.GetMySQLUser(), plugin.config.GetMySQLPassword());;
ps = con.prepareStatement(Query_UpdatePlayerGet);
if(UseUUIDs)
{
if(UseUUIDSeparators)
{
ps.setString(1, player.getUniqueId().toString());
}
else
{
ps.setString(1, player.getUniqueId().toString().replace("-", ""));
}
}
else
{
ps.setString(1, player.getName());
}
ps.setString(1, GetPlayerNameOrUUID(player));
ResultSet rs = ps.executeQuery();
if(rs.next())
{

View File

@ -85,16 +85,8 @@ public void UpdatePlayer(final Player player)
{
try
{
PreparedStatement ps;
ps = GetConnection().prepareStatement("SELECT `player_id` FROM `" + Table_Players + "` WHERE " + ((UseUUIDs) ? "`uuid`" : "`name`") + "=?;");
if(UseUUIDs)
{
ps.setString(1, player.getUniqueId().toString().replace("-", ""));
}
else
{
ps.setString(1, player.getName());
}
PreparedStatement ps = GetConnection().prepareStatement(Query_UpdatePlayerGet);
ps.setString(1, GetPlayerNameOrUUID(player));
ResultSet rs = ps.executeQuery();
if(rs.next())
{
@ -104,7 +96,7 @@ public void UpdatePlayer(final Player player)
{
return;
}
ps = GetConnection().prepareStatement("UPDATE `" + Table_Players + "` SET `name`=? WHERE `uuid`=?;");
ps = GetConnection().prepareStatement(Query_UpdatePlayerUUID);
ps.setString(1, player.getName());
ps.setString(2, player.getUniqueId().toString().replace("-", ""));
}
@ -112,7 +104,7 @@ public void UpdatePlayer(final Player player)
{
rs.close();
ps.close();
ps = GetConnection().prepareStatement("INSERT INTO `" + Table_Players + "` (`name`" + ((UseUUIDs) ? ",`uuid`" : "") + ") VALUES (?" + ((UseUUIDs) ? ",?" : "") + ");");
ps = GetConnection().prepareStatement(Query_UpdatePlayerAdd);
ps.setString(1, player.getName());
if(UseUUIDs)
{
@ -138,22 +130,7 @@ public void SaveBackpack(Backpack backpack)
if(backpack.getID() <= 0)
{
ps = GetConnection().prepareStatement(Query_GetPlayerID);
if(UseUUIDs)
{
if(UseUUIDSeparators)
{
ps.setString(1, backpack.getOwner().getUniqueId().toString());
}
else
{
ps.setString(1, backpack.getOwner().getUniqueId().toString().replace("-", ""));
}
ps.setString(1, backpack.getOwner().getUniqueId().toString().replace("-", ""));
}
else
{
ps.setString(1, backpack.getOwner().getName());
}
ps.setString(1, GetPlayerNameOrUUID(backpack.getOwner()));
ResultSet rs = ps.executeQuery();
if(rs.next())
{
@ -161,7 +138,7 @@ public void SaveBackpack(Backpack backpack)
}
else
{
plugin.log.warning("Faild saving backpack for: " + backpack.getOwner().getName());
plugin.log.warning("Faild saving backpack for: " + backpack.getOwner().getName() + " (Unable to get players ID)");
return;
}
rs.close();
@ -196,21 +173,7 @@ public Backpack LoadBackpack(OfflinePlayer player)
{
PreparedStatement ps = null; // Statement Variable
ps = GetConnection().prepareStatement(Query_GetBP);
if(UseUUIDs)
{
if(UseUUIDSeparators)
{
ps.setString(1, player.getUniqueId().toString());
}
else
{
ps.setString(1, player.getUniqueId().toString().replace("-", ""));
}
}
else
{
ps.setString(1, player.getName());
}
ps.setString(1, GetPlayerNameOrUUID(player));
ResultSet rs = ps.executeQuery();
if(!rs.next())
{

View File

@ -23,6 +23,8 @@
import java.util.Scanner;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
@ -43,7 +45,17 @@ public static String getNameFromUUID(String uuid)
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("UUID not found at Mojang (probably offline mode UUID, try Bukkit offline player ...");
if(!uuid.contains("-"))
{
uuid = uuid.replaceAll("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5");
}
OfflinePlayer oP = Bukkit.getServer().getOfflinePlayer(UUID.fromString(uuid));
if(oP != null)
{
name = oP.getName();
}
//e.printStackTrace();
}
return name;
}
@ -66,7 +78,8 @@ public static String getUUIDFromName(String name, boolean onlinemode, boolean wi
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Unable to get UUID for: " + name + "! Giving offline UUID!");
//e.printStackTrace();
uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8)).toString();
}
}