Skip to content

Commit

Permalink
Cleanup, fix up defaults, & allow filtering display
Browse files Browse the repository at this point in the history
Signed-off-by: Pablete1234 <[email protected]>
  • Loading branch information
Pablete1234 committed Sep 24, 2022
1 parent 4e9ec8c commit 9cefcb6
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 102 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,9 @@ default Component getComponent(boolean prefix) {
* @return A map of experimental settings.
*/
Map<String, Object> getExperiments();

default boolean getExperimentAsBool(String key, boolean def) {
Object exp = getExperiments().getOrDefault(key, def);
return exp instanceof Boolean ? (Boolean) exp : exp.toString().equals("true");
}
}
3 changes: 2 additions & 1 deletion core/src/main/java/tc/oc/pgm/controlpoint/ControlPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public ControlPoint(Match match, ControlPointDefinition definition) {
match.needModule(TeamMatchModule.class).getTeam(this.definition.getInitialOwner());
}

this.centerPoint = this.getCaptureRegion().getBounds().getCenterPoint();
Region capture = this.getCaptureRegion();
this.centerPoint = capture == null ? null : capture.getBounds().getCenterPoint();

this.playerTracker = new RegionPlayerTracker(match, this.getCaptureRegion());

Expand Down
47 changes: 30 additions & 17 deletions core/src/main/java/tc/oc/pgm/controlpoint/ControlPointModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import tc.oc.pgm.api.PGM;
import tc.oc.pgm.api.map.MapModule;
import tc.oc.pgm.api.map.MapTag;
import tc.oc.pgm.api.map.factory.MapFactory;
import tc.oc.pgm.api.map.factory.MapModuleFactory;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.match.MatchModule;
import tc.oc.pgm.controlpoint.ControlPointParser.Type;
import tc.oc.pgm.filters.FilterModule;
import tc.oc.pgm.goals.GoalMatchModule;
import tc.oc.pgm.regions.RegionModule;
Expand All @@ -23,16 +27,18 @@

