Implement more elements of persistence

This commit is contained in:
Sam 2017-03-29 16:27:26 +01:00
parent 61147c1e38
commit 7a5e8241aa
10 changed files with 195 additions and 75 deletions

View File

@ -2,6 +2,7 @@ package mineplex.gemhunters;
import mineplex.core.task.TaskManager;
import mineplex.core.texttutorial.TextTutorialManager;
import mineplex.gemhunters.join.JoinModule;
import mineplex.gemhunters.persistence.PersistenceModule;
import mineplex.gemhunters.tutorial.GemHuntersTutorial;
import net.minecraft.server.v1_8_R3.MinecraftServer;
@ -281,6 +282,7 @@ public class GemHunters extends JavaPlugin
require(InventoryModule.class);
require(LootModule.class);
require(ItemMapModule.class);
require(JoinModule.class);
require(ModerationModule.class);
require(MountModule.class);
require(PersistenceModule.class);

View File

@ -50,15 +50,8 @@ public class QuitNPC implements Listener
private final UUID _uuid;
private final PlayerInventory _inventory;
// Unlocked Slots
private final int _slotsUnlocked;
// Gems
private final int _gems;
// Quests
private final QuestPlayerData _questPlayerData;
public QuitNPC(QuitNPCModule npc, Player player, long quitMills)
{
// Managers
@ -91,47 +84,11 @@ public class QuitNPC implements Listener
_name = player.getName();
_uuid = player.getUniqueId();
_inventory = player.getInventory();
// Unlocked Slots
_slotsUnlocked = Managers.get(InventoryModule.class).getSlotsUnlocked(player);
// Gems
_gems = Managers.get(EconomyModule.class).Get(player);
// Quests
_questPlayerData = Managers.get(QuestModule.class).Get(player);
UtilServer.RegisterEvents(this);
}
public void restore(Player player)
{
// Player
player.getInventory().clear();
player.teleport(_entity.getLocation());
player.setHealth(_entity.getHealth());
// Inventory
int i = 0;
for (ItemStack itemStack : _inventory.getContents())
{
player.getInventory().setItem(i++, itemStack);
}
player.getInventory().setArmorContents(_inventory.getArmorContents());
// Unlocked Slots
Managers.get(InventoryModule.class).setSlotsUnlocked(player, _slotsUnlocked);
// Gems
// Subtract GEM_START_COST (100 by default) because EconomyModule adds
// this value on join regardless if they have an NPC.
Managers.get(EconomyModule.class).setStore(player, _gems - EconomyModule.GEM_START_COST);
// Quests
Managers.require(QuestModule.class).setPlayerData(player, _questPlayerData);
}
public void despawn(boolean pluginRemove)
{
QuitNPCDespawnEvent event = new QuitNPCDespawnEvent(this, pluginRemove);

View File

@ -7,6 +7,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import mineplex.core.common.util.UtilPlayer;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -90,7 +91,7 @@ public class QuitNPCModule extends MiniPlugin
{
Player player = event.getPlayer();
if (_tutorial.isInTutorial(player))
if (UtilPlayer.isSpectator(player))
{
return;
}

View File

@ -0,0 +1,79 @@
package mineplex.gemhunters.join;
import mineplex.core.MiniPlugin;
import mineplex.core.ReflectivelyCreateMiniPlugin;
import mineplex.core.account.CoreClient;
import mineplex.core.account.CoreClientManager;
import mineplex.gemhunters.death.quitnpc.QuitNPC;
import mineplex.gemhunters.death.quitnpc.QuitNPCModule;
import mineplex.gemhunters.economy.EconomyModule;
import mineplex.gemhunters.persistence.PersistenceData;
import mineplex.gemhunters.persistence.PersistenceModule;
import mineplex.gemhunters.persistence.PersistenceRepository;
import mineplex.gemhunters.quest.QuestModule;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
import java.util.function.Consumer;
@ReflectivelyCreateMiniPlugin
public class JoinModule extends MiniPlugin
{
private final CoreClientManager _client;
private final EconomyModule _economy;
private final QuestModule _quest;
private final PersistenceModule _persistence;
private final QuitNPCModule _npc;
private JoinModule()
{
super("Join");
_client = require(CoreClientManager.class);
_economy = require(EconomyModule.class);
_quest = require(QuestModule.class);
_persistence = require(PersistenceModule.class);
_npc = require(QuitNPCModule.class);
}
@EventHandler
public void playerJoin(PlayerJoinEvent event)
{
Player player = event.getPlayer();
CoreClient client = _client.Get(player);
PersistenceRepository repository = _persistence.getRepository();
Consumer<PersistenceData> response = data ->
{
_economy.setStore(player, data.getGems());
_quest.setPlayerData(player, data.getQuestData());
player.teleport(data.getLocation());
player.setHealth(data.getHealth());
player.setMaxHealth(data.getMaxHealth());
player.setFoodLevel(data.getHunger());
player.getInventory().clear();
player.getInventory().addItem(data.getItems());
player.getInventory().setArmorContents(data.getArmour());
};
player.getInventory().clear();
runAsync(() ->
{
repository.getPersistenceData(response, client);
if (_npc.hasNPC(player))
{
QuitNPC npc = _npc.getNPC(player);
npc.despawn(true);
}
if (repository.exists(client))
{
}
});
}
}

View File

@ -2,6 +2,7 @@ package mineplex.gemhunters.persistence;
import mineplex.gemhunters.quest.QuestPlayerData;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
public class PersistenceData
{
@ -9,12 +10,22 @@ public class PersistenceData
private final int _gems;
private final Location _location;
private final QuestPlayerData _questData;
private final int _health;
private final int _maxHealth;
private final int _hunger;
private final ItemStack[] _items;
private final ItemStack[] _armour;
public PersistenceData(int gems, Location location, QuestPlayerData questData)
public PersistenceData(int gems, Location location, QuestPlayerData questData, int health, int maxHealth, int hunger, ItemStack[] items, ItemStack[] armour)
{
_gems = gems;
_location = location;
_questData = questData;
_health = health;
_maxHealth = maxHealth;
_hunger = hunger;
_items = items;
_armour = armour;
}
public int getGems()
@ -32,4 +43,29 @@ public class PersistenceData
return _questData;
}
public int getHealth()
{
return _health;
}
public int getMaxHealth()
{
return _maxHealth;
}
public int getHunger()
{
return _hunger;
}
public ItemStack[] getItems()
{
return _items;
}
public ItemStack[] getArmour()
{
return _armour;
}
}

View File

@ -5,14 +5,21 @@ import mineplex.core.ReflectivelyCreateMiniPlugin;
import mineplex.core.account.CoreClient;
import mineplex.core.account.CoreClientManager;
import mineplex.gemhunters.death.event.QuitNPCDespawnEvent;
import mineplex.gemhunters.economy.CashOutModule;
import mineplex.gemhunters.economy.EconomyModule;
import mineplex.gemhunters.quest.QuestModule;
import mineplex.gemhunters.quest.QuestPlayerData;
import net.minecraft.server.v1_8_R3.EntityItemFrame;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
@ReflectivelyCreateMiniPlugin
public class PersistenceModule extends MiniPlugin
@ -35,26 +42,22 @@ public class PersistenceModule extends MiniPlugin
_repository = new PersistenceRepository();
}
@EventHandler(priority = EventPriority.HIGHEST)
public void playerJoin(PlayerJoinEvent event)
{
Player player = event.getPlayer();
CoreClient client = _client.Get(player);
runAsync(() -> _repository.getPersistenceData(response ->
{
_economy.setStore(player, response.getGems());
player.teleport(response.getLocation());
_quest.setPlayerData(player, response.getQuestData());
}, client));
}
@EventHandler
public void playerQuit(PlayerQuitEvent event)
{
Player player = event.getPlayer();
CoreClient client = _client.Get(player);
PersistenceData data = new PersistenceData(_economy.Get(player), player.getLocation(), _quest.Get(player));
int gems = _economy.Get(player);
Location location = player.getLocation();
QuestPlayerData quest = _quest.Get(player);
int health = (int) player.getHealth();
int maxHealth = (int) player.getMaxHealth();
int hunger = player.getFoodLevel();
ItemStack[] items = player.getInventory().getContents();
ItemStack[] armour = player.getInventory().getArmorContents();
PersistenceData data = new PersistenceData(gems, location, quest, health, maxHealth, hunger, items, armour);
runAsync(() -> _repository.savePersistence(client, data));
}
@ -76,4 +79,9 @@ public class PersistenceModule extends MiniPlugin
runAsync(() -> _client.getOrLoadClient(event.getNpc().getName(), _repository::deletePersistence));
}
}
public final PersistenceRepository getRepository()
{
return _repository;
}
}

