Add storage saving

This commit is contained in:
Lennart ten Wolde 2015-02-03 22:59:57 +01:00
parent 929d0e3770
commit d51f53378c
7 changed files with 255 additions and 97 deletions

3
.gitignore vendored
View file

@ -1,3 +1,6 @@
.idea/
*.iml
################# #################
## Eclipse ## Eclipse
################# #################

186
pom.xml
View file

@ -1,88 +1,114 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>me.lenis0012.mr</groupId> <groupId>me.lenis0012.mr</groupId>
<artifactId>Marriage</artifactId> <artifactId>Marriage</artifactId>
<version>1.2.6</version> <version>1.2.6</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Marriage</name> <name>Marriage</name>
<url>http://dev.bukkit.org/server-mods/marriage-reloaded/</url> <url>http://dev.bukkit.org/server-mods/marriage-reloaded/</url>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<repositories> <repositories>
<!-- Repo for access to Vault --> <!-- Repo for access to Vault -->
<repository> <repository>
<id>vault-repo</id> <id>vault-repo</id>
<url>http://ci.herocraftonline.com/plugin/repository/everything/</url> <url>http://ci.herocraftonline.com/plugin/repository/everything/</url>
</repository> </repository>
<!-- Repo for access to CraftBukkit --> <!-- Repo for access to CraftBukkit -->
<repository> <repository>
<id>bukkit-repo</id> <id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public</url> <url>http://repo.bukkit.org/content/groups/public</url>
</repository> </repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.bukkit</groupId> <groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId> <artifactId>craftbukkit</artifactId>
<version>1.7.10-R0.1-SNAPSHOT</version> <version>1.7.10-R0.1-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.milkbowl.vault</groupId> <groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId> <artifactId>Vault</artifactId>
<version>1.2.23-SNAPSHOT</version> <version>1.4.1</version>
<scope>provided</scope> <scope>provided</scope>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.dthielke</groupId> <groupId>com.dthielke</groupId>
<artifactId>Herochat</artifactId> <artifactId>Herochat</artifactId>
<version>5.6.7-SNAPSHOT</version> <version>5.6.7-SNAPSHOT</version>
<scope>system</scope> <scope>system</scope>
<systemPath>${project.basedir}/lib/HeroChat.jar</systemPath> <systemPath>${project.basedir}/lib/HeroChat.jar</systemPath>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>com.google.guava</groupId>
<artifactId>junit</artifactId> <artifactId>guava</artifactId>
<version>3.8.1</version> <version>17.0</version>
<scope>test</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Build --> <!-- Build -->
<build> <build>
<!-- Resources --> <!-- Resources -->
<resources> <resources>
<resource> <resource>
<directory>src/main/resources/</directory> <directory>src/main/resources/</directory>
<filtering>true</filtering> <filtering>true</filtering>
<includes> <includes>
<include>*</include> <include>*</include>
</includes> </includes>
</resource> </resource>
</resources> </resources>
<defaultGoal>clean install</defaultGoal> <defaultGoal>clean install</defaultGoal>
<finalName>Marriage</finalName> <finalName>Marriage</finalName>
<!-- Plugins --> <!-- Plugins -->
<plugins> <plugins>
<!-- Compiler plugin --> <!-- Compiler plugin -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version> <version>2.4</version>
<configuration> <configuration>
<source>1.6</source> <source>1.6</source>
<target>1.6</target> <target>1.6</target>
<showWarnings>true</showWarnings> <showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation> <showDeprecation>true</showDeprecation>
</configuration> </configuration>
</plugin> </plugin>
</plugins> <plugin>
</build> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.google.guava:guava</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> </project>

View file

