Skip to content

Commit

Permalink
Add modify-lives action
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher White <[email protected]>
  • Loading branch information
cswhite2000 committed Aug 12, 2023
1 parent c9c4b3e commit de13461
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 14 deletions.
15 changes: 15 additions & 0 deletions core/src/main/java/tc/oc/pgm/action/ActionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.action.actions.ActionNode;
import tc.oc.pgm.action.actions.BlitzLivesAction;
import tc.oc.pgm.action.actions.ExposedAction;
import tc.oc.pgm.action.actions.FillAction;
import tc.oc.pgm.action.actions.KillEntitiesAction;
Expand Down Expand Up @@ -289,4 +290,18 @@ public FillAction parseFill(Element el, Class<?> scope) throws InvalidXMLExcepti
filters.parseProperty(Node.fromAttr(el, "filter")),
XMLUtils.parseBoolean(el.getAttribute("events"), false));
}

@MethodParser("modify-lives")
public <T extends Filterable<?>> BlitzLivesAction<T> parseModifyLives(Element el, Class<T> scope)
throws InvalidXMLException {
scope = parseScope(el, scope);
Operation operation = Operation.parseOperation(Node.fromAttrOrSelf(el, "operation").getValue());
boolean showTitle = XMLUtils.parseBoolean(el.getAttribute("show-title"), true);
String expression = Node.fromRequiredAttr(el, "amount").getValue();
Formula<T> formula =
Formula.of(
expression, variables.getVariableNames(scope), variables.getContextBuilder(scope));

return new BlitzLivesAction<>(scope, operation, formula, showTitle);
}
}
30 changes: 30 additions & 0 deletions core/src/main/java/tc/oc/pgm/action/Operation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package tc.oc.pgm.action;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public enum Operation {
ADD("add", "addition", "plus"),
SUBTRACT("sub", "subtract", "minus"),
SET("set");

String[] aliases;

Operation(String... aliases) {
this.aliases = aliases;
}

static final Map<String, Operation> operationMap = new ConcurrentHashMap<>();

static {
for (Operation operation : Operation.values()) {
for (String alias : operation.aliases) {
operationMap.put(alias, operation);
}
}
}

public static Operation parseOperation(String operationString) {
return operationMap.get(operationString);
}
}
87 changes: 87 additions & 0 deletions core/src/main/java/tc/oc/pgm/action/actions/BlitzLivesAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package tc.oc.pgm.action.actions;

import java.util.function.Consumer;
import tc.oc.pgm.action.Operation;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.party.Party;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.blitz.BlitzMatchModule;
import tc.oc.pgm.filters.Filterable;
import tc.oc.pgm.util.math.Formula;

public class BlitzLivesAction<T extends Filterable<?>> extends AbstractAction<T> {

final Consumer<T> modifyLivesConsumer;
final Operation operation;
final Formula<T> formula;
final boolean showTitle;

public BlitzLivesAction(
Class<T> scope, Operation operation, Formula<T> formula, boolean showTitle) {
super(scope);
this.operation = operation;
this.formula = formula;
this.showTitle = showTitle;
this.modifyLivesConsumer = chooseScopeConsumer(scope);
}

public void changeLives(MatchPlayer player, int amount) {
BlitzMatchModule blitzMatchModule = player.getMatch().getModule(BlitzMatchModule.class);
if (blitzMatchModule == null) return;

switch (operation) {
case ADD:
blitzMatchModule.addLives(player.getId(), amount);
break;
case SUBTRACT:
blitzMatchModule.addLives(player.getId(), amount * -1);
break;
case SET:
blitzMatchModule.setLives(player.getId(), amount);
break;
}

if (showTitle) {
blitzMatchModule.showLivesTitle(player);
}
}

@Override
public void trigger(T t) {
modifyLivesConsumer.accept(t);
}

private Consumer<T> chooseScopeConsumer(Class<T> cls) {
if (MatchPlayer.class.isAssignableFrom(cls)) {
return this::triggerPlayer;
} else if (Party.class.isAssignableFrom(cls)) {
return this::triggerParty;
} else if (Match.class.isAssignableFrom(cls)) {
return this::triggerMatch;
} else {
throw new UnsupportedOperationException("Unknown Scope: " + cls);
}
}

public void triggerPlayer(T t) {
MatchPlayer player = (MatchPlayer) t;
int amount = (int) formula.applyAsDouble(t);
changeLives(player, amount);
}

public void triggerParty(T t) {
Party party = (Party) t;
int amount = (int) formula.applyAsDouble(t);
for (MatchPlayer player : party.getPlayers()) {
changeLives(player, amount);
}
}

public void triggerMatch(T t) {
Match match = (Match) t;
int amount = (int) formula.applyAsDouble(t);
for (MatchPlayer player : match.getParticipants()) {
changeLives(player, amount);
}
}
}
37 changes: 23 additions & 14 deletions core/src/main/java/tc/oc/pgm/blitz/BlitzMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ public int getNumOfLives(UUID id) {
return lifeManager.getLives(id);
}

public void addLives(UUID id, int lives) {
lifeManager.addLives(id, lives);
}

public void setLives(UUID id, int lives) {
lifeManager.setLives(id, lives);
}

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void handleDeath(final MatchPlayerDeathEvent event) {
MatchPlayer victim = event.getVictim();
Expand Down Expand Up @@ -123,23 +131,24 @@ public void handleJoin(final PlayerParticipationStartEvent event) {
@EventHandler
public void handleSpawn(final ParticipantSpawnEvent event) {
if (this.config.getBroadcastLives()) {
int lives = this.lifeManager.getLives(event.getPlayer().getId());
event
.getPlayer()
.showTitle(
title(
empty(),
translatable(
"blitz.livesRemaining",
NamedTextColor.RED,
translatable(
lives == 1 ? "misc.life" : "misc.lives",
NamedTextColor.AQUA,
text(lives))),
Title.Times.times(Duration.ZERO, fromTicks(60), fromTicks(20))));
MatchPlayer matchPlayer = event.getPlayer();
showLivesTitle(matchPlayer);
}
}

public void showLivesTitle(MatchPlayer matchPlayer) {
int lives = this.lifeManager.getLives(matchPlayer.getId());
matchPlayer.showTitle(
title(
empty(),
translatable(
"blitz.livesRemaining",
NamedTextColor.RED,
translatable(
lives == 1 ? "misc.life" : "misc.lives", NamedTextColor.AQUA, text(lives))),
Title.Times.times(Duration.ZERO, fromTicks(60), fromTicks(20))));
}

@EventHandler(priority = EventPriority.MONITOR)
public void onBlitzPlayerEliminated(final BlitzPlayerEliminatedEvent event) {
this.eliminatedPlayers.add(event.getPlayer().getId());
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/tc/oc/pgm/blitz/LifeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ public int addLives(UUID player, int dlives) {

return lives;
}

public void setLives(UUID player, int lives) {
assertNotNull(player, "player id");

this.livesLeft.put(player, Math.max(0, lives));
}
}

0 comments on commit de13461

Please sign in to comment.