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
#################

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"
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>
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>
<groupId>me.lenis0012.mr</groupId>
<artifactId>Marriage</artifactId>
<version>1.2.6</version>
<packaging>jar</packaging>
<groupId>me.lenis0012.mr</groupId>
<artifactId>Marriage</artifactId>
<version>1.2.6</version>
<packaging>jar</packaging>
<name>Marriage</name>
<url>http://dev.bukkit.org/server-mods/marriage-reloaded/</url>
<name>Marriage</name>
<url>http://dev.bukkit.org/server-mods/marriage-reloaded/</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<!-- Repo for access to Vault -->
<repository>
<id>vault-repo</id>
<url>http://ci.herocraftonline.com/plugin/repository/everything/</url>
</repository>
<!-- Repo for access to CraftBukkit -->
<repository>
<id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public</url>
</repository>
</repositories>
<repositories>
<!-- Repo for access to Vault -->
<repository>
<id>vault-repo</id>
<url>http://ci.herocraftonline.com/plugin/repository/everything/</url>
</repository>
<!-- Repo for access to CraftBukkit -->
<repository>
<id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.7.10-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId>
<version>1.2.23-SNAPSHOT</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.dthielke</groupId>
<artifactId>Herochat</artifactId>
<version>5.6.7-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/HeroChat.jar</systemPath>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.7.10-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId>
<version>1.4.1</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.dthielke</groupId>
<artifactId>Herochat</artifactId>
<version>5.6.7-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/HeroChat.jar</systemPath>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Build -->
<build>
<!-- Resources -->
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
<defaultGoal>clean install</defaultGoal>
<finalName>Marriage</finalName>
<!-- Plugins -->
<plugins>
<!-- Compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>
<!-- Build -->
<build>
<!-- Resources -->
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
<defaultGoal>clean install</defaultGoal>
<finalName>Marriage</finalName>
<!-- Plugins -->
<plugins>
<!-- Compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<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>

View file

@ -1,21 +1,37 @@
package com.lenis0012.bukkit.marriage2.internal;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
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.event.Listener;
import com.google.common.collect.Lists;
import com.lenis0012.bukkit.marriage2.Marriage;
import com.lenis0012.bukkit.marriage2.commands.Command;
import com.lenis0012.bukkit.marriage2.misc.BConfig;
public abstract class MarriageBase implements Marriage {
protected final MarriagePlugin plugin;
private final MarriageCommandExecutor commandExecutor;
private final ClassPath classPath;
private MarriageCommandExecutor commandExecutor;
public MarriageBase(MarriagePlugin 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);
plugin.getCommand("marry").setExecutor(commandExecutor);
}
@ -49,4 +65,34 @@ public abstract class MarriageBase implements Marriage {
public MarriageCommandExecutor getCommandExecutor() {
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.UUID;
import org.bukkit.event.Listener;
import com.lenis0012.bukkit.marriage2.MPlayer;
import com.lenis0012.bukkit.marriage2.commands.CommandDivorce;
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.commands.Command;
import com.lenis0012.bukkit.marriage2.config.Message;
import com.lenis0012.bukkit.marriage2.config.Settings;
import com.lenis0012.bukkit.marriage2.internal.data.DataManager;
import com.lenis0012.bukkit.marriage2.internal.data.MarriageData;
import com.lenis0012.bukkit.marriage2.internal.data.MarriagePlayer;
import com.lenis0012.bukkit.marriage2.listeners.DatabaseListener;
import com.lenis0012.bukkit.marriage2.misc.ListQuery;
public class MarriageCore extends MarriageBase {
@ -42,19 +38,16 @@ public class MarriageCore extends MarriageBase {
@Register(name = "listeners", type = Register.Type.ENABLE)
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)
public void registerCommands() {
register(CommandMarry.class);
register(CommandHome.class);
register(CommandSethome.class);
register(CommandGift.class);
register(CommandDivorce.class);
// Hidden/secret commands
register(CommandHelp.class);
for(Class<? extends Command> command : findClasses("com.lenis0012.bukkit.marriage2.commands", Command.class)) {
register(command);
}
}
@Override
@ -79,4 +72,21 @@ public class MarriageCore extends MarriageBase {
public ListQuery getMarriageList(int scale, int 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;
}
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 {
PreparedStatement ps = connection.prepareStatement(String.format(
"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 Location home;
private boolean pvpEnabled;
private int id = -1;
public MarriageData(UUID player1, UUID player2) {
this.player1 = player1;
@ -22,6 +23,7 @@ public class MarriageData implements MData {
}
public MarriageData(ResultSet data) throws SQLException {
this.id = data.getInt("id");
this.player1 = UUID.fromString(data.getString("player1"));
this.player2 = UUID.fromString(data.getString("player2"));
String world = data.getString("home_world");
@ -54,6 +56,10 @@ public class MarriageData implements MData {
ps.setBoolean(9, pvpEnabled);
}
protected int getId() {
return id;
}
@Override
public UUID getPlayer1Id() {
return player1;

View file

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