View File

@ -10,6 +10,8 @@ import mineplex.serverdata.database.column.ColumnInt;
import mineplex.serverdata.database.column.ColumnVarChar;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.sql.Connection;
import java.sql.ResultSet;
@ -22,8 +24,8 @@ public class PersistenceRepository extends RepositoryBase
{
private static final String GET_DATA = "SELECT * FROM gemHunters WHERE accountId=?;";
private static final String INSERT_DATA = "INSERT INTO gemHunters VALUES (?,?,?,?,?,?,?,?);";
private static final String UPDATE_DATA = "UPDATE gemHunters SET gems=?,x=?,y=?,z=?,yaw=?,pitch=?,quests=? WHERE accountId=?;";
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=?,items=?,armour=? WHERE accountId=?;";
private static final String DELETE_DATA = "DELETE FROM gemHunters WHERE accountId=?;";
private static final Gson GSON;
@ -50,6 +52,9 @@ public class PersistenceRepository extends RepositoryBase
if (resultSet.next())
{
int gems = resultSet.getInt("gems");
int health = resultSet.getInt("health");
int maxHealth = resultSet.getInt("maxHealth");
int hunger = resultSet.getInt("hunger");
int x = resultSet.getInt("x");
int y = resultSet.getInt("y");
int z = resultSet.getInt("z");
@ -57,9 +62,15 @@ public class PersistenceRepository extends RepositoryBase
int pitch = resultSet.getInt("pitch");
String quests = resultSet.getString("quests");
QuestPlayerData questData = GSON.fromJson(quests, QuestPlayerData.class);
String items = resultSet.getString("items");
ItemStack[] itemsData = GSON.fromJson(items, ItemStack[].class);
String armour = resultSet.getString("armour");
ItemStack[] armourData = GSON.fromJson(armour, ItemStack[].class);
_exists.add(accountId);
PersistenceData data = new PersistenceData(gems, new Location(Bukkit.getWorlds().get(0), x, y, z, yaw, pitch), questData);
Location location = new Location(Bukkit.getWorlds().get(0), x, y, z, yaw, pitch);
PersistenceData data = new PersistenceData(gems, location, questData, health, maxHealth, hunger, itemsData, armourData);
response.accept(data);
}
}, new ColumnInt("accountId", accountId));
@ -67,39 +78,67 @@ public class PersistenceRepository extends RepositoryBase
public void savePersistence(CoreClient client, PersistenceData data)
{
Bukkit.broadcastMessage("Saving data for " + client.getAccountId());
int accountId = client.getAccountId();
int gems = data.getGems();
int health = data.getHealth();
int maxHealth = data.getMaxHealth();
int hunger = data.getHunger();
int x = data.getLocation().getBlockX();
int y = data.getLocation().getBlockY();
int z = data.getLocation().getBlockZ();
int yaw = (int) data.getLocation().getYaw();
int pitch = (int) data.getLocation().getPitch();
ItemStack[] items = data.getItems();
if (_exists.contains(accountId))
for (int i = 0; i < items.length; i++)
{
ItemStack itemStack = items[i];
if (itemStack != null && (itemStack.getType() == Material.EMERALD || itemStack.getType() == Material.MAP))
{
Bukkit.broadcastMessage(itemStack.getType().name());
items[i] = null;
}
}
if (exists(client))
{
Bukkit.broadcastMessage("Update");
executeUpdate(UPDATE_DATA,
new ColumnInt("gems", gems),
new ColumnInt("health", health),
new ColumnInt("maxHealth", maxHealth),
new ColumnInt("hunger", hunger),
new ColumnInt("x", x),
new ColumnInt("y", y),
new ColumnInt("z", z),
new ColumnInt("yaw", yaw),
new ColumnInt("pitch", pitch),
new ColumnVarChar("quests", 500, GSON.toJson(data.getQuestData())),
new ColumnVarChar("items", 1000, GSON.toJson(data.getItems())),
new ColumnVarChar("armour", 100, GSON.toJson(data.getArmour())),
new ColumnInt("accountId", accountId)
);
}
else
{
Bukkit.broadcastMessage("Insert");
executeInsert(INSERT_DATA, null,
new ColumnInt("accountId", accountId),
new ColumnInt("gems", gems),
new ColumnInt("health", health),
new ColumnInt("maxHealth", maxHealth),
new ColumnInt("hunger", hunger),
new ColumnInt("x", x),
new ColumnInt("y", y),
new ColumnInt("z", z),
new ColumnInt("yaw", yaw),
new ColumnInt("pitch", pitch),
new ColumnVarChar("quests", 500, GSON.toJson(data.getQuestData()))
new ColumnVarChar("quests", 500, GSON.toJson(data.getQuestData())),
new ColumnVarChar("items", 1000, GSON.toJson(data.getItems())),
new ColumnVarChar("armour", 100, GSON.toJson(data.getArmour()))
);
}
@ -112,4 +151,9 @@ public class PersistenceRepository extends RepositoryBase
executeUpdate(DELETE_DATA, new ColumnInt("accountId", accountId));
}
public boolean exists(CoreClient client)
{
return _exists.contains(client.getAccountId());
}
}

