Skip to content

Commit

Permalink
Check visibility when broadcasting mode changes (#941)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick <[email protected]>
  • Loading branch information
CoWinkKeyDinkInc authored Nov 25, 2021
1 parent 1f6803b commit 0560918
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
10 changes: 9 additions & 1 deletion core/src/main/java/tc/oc/pgm/core/CoreMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public CoreMatchModule(Match match, List<Core> cores) {
this.cores = cores;
}

public List<Core> getCores() {
return this.cores;
}

@Override
public void enable() {
if (this.match.getMap().getProto().isOlderThan(MODES_IMPLEMENTATION_VERSION)) {
Expand Down Expand Up @@ -151,11 +155,15 @@ public void lavaProtection(final BlockTransformEvent event) {
}
}

@EventHandler(priority = EventPriority.MONITOR)
@EventHandler(priority = EventPriority.HIGHEST)
public void onObjectiveModeSwitch(final ObjectiveModeChangeEvent event) {
for (Core core : this.cores) {
if (core.getModes() == null || core.getModes().contains(event.getMode())) {
core.replaceBlocks(event.getMode().getMaterialData());
// if at least one of the cores are visible, the mode change message will be sent
if (core.isVisible()) {
event.setVisible(true);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,16 @@ public void processBlockDamage(BlockDamageEvent event) {
}
}

@EventHandler(priority = EventPriority.MONITOR)
@EventHandler(priority = EventPriority.HIGHEST)
public void onObjectiveModeSwitch(final ObjectiveModeChangeEvent event) {
for (Destroyable destroyable : this.destroyables) {
if (destroyable.getModes() == null || destroyable.getModes().contains(event.getMode())) {
double oldCompletion = destroyable.getCompletion();
destroyable.replaceBlocks(event.getMode().getMaterialData());
// if at least one of the destroyables are visible, the mode change message will be sent
if (destroyable.isVisible()) {
event.setVisible(true);
}
if (oldCompletion != destroyable.getCompletion()) {
// Multi-stage destroyables can have their total completion changed by this
this.match.callEvent(new DestroyableHealthChangeEvent(this.match, destroyable, null));
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/tc/oc/pgm/modes/ObjectiveModeChangeEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ public class ObjectiveModeChangeEvent extends MatchEvent {

private final Mode mode;
private String name;
private boolean visible;
private static final HandlerList handlers = new HandlerList();

public ObjectiveModeChangeEvent(Match match, final Mode mode) {
super(match);
this.mode = mode;
this.visible = false;

if (this.mode.getName() != null) {
this.name = this.mode.getName();
Expand All @@ -42,10 +44,18 @@ public ObjectiveModeChangeEvent(Match match, final Mode mode) {
}
}

public boolean isVisible() {
return this.visible;
}

public final Mode getMode() {
return this.mode;
}

public void setVisible(boolean visible) {
this.visible = visible;
}

public void setName(@Nonnull String name) {
this.name = Preconditions.checkNotNull(name, "name");
}
Expand Down
18 changes: 10 additions & 8 deletions core/src/main/java/tc/oc/pgm/modes/ObjectiveModesMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,15 @@ public boolean apply(@Nullable ModeChangeCountdown countdown) {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onObjectiveModeChange(ObjectiveModeChangeEvent event) {
Component broadcast =
text()
.append(text("> > > > ", NamedTextColor.DARK_AQUA))
.append(text(event.getName(), NamedTextColor.DARK_RED))
.append(text(" < < < <", NamedTextColor.DARK_AQUA))
.build();
event.getMatch().sendMessage(broadcast);
event.getMatch().playSound(SOUND);
if (event.isVisible()) {
Component broadcast =
text()
.append(text("> > > > ", NamedTextColor.DARK_AQUA))
.append(text(event.getName(), NamedTextColor.DARK_RED))
.append(text(" < < < <", NamedTextColor.DARK_AQUA))
.build();
match.sendMessage(broadcast);
match.playSound(SOUND);
}
}
}

0 comments on commit 0560918

Please sign in to comment.