From 216b7fd9964110a5e90dd81d784b7dfe4f1ee017 Mon Sep 17 00:00:00 2001 From: arcadeboss Date: Mon, 30 Sep 2024 02:40:35 +0000 Subject: [PATCH 1/5] Destroyable - Add sparks-match parameter Signed-off-by: arcadeboss --- .../java/tc/oc/pgm/destroyable/Destroyable.java | 15 +++++++++------ .../tc/oc/pgm/destroyable/DestroyableFactory.java | 7 +++++++ .../tc/oc/pgm/destroyable/DestroyableModule.java | 3 +++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/destroyable/Destroyable.java b/core/src/main/java/tc/oc/pgm/destroyable/Destroyable.java index 0ba2092310..7d875dd63c 100644 --- a/core/src/main/java/tc/oc/pgm/destroyable/Destroyable.java +++ b/core/src/main/java/tc/oc/pgm/destroyable/Destroyable.java @@ -356,12 +356,15 @@ public DestroyableHealthChange handleBlockChange( NMS_HACKS.skipFireworksLaunch(firework); } - // Players more than 64m away will not see or hear the fireworks, so just play the sound - // for them - for (MatchPlayer listener : this.getOwner().getMatch().getPlayers()) { - if (listener.getBukkit().getLocation().distance(blockLocation) > 64) { - listener.playSound(Sounds.OBJECTIVE_FIREWORKS_FAR); - listener.playSound(Sounds.OBJECTIVE_FIREWORKS_TWINKLE); + if (this.definition.hasSparksMatch()) { + // Players more than 64m away will not see or hear the fireworks, so play sound for them + for (MatchPlayer listener : this.getOwner().getMatch().getPlayers()) { + if (listener.getBukkit().getLocation().distance(blockLocation) > 64) { + listener.playSound( + sound(key("fireworks.blast_far"), Sound.Source.MASTER, 0.75f, 1f)); + listener.playSound( + sound(key("fireworks.twinkle_far"), Sound.Source.MASTER, 0.75f, 1f)); + } } } } diff --git a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java index eaed8dfd13..f1907ed053 100644 --- a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java +++ b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java @@ -19,6 +19,7 @@ public class DestroyableFactory extends ProximityGoalDefinition { protected final ImmutableSet modeList; protected final boolean showProgress; protected final boolean sparks; + protected final boolean sparksMatch; protected final boolean repairable; public DestroyableFactory( @@ -34,6 +35,7 @@ public DestroyableFactory( @Nullable ImmutableSet modeList, boolean showProgress, boolean sparks, + boolean sparksMatch, boolean repairable) { super(id, name, required, showOptions, owner, proximityMetric); this.region = region; @@ -42,6 +44,7 @@ public DestroyableFactory( this.modeList = modeList; this.showProgress = showProgress; this.sparks = sparks; + this.sparksMatch = sparksMatch; this.repairable = repairable; } @@ -69,6 +72,10 @@ public boolean hasSparks() { return this.sparks; } + public boolean hasSparksMatch() { + return this.sparksMatch; + } + public boolean isRepairable() { return this.repairable; } diff --git a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java index 307dcdfdda..2602952278 100644 --- a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java +++ b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java @@ -128,6 +128,8 @@ public DestroyableModule parse(MapFactory context, Logger logger, Document doc) boolean showProgress = XMLUtils.parseBoolean(destroyableEl.getAttribute("show-progress"), false); boolean sparks = XMLUtils.parseBoolean(destroyableEl.getAttribute("sparks"), false); + boolean sparksMatch = + XMLUtils.parseBoolean(destroyableEl.getAttribute("sparks-match"), true); boolean repairable = XMLUtils.parseBoolean(destroyableEl.getAttribute("repairable"), true); ShowOptions options = ShowOptions.parse(context.getFilters(), destroyableEl); Boolean required = XMLUtils.parseBoolean(destroyableEl.getAttribute("required"), null); @@ -147,6 +149,7 @@ public DestroyableModule parse(MapFactory context, Logger logger, Document doc) modeSet, showProgress, sparks, + sparksMatch, repairable); context.getFeatures().addFeature(destroyableEl, factory); From 46d15ab326774485a74b8ced29ee6b6dd628c7d2 Mon Sep 17 00:00:00 2001 From: arcadeboss Date: Tue, 1 Oct 2024 02:50:25 +0000 Subject: [PATCH 2/5] Destroyable - Remove sparks-match, Add new sparks value 'near' Signed-off-by: arcadeboss --- .../tc/oc/pgm/destroyable/Destroyable.java | 10 ++--- .../pgm/destroyable/DestroyableFactory.java | 44 ++++++++++++++----- .../oc/pgm/destroyable/DestroyableModule.java | 10 ++--- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/destroyable/Destroyable.java b/core/src/main/java/tc/oc/pgm/destroyable/Destroyable.java index 7d875dd63c..a4050b94c3 100644 --- a/core/src/main/java/tc/oc/pgm/destroyable/Destroyable.java +++ b/core/src/main/java/tc/oc/pgm/destroyable/Destroyable.java @@ -330,7 +330,7 @@ public DestroyableHealthChange handleBlockChange( if (deltaHealth < 0) { touch(player); - if (this.definition.hasSparks()) { + if (this.definition.isSparksActive()) { Location blockLocation = BlockVectors.center(oldState); Instant now = Instant.now(); @@ -356,14 +356,12 @@ public DestroyableHealthChange handleBlockChange( NMS_HACKS.skipFireworksLaunch(firework); } - if (this.definition.hasSparksMatch()) { + if (this.definition.isSparksAll()) { // Players more than 64m away will not see or hear the fireworks, so play sound for them for (MatchPlayer listener : this.getOwner().getMatch().getPlayers()) { if (listener.getBukkit().getLocation().distance(blockLocation) > 64) { - listener.playSound( - sound(key("fireworks.blast_far"), Sound.Source.MASTER, 0.75f, 1f)); - listener.playSound( - sound(key("fireworks.twinkle_far"), Sound.Source.MASTER, 0.75f, 1f)); + listener.playSound(Sounds.OBJECTIVE_FIREWORKS_FAR); + listener.playSound(Sounds.OBJECTIVE_FIREWORKS_TWINKLE); } } } diff --git a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java index f1907ed053..eef0844027 100644 --- a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java +++ b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java @@ -1,6 +1,7 @@ package tc.oc.pgm.destroyable; import com.google.common.collect.ImmutableSet; +import org.jdom2.Element; import org.jetbrains.annotations.Nullable; import tc.oc.pgm.api.feature.FeatureInfo; import tc.oc.pgm.api.region.Region; @@ -10,16 +11,37 @@ import tc.oc.pgm.modes.Mode; import tc.oc.pgm.teams.TeamFactory; import tc.oc.pgm.util.material.MaterialMatcher; +import tc.oc.pgm.util.xml.InvalidXMLException; @FeatureInfo(name = "destroyable") public class DestroyableFactory extends ProximityGoalDefinition { + public static enum SparksType { + SPARKS_ALL("true"), + SPARKS_NONE("false"), + SPARKS_NEAR("near"); + + private final String name; + + private SparksType(String name) { + this.name = name; + } + + public static SparksType fromString(String name, Element el) throws InvalidXMLException { + for (SparksType i : SparksType.values()) { + if (i.name.equalsIgnoreCase(name)) { + return i; + } + } + throw new InvalidXMLException("Unknown Destroyable sparks type: " + name, el); + } + } + protected final Region region; protected final MaterialMatcher materials; protected final double destructionRequired; protected final ImmutableSet modeList; protected final boolean showProgress; - protected final boolean sparks; - protected final boolean sparksMatch; + protected final SparksType sparks; protected final boolean repairable; public DestroyableFactory( @@ -34,17 +56,17 @@ public DestroyableFactory( double destructionRequired, @Nullable ImmutableSet modeList, boolean showProgress, - boolean sparks, - boolean sparksMatch, - boolean repairable) { + String sparks, + boolean repairable, + Element el) + throws InvalidXMLException { super(id, name, required, showOptions, owner, proximityMetric); this.region = region; this.materials = materials; this.destructionRequired = destructionRequired; this.modeList = modeList; this.showProgress = showProgress; - this.sparks = sparks; - this.sparksMatch = sparksMatch; + this.sparks = SparksType.fromString(sparks, el); this.repairable = repairable; } @@ -68,12 +90,12 @@ public boolean getShowProgress() { return this.showProgress; } - public boolean hasSparks() { - return this.sparks; + public boolean isSparksActive() { + return this.sparks != SparksType.SPARKS_NONE; } - public boolean hasSparksMatch() { - return this.sparksMatch; + public boolean isSparksAll() { + return this.sparks != SparksType.SPARKS_NEAR; } public boolean isRepairable() { diff --git a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java index 2602952278..7f81cfa6e0 100644 --- a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java +++ b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java @@ -127,9 +127,9 @@ public DestroyableModule parse(MapFactory context, Logger logger, Document doc) boolean showProgress = XMLUtils.parseBoolean(destroyableEl.getAttribute("show-progress"), false); - boolean sparks = XMLUtils.parseBoolean(destroyableEl.getAttribute("sparks"), false); - boolean sparksMatch = - XMLUtils.parseBoolean(destroyableEl.getAttribute("sparks-match"), true); + String sparks = destroyableEl.getAttributeValue("sparks") != null + ? destroyableEl.getAttributeValue("sparks") + : "false"; boolean repairable = XMLUtils.parseBoolean(destroyableEl.getAttribute("repairable"), true); ShowOptions options = ShowOptions.parse(context.getFilters(), destroyableEl); Boolean required = XMLUtils.parseBoolean(destroyableEl.getAttribute("required"), null); @@ -149,8 +149,8 @@ public DestroyableModule parse(MapFactory context, Logger logger, Document doc) modeSet, showProgress, sparks, - sparksMatch, - repairable); + repairable, + destroyableEl); context.getFeatures().addFeature(destroyableEl, factory); destroyables.add(factory); From b3438f7a84cd91a0c45f342e220638e763272410 Mon Sep 17 00:00:00 2001 From: arcadeboss Date: Thu, 3 Oct 2024 02:14:25 +0000 Subject: [PATCH 3/5] Sparks - Review updates Signed-off-by: arcadeboss --- .../pgm/destroyable/DestroyableFactory.java | 35 +++++-------------- .../oc/pgm/destroyable/DestroyableModule.java | 9 +++-- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java index eef0844027..d7184af2db 100644 --- a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java +++ b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java @@ -1,7 +1,6 @@ package tc.oc.pgm.destroyable; import com.google.common.collect.ImmutableSet; -import org.jdom2.Element; import org.jetbrains.annotations.Nullable; import tc.oc.pgm.api.feature.FeatureInfo; import tc.oc.pgm.api.region.Region; @@ -11,29 +10,13 @@ import tc.oc.pgm.modes.Mode; import tc.oc.pgm.teams.TeamFactory; import tc.oc.pgm.util.material.MaterialMatcher; -import tc.oc.pgm.util.xml.InvalidXMLException; @FeatureInfo(name = "destroyable") public class DestroyableFactory extends ProximityGoalDefinition { public static enum SparksType { - SPARKS_ALL("true"), - SPARKS_NONE("false"), - SPARKS_NEAR("near"); - - private final String name; - - private SparksType(String name) { - this.name = name; - } - - public static SparksType fromString(String name, Element el) throws InvalidXMLException { - for (SparksType i : SparksType.values()) { - if (i.name.equalsIgnoreCase(name)) { - return i; - } - } - throw new InvalidXMLException("Unknown Destroyable sparks type: " + name, el); - } + FALSE, // none + NEAR, // normal sparks + TRUE; // normal/far sparks } protected final Region region; @@ -56,17 +39,15 @@ public DestroyableFactory( double destructionRequired, @Nullable ImmutableSet modeList, boolean showProgress, - String sparks, - boolean repairable, - Element el) - throws InvalidXMLException { + SparksType sparks, + boolean repairable) { super(id, name, required, showOptions, owner, proximityMetric); this.region = region; this.materials = materials; this.destructionRequired = destructionRequired; this.modeList = modeList; this.showProgress = showProgress; - this.sparks = SparksType.fromString(sparks, el); + this.sparks = sparks; this.repairable = repairable; } @@ -91,11 +72,11 @@ public boolean getShowProgress() { } public boolean isSparksActive() { - return this.sparks != SparksType.SPARKS_NONE; + return this.sparks != SparksType.FALSE; } public boolean isSparksAll() { - return this.sparks != SparksType.SPARKS_NEAR; + return this.sparks == SparksType.TRUE; } public boolean isRepairable() { diff --git a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java index 7f81cfa6e0..165cfdbf7a 100644 --- a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java +++ b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java @@ -18,6 +18,7 @@ import tc.oc.pgm.api.match.MatchModule; import tc.oc.pgm.api.region.Region; import tc.oc.pgm.blockdrops.BlockDropsModule; +import tc.oc.pgm.destroyable.DestroyableFactory.SparksType; import tc.oc.pgm.goals.GoalMatchModule; import tc.oc.pgm.goals.ProximityMetric; import tc.oc.pgm.goals.ShowOptions; @@ -127,9 +128,8 @@ public DestroyableModule parse(MapFactory context, Logger logger, Document doc) boolean showProgress = XMLUtils.parseBoolean(destroyableEl.getAttribute("show-progress"), false); - String sparks = destroyableEl.getAttributeValue("sparks") != null - ? destroyableEl.getAttributeValue("sparks") - : "false"; + SparksType sparks = XMLUtils.parseEnum( + Node.fromAttr(destroyableEl, "sparks"), SparksType.class, SparksType.FALSE); boolean repairable = XMLUtils.parseBoolean(destroyableEl.getAttribute("repairable"), true); ShowOptions options = ShowOptions.parse(context.getFilters(), destroyableEl); Boolean required = XMLUtils.parseBoolean(destroyableEl.getAttribute("required"), null); @@ -149,8 +149,7 @@ public DestroyableModule parse(MapFactory context, Logger logger, Document doc) modeSet, showProgress, sparks, - repairable, - destroyableEl); + repairable); context.getFeatures().addFeature(destroyableEl, factory); destroyables.add(factory); From 5cf84c397a7c0a2c139e244c44cff34af3680218 Mon Sep 17 00:00:00 2001 From: arcadeboss Date: Thu, 3 Oct 2024 04:01:10 +0000 Subject: [PATCH 4/5] Sparks - Review updates - fix build Signed-off-by: arcadeboss --- .../main/java/tc/oc/pgm/destroyable/DestroyableFactory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java index d7184af2db..313ac9b065 100644 --- a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java +++ b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java @@ -15,8 +15,8 @@ public class DestroyableFactory extends ProximityGoalDefinition { public static enum SparksType { FALSE, // none - NEAR, // normal sparks - TRUE; // normal/far sparks + NEAR, // normal sparks + TRUE; // normal/far sparks } protected final Region region; From 33a45fefecea20c8ccf0efe7f245191267c53562 Mon Sep 17 00:00:00 2001 From: Pablo Herrera Date: Tue, 8 Oct 2024 07:31:41 +0200 Subject: [PATCH 5/5] Fix aliased not working for xml parsed enums Signed-off-by: Pablo Herrera --- .../pgm/destroyable/DestroyableFactory.java | 32 ++++++++++++++----- .../oc/pgm/destroyable/DestroyableModule.java | 2 +- .../java/tc/oc/pgm/util/text/TextParser.java | 24 ++++++++++---- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java index 313ac9b065..39ab2ffe07 100644 --- a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java +++ b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableFactory.java @@ -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; @@ -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; @@ -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 iterator() { + return Iterators.forArray(names); + } + } } diff --git a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java index 165cfdbf7a..6ae487021d 100644 --- a/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java +++ b/core/src/main/java/tc/oc/pgm/destroyable/DestroyableModule.java @@ -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); diff --git a/util/src/main/java/tc/oc/pgm/util/text/TextParser.java b/util/src/main/java/tc/oc/pgm/util/text/TextParser.java index d9b15d32cc..2c3a1f1de9 100644 --- a/util/src/main/java/tc/oc/pgm/util/text/TextParser.java +++ b/util/src/main/java/tc/oc/pgm/util/text/TextParser.java @@ -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; @@ -343,7 +344,7 @@ public static > 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); } @@ -354,6 +355,17 @@ public static > E parseEnum( return value; } + private static > 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. * @@ -434,11 +446,11 @@ public static Component parseComponent(String text) throws TextException { * *

Accepts full qualified json strings as components. * - *

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 "§". + *

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.