@ -1,21 +1,37 @@
package com.lenis0012.bukkit.marriage2.internal; package com.lenis0012.bukkit.marriage2.internal;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.minecraft.util.com.google.common.reflect.ClassPath;
import net.minecraft.util.com.google.common.reflect.ClassPath.ClassInfo;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import com.google.common.collect.Lists;
import com.lenis0012.bukkit.marriage2.Marriage; import com.lenis0012.bukkit.marriage2.Marriage;
import com.lenis0012.bukkit.marriage2.commands.Command; import com.lenis0012.bukkit.marriage2.commands.Command;
import com.lenis0012.bukkit.marriage2.misc.BConfig; import com.lenis0012.bukkit.marriage2.misc.BConfig;
public abstract class MarriageBase implements Marriage { public abstract class MarriageBase implements Marriage {
protected final MarriagePlugin plugin; protected final MarriagePlugin plugin;
private final MarriageCommandExecutor commandExecutor; private final ClassPath classPath;
private MarriageCommandExecutor commandExecutor;
public MarriageBase(MarriagePlugin plugin) { public MarriageBase(MarriagePlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
try {
this.classPath = ClassPath.from(getClass().getClassLoader());
} catch (IOException e) {
throw new RuntimeException("Failed to intialize class path!", e);
}
}
void enable() {
this.commandExecutor = new MarriageCommandExecutor(this); this.commandExecutor = new MarriageCommandExecutor(this);
plugin.getCommand("marry").setExecutor(commandExecutor); plugin.getCommand("marry").setExecutor(commandExecutor);
} }
@ -49,4 +65,34 @@ public abstract class MarriageBase implements Marriage {
public MarriageCommandExecutor getCommandExecutor() { public MarriageCommandExecutor getCommandExecutor() {
return commandExecutor; return commandExecutor;
} }
@SuppressWarnings("unchecked")
protected <T> List<Class<? extends T>> findClasses(String pkg, Class<T> type, Object... params) {
List<Class<? extends T>> list = Lists.newArrayList();
for(ClassInfo info : classPath.getTopLevelClassesRecursive(pkg)) {
try {
Class<?> clazz = Class.forName(info.getName());
if(type.isAssignableFrom(clazz)) {
list.add((Class<? extends T>) clazz);
}
} catch(Exception e) {
plugin.getLogger().log(Level.WARNING, "Failed to intiate class", e);
}
}
return list;
}
protected <T> List<T> findObjects(String pkg, Class<T> type, Object... params) {
List<T> list = Lists.newArrayList();
for(Class<? extends T> clazz : findClasses(pkg, type)) {
try {
list.add(type.cast(clazz.getConstructors()[0].newInstance(params)));
} catch (Exception e) {
plugin.getLogger().log(Level.WARNING, "Failed to construct class", e);
}
}
return list;
}
} }

View file

@ -5,19 +5,15 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.bukkit.event.Listener;
import com.lenis0012.bukkit.marriage2.MPlayer; import com.lenis0012.bukkit.marriage2.MPlayer;
import com.lenis0012.bukkit.marriage2.commands.CommandDivorce; import com.lenis0012.bukkit.marriage2.commands.Command;
import com.lenis0012.bukkit.marriage2.commands.CommandGift;
import com.lenis0012.bukkit.marriage2.commands.CommandHelp;
import com.lenis0012.bukkit.marriage2.commands.CommandHome;
import com.lenis0012.bukkit.marriage2.commands.CommandMarry;
import com.lenis0012.bukkit.marriage2.commands.CommandSethome;
import com.lenis0012.bukkit.marriage2.config.Message; import com.lenis0012.bukkit.marriage2.config.Message;
import com.lenis0012.bukkit.marriage2.config.Settings; import com.lenis0012.bukkit.marriage2.config.Settings;
import com.lenis0012.bukkit.marriage2.internal.data.DataManager; import com.lenis0012.bukkit.marriage2.internal.data.DataManager;
import com.lenis0012.bukkit.marriage2.internal.data.MarriageData; import com.lenis0012.bukkit.marriage2.internal.data.MarriageData;
import com.lenis0012.bukkit.marriage2.internal.data.MarriagePlayer; import com.lenis0012.bukkit.marriage2.internal.data.MarriagePlayer;
import com.lenis0012.bukkit.marriage2.listeners.DatabaseListener;
import com.lenis0012.bukkit.marriage2.misc.ListQuery; import com.lenis0012.bukkit.marriage2.misc.ListQuery;
public class MarriageCore extends MarriageBase { public class MarriageCore extends MarriageBase {
@ -42,19 +38,16 @@ public class MarriageCore extends MarriageBase {
@Register(name = "listeners", type = Register.Type.ENABLE) @Register(name = "listeners", type = Register.Type.ENABLE)
public void registerListeners() { public void registerListeners() {
register(new DatabaseListener(this)); for(Listener listener : findObjects("com.lenis0012.bukkit.marriage2.listeners", Listener.class, this)) {
register(listener);
}
} }
@Register(name = "commands", type = Register.Type.ENABLE) @Register(name = "commands", type = Register.Type.ENABLE)
public void registerCommands() { public void registerCommands() {
register(CommandMarry.class); for(Class<? extends Command> command : findClasses("com.lenis0012.bukkit.marriage2.commands", Command.class)) {
register(CommandHome.class); register(command);
register(CommandSethome.class); }
register(CommandGift.class);
register(CommandDivorce.class);
// Hidden/secret commands
register(CommandHelp.class);
} }
@Override @Override
@ -79,4 +72,21 @@ public class MarriageCore extends MarriageBase {
public ListQuery getMarriageList(int scale, int page) { public ListQuery getMarriageList(int scale, int page) {
return dataManager.listMarriages(page, page); return dataManager.listMarriages(page, page);
} }
/**
* Unload player from the memory
*
* @param uuid of player
*/
public void unloadPlayer(UUID uuid) {
final MarriagePlayer mPlayer = players.remove(uuid);
if(mPlayer != null) {
new Thread() {
@Override
public void run() {
dataManager.savePlayer(mPlayer);
}
}.start();
}
}
} }

View file

@ -102,6 +102,65 @@ public class DataManager {
return player; return player;
} }
public void savePlayer(MarriagePlayer player) {
Connection connection = newConnection();
try {
PreparedStatement ps = connection.prepareStatement(String.format(
"SELECT * FROM %splayers WHERE unique_user_id=?;", prefix));
ps.setString(1, player.getUniqueId().toString());
ResultSet result = ps.executeQuery();
if(result.next()) {
// Already in database (update)
if(!ps.isClosed()) ps.close();
ps = connection.prepareStatement(String.format(
"UPDATE %splayers SET gender=?,lastlogin=? WHERE unique_user_id=?;", prefix));
ps.setString(1, player.getGender().toString());
ps.setLong(2, System.currentTimeMillis());
ps.setString(3, player.getUniqueId().toString());
ps.executeUpdate();
} else {
// Not in database yet
if(!ps.isClosed()) ps.close();
ps = connection.prepareStatement(String.format(
"INSERT INTO %splayers (unique_user_id,gender,lastlogin) VALUES(?,?,?);", prefix));
ps.setString(1, player.getUniqueId().toString());
ps.setString(2, player.getGender().toString());
ps.setLong(3, System.currentTimeMillis());
ps.executeUpdate();
}
// Save marriages
if(player.getMarriage() != null) {
MarriageData mdata = (MarriageData) player.getMarriage();
if(mdata.getId() >= 0) {
// Update existing entry
if(!ps.isClosed()) ps.close();
ps = connection.prepareStatement(String.format(
"UPDATE %sdata SET player1=?,player2=?,home_word=?,home_x=?,home_y=?,home_z=?,home_yaw=?,home_pitch=?,pvp_enabled=? WHERE id=?;", prefix));
mdata.save(ps);
ps.setInt(10, mdata.getId());
ps.executeUpdate();
} else {
if(!ps.isClosed()) ps.close();
ps = connection.prepareStatement(String.format(
"INSERT INTO %splayers (player1,player2,home_world,home_x,home_y,home_z,home_yaw,home_pitch,pvp_enabled) VALUES(?,?,?,?,?,?,?,?,?);", prefix));
mdata.save(ps);
ps.executeUpdate();
}
}
} catch (SQLException e) {
core.getLogger().log(Level.WARNING, "Failed to load player data", e);
} finally {
if(connection != null) {
try {
connection.close();
} catch (SQLException e) {
;
}
}
}
}
private void loadMarriages(Connection connection, MarriagePlayer player, boolean alt) throws SQLException { private void loadMarriages(Connection connection, MarriagePlayer player, boolean alt) throws SQLException {
PreparedStatement ps = connection.prepareStatement(String.format( PreparedStatement ps = connection.prepareStatement(String.format(
"SELECT * FROM %sdata WHERE %s=?;", prefix, alt ? "player2" : "player1", prefix)); "SELECT * FROM %sdata WHERE %s=?;", prefix, alt ? "player2" : "player1", prefix));

View file

@ -15,6 +15,7 @@ public class MarriageData implements MData {
private final UUID player2; private final UUID player2;
private Location home; private Location home;
private boolean pvpEnabled; private boolean pvpEnabled;
private int id = -1;
public MarriageData(UUID player1, UUID player2) { public MarriageData(UUID player1, UUID player2) {
this.player1 = player1; this.player1 = player1;
@ -22,6 +23,7 @@ public class MarriageData implements MData {
} }
public MarriageData(ResultSet data) throws SQLException { public MarriageData(ResultSet data) throws SQLException {
this.id = data.getInt("id");
this.player1 = UUID.fromString(data.getString("player1")); this.player1 = UUID.fromString(data.getString("player1"));
this.player2 = UUID.fromString(data.getString("player2")); this.player2 = UUID.fromString(data.getString("player2"));
String world = data.getString("home_world"); String world = data.getString("home_world");
@ -54,6 +56,10 @@ public class MarriageData implements MData {
ps.setBoolean(9, pvpEnabled); ps.setBoolean(9, pvpEnabled);
} }
protected int getId() {
return id;
}
@Override @Override
public UUID getPlayer1Id() { public UUID getPlayer1Id() {
return player1; return player1;

View file

@ -1,11 +1,13 @@
package com.lenis0012.bukkit.marriage2.listeners; package com.lenis0012.bukkit.marriage2.listeners;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent; import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result; import org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import com.lenis0012.bukkit.marriage2.internal.MarriageCore; import com.lenis0012.bukkit.marriage2.internal.MarriageCore;
@ -27,4 +29,10 @@ public class DatabaseListener implements Listener {
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
core.getMPlayer(event.getPlayer().getUniqueId()); core.getMPlayer(event.getPlayer().getUniqueId());
} }
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
core.unloadPlayer(player.getUniqueId());
}
} }