use of com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile in project Quests by LMBishop.
the class ShopGUIPlusBuyCertainTaskType method afterTransaction.
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void afterTransaction(ShopPostTransactionEvent event) {
ShopTransactionResult result = event.getResult();
ShopAction shopAction = result.getShopAction();
if (shopAction != ShopAction.BUY) {
return;
}
Player player = result.getPlayer();
QPlayerManager playerManager = this.plugin.getPlayerManager();
QPlayer qplayer = playerManager.getPlayer(player.getUniqueId());
if (qplayer == null) {
return;
}
World world = player.getWorld();
String worldName = world.getName();
ShopItem shopItem = result.getShopItem();
Shop shop = shopItem.getShop();
String shopId = shop.getId();
String itemId = shopItem.getId();
int amountBought = result.getAmount();
List<Quest> registeredQuests = super.getRegisteredQuests();
for (Quest quest : registeredQuests) {
if (!qplayer.hasStartedQuest(quest)) {
continue;
}
QuestProgressFile questProgressFile = qplayer.getQuestProgressFile();
QuestProgress questProgress = questProgressFile.getQuestProgress(quest);
String questTypeName = super.getType();
List<Task> taskList = quest.getTasksOfType(questTypeName);
for (Task task : taskList) {
if (!TaskUtils.validateWorld(worldName, task)) {
continue;
}
String taskId = task.getId();
TaskProgress taskProgress = questProgress.getTaskProgress(taskId);
if (taskProgress.isCompleted()) {
continue;
}
String taskShopId = (String) task.getConfigValue("shop-id");
if (taskShopId == null || !taskShopId.equals(shopId)) {
continue;
}
String taskItemId = (String) task.getConfigValue("item-id");
if (taskItemId == null || !taskItemId.equals(itemId)) {
continue;
}
int amountNeeded = (int) task.getConfigValue("amount");
int progressAmount;
Object progress = taskProgress.getProgress();
if (progress == null) {
progressAmount = 0;
} else {
progressAmount = (int) progress;
}
int newProgress = (progressAmount + amountBought);
taskProgress.setProgress(newProgress);
if (newProgress >= amountNeeded) {
taskProgress.setCompleted(true);
}
}
}
}
use of com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile in project Quests by LMBishop.
the class ShopGUIPlusSellCertainTaskType method afterTransaction.
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void afterTransaction(ShopPostTransactionEvent event) {
ShopTransactionResult result = event.getResult();
ShopAction shopAction = result.getShopAction();
if (shopAction != ShopAction.SELL && shopAction != ShopAction.SELL_ALL) {
return;
}
Player player = result.getPlayer();
QPlayerManager playerManager = this.plugin.getPlayerManager();
QPlayer qplayer = playerManager.getPlayer(player.getUniqueId());
if (qplayer == null) {
return;
}
World world = player.getWorld();
String worldName = world.getName();
ShopItem shopItem = result.getShopItem();
Shop shop = shopItem.getShop();
String shopId = shop.getId();
String itemId = shopItem.getId();
int amountSold = result.getAmount();
List<Quest> registeredQuests = super.getRegisteredQuests();
for (Quest quest : registeredQuests) {
if (!qplayer.hasStartedQuest(quest)) {
continue;
}
QuestProgressFile questProgressFile = qplayer.getQuestProgressFile();
QuestProgress questProgress = questProgressFile.getQuestProgress(quest);
String questTypeName = super.getType();
List<Task> taskList = quest.getTasksOfType(questTypeName);
for (Task task : taskList) {
if (!TaskUtils.validateWorld(worldName, task)) {
continue;
}
String taskId = task.getId();
TaskProgress taskProgress = questProgress.getTaskProgress(taskId);
if (taskProgress.isCompleted()) {
continue;
}
String taskShopId = (String) task.getConfigValue("shop-id");
if (taskShopId == null || !taskShopId.equals(shopId)) {
continue;
}
String taskItemId = (String) task.getConfigValue("item-id");
if (taskItemId == null || !taskItemId.equals(itemId)) {
continue;
}
int amountNeeded = (int) task.getConfigValue("amount");
int progressAmount;
Object progress = taskProgress.getProgress();
if (progress == null) {
progressAmount = 0;
} else {
progressAmount = (int) progress;
}
int newProgress = (progressAmount + amountSold);
taskProgress.setProgress(newProgress);
if (newProgress >= amountNeeded) {
taskProgress.setCompleted(true);
}
}
}
}
use of com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile in project Quests by LMBishop.
the class EssentialsBalanceTaskType method onStart.
@Override
public void onStart(Quest quest, Task task, UUID playerUUID) {
Player player = Bukkit.getPlayer(playerUUID);
Essentials ess = (Essentials) Bukkit.getPluginManager().getPlugin("Essentials");
if (player != null && player.isOnline() && ess != null) {
QPlayer qPlayer = plugin.getPlayerManager().getPlayer(playerUUID);
if (qPlayer == null) {
return;
}
QuestProgressFile questProgressFile = qPlayer.getQuestProgressFile();
QuestProgress questProgress = questProgressFile.getQuestProgress(quest);
TaskProgress taskProgress = questProgress.getTaskProgress(task.getId());
int earningsNeeded = (int) task.getConfigValue("amount");
BigDecimal money = ess.getUser(player).getMoney();
taskProgress.setProgress(money);
if (money.compareTo(BigDecimal.valueOf(earningsNeeded)) > 0) {
taskProgress.setCompleted(true);
}
}
}
use of com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile in project Quests by LMBishop.
the class QPlayerManager method loadPlayer.
/**
* Load the player if they exist, otherwise create a new {@link QuestProgressFile}.
* This will have no effect if player is already loaded. Can be invoked asynchronously.
*
* @param uuid the uuid of the player
*/
public void loadPlayer(UUID uuid) {
plugin.getQuestsLogger().debug("Loading player " + uuid + ".");
qPlayers.computeIfAbsent(uuid, s -> {
QuestProgressFile questProgressFile = storageProvider.loadProgressFile(uuid);
if (questProgressFile == null)
return null;
return new QPlayer(plugin, uuid, new QPlayerPreferences(null), questProgressFile, activeQuestController);
});
}
use of com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile in project Quests by LMBishop.
the class MySqlStorageProvider method loadProgressFile.
@Override
@Nullable
public QuestProgressFile loadProgressFile(@NotNull UUID uuid) {
Objects.requireNonNull(uuid, "uuid cannot be null");
if (fault)
return null;
Map<String, Quest> presentQuests = new HashMap<>(plugin.getQuestManager().getQuests());
boolean validateQuests = plugin.getQuestsConfig().getBoolean("options.verify-quest-exists-on-load", true);
QuestProgressFile questProgressFile = new QuestProgressFile(uuid, plugin);
try (Connection connection = hikari.getConnection()) {
plugin.getQuestsLogger().debug("Querying player " + uuid);
Map<String, QuestProgress> questProgressMap = new HashMap<>();
try (PreparedStatement ps = connection.prepareStatement(this.statementProcessor.apply(SELECT_PLAYER_QUEST_PROGRESS))) {
ps.setString(1, uuid.toString());
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String questId = rs.getString(1);
boolean started = rs.getBoolean(2);
long startedDate = rs.getLong(3);
boolean completed = rs.getBoolean(4);
boolean completedBefore = rs.getBoolean(5);
long completionDate = rs.getLong(6);
if (validateQuests && !presentQuests.containsKey(questId))
continue;
QuestProgress questProgress = new QuestProgress(plugin, questId, completed, completedBefore, completionDate, uuid, started, startedDate);
questProgressMap.put(questId, questProgress);
}
}
}
try (PreparedStatement ps = connection.prepareStatement(this.statementProcessor.apply(SELECT_PLAYER_TASK_PROGRESS))) {
ps.setString(1, uuid.toString());
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String questId = rs.getString(1);
String taskId = rs.getString(2);
boolean completed = rs.getBoolean(3);
String encodedProgress = rs.getString(4);
String type = rs.getString(5);
Object progress;
try {
if (type == null) {
progress = null;
} else if (type.equals("double")) {
progress = Double.valueOf(encodedProgress);
} else if (type.equals("float")) {
progress = Float.valueOf(encodedProgress);
} else if (type.equals("int")) {
progress = Integer.valueOf(encodedProgress);
} else {
throw new RuntimeException("unknown data type '" + type + "'");
}
} catch (NumberFormatException ex) {
plugin.getQuestsLogger().warning("Cannot retrieve progress for task '" + taskId + "' in quest '" + questId + "' for player " + uuid + " since data is malformed!");
continue;
} catch (RuntimeException ex) {
if (ex.getMessage().startsWith("unknown data type ")) {
plugin.getQuestsLogger().warning("Cannot retrieve progress for task '" + taskId + "' in quest '" + questId + "' for player " + uuid + ": " + ex.getMessage());
continue;
} else {
throw ex;
}
}
QuestProgress linkedQuestProgress = questProgressMap.get(questId);
if (linkedQuestProgress == null)
continue;
if (validateQuests) {
if (!presentQuests.containsKey(questId))
continue;
if (presentQuests.get(questId).getTaskById(taskId) == null)
continue;
}
TaskProgress questProgress = new TaskProgress(linkedQuestProgress, taskId, progress, uuid, completed);
linkedQuestProgress.addTaskProgress(questProgress);
}
}
}
for (QuestProgress questProgress : questProgressMap.values()) {
questProgressFile.addQuestProgress(questProgress);
}
} catch (SQLException e) {
plugin.getQuestsLogger().severe("Failed to load player: " + uuid + "!");
e.printStackTrace();
return null;
}
return questProgressFile;
}
Aggregations