diff --git a/src/main/java/ce/ajneb97/managers/EventsManager.java b/src/main/java/ce/ajneb97/managers/EventsManager.java index 6ee6095..64c891f 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,118 +163,249 @@ private CheckConditionsResult checkConditions(ConditionEvent conditionEvent, boo //Check condition lines ArrayList storedVariables = conditionEvent.getEventVariables(); + List conditionsResults = new ArrayList<>(); + for(int i=0;i 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.valueOf(firstArg); - secondArgNum = Double.valueOf(secondArg); - }catch(NumberFormatException e){ - + } + 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; + } } - - 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 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; + 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){ - return 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()); - return new CheckConditionsResult(true,executedActionGroup); } } //If all condition lines are approved, the event is executed with the default actions. debugManager.sendActionsMessage(eventName,"default",conditionEvent.getPlayer()); - return new CheckConditionsResult(true,null); + conditionsResults.add(new CheckConditionsResult(true,null)); + return conditionsResults; } public boolean checkToConditionAction(List conditionGroup, Player player, boolean isPlaceholderAPI, @@ -306,11 +441,9 @@ public boolean checkToConditionAction(List conditionGroup, Player player double firstArgNum = 0; double secondArgNum = 0; try{ - firstArgNum = Double.valueOf(firstArg); - secondArgNum = Double.valueOf(secondArg); - }catch(NumberFormatException e){ - - } + firstArgNum = Double.parseDouble(firstArg); + secondArgNum = Double.parseDouble(secondArg); + }catch(NumberFormatException ignored){} switch(conditionalType){ case EQUALS: @@ -327,6 +460,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 +480,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..3ba7c06 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; @@ -154,21 +155,45 @@ private boolean verifyRandomVariable(String line){ } public boolean verifyCondition(String line) { - String[] sepExecute = line.split(" execute "); - String[] sepOr = sepExecute[0].split(" or "); - for(int i=0;i=") && !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; + }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"), GREATER_EQUALS(">="), LOWER("<"), - LOWER_EQUALS("<="); - + LOWER_EQUALS("<="), + 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; } @@ -24,4 +34,8 @@ public enum ConditionalType { public String getText() { return text; } + + public static boolean isValidConditionType(String string) { + return AVAILABLE_TYPES.contains(string); + } }