Skip to content

Commit

Permalink
Add near sparks option to destroyables (#1405)
Browse files Browse the repository at this point in the history
Signed-off-by: arcadeboss <[email protected]>
Signed-off-by: Pablo Herrera <[email protected]>
Co-authored-by: Pablo Herrera <[email protected]>
  • Loading branch information
arcadeboss and Pablete1234 authored Oct 9, 2024
1 parent 7aa3109 commit ddae132
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
15 changes: 8 additions & 7 deletions core/src/main/java/tc/oc/pgm/destroyable/Destroyable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -356,12 +356,13 @@ 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.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(Sounds.OBJECTIVE_FIREWORKS_FAR);
listener.playSound(Sounds.OBJECTIVE_FIREWORKS_TWINKLE);
}
}
}
}
Expand Down
34 changes: 30 additions & 4 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,6 +12,7 @@
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")
Expand All @@ -18,7 +22,7 @@ public class DestroyableFactory extends ProximityGoalDefinition {
protected final double destructionRequired;
protected final ImmutableSet<Mode> modeList;
protected final boolean showProgress;
protected final boolean sparks;
protected final SparksType sparks;
protected final boolean repairable;

public DestroyableFactory(
Expand All @@ -33,7 +37,7 @@ public DestroyableFactory(
double destructionRequired,
@Nullable ImmutableSet<Mode> modeList,
boolean showProgress,
boolean sparks,
SparksType sparks,
boolean repairable) {
super(id, name, required, showOptions, owner, proximityMetric);
this.region = region;
Expand Down Expand Up @@ -65,11 +69,33 @@ public boolean getShowProgress() {
return this.showProgress;
}

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

public boolean isSparksAll() {
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 @@ -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;
Expand Down Expand Up @@ -127,7 +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);
SparksType sparks = XMLUtils.parseEnum(
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 ddae132

Please sign in to comment.