Search in sources :

Example 1 with QuestProgressFile

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);
            }
        }
    }
}
Also used : QuestProgress(com.leonardobishop.quests.common.player.questprogressfile.QuestProgress) ShopAction(net.brcdev.shopgui.shop.ShopManager.ShopAction) QPlayer(com.leonardobishop.quests.common.player.QPlayer) Player(org.bukkit.entity.Player) Task(com.leonardobishop.quests.common.quest.Task) ShopTransactionResult(net.brcdev.shopgui.shop.ShopTransactionResult) ShopItem(net.brcdev.shopgui.shop.ShopItem) TaskProgress(com.leonardobishop.quests.common.player.questprogressfile.TaskProgress) World(org.bukkit.World) QPlayerManager(com.leonardobishop.quests.common.player.QPlayerManager) QPlayer(com.leonardobishop.quests.common.player.QPlayer) Quest(com.leonardobishop.quests.common.quest.Quest) QuestProgressFile(com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile) Shop(net.brcdev.shopgui.shop.Shop) EventHandler(org.bukkit.event.EventHandler)

Example 2 with QuestProgressFile

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);
            }
        }
    }
}
Also used : QuestProgress(com.leonardobishop.quests.common.player.questprogressfile.QuestProgress) ShopAction(net.brcdev.shopgui.shop.ShopManager.ShopAction) QPlayer(com.leonardobishop.quests.common.player.QPlayer) Player(org.bukkit.entity.Player) Task(com.leonardobishop.quests.common.quest.Task) ShopTransactionResult(net.brcdev.shopgui.shop.ShopTransactionResult) ShopItem(net.brcdev.shopgui.shop.ShopItem) TaskProgress(com.leonardobishop.quests.common.player.questprogressfile.TaskProgress) World(org.bukkit.World) QPlayerManager(com.leonardobishop.quests.common.player.QPlayerManager) QPlayer(com.leonardobishop.quests.common.player.QPlayer) Quest(com.leonardobishop.quests.common.quest.Quest) QuestProgressFile(com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile) Shop(net.brcdev.shopgui.shop.Shop) EventHandler(org.bukkit.event.EventHandler)

Example 3 with QuestProgressFile

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);
        }
    }
}
Also used : QuestProgress(com.leonardobishop.quests.common.player.questprogressfile.QuestProgress) QPlayer(com.leonardobishop.quests.common.player.QPlayer) Player(org.bukkit.entity.Player) Essentials(com.earth2me.essentials.Essentials) TaskProgress(com.leonardobishop.quests.common.player.questprogressfile.TaskProgress) QPlayer(com.leonardobishop.quests.common.player.QPlayer) QuestProgressFile(com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile) BigDecimal(java.math.BigDecimal)

Example 4 with QuestProgressFile

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);
    });
}
Also used : QuestProgressFile(com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile)

Example 5 with QuestProgressFile

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;
}
Also used : QuestProgress(com.leonardobishop.quests.common.player.questprogressfile.QuestProgress) TaskProgress(com.leonardobishop.quests.common.player.questprogressfile.TaskProgress) Quest(com.leonardobishop.quests.common.quest.Quest) QuestProgressFile(com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

QuestProgressFile (com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile)18 Quest (com.leonardobishop.quests.common.quest.Quest)10 QuestProgress (com.leonardobishop.quests.common.player.questprogressfile.QuestProgress)8 QPlayer (com.leonardobishop.quests.common.player.QPlayer)7 TaskProgress (com.leonardobishop.quests.common.player.questprogressfile.TaskProgress)7 Player (org.bukkit.entity.Player)5 File (java.io.File)4 IOException (java.io.IOException)4 Task (com.leonardobishop.quests.common.quest.Task)3 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)3 EventHandler (org.bukkit.event.EventHandler)3 QPlayerManager (com.leonardobishop.quests.common.player.QPlayerManager)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 Shop (net.brcdev.shopgui.shop.Shop)2 ShopItem (net.brcdev.shopgui.shop.ShopItem)2 ShopAction (net.brcdev.shopgui.shop.ShopManager.ShopAction)2 ShopTransactionResult (net.brcdev.shopgui.shop.ShopTransactionResult)2 World (org.bukkit.World)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2