Search in sources :

Example 1 with RequirementInjectConfigurationSection

use of com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectConfigurationSection in project AdvancedCore by BenCodez.

the class RewardHandler method loadInjectedRequirements.

public void loadInjectedRequirements() {
    injectedRequirements.add(new RequirementInjectDouble("Chance", 100) {

        @Override
        public boolean onRequirementsRequest(Reward reward, AdvancedCoreUser user, double num, RewardOptions rewardOptions) {
            if (rewardOptions.isIgnoreChance()) {
                return true;
            }
            return MiscUtils.getInstance().checkChance(num, 100);
        }
    }.priority(100).addEditButton(new EditGUIButton(new ItemBuilder("DROPPER"), new EditGUIValueNumber("Chance", null) {

        @Override
        public void setValue(Player player, Number value) {
            RewardEditData reward = (RewardEditData) getInv().getData("Reward");
            reward.setValue(getKey(), value.intValue());
            plugin.reloadAdvancedCore(false);
            reward.reOpenEditGUI(player);
        }
    }.addLore("Set chance for reward to execute"))).validator(new RequirementInjectValidator() {

        @Override
        public void onValidate(Reward reward, RequirementInject inject, ConfigurationSection data) {
            if (data.getDouble(inject.getPath(), 0) == 100) {
                warning(reward, inject, "Chance is 100, if intended then remove the chance option, as it's unneeded");
            } else if (data.getDouble(inject.getPath(), 0) > 100) {
                warning(reward, inject, "Chance is greater than 100, this will always give the reward");
            } else if (data.getDouble(inject.getPath(), 1) == 0) {
                warning(reward, inject, "Chance can not be 0");
            } else if (data.getDouble(inject.getPath(), 1) < 0) {
                warning(reward, inject, "Chance can not be negative");
            }
        }
    }));
    injectedRequirements.add(new RequirementInjectInt("RewardExpiration", -1) {

        @Override
        public boolean onRequirementsRequest(Reward reward, AdvancedCoreUser user, int num, RewardOptions rewardOptions) {
            if (rewardOptions.getPlaceholders().containsKey("ExecDate") && num > 0) {
                long execDate = Long.parseLong(rewardOptions.getPlaceholders().get("ExecDate"));
                debug("ExecDate: " + execDate + ", plus time: " + (execDate + num * 60 * 1000) + ", current time: " + System.currentTimeMillis());
                if (execDate + num * 60 * 1000 > System.currentTimeMillis()) {
                    return true;
                } else {
                    return false;
                }
            }
            return true;
        }
    }.priority(100).addEditButton(new EditGUIButton(new ItemBuilder("CLOCK"), new EditGUIValueNumber("RewardExpiration", null) {

        @Override
        public void setValue(Player player, Number value) {
            RewardEditData reward = (RewardEditData) getInv().getData("Reward");
            reward.setValue(getKey(), value.intValue());
            plugin.reloadAdvancedCore(false);
            reward.reOpenEditGUI(player);
        }
    }.addLore("Time before reward expires, if not executed").addLore("In minutes"))));
    injectedRequirements.add(new RequirementInjectString("Permission", "") {

        @Override
        public boolean onRequirementsRequest(Reward reward, AdvancedCoreUser user, String str, RewardOptions rewardOptions) {
            if (!reward.getConfig().getRequirePermission()) {
                return true;
            }
            if (str.isEmpty()) {
                str = "AdvancedCore.Reward." + reward.getName();
            }
            boolean reverse = false;
            if (str.startsWith("!")) {
                reverse = true;
                str = str.substring(1);
            }
            boolean perm = PlayerUtils.getInstance().hasServerPermission(UUID.fromString(user.getUUID()), str);
            if (reverse) {
                perm = !perm;
            }
            if (!perm) {
                plugin.getLogger().info(user.getPlayerName() + " does not have permission " + str + " to get reward " + reward.getName() + ", reverse: " + reverse);
                return false;
            }
            return true;
        }
    }.priority(100).alwaysForce().addEditButton(new EditGUIButton(new ItemBuilder("IRON_DOOR"), new EditGUIValueString("Permission", null) {

        @Override
        public void setValue(Player player, String value) {
            RewardEditData reward = (RewardEditData) getInv().getData("Reward");
            reward.setValue(getKey(), value);
            plugin.reloadAdvancedCore(false);
            reward.reOpenEditGUI(player);
        }
    }.addLore("Set permission required to be given, set RequirePermission to true if using this"))).addEditButton(new EditGUIButton(new EditGUIValueBoolean("RequirePermission", null) {

        @Override
        public void setValue(Player player, boolean value) {
            RewardEditData reward = (RewardEditData) getInv().getData("Reward");
            reward.setValue(getKey(), value);
            plugin.reloadAdvancedCore(false);
            reward.reOpenEditGUI(player);
        }
    }.addLore("If true, permission is required to run reward"))).validator(new RequirementInjectValidator() {

        @Override
        public void onValidate(Reward reward, RequirementInject inject, ConfigurationSection data) {
            if (!data.getBoolean("RequirePermission", false)) {
                if (!data.getString("Permission", "").isEmpty()) {
                    warning(reward, inject, "Detected permission set but RequirePermission is false");
                }
            }
        }
    }.addPath("RequirePermission")));
    injectedRequirements.add(new RequirementInjectString("Server", "") {

        @Override
        public boolean onRequirementsRequest(Reward reward, AdvancedCoreUser user, String str, RewardOptions rewardOptions) {
            // set server to match
            String serverToMatch = str;
            boolean hadPlaceholder = false;
            if (str.isEmpty()) {
                if (rewardOptions.getPlaceholders().containsKey("Server")) {
                    serverToMatch = rewardOptions.getPlaceholders().get("Server");
                    hadPlaceholder = true;
                } else if (!rewardOptions.getServer().isEmpty()) {
                    serverToMatch = rewardOptions.getServer();
                }
            }
            String currentServer = AdvancedCorePlugin.getInstance().getOptions().getServer();
            if (!serverToMatch.isEmpty()) {
                debug("Current Server: " + currentServer + ", ServerToMatch: " + serverToMatch);
                boolean matched = serverToMatch.equalsIgnoreCase(currentServer);
                if (!matched && !hadPlaceholder) {
                    // add server for offline reward
                    rewardOptions.addPlaceholder("Server", serverToMatch);
                }
                return matched;
            }
            return true;
        }
    }.priority(100).allowReattempt().alwaysForceNoData().addEditButton(new EditGUIButton(new ItemBuilder(Material.PAPER), new EditGUIValueString("Server", null) {

        @Override
        public void setValue(Player player, String value) {
            RewardEditData reward = (RewardEditData) getInv().getData("Reward");
            reward.setValue(getKey(), value);
            plugin.reloadAdvancedCore(false);
        }
    }.addOptions(Bukkit.getServer().getName()).addLore("Server to execute reward on"))));
    injectedRequirements.add(new RequirementInjectStringList("Worlds", new ArrayList<String>()) {

        @Override
        public boolean onRequirementsRequest(Reward reward, AdvancedCoreUser user, ArrayList<String> worlds, RewardOptions rewardOptions) {
            if (worlds.isEmpty()) {
                return true;
            }
            Player player = user.getPlayer();
            if (player == null) {
                return false;
            }
            reward.checkRewardFile();
            String world = player.getWorld().getName();
            if (worlds.contains(world)) {
                return true;
            }
            user.setCheckWorld(true);
            return false;
        }
    }.priority(100).allowReattempt().alwaysForce().addEditButton(new EditGUIButton(new ItemBuilder("END_PORTAL_FRAME"), new EditGUIValueList("Worlds", null) {

        @Override
        public void setValue(Player player, ArrayList<String> value) {
            RewardEditData reward = (RewardEditData) getInv().getData("Reward");
            reward.setValue(getKey(), value);
            plugin.reloadAdvancedCore(false);
            reward.reOpenEditGUI(player);
        }
    }.addLore("Worlds to execute reward in, only executes into one reward"))).validator(new RequirementInjectValidator() {

        @Override
        @SuppressWarnings("unchecked")
        public void onValidate(Reward reward, RequirementInject inject, ConfigurationSection data) {
            ArrayList<String> list = (ArrayList<String>) data.getList("Worlds", null);
            if (list != null) {
                if (list.isEmpty()) {
                    warning(reward, inject, "No worlds were listed");
                }
            }
        }
    }));
    injectedRequirements.add(new RequirementInjectString("RewardType", "BOTH") {

        @Override
        public boolean onRequirementsRequest(Reward reward, AdvancedCoreUser user, String type, RewardOptions rewardOptions) {
            if (rewardOptions.isOnline()) {
                if (type.equalsIgnoreCase("offline")) {
                    plugin.debug("Reward Type Don't match");
                    return false;
                }
            } else {
                if (type.equalsIgnoreCase("online")) {
                    plugin.debug("Reward Type Don't match");
                    return false;
                }
            }
            return true;
        }
    }.priority(100).addEditButton(new EditGUIButton(new ItemBuilder("REDSTONE_TORCH"), new EditGUIValueString("RewardType", null) {

        @Override
        public void setValue(Player player, String value) {
            RewardEditData reward = (RewardEditData) getInv().getData("Reward");
            reward.setValue(getKey(), value);
            plugin.reloadAdvancedCore(false);
            reward.reOpenEditGUI(player);
        }
    }.addOptions("ONLINE", "OFFLINE", "BOTH").addLore("Define whether should execute if player was offline/online"))));
    injectedRequirements.add(new RequirementInjectString("JavascriptExpression", "") {

        @Override
        public boolean onRequirementsRequest(Reward reward, AdvancedCoreUser user, String expression, RewardOptions rewardOptions) {
            if (expression.equals("") || new JavascriptEngine().addPlayer(user.getOfflinePlayer()).getBooleanValue(StringParser.getInstance().replacePlaceHolder(expression, rewardOptions.getPlaceholders()))) {
                return true;
            }
            return false;
        }
    }.priority(90).addEditButton(new EditGUIButton(new ItemBuilder("DETECTOR_RAIL"), new EditGUIValueString("JavascriptExpression", null) {

        @Override
        public void setValue(Player player, String value) {
            RewardEditData reward = (RewardEditData) getInv().getData("Reward");
            reward.setValue(getKey(), value);
            plugin.reloadAdvancedCore(false);
        }
    }.addLore("Javascript expression required to run reward"))).validator(new RequirementInjectValidator() {

        @Override
        public void onValidate(Reward reward, RequirementInject inject, ConfigurationSection data) {
            String str = data.getString("JavascriptExpression", null);
            if (str != null) {
                if (str.isEmpty()) {
                    warning(reward, inject, "No javascript expression set");
                }
            }
        }
    }));
    injectedRequirements.add(new RequirementInjectConfigurationSection("LocationDistance") {

        @Override
        public boolean onRequirementsRequested(Reward reward, AdvancedCoreUser user, ConfigurationSection section, RewardOptions rewardOptions) {
            if (!user.isOnline()) {
                plugin.debug("user not online");
                return false;
            }
            Location loc = new Location(Bukkit.getWorld(section.getString("World")), section.getInt("X"), section.getInt("Y"), section.getInt("Z"));
            Location pLoc = user.getPlayer().getLocation();
            if (!loc.getWorld().getName().equals(pLoc.getWorld().getName())) {
                plugin.debug("Worlds don't match");
                return false;
            }
            if (pLoc.distance(loc) < section.getInt("Distance")) {
                return true;
            }
            return false;
        }
    }.priority(90).validator(new RequirementInjectValidator() {

        @Override
        public void onValidate(Reward reward, RequirementInject inject, ConfigurationSection data) {
            if (!data.isConfigurationSection("LocationDistance")) {
                return;
            }
            ConfigurationSection section = data.getConfigurationSection("LocationDistance");
            try {
                new Location(Bukkit.getWorld(section.getString("World")), section.getInt("X"), section.getInt("Y"), section.getInt("Z"));
            } catch (Exception e) {
                warning(reward, inject, "Failed to get location for LocationDistance");
                e.printStackTrace();
            }
            if (section.getInt("Distance") < 0) {
                warning(reward, inject, "Invalid distance for LocationDistance");
            }
        }
    }).addEditButton(new EditGUIButton(new ItemBuilder("MAP"), new EditGUIValueInventory("LocationDistance") {

        @Override
        public void openInventory(ClickEvent clickEvent) {
            RewardEditData reward = (RewardEditData) getInv().getData("Reward");
            new RewardEditLocationDistance() {

                @Override
                public void setVal(String key, Object value) {
                    RewardEditData reward = (RewardEditData) getInv().getData("Reward");
                    reward.setValue(key, value);
                    plugin.reloadAdvancedCore(false);
                }
            }.open(clickEvent.getPlayer(), reward);
        }
    }.addLore("Require player to be within a certain distance of locaction to get reward"))));
    for (RequirementInject reward : injectedRequirements) {
        reward.setInternalReward(true);
    }
    sortInjectedRequirements();
}
Also used : EditGUIValueBoolean(com.bencodez.advancedcore.api.inventory.editgui.valuetypes.EditGUIValueBoolean) ClickEvent(com.bencodez.advancedcore.api.inventory.BInventory.ClickEvent) RequirementInjectString(com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectString) ArrayList(java.util.ArrayList) RewardEditLocationDistance(com.bencodez.advancedcore.api.rewards.editbuttons.RewardEditLocationDistance) RequirementInjectStringList(com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectStringList) RewardInjectString(com.bencodez.advancedcore.api.rewards.injected.RewardInjectString) EditGUIValueString(com.bencodez.advancedcore.api.inventory.editgui.valuetypes.EditGUIValueString) RequirementInjectString(com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectString) RequirementInjectConfigurationSection(com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectConfigurationSection) RequirementInjectInt(com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectInt) EditGUIValueNumber(com.bencodez.advancedcore.api.inventory.editgui.valuetypes.EditGUIValueNumber) EditGUIValueInventory(com.bencodez.advancedcore.api.inventory.editgui.valuetypes.EditGUIValueInventory) RequirementInjectValidator(com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectValidator) Player(org.bukkit.entity.Player) EditGUIValueString(com.bencodez.advancedcore.api.inventory.editgui.valuetypes.EditGUIValueString) RequirementInjectDouble(com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectDouble) JavascriptEngine(com.bencodez.advancedcore.api.javascript.JavascriptEngine) FileDirectoryException(com.bencodez.advancedcore.api.exceptions.FileDirectoryException) AdvancedCoreUser(com.bencodez.advancedcore.api.user.AdvancedCoreUser) ItemBuilder(com.bencodez.advancedcore.api.item.ItemBuilder) EditGUIValueList(com.bencodez.advancedcore.api.inventory.editgui.valuetypes.EditGUIValueList) EditGUIButton(com.bencodez.advancedcore.api.inventory.editgui.EditGUIButton) EditGUIValueNumber(com.bencodez.advancedcore.api.inventory.editgui.valuetypes.EditGUIValueNumber) RewardEditAdvancedRandomReward(com.bencodez.advancedcore.api.rewards.editbuttons.RewardEditAdvancedRandomReward) RequirementInject(com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInject) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) RequirementInjectConfigurationSection(com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectConfigurationSection) RewardInjectConfigurationSection(com.bencodez.advancedcore.api.rewards.injected.RewardInjectConfigurationSection) Location(org.bukkit.Location)

