diff --git a/Plugins/Mineplex.Core/src/mineplex/core/texttutorial/TextTutorialManager.java b/Plugins/Mineplex.Core/src/mineplex/core/texttutorial/TextTutorialManager.java index cd444453e..5a76c4021 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/texttutorial/TextTutorialManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/texttutorial/TextTutorialManager.java @@ -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; @@ -75,6 +76,8 @@ public class TextTutorialManager extends MiniPlugin { other.hidePlayer(event.getPlayer()); } + + ((CraftPlayer) event.getPlayer()).getHandle().spectating = true; return; } @@ -116,7 +119,9 @@ public class TextTutorialManager extends MiniPlugin { other.showPlayer(player); } - + + ((CraftPlayer) player).getHandle().spectating = false; + //Gems if (tut.getGemReward() > 0) { diff --git a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/GemHunters.java b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/GemHunters.java index 76c0c5c84..8e53e7fd0 100644 --- a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/GemHunters.java +++ b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/GemHunters.java @@ -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); diff --git a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/economy/EconomyModule.java b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/economy/EconomyModule.java index db11cf43f..d1b803c6a 100644 --- a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/economy/EconomyModule.java +++ b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/economy/EconomyModule.java @@ -116,6 +116,11 @@ public class EconomyModule extends MiniClientPlugin Set(player, gems); } + public void setStore(UUID player, int gems) + { + Set(player, gems); + } + public int getGems(Player player) { return Get(player); diff --git a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceData.java b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceData.java new file mode 100644 index 000000000..d939056fd --- /dev/null +++ b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceData.java @@ -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; + } + +} diff --git a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceModule.java b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceModule.java new file mode 100644 index 000000000..b1a343fb0 --- /dev/null +++ b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceModule.java @@ -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 _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); + } +} diff --git a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceRepository.java b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceRepository.java new file mode 100644 index 000000000..e6e6fa3b0 --- /dev/null +++ b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/persistence/PersistenceRepository.java @@ -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 _exists; + + public PersistenceRepository(DataSource source) + { + _dataSource = source; + _client = Managers.require(CoreClientManager.class); + _worldData = Managers.require(WorldDataModule.class); + + _exists = new ArrayList<>(); + } + + public ILoginProcessor buildPersistenceDataLoginProcessor(BiConsumer 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(); + } +} diff --git a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/spawn/SpawnModule.java b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/spawn/SpawnModule.java index 9ffa6b0c2..93bbcece0 100644 --- a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/spawn/SpawnModule.java +++ b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/spawn/SpawnModule.java @@ -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) { diff --git a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/world/WorldDataModule.java b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/world/WorldDataModule.java index 6bce809d5..b7315dc08 100644 --- a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/world/WorldDataModule.java +++ b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/world/WorldDataModule.java @@ -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); } @@ -280,7 +286,7 @@ public class WorldDataModule extends MiniPlugin return CUSTOM_LOCAITONS.get(id); } - + public Map> getAllSpawnLocations() { return SPAWN_LOCATIONS; diff --git a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/world/WorldListeners.java b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/world/WorldListeners.java index b81b81279..3688a3f17 100644 --- a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/world/WorldListeners.java +++ b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/world/WorldListeners.java @@ -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, () -> {