From a9778c94bbd0b86b1003041a75f94addf72e8f07 Mon Sep 17 00:00:00 2001 From: AlonsoAliaga Date: Mon, 24 Jul 2023 15:41:21 -0500 Subject: [PATCH 1/3] Added executecontinue and some ConditionTypes. --- .../ce/ajneb97/managers/EventsManager.java | 151 +++++++++++------- .../ce/ajneb97/managers/VerifyManager.java | 2 +- .../ce/ajneb97/model/ConditionalType.java | 9 +- 3 files changed, 102 insertions(+), 60 deletions(-) diff --git a/src/main/java/ce/ajneb97/managers/EventsManager.java b/src/main/java/ce/ajneb97/managers/EventsManager.java index 6ee6095..a83b675 100644 --- a/src/main/java/ce/ajneb97/managers/EventsManager.java +++ b/src/main/java/ce/ajneb97/managers/EventsManager.java @@ -74,66 +74,70 @@ public void checkSingularEvent(ConditionEvent conditionEvent,CEEvent event){ } //Check condition list - CheckConditionsResult conditionsResult = checkConditions(conditionEvent,isPlaceholderAPI); - if(!conditionsResult.isConditionsAccomplished()){ - return; - } - String executeActionGroup = conditionsResult.getExecuteActionGroup(); + List conditionsResults = checkConditions(conditionEvent,isPlaceholderAPI); + for (CheckConditionsResult conditionsResult : conditionsResults) { + if(!conditionsResult.isConditionsAccomplished()){ + return; + } + String executeActionGroup = conditionsResult.getExecuteActionGroup(); - PlayerManager playerManager = plugin.getPlayerManager(); - MessagesManager messagesManager = plugin.getMessagesManager(); + PlayerManager playerManager = plugin.getPlayerManager(); + MessagesManager messagesManager = plugin.getMessagesManager(); - boolean bypassCooldown = false; + boolean bypassCooldown = false; - if(player != null){ - bypassCooldown = player.hasPermission("conditionalevents.bypasscooldown."+event.getName()); + if(player != null){ + bypassCooldown = player.hasPermission("conditionalevents.bypasscooldown."+event.getName()); - //Check One time - if(event.isOneTime()){ - boolean isOneTime = playerManager.getEventOneTime(event.getName(),player); - if(isOneTime){ - ExecutedEvent executedEvent = new ExecutedEvent(player, conditionEvent.getEventVariables(), event, - "one_time", conditionEvent.getMinecraftEvent(), conditionEvent.getTarget(), plugin); - executedEvent.executeActions(); - return; + //Check One time + if(event.isOneTime()){ + boolean isOneTime = playerManager.getEventOneTime(event.getName(),player); + if(isOneTime){ + ExecutedEvent executedEvent = new ExecutedEvent(player, conditionEvent.getEventVariables(), event, + "one_time", conditionEvent.getMinecraftEvent(), conditionEvent.getTarget(), plugin); + executedEvent.executeActions(); + return; + } } - } - //Check Cooldown - if(event.getCooldown() != 0 && !bypassCooldown){ - long eventCooldownMillis = playerManager.getEventCooldown(event.getName(),player)+(event.getCooldown()*1000); - long currentTimeMillis = System.currentTimeMillis(); - if(eventCooldownMillis > currentTimeMillis){ - String timeString = TimeUtils.getTime((eventCooldownMillis-currentTimeMillis)/1000,messagesManager); - conditionEvent.getEventVariables().add(new StoredVariable("%time%",timeString)); - ExecutedEvent executedEvent = new ExecutedEvent(player, conditionEvent.getEventVariables(), event, - "cooldown", conditionEvent.getMinecraftEvent(), conditionEvent.getTarget(), plugin); - executedEvent.executeActions(); - return; + //Check Cooldown + if(event.getCooldown() != 0 && !bypassCooldown){ + long eventCooldownMillis = playerManager.getEventCooldown(event.getName(),player)+(event.getCooldown()*1000); + long currentTimeMillis = System.currentTimeMillis(); + if(eventCooldownMillis > currentTimeMillis){ + String timeString = TimeUtils.getTime((eventCooldownMillis-currentTimeMillis)/1000,messagesManager); + conditionEvent.getEventVariables().add(new StoredVariable("%time%",timeString)); + ExecutedEvent executedEvent = new ExecutedEvent(player, conditionEvent.getEventVariables(), event, + "cooldown", conditionEvent.getMinecraftEvent(), conditionEvent.getTarget(), plugin); + executedEvent.executeActions(); + return; + } } } - } - if(player != null){ - //Set One Time - if(event.isOneTime()){ - if(!event.getPreventOneTimeActivationActionGroups().contains(executeActionGroup)){ - playerManager.setEventOneTime(event.getName(),player); + if(player != null){ + //Set One Time + if(event.isOneTime()){ + if(!event.getPreventOneTimeActivationActionGroups().contains(executeActionGroup)){ + playerManager.setEventOneTime(event.getName(),player); + } } - } - //Set Cooldown - if(event.getCooldown() != 0 && !bypassCooldown){ - if(!event.getPreventCooldownActivationActionGroups().contains(executeActionGroup)){ - playerManager.setEventCooldown(event.getName(),player); + //Set Cooldown + if(event.getCooldown() != 0 && !bypassCooldown){ + if(!event.getPreventCooldownActivationActionGroups().contains(executeActionGroup)){ + playerManager.setEventCooldown(event.getName(),player); + } } } - } - //Execute actions - ExecutedEvent executedEvent = new ExecutedEvent(player, conditionEvent.getEventVariables(), event, - executeActionGroup, conditionEvent.getMinecraftEvent(), conditionEvent.getTarget(), plugin); - executedEvent.executeActions(); + //Execute actions + ExecutedEvent executedEvent = new ExecutedEvent(player, conditionEvent.getEventVariables(), event, + executeActionGroup, conditionEvent.getMinecraftEvent(), conditionEvent.getTarget(), plugin); + executedEvent.executeActions(); + } + //Check condition list + //CheckConditionsResult conditionsResult = checkConditions(conditionEvent,isPlaceholderAPI); } public ArrayList getValidEvents(EventType eventType){ @@ -146,7 +150,7 @@ public ArrayList getValidEvents(EventType eventType){ return validEvents; } - private CheckConditionsResult checkConditions(ConditionEvent conditionEvent, boolean isPlaceholderAPI){ + private List checkConditions(ConditionEvent conditionEvent, boolean isPlaceholderAPI){ List conditions = new ArrayList(conditionEvent.getCurrentEvent().getConditions()); String eventName = conditionEvent.getCurrentEvent().getName(); Player player = conditionEvent.getPlayer(); @@ -159,11 +163,19 @@ private CheckConditionsResult checkConditions(ConditionEvent conditionEvent, boo //Check condition lines ArrayList storedVariables = conditionEvent.getEventVariables(); + List conditionsResults = new ArrayList<>(); + for(int i=0;i conditionGroup, Player player, boolean isPlaceholderAPI, @@ -306,8 +331,8 @@ public boolean checkToConditionAction(List conditionGroup, Player player double firstArgNum = 0; double secondArgNum = 0; try{ - firstArgNum = Double.valueOf(firstArg); - secondArgNum = Double.valueOf(secondArg); + firstArgNum = Double.parseDouble(firstArg); + secondArgNum = Double.parseDouble(secondArg); }catch(NumberFormatException e){ } @@ -327,6 +352,14 @@ public boolean checkToConditionAction(List conditionGroup, Player player if(firstArgLower.startsWith(secondArgLower)) approvedLine = true;break; case NOT_STARTS_WITH: if(!firstArgLower.startsWith(secondArgLower)) approvedLine = true;break; + case ENDS_WITH: + if(firstArgLower.endsWith(secondArgLower)) approvedLine = true;break; + case NOT_ENDS_WITH: + if(!firstArgLower.endsWith(secondArgLower)) approvedLine = true;break; + case MATCHES_WITH: + if(firstArg.matches(secondArg)) approvedLine = true;break; + case NOT_MATCHES_WITH: + if(!firstArg.matches(secondArg)) approvedLine = true;break; case CONTAINS: if(firstArgLower.contains(secondArgLower)) approvedLine = true;break; case NOT_CONTAINS: @@ -339,6 +372,10 @@ public boolean checkToConditionAction(List conditionGroup, Player player if(firstArgNum < secondArgNum) approvedLine = true;break; case LOWER_EQUALS: if(firstArgNum <= secondArgNum) approvedLine = true;break; + case IS_MULTIPLE_OF: + if(firstArgNum % secondArgNum == 0) approvedLine = true;break; + case NOT_IS_MULTIPLE_OF: + if(firstArgNum % secondArgNum != 0) approvedLine = true;break; } } if(approvedLine){ diff --git a/src/main/java/ce/ajneb97/managers/VerifyManager.java b/src/main/java/ce/ajneb97/managers/VerifyManager.java index 1ba8288..878c577 100644 --- a/src/main/java/ce/ajneb97/managers/VerifyManager.java +++ b/src/main/java/ce/ajneb97/managers/VerifyManager.java @@ -154,7 +154,7 @@ private boolean verifyRandomVariable(String line){ } public boolean verifyCondition(String line) { - String[] sepExecute = line.split(" execute "); + String[] sepExecute = line.contains(" executecontinue ") ? line.split(" executecontinue ") : line.split(" execute "); String[] sepOr = sepExecute[0].split(" or "); for(int i=0;i"), GREATER_EQUALS(">="), LOWER("<"), - LOWER_EQUALS("<="); - + LOWER_EQUALS("<="), + IS_MULTIPLE_OF("isMultipleOf"), + NOT_IS_MULTIPLE_OF("!isMultipleOf"); private String text; ConditionalType(String text) { this.text = text; From 633b7d2851064099fec3e331e35c400b7df0fe21 Mon Sep 17 00:00:00 2001 From: AlonsoAliaga Date: Sat, 29 Jul 2023 18:22:32 -0500 Subject: [PATCH 2/3] Improved condition type checks. --- src/main/java/ce/ajneb97/managers/EventsManager.java | 8 ++------ src/main/java/ce/ajneb97/managers/VerifyManager.java | 9 ++++++++- src/main/java/ce/ajneb97/model/ConditionalType.java | 9 +++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/ce/ajneb97/managers/EventsManager.java b/src/main/java/ce/ajneb97/managers/EventsManager.java index a83b675..c087a49 100644 --- a/src/main/java/ce/ajneb97/managers/EventsManager.java +++ b/src/main/java/ce/ajneb97/managers/EventsManager.java @@ -217,9 +217,7 @@ private List checkConditions(ConditionEvent conditionEven try{ firstArgNum = Double.parseDouble(firstArg); secondArgNum = Double.parseDouble(secondArg); - }catch(NumberFormatException e){ - - } + }catch(NumberFormatException ignored){} switch(conditionalType){ case EQUALS: @@ -333,9 +331,7 @@ public boolean checkToConditionAction(List conditionGroup, Player player try{ firstArgNum = Double.parseDouble(firstArg); secondArgNum = Double.parseDouble(secondArg); - }catch(NumberFormatException e){ - - } + }catch(NumberFormatException ignored){} switch(conditionalType){ case EQUALS: diff --git a/src/main/java/ce/ajneb97/managers/VerifyManager.java b/src/main/java/ce/ajneb97/managers/VerifyManager.java index 878c577..7fd1e18 100644 --- a/src/main/java/ce/ajneb97/managers/VerifyManager.java +++ b/src/main/java/ce/ajneb97/managers/VerifyManager.java @@ -3,6 +3,7 @@ import ce.ajneb97.ConditionalEvents; import ce.ajneb97.configs.CEConfig; import ce.ajneb97.model.CEEvent; +import ce.ajneb97.model.ConditionalType; import ce.ajneb97.model.EventType; import ce.ajneb97.model.actions.ActionGroup; import ce.ajneb97.model.actions.ActionTargeter; @@ -161,13 +162,19 @@ public boolean verifyCondition(String line) { if(sep.length < 3) { return false; } + if(!ConditionalType.isValidConditionType(sep[1])) return false; + /* To remove? if(!sep[1].equals("!=") && !sep[1].equals("==") && !sep[1].equals(">=") && !sep[1].equals("<=") && !sep[1].equals(">") && !sep[1].equals("<") && !sep[1].equals("equals") && !sep[1].equals("!equals") && !sep[1].equals("contains") && !sep[1].equals("!contains") && !sep[1].equals("startsWith") && !sep[1].equals("!startsWith") - && !sep[1].equals("equalsIgnoreCase") && !sep[1].equals("!equalsIgnoreCase")) { + && !sep[1].equals("equalsIgnoreCase") && !sep[1].equals("!equalsIgnoreCase") + && !sep[1].equals("endsWith") && !sep[1].equals("!endsWith") + && !sep[1].equals("matchesWith") && !sep[1].equals("!matchesWith") + && !sep[1].equals("isMultipleOf") && !sep[1].equals("!isMultipleOf")) { return false; } + */ } return true; } diff --git a/src/main/java/ce/ajneb97/model/ConditionalType.java b/src/main/java/ce/ajneb97/model/ConditionalType.java index 3d95b98..514c1e6 100644 --- a/src/main/java/ce/ajneb97/model/ConditionalType.java +++ b/src/main/java/ce/ajneb97/model/ConditionalType.java @@ -1,5 +1,9 @@ package ce.ajneb97.model; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + public enum ConditionalType { EQUALS("=="), NOT_EQUALS("!="), @@ -22,6 +26,7 @@ public enum ConditionalType { IS_MULTIPLE_OF("isMultipleOf"), NOT_IS_MULTIPLE_OF("!isMultipleOf"); private String text; + private static final List AVAILABLE_TYPES = Arrays.stream(values()).map(Enum::name).collect(Collectors.toList()); ConditionalType(String text) { this.text = text; } @@ -29,4 +34,8 @@ public enum ConditionalType { public String getText() { return text; } + + public static boolean isValidConditionType(String string) { + return AVAILABLE_TYPES.contains(string); + } } From 61aa92901ae4f6f1ead80c324684ac3bac137225 Mon Sep 17 00:00:00 2001 From: AlonsoAliaga Date: Sun, 30 Jul 2023 12:37:46 -0500 Subject: [PATCH 3/3] Improved condition type checks. --- .../ce/ajneb97/managers/EventsManager.java | 302 ++++++++++++------ .../ce/ajneb97/managers/VerifyManager.java | 34 +- 2 files changed, 233 insertions(+), 103 deletions(-) diff --git a/src/main/java/ce/ajneb97/managers/EventsManager.java b/src/main/java/ce/ajneb97/managers/EventsManager.java index c087a49..64c891f 100644 --- a/src/main/java/ce/ajneb97/managers/EventsManager.java +++ b/src/main/java/ce/ajneb97/managers/EventsManager.java @@ -170,6 +170,7 @@ private List checkConditions(ConditionEvent conditionEven boolean approvedLine = false; String executedActionGroup = null; boolean isExecuteContinue = false; + //Checking if executecontinue first if (conditionLine.contains(" executecontinue ")) { String[] sep = conditionLine.split(" executecontinue "); conditionLine = sep[0]; @@ -182,111 +183,222 @@ private List checkConditions(ConditionEvent conditionEven } String conditionLineWithReplacements = ""; - String[] orConditions = conditionLine.split(" or "); - for(int c=0;c secondArgNum) miniConditionApproved = true; break; + case GREATER_EQUALS: + if(firstArgNum >= secondArgNum) miniConditionApproved = true; break; + case LOWER: + if(firstArgNum < secondArgNum) miniConditionApproved = true; break; + case LOWER_EQUALS: + if(firstArgNum <= secondArgNum) miniConditionApproved = true; break; + case IS_MULTIPLE_OF: + if(firstArgNum % secondArgNum == 0) miniConditionApproved = true; break; + case NOT_IS_MULTIPLE_OF: + if(firstArgNum % secondArgNum != 0) miniConditionApproved = true; break; + } + break; } - - String firstArg = !mathFormulas ? arg1 : MathUtils.calculate(arg1); - String secondArg = !mathFormulas ? arg2 : MathUtils.calculate(arg2); - - String firstArgLower = firstArg.toLowerCase();String secondArgLower = secondArg.toLowerCase(); - double firstArgNum = 0; - double secondArgNum = 0; - try{ - firstArgNum = Double.parseDouble(firstArg); - secondArgNum = Double.parseDouble(secondArg); - }catch(NumberFormatException ignored){} - - switch(conditionalType){ - case EQUALS: - case EQUALS_LEGACY: - if(firstArg.equals(secondArg)) approvedLine = true;break; - case NOT_EQUALS: - case NOT_EQUALS_LEGACY: - if(!firstArg.equals(secondArg)) approvedLine = true;break; - case EQUALS_IGNORE_CASE: - if(firstArg.equalsIgnoreCase(secondArg)) approvedLine = true;break; - case NOT_EQUALS_IGNORE_CASE: - if(!firstArg.equalsIgnoreCase(secondArg)) approvedLine = true;break; - case STARTS_WITH: - if(firstArgLower.startsWith(secondArgLower)) approvedLine = true;break; - case NOT_STARTS_WITH: - if(!firstArgLower.startsWith(secondArgLower)) approvedLine = true;break; - case ENDS_WITH: - if(firstArgLower.endsWith(secondArgLower)) approvedLine = true;break; - case NOT_ENDS_WITH: - if(!firstArgLower.endsWith(secondArgLower)) approvedLine = true;break; - case MATCHES_WITH: - if(firstArg.matches(secondArg)) approvedLine = true;break; - case NOT_MATCHES_WITH: - if(!firstArg.matches(secondArg)) approvedLine = true;break; - case CONTAINS: - if(firstArgLower.contains(secondArgLower)) approvedLine = true;break; - case NOT_CONTAINS: - if(!firstArgLower.contains(secondArgLower)) approvedLine = true;break; - case GREATER: - if(firstArgNum > secondArgNum) approvedLine = true;break; - case GREATER_EQUALS: - if(firstArgNum >= secondArgNum) approvedLine = true;break; - case LOWER: - if(firstArgNum < secondArgNum) approvedLine = true;break; - case LOWER_EQUALS: - if(firstArgNum <= secondArgNum) approvedLine = true;break; - case IS_MULTIPLE_OF: - if(firstArgNum % secondArgNum == 0) approvedLine = true;break; - case NOT_IS_MULTIPLE_OF: - if(firstArgNum % secondArgNum != 0) approvedLine = true;break; + } + if(!miniConditionApproved){ + break; + }else if(c + 1 == andConditions.length) { + approvedLine = true; + break; + } + } + //Bukkit.getConsoleSender().sendMessage(conditionLine+" -> ¿aprobada? "+approvedLine); + debugManager.sendConditionMessage(eventName,conditionLineWithReplacements,approvedLine,conditionEvent.getPlayer(),i==0); + //If approvedLine is false, the conditions are not satisfied. Returns FALSE. + //If approvedLine is false, but there is an executedActionGroup, it will continue with the next + // condition line. + //If approvedLine is true, it will continue with the next condition line. + //If approvedLine is true and there is an executedActionGroup selected, the method + // will return this action group. + if(!approvedLine){ + if(executedActionGroup == null){ + conditionsResults.add(new CheckConditionsResult(false,null)); + if(!isExecuteContinue) return conditionsResults; + } + continue; + } + if(approvedLine && executedActionGroup != null){ + debugManager.sendActionsMessage(eventName,executedActionGroup,conditionEvent.getPlayer()); + conditionsResults.add(new CheckConditionsResult(true,executedActionGroup)); + if(!isExecuteContinue) return conditionsResults; + } + } else { + String[] orConditions = conditionLine.split(" or "); + for(int c=0;c secondArgNum) approvedLine = true;break; + case GREATER_EQUALS: + if(firstArgNum >= secondArgNum) approvedLine = true;break; + case LOWER: + if(firstArgNum < secondArgNum) approvedLine = true;break; + case LOWER_EQUALS: + if(firstArgNum <= secondArgNum) approvedLine = true;break; + case IS_MULTIPLE_OF: + if(firstArgNum % secondArgNum == 0) approvedLine = true;break; + case NOT_IS_MULTIPLE_OF: + if(firstArgNum % secondArgNum != 0) approvedLine = true;break; + } + } + if(approvedLine){ + break; } } if(approvedLine){ break; } } - if(approvedLine){ - break; + //Bukkit.getConsoleSender().sendMessage(conditionLine+" -> ¿aprobada? "+approvedLine); + debugManager.sendConditionMessage(eventName,conditionLineWithReplacements,approvedLine,conditionEvent.getPlayer(),i==0); + //If approvedLine is false, the conditions are not satisfied. Returns FALSE. + //If approvedLine is false, but there is an executedActionGroup, it will continue with the next + // condition line. + //If approvedLine is true, it will continue with the next condition line. + //If approvedLine is true and there is an executedActionGroup selected, the method + // will return this action group. + if(!approvedLine){ + if(executedActionGroup == null){ + conditionsResults.add(new CheckConditionsResult(false,null)); + if(!isExecuteContinue) return conditionsResults; + } + continue; } - } - //Bukkit.getConsoleSender().sendMessage(conditionLine+" -> ¿aprobada? "+approvedLine); - debugManager.sendConditionMessage(eventName,conditionLineWithReplacements,approvedLine,conditionEvent.getPlayer(),i==0); - //If approvedLine is false, the conditions are not satisfied. Returns FALSE. - //If approvedLine is false, but there is an executedActionGroup, it will continue with the next - // condition line. - //If approvedLine is true, it will continue with the next condition line. - //If approvedLine is true and there is an executedActionGroup selected, the method - // will return this action group. - if(!approvedLine){ - if(executedActionGroup == null){ - conditionsResults.add(new CheckConditionsResult(false,null)); + if(approvedLine && executedActionGroup != null){ + debugManager.sendActionsMessage(eventName,executedActionGroup,conditionEvent.getPlayer()); + conditionsResults.add(new CheckConditionsResult(true,executedActionGroup)); if(!isExecuteContinue) return conditionsResults; } - continue; - } - if(approvedLine && executedActionGroup != null){ - debugManager.sendActionsMessage(eventName,executedActionGroup,conditionEvent.getPlayer()); - conditionsResults.add(new CheckConditionsResult(true,executedActionGroup)); - if(!isExecuteContinue) return conditionsResults; } } diff --git a/src/main/java/ce/ajneb97/managers/VerifyManager.java b/src/main/java/ce/ajneb97/managers/VerifyManager.java index 7fd1e18..3ba7c06 100644 --- a/src/main/java/ce/ajneb97/managers/VerifyManager.java +++ b/src/main/java/ce/ajneb97/managers/VerifyManager.java @@ -156,13 +156,17 @@ private boolean verifyRandomVariable(String line){ public boolean verifyCondition(String line) { String[] sepExecute = line.contains(" executecontinue ") ? line.split(" executecontinue ") : line.split(" execute "); - String[] sepOr = sepExecute[0].split(" or "); - for(int i=0;i=") && !sep[1].equals("<=") && !sep[1].equals(">") && !sep[1].equals("<") @@ -175,7 +179,21 @@ public boolean verifyCondition(String line) { return false; } */ + } + return true; + }else{ + //To prevent (for now) that condition contains "and" and "or" (Mixed conditions) + //In a future, probably a system to support parenthesis might be a good idea to support ( or ) and ( or ) + if(sepExecute[0].contains(" or ")) return false; + String[] sepOr = sepExecute[0].split(" and "); + for(int i=0;i