Skip to content

Commit

Permalink
Fix objective event performance issues
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 committed Jul 19, 2024
1 parent 6bedcf1 commit 3534858
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 42 deletions.
15 changes: 9 additions & 6 deletions core/src/main/java/tc/oc/pgm/api/match/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
*
* <p>Each {@link Match} should operate like an independent {@link org.bukkit.Server}, and ensure
* its resources, such as {@link MatchPlayer}s, can only interact with other resources in the same
* {@link Match}. This should allow multiple {@link Match}es to run concurrently on the same {@link
* org.bukkit.Server}, as long as resources are cleaned up after {@link #unload()}.
* {@link Match}. This should allow multiple {@link Match}es to run concurrently on the same
* {@link org.bukkit.Server}, as long as resources are cleaned up after {@link #unload()}.
*/
public interface Match
extends MatchPlayerResolver,
Expand Down Expand Up @@ -254,8 +254,8 @@ default boolean finish() {
/**
* Get the {@link CountdownContext} for the {@link Match}.
*
* <p>There are several places in the code that assume this should be a {@link
* tc.oc.pgm.countdowns.SingleCountdownContext}. Will need to be fixed later.
* <p>There are several places in the code that assume this should be a
* {@link tc.oc.pgm.countdowns.SingleCountdownContext}. Will need to be fixed later.
*
* @return The {@link CountdownContext}.
*/
Expand Down Expand Up @@ -420,13 +420,16 @@ default boolean setParty(MatchPlayer player, Party party) {
Collection<VictoryCondition> getVictoryConditions();

/**
* Calculate whether a {@link VictoryCondition} has been meet, and if so, transition the {@link
* Match} to {@link MatchPhase#FINISHED}.
* Calculate whether a {@link VictoryCondition} has been meet, and if so, transition the
* {@link Match} to {@link MatchPhase#FINISHED}.
*
* @return If the {@link Match} was just ended.
*/
boolean calculateVictory();

/** Invalidates the ranking of participating competitors. */
void invalidateRanking();

/**
* Get whether friendly fire should be on or off.
*
Expand Down
6 changes: 2 additions & 4 deletions core/src/main/java/tc/oc/pgm/goals/GoalMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,11 @@ public GoalProgress getProgress(Competitor competitor) {
return progress;
}

protected void updateProgress(Goal goal) {
protected void updateProgress(Goal<?> goal) {
for (Competitor competitor : competitorsByGoal.get(goal)) {
progressByCompetitor.put(competitor, new GoalProgress(competitor));
}

// Forces team rankings to be invalidated
match.getWinners();
match.invalidateRanking();
}

// TODO: These events will often be fired together.. debounce them somehow?
Expand Down
64 changes: 32 additions & 32 deletions core/src/main/java/tc/oc/pgm/match/MatchImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ protected MatchImpl(String id, MapContext map, World world) {
this.state = new AtomicReference<>(MatchPhase.IDLE);
this.start = new AtomicLong(0);
this.end = new AtomicLong(0);
this.capacity =
new AtomicInteger(map.getInfo().getMaxPlayers().stream().mapToInt(i -> i).sum());
this.capacity = new AtomicInteger(
map.getInfo().getMaxPlayers().stream().mapToInt(i -> i).sum());
this.executors = new EnumMap<>(MatchScope.class);
this.listeners = new EnumMap<>(MatchScope.class);
this.tickables = new EnumMap<>(MatchScope.class);
Expand All @@ -147,21 +147,17 @@ protected MatchImpl(String id, MapContext map, World world) {
this.players = new ConcurrentHashMap<>();
this.partyChanges = new WeakHashMap<>();
this.parties = new LinkedHashSet<>();
this.victory =
new RankedSet<>(
Comparator.<VictoryCondition, Boolean>comparing(
vc -> !vc.isCompleted(this), Boolean::compare)
.thenComparing(VictoryCondition::getPriority));
this.victory = new RankedSet<>(Comparator.<VictoryCondition, Boolean>comparing(
vc -> !vc.isCompleted(this), Boolean::compare)
.thenComparing(VictoryCondition::getPriority));
this.competitors = new HashSet<>();
this.winners =
new RankedSet<>(
(Competitor a, Competitor b) -> {
for (VictoryCondition condition : getVictoryConditions()) {
int result = condition.compare(a, b);
if (result != 0 || condition.isFinal(this)) return result;
}
return 0;
});
this.winners = new RankedSet<>((Competitor a, Competitor b) -> {
for (VictoryCondition condition : getVictoryConditions()) {
int result = condition.compare(a, b);
if (result != 0 || condition.isFinal(this)) return result;
}
return 0;
});
this.queuedParticipants = new AtomicReference<>();
this.observers = new ObserverParty(this);
this.features = new MatchFeatureContext();
Expand Down Expand Up @@ -340,8 +336,10 @@ public void execute(Listener other, Event event) throws EventException {
}

private void startListener(Listener listener) {
for (Map.Entry<Class<? extends Event>, Set<RegisteredListener>> entry :
PGM.get().getPluginLoader().createRegisteredListeners(listener, PGM.get()).entrySet()) {
for (Map.Entry<Class<? extends Event>, Set<RegisteredListener>> entry : PGM.get()
.getPluginLoader()
.createRegisteredListeners(listener, PGM.get())
.entrySet()) {
Class<? extends Event> eventClass = entry.getKey();
HandlerList handlerList = Events.getEventListeners(eventClass);

Expand Down Expand Up @@ -464,10 +462,9 @@ public void removePlayer(Player bukkit) {
@Override
public boolean setParty(MatchPlayer player, Party party, @Nullable JoinRequest request) {
if (request == null)
request =
party instanceof Team
? JoinRequest.of((Team) party, JoinRequest.Flag.FORCE)
: JoinRequest.force();
request = party instanceof Team
? JoinRequest.of((Team) party, JoinRequest.Flag.FORCE)
: JoinRequest.force();
return setOrClearPlayerParty(player, assertNotNull(party), request);
}

Expand Down Expand Up @@ -510,13 +507,12 @@ private boolean setOrClearPlayerParty(
// to detect nested calls for the same player, which we definitely do not want.
Party nested = partyChanges.put(player, newParty);
if (nested != null) {
throw new IllegalStateException(
"Nested party change: "
+ player
+ " tried to join "
+ newParty
+ " in the middle of joining "
+ nested);
throw new IllegalStateException("Nested party change: "
+ player
+ " tried to join "
+ newParty
+ " in the middle of joining "
+ nested);
}

if (oldParty instanceof Competitor) {
Expand Down Expand Up @@ -663,6 +659,11 @@ public Collection<Competitor> getSortedCompetitors() {
return ImmutableList.copyOf(winners);
}

@Override
public void invalidateRanking() {
winners.invalidateRanking();
}

@Override
public Collection<Competitor> getWinners() {
winners.invalidateRanking();
Expand Down Expand Up @@ -811,9 +812,8 @@ protected MatchModule createModule(MatchModuleFactory<?> factory) throws ModuleL
matchModules.put(module.getClass(), module);

if (module instanceof Listener && getListenerScope((Listener) module) == null) {
logger.warning(
module.getClass().getSimpleName()
+ " implements Listener but is not annotated with @ListenerScope");
logger.warning(module.getClass().getSimpleName()
+ " implements Listener but is not annotated with @ListenerScope");
}

if (module instanceof Listener) {
Expand Down

0 comments on commit 3534858

Please sign in to comment.