Skip to content

Commit

Permalink
Team becomes its own filter! (#1465)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 authored Dec 29, 2024
1 parent 5803438 commit 7070abd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
10 changes: 4 additions & 6 deletions core/src/main/java/tc/oc/pgm/filters/parse/FilterParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import tc.oc.pgm.filters.matcher.party.GoalFilter;
import tc.oc.pgm.filters.matcher.party.RankFilter;
import tc.oc.pgm.filters.matcher.party.ScoreFilter;
import tc.oc.pgm.filters.matcher.party.TeamFilter;
import tc.oc.pgm.filters.matcher.player.CanFlyFilter;
import tc.oc.pgm.filters.matcher.player.CarryingFlagFilter;
import tc.oc.pgm.filters.matcher.player.CarryingItemFilter;
Expand Down Expand Up @@ -80,7 +79,6 @@
import tc.oc.pgm.goals.GoalDefinition;
import tc.oc.pgm.regions.BlockBoundedValidation;
import tc.oc.pgm.teams.TeamFactory;
import tc.oc.pgm.teams.Teams;
import tc.oc.pgm.util.MethodParser;
import tc.oc.pgm.util.MethodParsers;
import tc.oc.pgm.util.StringUtils;
Expand Down Expand Up @@ -193,8 +191,8 @@ public Filter parseFilterProperty(Element el, String name, @Nullable Filter def)
}

/**
* Return a list containing any and all of the following: - A filter reference in an attribute of
* the given name - Inline filters inside child tags of the given name
* @param name the attribute/child name
* @return list with all filters defined as either attribute or child named {@param name}
*/
public List<Filter> parseFiltersProperty(Element el, String name) throws InvalidXMLException {
List<Filter> filters = new ArrayList<>();
Expand Down Expand Up @@ -240,8 +238,8 @@ public Filter parseNot(Element el) throws InvalidXMLException {
}

@MethodParser("team")
public TeamFilter parseTeam(Element el) throws InvalidXMLException {
return new TeamFilter(Teams.getTeamRef(new Node(el), this.factory));
public Filter parseTeam(Element el) throws InvalidXMLException {
return FilterWrapper.of(el, parseReference(new Node(el)));
}

@MethodParser("same-team")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import tc.oc.pgm.api.map.factory.MapFactory;
import tc.oc.pgm.api.region.Region;
import tc.oc.pgm.filters.matcher.block.MaterialFilter;
import tc.oc.pgm.filters.matcher.party.TeamFilter;
import tc.oc.pgm.filters.operator.AnyFilter;
import tc.oc.pgm.filters.operator.FilterNode;
import tc.oc.pgm.filters.operator.InverseFilter;
import tc.oc.pgm.teams.Teams;
import tc.oc.pgm.util.MethodParser;
import tc.oc.pgm.util.material.MaterialMatcher;
import tc.oc.pgm.util.xml.InvalidXMLException;
Expand Down Expand Up @@ -114,6 +116,12 @@ public Filter parseFilter(Element el) throws InvalidXMLException {
}
}

// Legacy has separate context, team wouldn't be found in the filter context.
@MethodParser("team")
public TeamFilter parseTeam(Element el) throws InvalidXMLException {
return new TeamFilter(Teams.getTeamRef(new Node(el), this.factory));
}

// Legacy not allows for multiple children and is an implicit and
@MethodParser("not")
public Filter parseNot(Element el) throws InvalidXMLException {
Expand Down
27 changes: 26 additions & 1 deletion core/src/main/java/tc/oc/pgm/teams/TeamFactory.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package tc.oc.pgm.teams;

import java.util.Collection;
import java.util.Collections;
import org.bukkit.ChatColor;
import org.bukkit.DyeColor;
import org.bukkit.event.Event;
import org.bukkit.scoreboard.NameTagVisibility;
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.api.feature.FeatureInfo;
import tc.oc.pgm.api.filter.query.PartyQuery;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.party.Party;
import tc.oc.pgm.events.PlayerPartyChangeEvent;
import tc.oc.pgm.features.SelfIdentifyingFeatureDefinition;
import tc.oc.pgm.filters.matcher.TypedFilter;

/** Immutable class to represent a team in a map that is not tied to any specific match. */
@FeatureInfo(name = "team")
public class TeamFactory extends SelfIdentifyingFeatureDefinition {
public class TeamFactory extends SelfIdentifyingFeatureDefinition
implements TypedFilter<PartyQuery> {
protected final String defaultName;
protected final boolean defaultNamePlural;
protected final ChatColor defaultColor;
Expand Down Expand Up @@ -136,4 +144,21 @@ public int getOverfillSlots() {
public NameTagVisibility getNameTagVisibility() {
return nameTagVisibility;
}

// Filter implementation:
@Override
public Class<? extends PartyQuery> queryType() {
return PartyQuery.class;
}

@Override
public boolean matches(PartyQuery query) {
final Party party = query.getParty();
return party instanceof Team team && team.isInstance(this);
}

@Override
public Collection<Class<? extends Event>> getRelevantEvents() {
return Collections.singleton(PlayerPartyChangeEvent.class);
}
}

0 comments on commit 7070abd

Please sign in to comment.