Skip to content

Commit

Permalink
Changed ConditionList to support AND and OR.
Browse files Browse the repository at this point in the history
Added ORCondition
  • Loading branch information
Xemorr committed Sep 24, 2021
1 parent 17e6446 commit b3a2fb0
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<dependency>
<groupId>me.xemor</groupId>
<artifactId>configurationdata</artifactId>
<version>1.17-SNAPSHOT</version>
<version>1.17.2-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion skillslibrary.iml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<orderEntry type="library" scope="RUNTIME" name="Maven: net.kyori:adventure-text-serializer-gson-legacy-impl:4.9.1" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: net.kyori:adventure-platform-facet:4.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: net.kyori:adventure-platform-viaversion:4.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: me.xemor:configurationdata:1.17-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: me.xemor:configurationdata:1.17.2-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: it.unimi.dsi:fastutil:8.1.0" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:20.1.0" level="project" />
<orderEntry type="library" name="Maven: net.kyori:adventure-api:4.9.1" level="project" />
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/xemor/skillslibrary2/Skill.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Skill(ConfigurationSection skillSection) {
}

public boolean handleEffects(Entity entity, Object... objects) {
if (trigger.getConditions().areConditionsTrue(entity, objects)) {
if (trigger.getConditions().ANDConditions(entity, false, objects)) {
return effects.handleEffects(entity, objects);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@ private void loadConditions(ConfigurationSection conditionsSection) {
}
}

@Deprecated
public boolean areConditionsTrue(Entity entity, Object... objects) {
return ANDConditions(entity, false, objects);
}

public boolean ANDConditions(Entity entity, boolean exact, Object... objects) {
Object otherObject = objects.length == 0 ? null : objects[0];
for (Condition condition : conditions) {
if (condition instanceof EntityCondition && condition.getMode().runs(Mode.SELF)) {
if (condition instanceof EntityCondition && condition.getMode().runs(Mode.SELF) && (!exact || otherObject == null)) {
EntityCondition entityCondition = (EntityCondition) condition;
boolean result = entityCondition.isTrue(entity);
if (!result) return false;
Expand All @@ -63,6 +68,28 @@ else if (condition instanceof LocationCondition && otherObject instanceof Locati
return true;
}

public boolean ORConditions(Entity entity, boolean exact, Object... objects) {
Object otherObject = objects.length == 0 ? null : objects[0];
for (Condition condition : conditions) {
if (condition instanceof EntityCondition && condition.getMode().runs(Mode.SELF) && (!exact || otherObject == null)) {
EntityCondition entityCondition = (EntityCondition) condition;
boolean result = entityCondition.isTrue(entity);
if (result) return true;
}
if (condition instanceof TargetCondition && otherObject instanceof Entity && condition.getMode().runs(Mode.OTHER)) {
TargetCondition targetCondition = (TargetCondition) condition;
boolean result = targetCondition.isTrue(entity, (Entity) objects[0]);
if (result) return true;
}
else if (condition instanceof LocationCondition && otherObject instanceof Location && condition.getMode().runs(Mode.LOCATION)) {
LocationCondition locationCondition = (LocationCondition) condition;
boolean result = locationCondition.isTrue(entity, (Location) objects[0]);
if (!result) return true;
}
}
return false;
}

public void addCondition(Condition condition) {
conditions.add(0, condition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Conditions {
register("HEIGHT", HeightCondition.class);
register("BLOCK", BlockCondition.class);
register("ITEM", ItemComparisonCondition.class);
register("OR", ORCondition.class);
}

public static void register(String name, Class<? extends Condition> triggerDataClass) {
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/me/xemor/skillslibrary2/conditions/ORCondition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package me.xemor.skillslibrary2.conditions;

import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;

public class ORCondition extends Condition implements EntityCondition, TargetCondition, LocationCondition, ItemStackCondition {

private final ConditionList conditions;

public ORCondition(int condition, ConfigurationSection configurationSection) {
super(condition, configurationSection);
conditions = new ConditionList(configurationSection.getConfigurationSection("conditions"));
}

@Override
public boolean isTrue(Entity entity) {
return conditions.ORConditions(entity, true);
}

@Override
public boolean isTrue(Entity entity, ItemStack itemStack) {
return conditions.ORConditions(entity, true, itemStack);
}

@Override
public boolean isTrue(Entity entity, Location location) {
return conditions.ORConditions(entity, true, location);
}

@Override
public boolean isTrue(Entity entity, Entity target) {
return conditions.ORConditions(entity, true, target);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public boolean useEffect(Entity entity) {
@Override
public boolean useEffect(Entity livingEntity, Entity target) {
LivingEntity nearest = getNearest(livingEntity, target.getLocation());
if (nearest == null) return false;
return handleEffects(livingEntity, nearest);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public SwitcherooWrapperEffect(int effect, ConfigurationSection configurationSec

@Override
public boolean useEffect(Entity entity, Entity target) {
handleEffects(target, entity);
return false;
return handleEffects(target, entity);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ public WrapperEffect(int effect, ConfigurationSection configurationSection) {
else {
effects = new EffectList();
}



}

public boolean handleEffects(Entity entity, Object... objects) {
if (conditions.areConditionsTrue(entity, objects)) {
if (conditions.ANDConditions(entity, false, objects)) {
return effects.handleExactEffects(entity, objects);
}
return false;
Expand Down

0 comments on commit b3a2fb0

Please sign in to comment.