Super rough and ready persistence

This commit is contained in:
Sam 2017-03-18 23:33:30 +00:00
parent ace17be1ca
commit f56b940a73
9 changed files with 287 additions and 12 deletions

View File

@ -5,6 +5,7 @@ import java.util.Iterator;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -76,6 +77,8 @@ public class TextTutorialManager extends MiniPlugin
other.hidePlayer(event.getPlayer());
}
((CraftPlayer) event.getPlayer()).getHandle().spectating = true;
return;
}
}
@ -117,6 +120,8 @@ public class TextTutorialManager extends MiniPlugin
other.showPlayer(player);
}
((CraftPlayer) player).getHandle().spectating = false;
//Gems
if (tut.getGemReward() > 0)
{

View File

@ -1,5 +1,9 @@
package mineplex.gemhunters;
import mineplex.core.task.TaskManager;
import mineplex.core.texttutorial.TextTutorialManager;
import mineplex.gemhunters.persistence.PersistenceModule;
import mineplex.gemhunters.tutorial.GemHuntersTutorial;
import net.minecraft.server.v1_8_R3.MinecraftServer;
import org.bukkit.Bukkit;
@ -260,8 +264,8 @@ public class GemHunters extends JavaPlugin
gadgetManager.setGadgetEnabled(false);
// Tutorials
//TextTutorialManager tutorialManager = new TextTutorialManager(this, donationManager, new TaskManager(this, clientManager));
//tutorialManager.addTutorial(new GemHuntersTutorial());
TextTutorialManager tutorialManager = new TextTutorialManager(this, donationManager, new TaskManager(this, clientManager));
tutorialManager.addTutorial(new GemHuntersTutorial());
// Now we finally get to enable the Gem Hunters modules
// Though if any other module needs one of these it will be generated in
@ -277,6 +281,7 @@ public class GemHunters extends JavaPlugin
require(ItemMapModule.class);
require(ModerationModule.class);
require(MountModule.class);
require(PersistenceModule.class);
require(PlayerStatusModule.class);
require(QuestModule.class);
require(QuitNPCModule.class);

View File

@ -116,6 +116,11 @@ public class EconomyModule extends MiniClientPlugin<Integer>
Set(player, gems);
}
public void setStore(UUID player, int gems)
{
Set(player, gems);
}
public int getGems(Player player)
{
return Get(player);

View File

@ -0,0 +1,27 @@
package mineplex.gemhunters.persistence;
import org.bukkit.Location;
public class PersistenceData
{
private int _gems;
private Location _location;
public PersistenceData(int gems, Location location)
{
_gems = gems;
_location = location;
}
public int getGems()
{
return _gems;
}
public Location getLocation()
{
return _location;
}
}

View File

@ -0,0 +1,74 @@
package mineplex.gemhunters.persistence;
import mineplex.core.MiniPlugin;
import mineplex.core.ReflectivelyCreateMiniPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.gemhunters.economy.EconomyModule;
import mineplex.serverdata.database.DBPool;
import org.bukkit.Location;
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.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@ReflectivelyCreateMiniPlugin
public class PersistenceModule extends MiniPlugin
{
private final CoreClientManager _client;
private final EconomyModule _economy;
private final PersistenceRepository _repository;
private final Map<UUID, Location> _toTeleport;
public PersistenceModule()
{
super("Persistence");
_client = require(CoreClientManager.class);
_economy = require(EconomyModule.class);
_repository = new PersistenceRepository(DBPool.getAccount());
_toTeleport = new HashMap<>();
_client.addStoredProcedureLoginProcessor(_repository.buildPersistenceDataLoginProcessor((uuid, data) ->
{
_economy.setStore(uuid, data.getGems());
_toTeleport.put(uuid, data.getLocation());
}));
}
@EventHandler(priority = EventPriority.HIGHEST)
public void playerJoin(PlayerJoinEvent event)
{
Player player = event.getPlayer();
if (_toTeleport.containsKey(player.getUniqueId()))
{
player.teleport(_toTeleport.get(player.getUniqueId()));
}
}
@EventHandler
public void playerQuit(PlayerQuitEvent event)
{
Player player = event.getPlayer();
_repository.savePersistence(event.getPlayer(), new PersistenceData(_economy.Get(player), player.getLocation()));
}
@EventHandler
public void playerDeath(PlayerDeathEvent event)
{
Player player = event.getEntity();
_repository.deletePersistence(player);
}
}

View File

@ -0,0 +1,153 @@
package mineplex.gemhunters.persistence;
import mineplex.core.Managers;
import mineplex.core.account.CoreClientManager;
import mineplex.core.account.ILoginProcessor;
import mineplex.core.common.util.UtilServer;
import mineplex.gemhunters.world.WorldDataModule;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.BiConsumer;
public class PersistenceRepository
{
private static final String INSERT_PERSISTENCE = "INSERT INTO gemHunters VALUES (?,?,?);";
private static final String UPDATE_PERSISTENCE = "UPDATE gemHunters SET gems=?,location=? WHERE accountId=?;";
private static final String DELETE_PERSISTENCE = "DELETE FROM gemHunters WHERE accountId=?;";
private final CoreClientManager _client;
private final WorldDataModule _worldData;
private final DataSource _dataSource;
private final List<Integer> _exists;
public PersistenceRepository(DataSource source)
{
_dataSource = source;
_client = Managers.require(CoreClientManager.class);
_worldData = Managers.require(WorldDataModule.class);
_exists = new ArrayList<>();
}
public ILoginProcessor buildPersistenceDataLoginProcessor(BiConsumer<UUID, PersistenceData> consumer)
{
return new ILoginProcessor()
{
@Override
public String getName()
{
return "Gem Hunters Persistence";
}
@Override
public void processLoginResultSet(String playerName, UUID uuid, int accountId, ResultSet resultSet) throws SQLException
{
if (resultSet.next())
{
_exists.add(accountId);
consumer.accept(uuid, fromResultSet(resultSet));
}
}
@Override
public String getQuery(int accountId, String uuid, String name)
{
return "SELECT * FROM gemHunters WHERE accountId=" + accountId + ";";
}
};
}
public void savePersistence(Player player, PersistenceData data)
{
int accountId = _client.Get(player).getAccountId();
UtilServer.runAsync(() ->
{
try (Connection connection = _dataSource.getConnection())
{
boolean update = _exists.contains(accountId);
PreparedStatement statement = connection.prepareStatement(update ? UPDATE_PERSISTENCE : INSERT_PERSISTENCE);
if (update)
{
statement.setInt(1, data.getGems());
statement.setString(2, locationToString(data.getLocation()));
statement.setInt(3, accountId);
}
else
{
statement.setInt(1, accountId);
statement.setInt(2, data.getGems());
statement.setString(3, locationToString(data.getLocation()));
}
_exists.remove(Integer.valueOf(accountId));
statement.executeUpdate();
}
catch (SQLException e)
{
throw new CompletionException(e);
}
});
}
public void deletePersistence(Player player)
{
int accountId = _client.Get(player).getAccountId();
if (!_exists.contains(accountId))
{
return;
}
UtilServer.runAsync(() ->
{
try (Connection connection = _dataSource.getConnection())
{
PreparedStatement statement = connection.prepareStatement(DELETE_PERSISTENCE);
statement.setInt(1, accountId);
statement.executeUpdate();
}
catch (SQLException e)
{
throw new CompletionException(e);
}
});
}
private PersistenceData fromResultSet(ResultSet set) throws SQLException
{
int gems = set.getInt(2);
Location location = stringToLocation(set.getString(3));
return new PersistenceData(gems, location);
}
private Location stringToLocation(String loc)
{
String[] coords = loc.split(",");
return new Location(_worldData.World, Integer.valueOf(coords[0]) + 0.5, Integer.valueOf(coords[1]), Integer.valueOf(coords[2]) + 0.5, Float.valueOf(coords[3]), Float.valueOf(coords[4]));
}
private String locationToString(Location loc)
{
return loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ() + "," + loc.getYaw() + "," + loc.getPitch();
}
}

View File

@ -88,13 +88,13 @@ public class SpawnModule extends MiniPlugin
}
player.teleport(_spawn);
player.setFoodLevel(20);
player.setExhaustion(0);
player.setLevel(0);
player.setExp(0);
player.getInventory().clear();
player.getInventory().setArmorContents(null);
player.updateInventory();
// player.setFoodLevel(20);
// player.setExhaustion(0);
// player.setLevel(0);
// player.setExp(0);
// player.getInventory().clear();
// player.getInventory().setArmorContents(null);
// player.updateInventory();
if (_npcsSpawned)
{

View File

@ -112,7 +112,9 @@ public class WorldDataModule extends MiniPlugin
{
Location loc = stringToLocation(tokens[i]);
if (loc == null)
{
continue;
}
loc.setYaw(currentDirection);
@ -132,7 +134,9 @@ public class WorldDataModule extends MiniPlugin
{
Location loc = stringToLocation(tokens[i]);
if (loc == null)
{
continue;
}
currentData.add(loc);
}
@ -150,7 +154,9 @@ public class WorldDataModule extends MiniPlugin
{
Location loc = stringToLocation(tokens[i]);
if (loc == null)
{
continue;
}
currentData.add(loc);
}

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, () -> {