Aggregations

FileDirectoryException (com.bencodez.advancedcore.api.exceptions.FileDirectoryException)1 ClickEvent (com.bencodez.advancedcore.api.inventory.BInventory.ClickEvent)1 EditGUIButton (com.bencodez.advancedcore.api.inventory.editgui.EditGUIButton)1 EditGUIValueBoolean (com.bencodez.advancedcore.api.inventory.editgui.valuetypes.EditGUIValueBoolean)1 EditGUIValueInventory (com.bencodez.advancedcore.api.inventory.editgui.valuetypes.EditGUIValueInventory)1 EditGUIValueList (com.bencodez.advancedcore.api.inventory.editgui.valuetypes.EditGUIValueList)1 EditGUIValueNumber (com.bencodez.advancedcore.api.inventory.editgui.valuetypes.EditGUIValueNumber)1 EditGUIValueString (com.bencodez.advancedcore.api.inventory.editgui.valuetypes.EditGUIValueString)1 ItemBuilder (com.bencodez.advancedcore.api.item.ItemBuilder)1 JavascriptEngine (com.bencodez.advancedcore.api.javascript.JavascriptEngine)1 RewardEditAdvancedRandomReward (com.bencodez.advancedcore.api.rewards.editbuttons.RewardEditAdvancedRandomReward)1 RewardEditLocationDistance (com.bencodez.advancedcore.api.rewards.editbuttons.RewardEditLocationDistance)1 RewardInjectConfigurationSection (com.bencodez.advancedcore.api.rewards.injected.RewardInjectConfigurationSection)1 RewardInjectString (com.bencodez.advancedcore.api.rewards.injected.RewardInjectString)1 RequirementInject (com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInject)1 RequirementInjectConfigurationSection (com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectConfigurationSection)1 RequirementInjectDouble (com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectDouble)1 RequirementInjectInt (com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectInt)1 RequirementInjectString (com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectString)1 RequirementInjectStringList (com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInjectStringList)1