From c341671ffc3420b4cf742b22bb6a7a4f5d7a8ccb Mon Sep 17 00:00:00 2001 From: cnr Date: Wed, 5 Jul 2017 21:33:55 -0700 Subject: [PATCH] Move GH quit NPCs to redis; use region when saving GH data --- .../death/quitnpc/QuitNPCModule.java | 2 +- .../death/quitnpc/QuitNPCRepository.java | 56 ++++--------------- .../persistence/PersistenceRepository.java | 35 ++++++------ 3 files changed, 30 insertions(+), 63 deletions(-) diff --git a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/death/quitnpc/QuitNPCModule.java b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/death/quitnpc/QuitNPCModule.java index 109e1039f..2bc115159 100644 --- a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/death/quitnpc/QuitNPCModule.java +++ b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/death/quitnpc/QuitNPCModule.java @@ -101,7 +101,7 @@ public class QuitNPCModule extends MiniPlugin { try { - String npcServer = _repo.loadNpcServer(event.getUniqueId()).get(); + String npcServer = _repo.loadNpcServer(event.getUniqueId()).join(); if (npcServer == null || npcServer.isEmpty()) { return; diff --git a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/death/quitnpc/QuitNPCRepository.java b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/death/quitnpc/QuitNPCRepository.java index 6db02ac7f..c01b2d91e 100644 --- a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/death/quitnpc/QuitNPCRepository.java +++ b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/death/quitnpc/QuitNPCRepository.java @@ -1,69 +1,33 @@ package mineplex.gemhunters.death.quitnpc; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.util.UUID; import java.util.concurrent.CompletableFuture; import mineplex.core.common.util.UtilServer; -import mineplex.serverdata.database.DBPool; -import mineplex.serverdata.database.RepositoryBase; -import mineplex.serverdata.database.column.ColumnVarChar; +import mineplex.serverdata.Region; +import mineplex.serverdata.redis.RedisRepository; -public class QuitNPCRepository extends RepositoryBase +public class QuitNPCRepository extends RedisRepository { - private static final String GET_DATA = "SELECT serverName FROM gemHuntersQuitNpcs WHERE uuid=?;"; - private static final String INSERT_DATA = "INSERT INTO gemHuntersQuitNpcs (uuid, serverName) VALUES (?, ?);"; - private static final String DELETE_DATA = "DELETE FROM gemHuntersQuitNpcs WHERE uuid=?;"; + private static final String REDIS_KEY_PREFIX = "GemHuntersNPC."; public QuitNPCRepository() { - super(DBPool.getAccount()); + super(Region.ALL); } - + public CompletableFuture loadNpcServer(UUID uuid) { - return CompletableFuture.supplyAsync(() -> - { - try (Connection conn = getConnection()) - { - PreparedStatement stmt = conn.prepareStatement(GET_DATA); - stmt.setString(1, uuid.toString()); - - String serverName = null; - - ResultSet resultSet = stmt.executeQuery(); - if (resultSet.next()) - { - serverName = resultSet.getString("serverName"); - } - - return serverName; - } - catch (Exception e) - { - e.printStackTrace(); - return null; - } - }); + return CompletableFuture.supplyAsync(() -> getResource(false).get(getKey(REDIS_KEY_PREFIX + uuid.toString()))); } public void deleteNpc(UUID uuid) { - final String uuidStr = uuid.toString(); - UtilServer.runAsync(() -> - { - executeUpdate(DELETE_DATA, new ColumnVarChar("uuid", uuidStr.length(), uuidStr)); - }); + UtilServer.runAsync(() -> getResource(true).del(getKey(REDIS_KEY_PREFIX + uuid.toString()))); } - + public void insertNpc(UUID uuid, String serverName) { - final String uuidStr = uuid.toString(); - UtilServer.runAsync(() -> - { - executeInsert(INSERT_DATA, null, new ColumnVarChar("uuid", uuidStr.length(), uuidStr), new ColumnVarChar("serverName", serverName.length(), serverName)); - }); + UtilServer.runAsync(() -> getResource(true).setex(REDIS_KEY_PREFIX + uuid.toString(), 60, serverName)); } } \ No newline at end of file diff --git a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceRepository.java b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceRepository.java index 6ce1437d3..3e9d86782 100644 --- a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceRepository.java +++ b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceRepository.java @@ -1,6 +1,21 @@ package mineplex.gemhunters.persistence; +import java.lang.reflect.Constructor; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + import com.google.gson.Gson; + import mineplex.core.account.CoreClient; import mineplex.core.common.util.UtilServer; import mineplex.gemhunters.quest.QuestPlayerData; @@ -10,26 +25,13 @@ import mineplex.serverdata.database.RepositoryBase; import mineplex.serverdata.database.column.ColumnInt; import mineplex.serverdata.database.column.ColumnTimestamp; import mineplex.serverdata.database.column.ColumnVarChar; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; - -import java.lang.reflect.Constructor; -import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.function.Consumer; public class PersistenceRepository extends RepositoryBase { private static final String GET_DATA = "SELECT * FROM gemHunters WHERE accountId=? AND region=?;"; - private static final String INSERT_DATA = "INSERT INTO gemHunters VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; - private static final String UPDATE_DATA = "UPDATE gemHunters SET gems=?,health=?,maxHealth=?,hunger=?,x=?,y=?,z=?,yaw=?,pitch=?,quests=?,slots=?,items=?,armour=?,saveTime=?,cashOutTime=? WHERE accountId=?;"; + private static final String INSERT_DATA = "INSERT INTO gemHunters (accountId, region, gems, health, maxHealth, hunger, x, y, z, yaw, pitch, quests, slots, items, armour, saveTime, cashOutTime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; + private static final String UPDATE_DATA = "UPDATE gemHunters SET gems=?,health=?,maxHealth=?,hunger=?,x=?,y=?,z=?,yaw=?,pitch=?,quests=?,slots=?,items=?,armour=?,saveTime=?,cashOutTime=? WHERE accountId=? AND region=?;"; private static final String DELETE_DATA = "DELETE FROM gemHunters WHERE accountId=?;"; private static final Gson GSON; private static final ItemStack AIR = new ItemStack(Material.AIR); @@ -180,7 +182,8 @@ public class PersistenceRepository extends RepositoryBase new ColumnVarChar("armour", 1000, GSON.toJson(armourMap)), new ColumnTimestamp("saveTime", saveTime), new ColumnInt("cashOutTime", cashOutTime), - new ColumnInt("accountId", accountId) + new ColumnInt("accountId", accountId), + new ColumnVarChar("region", 2, region.toString()) ); } else