Skip to content

Commit

Permalink
Fix aliased not working for xml parsed enums
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <pabloherrerapalacio@gmail.com>
  • Loading branch information
Pablete1234 committed Oct 8, 2024
1 parent 5cf84c3 commit 33a45fe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
32 changes: 24 additions & 8 deletions core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package tc.oc.pgm.destroyable;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import java.util.Iterator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.api.feature.FeatureInfo;
import tc.oc.pgm.api.region.Region;
Expand All @@ -9,16 +12,11 @@
import tc.oc.pgm.goals.ShowOptions;
import tc.oc.pgm.modes.Mode;
import tc.oc.pgm.teams.TeamFactory;
import tc.oc.pgm.util.Aliased;
import tc.oc.pgm.util.material.MaterialMatcher;

@FeatureInfo(name = "destroyable")
public class DestroyableFactory extends ProximityGoalDefinition {
public static enum SparksType {
FALSE, // none
NEAR, // normal sparks
TRUE; // normal/far sparks
}

protected final Region region;
protected final MaterialMatcher materials;
protected final double destructionRequired;
Expand Down Expand Up @@ -72,14 +70,32 @@ public boolean getShowProgress() {
}

public boolean isSparksActive() {
return this.sparks != SparksType.FALSE;
return this.sparks != SparksType.NONE;
}

public boolean isSparksAll() {
return this.sparks == SparksType.TRUE;
return this.sparks == SparksType.ALL;
}

public boolean isRepairable() {
return this.repairable;
}

public enum SparksType implements Aliased {
NONE("false", "no", "off"),
NEAR("near"),
ALL("true", "yes", "on");

private final String[] names;

SparksType(String... names) {
this.names = names;
}

@NotNull
@Override
public Iterator<String> iterator() {
return Iterators.forArray(names);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public DestroyableModule parse(MapFactory context, Logger logger, Document doc)
boolean showProgress =
XMLUtils.parseBoolean(destroyableEl.getAttribute("show-progress"), false);
SparksType sparks = XMLUtils.parseEnum(
Node.fromAttr(destroyableEl, "sparks"), SparksType.class, SparksType.FALSE);
Node.fromAttr(destroyableEl, "sparks"), SparksType.class, SparksType.NONE);
boolean repairable = XMLUtils.parseBoolean(destroyableEl.getAttribute("repairable"), true);
ShowOptions options = ShowOptions.parse(context.getFilters(), destroyableEl);
Boolean required = XMLUtils.parseBoolean(destroyableEl.getAttribute("required"), null);
Expand Down
24 changes: 18 additions & 6 deletions util/src/main/java/tc/oc/pgm/util/text/TextParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.bukkit.Chunk;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import tc.oc.pgm.util.Aliased;
import tc.oc.pgm.util.StringUtils;
import tc.oc.pgm.util.TimeUtils;
import tc.oc.pgm.util.Version;
Expand Down Expand Up @@ -343,7 +344,7 @@ public static <E extends Enum<E>> E parseEnum(
String name = text.replace(' ', '_');
E value = StringUtils.bestFuzzyMatch(name, type);

if (value == null || (!fuzzyMatch && !name.equalsIgnoreCase(value.name()))) {
if (value == null || (!fuzzyMatch && !isExactMatch(name, value))) {
throw invalidFormat(text, type, value != null ? value.name().toLowerCase() : null, null);
}

Expand All @@ -354,6 +355,17 @@ public static <E extends Enum<E>> E parseEnum(
return value;
}

private static <E extends Enum<E>> boolean isExactMatch(String text, E value) {
if (value instanceof Aliased aliased) {
for (String aliases : aliased) {
if (text.equalsIgnoreCase(aliases)) return true;
}
return false;
} else {
return text.equalsIgnoreCase(value.name());
}
}

/**
* Parses text into an enum.
*
Expand Down Expand Up @@ -434,11 +446,11 @@ public static Component parseComponent(String text) throws TextException {
*
* <p>Accepts full qualified json strings as components.
*
* <p>This method is mainly for backwards compatability for {@link
* XMLUtils#parseFormattedText(Node, Component)}. Previously using {@link #parseComponent(String)}
* with the result from {@code parseFormattedText} would bug out when sent to older clients, since
* the LegacyComponentSerializer expects "&" but {@link BukkitUtils#colorize(String)}(Used in the
* XMLUtils method) results in using "§".
* <p>This method is mainly for backwards compatability for
* {@link XMLUtils#parseFormattedText(Node, Component)}. Previously using
* {@link #parseComponent(String)} with the result from {@code parseFormattedText} would bug out
* when sent to older clients, since the LegacyComponentSerializer expects "&" but
* {@link BukkitUtils#colorize(String)}(Used in the XMLUtils method) results in using "§".
*
* @param text The text.
* @return a Component.
Expand Down

0 comments on commit 33a45fe

Please sign in to comment.