use of com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInject in project AdvancedCore by BenCodez.
the class RewardEditGUI method openRewardGUIRequirements.
public void openRewardGUIRequirements(Player player, RewardEditData rewardEditData, String rewardName) {
EditGUI inv = new EditGUI("Requirements: " + rewardName);
inv.requirePermission(AdvancedCorePlugin.getInstance().getOptions().getPermPrefix() + ".RewardEdit");
inv.addData("Reward", rewardEditData);
inv.addButton(new EditGUIButton(new ItemBuilder(Material.PAPER), new EditGUIValueBoolean("ForceOffline", rewardEditData.getValue("ForceOffline")) {
@Override
public void setValue(Player player, boolean value) {
RewardEditData reward = (RewardEditData) getInv().getData("Reward");
reward.setValue(getKey(), value);
plugin.reloadAdvancedCore(false);
}
}.addLore("Force reward to execute to run while player is offline")));
inv.addButton(new EditGUIButton(new ItemBuilder(Material.PAPER), new EditGUIValueInventory("Delayed") {
@Override
public void openInventory(ClickEvent clickEvent) {
RewardEditData reward = (RewardEditData) getInv().getData("Reward");
new RewardEditDelayed() {
@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("Delay reward from being executed")));
inv.addButton(new EditGUIButton(new ItemBuilder(Material.PAPER), new EditGUIValueInventory("Timed") {
@Override
public void openInventory(ClickEvent clickEvent) {
RewardEditData reward = (RewardEditData) getInv().getData("Reward");
new RewardEditTimed() {
@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("Execute reward at certain time")));
for (RequirementInject injectReward : plugin.getRewardHandler().getInjectedRequirements()) {
if (injectReward.isEditable()) {
for (BInventoryButton b : injectReward.getEditButtons()) {
if (b instanceof EditGUIButton) {
EditGUIButton eb = (EditGUIButton) b;
if (eb.getEditor().isCanGetValue()) {
eb.getEditor().setCurrentValue(rewardEditData.getValue(eb.getEditor().getKey()));
}
inv.addButton(eb);
} else {
inv.addButton(b);
}
}
}
}
inv.sort();
inv.addButton(new BInventoryButton(new ItemBuilder(Material.BARRIER).setName("&cGo back")) {
@Override
public void onClick(ClickEvent clickEvent) {
openRewardGUI(clickEvent.getPlayer(), rewardEditData, rewardName);
}
});
inv.openInventory(player);
}
use of com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInject in project AdvancedCore by BenCodez.
the class Reward method giveReward.
public void giveReward(AdvancedCoreUser user, RewardOptions rewardOptions) {
if (!AdvancedCorePlugin.getInstance().getOptions().isProcessRewards()) {
AdvancedCorePlugin.getInstance().getLogger().warning("Processing rewards is disabled");
return;
}
if (rewardOptions == null) {
rewardOptions = new RewardOptions();
}
if (!rewardOptions.getPlaceholders().containsKey("ExecDate")) {
rewardOptions.addPlaceholder("ExecDate", "" + System.currentTimeMillis());
}
if (!rewardOptions.getPlaceholders().containsKey("date")) {
try {
LocalDateTime ldt = LocalDateTime.now();
Date date = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
rewardOptions.addPlaceholder("Date", "" + new SimpleDateFormat(plugin.getOptions().getFormatRewardTimeFormat()).format(date));
} catch (Exception e) {
e.printStackTrace();
}
}
PlayerRewardEvent event = new PlayerRewardEvent(this, user, rewardOptions);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
plugin.debug("Reward " + name + " was cancelled for " + user.getPlayerName());
return;
}
if (rewardOptions.isCheckTimed()) {
if (checkDelayed(user, rewardOptions.getPlaceholders()) || checkTimed(user, rewardOptions.getPlaceholders())) {
return;
}
}
if (!rewardOptions.isOnlineSet()) {
rewardOptions.setOnline(user.isOnline());
}
for (RewardPlaceholderHandle handle : plugin.getRewardHandler().getPlaceholders()) {
if (handle.isPreProcess()) {
rewardOptions.addPlaceholder(handle.getKey(), handle.getValue(this, user));
}
}
// Check requirements
boolean allowOffline = false;
boolean canGive = true;
if (!rewardOptions.isIgnoreRequirements()) {
for (RequirementInject inject : plugin.getRewardHandler().getInjectedRequirements()) {
try {
plugin.extraDebug(getRewardName() + ": Checking requirement " + inject.getPath() + ":" + inject.getPriority());
if (!inject.onRequirementRequest(this, user, getConfig().getConfigData(), rewardOptions)) {
plugin.debug(getRewardName() + ": Requirement failed " + inject.getPath() + ":" + inject.isAllowReattempt());
canGive = false;
if (inject.isAllowReattempt()) {
allowOffline = true;
} else {
return;
}
}
} catch (Exception e) {
plugin.debug("Failed to check requirement " + inject.getPath());
e.printStackTrace();
canGive = false;
}
}
}
if ((plugin.getOptions().isTreatVanishAsOffline() && user.isVanished())) {
checkRewardFile();
user.addOfflineRewards(this, rewardOptions.getPlaceholders());
plugin.getLogger().info(getRewardName() + ": " + user.getPlayerName() + " is vanished, saving reward offline");
return;
}
// save reward for offline
if (((((!rewardOptions.isOnline() || rewardOptions.getServer() != null) && !user.isOnline()) || allowOffline) && (!isForceOffline() && !rewardOptions.isForceOffline()))) {
if (rewardOptions.isGiveOffline()) {
checkRewardFile();
user.addOfflineRewards(this, rewardOptions.getPlaceholders());
plugin.debug("Saving offline reward " + getRewardName() + " for " + user.getPlayerName());
}
return;
}
// give reward
if (canGive || isForceOffline() || rewardOptions.isForceOffline()) {
plugin.debug(name + ": Passed requirements, attempting to give to " + user.getPlayerName() + "/" + user.getUUID());
giveRewardUser(user, rewardOptions.getPlaceholders(), rewardOptions);
}
}
use of com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInject 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();
}
Aggregations