public class ControlPointModule implements MapModule<ControlPointMatchModule> {

private static final Collection<MapTag> TAGS_CP =
ImmutableList.of(new MapTag("cp", "controlpoint", "Control the Point", true, false));
private static final Collection<MapTag> TAGS_KOTH =
ImmutableList.of(new MapTag("koth", "controlpoint", "King of the Hill", true, false));
private static final MapTag CP =
new MapTag("cp", "controlpoint", "Control the Point", true, false);
private static final MapTag KOTH =
new MapTag("koth", "controlpoint", "King of the Hill", true, false);
private static final MapTag PAYLOAD = new MapTag("pd", "payload", "Payload", true, false);

private final List<ControlPointDefinition> definitions;
private final boolean koth;
private final Collection<MapTag> tags;

public ControlPointModule(List<ControlPointDefinition> definitions, boolean koth) {
public ControlPointModule(List<ControlPointDefinition> definitions, Collection<MapTag> tags) {
this.definitions = definitions;
this.koth = koth;
this.tags = tags;
}

@Override
Expand All @@ -56,7 +62,7 @@ public ControlPointMatchModule createMatchModule(Match match) {

@Override
public Collection<MapTag> getTags() {
return this.koth ? TAGS_KOTH : TAGS_CP;
return this.tags;
}

public static class Factory implements MapModuleFactory<ControlPointModule> {
Expand All @@ -74,40 +80,47 @@ public Collection<Class<? extends MapModule>> getSoftDependencies() {
public ControlPointModule parse(MapFactory factory, Logger logger, Document doc)
throws InvalidXMLException {
List<ControlPointDefinition> definitions = new ArrayList<>();
Set<MapTag> tags = new HashSet<>();
AtomicInteger serialNumber = new AtomicInteger(1);
boolean koth = false;

for (Element elControlPoint :
XMLUtils.flattenElements(doc.getRootElement(), "control-points", "control-point")) {
tags.add(CP);
ControlPointDefinition definition =
ControlPointParser.parseControlPoint(
factory, elControlPoint, ControlPointParser.Type.POINT, serialNumber);
ControlPointParser.parseControlPoint(factory, elControlPoint, Type.POINT, serialNumber);
factory.getFeatures().addFeature(elControlPoint, definition);
definitions.add(definition);
}

for (Element kingEl : doc.getRootElement().getChildren("king")) {
for (Element hillEl : XMLUtils.flattenElements(kingEl, "hills", "hill")) {
koth = true;
tags.add(KOTH);
ControlPointDefinition definition =
ControlPointParser.parseControlPoint(
factory, hillEl, ControlPointParser.Type.KING, serialNumber);
ControlPointParser.parseControlPoint(factory, hillEl, Type.HILL, serialNumber);
factory.getFeatures().addFeature(kingEl, definition);
definitions.add(definition);
}
}

boolean payloadEnabled = PGM.get().getConfiguration().getExperimentAsBool("payload", false);

for (Element payloadEl :
XMLUtils.flattenElements(doc.getRootElement(), "payloads", "payload")) {
if (!payloadEnabled)
throw new InvalidXMLException(
"Payload is in experimental & unfinished game-mode, everything bound to change, including XML syntax."
+ "To enable the feature, change experiments.payload to true in pgm config.",
payloadEl);

tags.add(PAYLOAD);
ControlPointDefinition definition =
ControlPointParser.parseControlPoint(
factory, payloadEl, ControlPointParser.Type.PAYLOAD, serialNumber);
ControlPointParser.parseControlPoint(factory, payloadEl, Type.PAYLOAD, serialNumber);
factory.getFeatures().addFeature(payloadEl, definition);
definitions.add(definition);
}

if (!definitions.isEmpty()) {
return new ControlPointModule(definitions, koth);
return new ControlPointModule(definitions, tags);
} else {
return null;
}
Expand Down
95 changes: 50 additions & 45 deletions core/src/main/java/tc/oc/pgm/controlpoint/ControlPointParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import tc.oc.pgm.api.filter.Filter;
import tc.oc.pgm.api.map.factory.MapFactory;
import tc.oc.pgm.api.region.Region;
import tc.oc.pgm.filters.matcher.StaticFilter;
import tc.oc.pgm.filters.matcher.block.BlockFilter;
import tc.oc.pgm.filters.operator.AnyFilter;
import tc.oc.pgm.filters.parse.FilterParser;
import tc.oc.pgm.goals.ShowOptions;
import tc.oc.pgm.payload.PayloadDefinition;
import tc.oc.pgm.regions.BlockBoundedValidation;
import tc.oc.pgm.regions.EverywhereRegion;
import tc.oc.pgm.regions.RegionParser;
import tc.oc.pgm.teams.TeamFactory;
import tc.oc.pgm.teams.TeamModule;
Expand All @@ -35,43 +37,45 @@ public abstract class ControlPointParser {
new BlockFilter(Material.STAINED_GLASS_PANE));

public enum Type {
KING,
HILL,
POINT,
PAYLOAD
};
}

public static ControlPointDefinition parseControlPoint(
MapFactory factory, Element elControlPoint, Type type, AtomicInteger serialNumber)
MapFactory factory, Element el, Type type, AtomicInteger serialNumber)
throws InvalidXMLException {
String id = elControlPoint.getAttributeValue("id");
String id = el.getAttributeValue("id");
RegionParser regionParser = factory.getRegions();
FilterParser filterParser = factory.getFilters();

Region captureRegion =
regionParser.parseProperty(
Node.fromRequiredChildOrAttr(elControlPoint, "capture-region", "capture"));
type == Type.PAYLOAD
? EverywhereRegion.INSTANCE
: regionParser.parseProperty(
Node.fromRequiredChildOrAttr(el, "capture-region", "capture"));
Region progressDisplayRegion =
regionParser.parseProperty(
Node.fromChildOrAttr(elControlPoint, "progress-display-region", "progress"),
Node.fromChildOrAttr(el, "progress-display-region", "progress"),
BlockBoundedValidation.INSTANCE);
Region ownerDisplayRegion =
regionParser.parseProperty(
Node.fromChildOrAttr(elControlPoint, "owner-display-region", "captured"),
Node.fromChildOrAttr(el, "owner-display-region", "captured"),
BlockBoundedValidation.INSTANCE);

Filter captureFilter = filterParser.parseFilterProperty(elControlPoint, "capture-filter");
Filter playerFilter = filterParser.parseFilterProperty(elControlPoint, "player-filter");
Filter captureFilter = filterParser.parseFilterProperty(el, "capture-filter");
Filter playerFilter = filterParser.parseFilterProperty(el, "player-filter");

Filter visualMaterials;
List<Filter> filters = filterParser.parseFiltersProperty(elControlPoint, "visual-materials");
List<Filter> filters = filterParser.parseFiltersProperty(el, "visual-materials");
if (filters.isEmpty()) {
visualMaterials = VISUAL_MATERIALS;
} else {
visualMaterials = AnyFilter.of(filters);
}

String name;
Attribute attrName = elControlPoint.getAttribute("name");
Attribute attrName = el.getAttribute("name");

if (attrName != null) {
name = attrName.getValue();
Expand All @@ -83,74 +87,73 @@ public static ControlPointDefinition parseControlPoint(

TeamModule teams = factory.getModule(TeamModule.class);
TeamFactory initialOwner =
teams == null
? null
: teams.parseTeam(elControlPoint.getAttribute("initial-owner"), factory);
Vector capturableDisplayBeacon = XMLUtils.parseVector(elControlPoint.getAttribute("beacon"));
teams == null ? null : teams.parseTeam(el.getAttribute("initial-owner"), factory);
Vector capturableDisplayBeacon = XMLUtils.parseVector(el.getAttribute("beacon"));
Duration timeToCapture =
XMLUtils.parseDuration(elControlPoint.getAttribute("capture-time"), Duration.ofSeconds(30));
XMLUtils.parseDuration(el.getAttribute("capture-time"), Duration.ofSeconds(30));

final double decayRate, recoveryRate, ownedDecayRate, contestedRate;
final Node attrIncremental = Node.fromAttr(elControlPoint, "incremental");
final Node attrDecay = Node.fromAttr(elControlPoint, "decay", "decay-rate");
final Node attrRecovery = Node.fromAttr(elControlPoint, "recovery", "recovery-rate");
final Node attrOwnedDecay = Node.fromAttr(elControlPoint, "owned-decay", "owned-decay-rate");
final Node attrContested = Node.fromAttr(elControlPoint, "contested", "contested-rate");
boolean koth = type == Type.KING;
final Node attrIncremental = Node.fromAttr(el, "incremental");
final Node attrDecay = Node.fromAttr(el, "decay", "decay-rate");
final Node attrRecovery = Node.fromAttr(el, "recovery", "recovery-rate");
final Node attrOwnedDecay = Node.fromAttr(el, "owned-decay", "owned-decay-rate");
final Node attrContested = Node.fromAttr(el, "contested", "contested-rate");
boolean koth = type == Type.HILL;
boolean pd = type == Type.PAYLOAD;

if (attrIncremental == null) {
recoveryRate =
XMLUtils.parseNumber(attrRecovery, Double.class, koth ? 1D : Double.POSITIVE_INFINITY);
XMLUtils.parseNumber(
attrRecovery, Double.class, koth || pd ? 1D : Double.POSITIVE_INFINITY);
decayRate =
XMLUtils.parseNumber(attrDecay, Double.class, koth ? 0.0 : Double.POSITIVE_INFINITY);
XMLUtils.parseNumber(
attrDecay, Double.class, koth || pd ? 0.0 : Double.POSITIVE_INFINITY);
ownedDecayRate = XMLUtils.parseNumber(attrOwnedDecay, Double.class, 0.0);
} else {
if (attrDecay != null || attrRecovery != null || attrOwnedDecay != null)
throw new InvalidXMLException(
"Cannot combine this attribute with incremental",
attrDecay != null ? attrDecay : attrRecovery != null ? attrRecovery : attrOwnedDecay);

final boolean incremental = XMLUtils.parseBoolean(attrIncremental, koth);
final boolean incremental = XMLUtils.parseBoolean(attrIncremental, koth || pd);
recoveryRate = incremental ? 1.0 : Double.POSITIVE_INFINITY;
decayRate = incremental ? 0.0 : Double.POSITIVE_INFINITY;
ownedDecayRate = 0.0;
}
contestedRate = XMLUtils.parseNumber(attrContested, Double.class, decayRate);

float timeMultiplier =
XMLUtils.parseNumber(
elControlPoint.getAttribute("time-multiplier"), Float.class, koth ? 0.1f : 0f);
boolean neutralState =
XMLUtils.parseBoolean(elControlPoint.getAttribute("neutral-state"), koth);
XMLUtils.parseNumber(el.getAttribute("time-multiplier"), Float.class, koth ? 0.1f : 0f);
boolean neutralState = XMLUtils.parseBoolean(el.getAttribute("neutral-state"), koth || pd);

if (!neutralState && ownedDecayRate > 0) {
throw new InvalidXMLException("This attribute requires a neutral state.", attrOwnedDecay);
}
boolean permanent = XMLUtils.parseBoolean(elControlPoint.getAttribute("permanent"), false);
boolean permanent = XMLUtils.parseBoolean(el.getAttribute("permanent"), false);
float pointsPerSecond =
XMLUtils.parseNumber(elControlPoint.getAttribute("points"), Float.class, 1f);
XMLUtils.parseNumber(el.getAttribute("points"), Float.class, pd ? 0f : 1f);
float pointsOwner =
XMLUtils.parseNumber(elControlPoint.getAttribute("owner-points"), Float.class, 0f);
XMLUtils.parseNumber(el.getAttribute("owner-points"), Float.class, pd ? 1f : 0f);
float pointsGrowth =
XMLUtils.parseNumber(
elControlPoint.getAttribute("points-growth"), Float.class, Float.POSITIVE_INFINITY);
boolean showProgress =
XMLUtils.parseBoolean(elControlPoint.getAttribute("show-progress"), koth);
ShowOptions options = ShowOptions.parse(elControlPoint);
Boolean required = XMLUtils.parseBoolean(elControlPoint.getAttribute("required"), null);
el.getAttribute("points-growth"), Float.class, Float.POSITIVE_INFINITY);
boolean showProgress = XMLUtils.parseBoolean(el.getAttribute("show-progress"), koth || pd);
ShowOptions options = ShowOptions.parse(el);
Boolean required = XMLUtils.parseBoolean(el.getAttribute("required"), null);

ControlPointDefinition.CaptureCondition captureCondition =
XMLUtils.parseEnum(
Node.fromAttr(elControlPoint, "capture-rule"),
Node.fromAttr(el, "capture-rule"),
ControlPointDefinition.CaptureCondition.class,
"capture rule",
ControlPointDefinition.CaptureCondition.EXCLUSIVE);

if (type == Type.PAYLOAD) {
if (pd) {
BlockVector location =
BlockVectors.center(
XMLUtils.parseVector(Node.fromRequiredAttr(elControlPoint, "location")));
double radius =
XMLUtils.parseNumber(Node.fromRequiredAttr(elControlPoint, "radius"), Double.class);
BlockVectors.center(XMLUtils.parseVector(Node.fromRequiredAttr(el, "location")));
double radius = XMLUtils.parseNumber(Node.fromRequiredAttr(el, "radius"), Double.class);
Filter displayFilter =
filterParser.parseFilterProperty(el, "display-filter", StaticFilter.ALLOW);
return new PayloadDefinition(
id,
name,
Expand Down Expand Up @@ -178,8 +181,10 @@ public static ControlPointDefinition parseControlPoint(
pointsGrowth,
showProgress,
location,
radius);
radius,
displayFilter);
}

return new ControlPointDefinition(
id,
name,
Expand Down
25 changes: 13 additions & 12 deletions core/src/main/java/tc/oc/pgm/fireworks/FireworkMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Firework;
import org.bukkit.event.EventHandler;
Expand Down Expand Up @@ -219,12 +220,9 @@ public void spawnFireworkDisplay(
double angle = 2 * Math.PI / count * i;
double dx = radius * Math.cos(angle);
double dz = radius * Math.sin(angle);
Location baseLocation = center.clone().add(dx, 0, dz);
Location loc = getOpenSpaceAbove(center.clone().add(dx, 0, dz));

Block block = baseLocation.getBlock();
if (block == null || !block.getType().isOccluding()) {
spawnFirework(getOpenSpaceAbove(baseLocation), effect, power);
}
if (loc != null) spawnFirework(loc, effect, power);
}
}

Expand Down Expand Up @@ -254,14 +252,17 @@ public static Firework spawnFirework(Location location, FireworkEffect effect, i
public static Location getOpenSpaceAbove(Location location) {
Preconditions.checkNotNull(location, "location");

Location result = location.clone();
while (true) {
Block block = result.getBlock();
if (block == null || block.getType() == Material.AIR) break;
Block block = location.getBlock();

result.setY(result.getY() + 1);
}
int maxSearch = 5;
while (block != null && block.getType().isOccluding() && maxSearch-- > 0)
block = block.getRelative(BlockFace.UP);
if (maxSearch < 0) return null;

maxSearch = 40;
while (block != null && block.getType() != Material.AIR && maxSearch-- > 0)
block = block.getRelative(BlockFace.UP);

return result;
return maxSearch < 0 || block == null ? null : block.getLocation();
}
}
3 changes: 1 addition & 2 deletions core/src/main/java/tc/oc/pgm/match/MatchManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public MatchManagerImpl(Logger logger) {
this.matchByWorld = new HashMap<>();

final Config config = PGM.get().getConfiguration();
this.unloadNonMatches =
config.getExperiments().getOrDefault("unload-non-match-worlds", "false").equals("true");
this.unloadNonMatches = config.getExperimentAsBool("unload-non-match-worlds", false);

long delaySecs = (config.getStartTime().getSeconds() + 1) / 2;
try {
Expand Down
Loading

0 comments on commit 9cefcb6

Please sign in to comment.