View File

@ -4,6 +4,7 @@ import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import mineplex.core.common.util.*;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
@ -25,14 +26,7 @@ import org.bukkit.metadata.FixedMetadataValue;
import mineplex.core.MiniClientPlugin;
import mineplex.core.ReflectivelyCreateMiniPlugin;
import mineplex.core.common.currency.GlobalCurrency;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilInv;
import mineplex.core.common.util.UtilItem;
import mineplex.core.common.util.UtilItem.ItemAttribute;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilTextMiddle;
import mineplex.core.common.util.UtilTime;
import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.menu.Menu;
import mineplex.core.recharge.Recharge;
@ -491,7 +485,7 @@ public class QuestModule extends MiniClientPlugin<QuestPlayerData>
@EventHandler
public void debug(PlayerCommandPreprocessEvent event)
{
if (event.getMessage().startsWith("/test"))
if (event.getMessage().startsWith("/questdatatest"))
{
event.setCancelled(true);
Player player = event.getPlayer();

View File

@ -83,7 +83,6 @@ public class SpawnModule extends MiniPlugin
QuitNPC npc = _npc.getNPC(player);
npc.despawn(true);
npc.restore(player);
return;
}

View File

@ -49,7 +49,7 @@ public class WorldListeners implements Listener
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
//@EventHandler
@EventHandler
public void deletePlayerData(PlayerQuitEvent event)
{
_plugin.getServer().getScheduler().runTaskLater(_plugin, () -> {