Skip to content

Commit

Permalink
Change render for renderOnce from Player to RenderContext
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyJayJay committed Apr 19, 2019
1 parent a94c4bf commit f86b026
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@
*/
public abstract class AbstractMapRenderer extends MapRenderer {

private final Set<Player> alreadyReceived;
private final Set<RenderContext> alreadyReceived;
private final boolean renderForAllPlayers, renderOnce;
private final Set<Player> receivers;
private final Predicate<RenderContext> precondition;

private boolean stop;

protected AbstractMapRenderer(Set<Player> receivers, boolean renderOnce, Predicate<RenderContext> precondition) {
super(!receivers.isEmpty());
this.renderForAllPlayers = receivers.isEmpty();
this.receivers = receivers;
this.renderOnce = renderOnce;
this.precondition = precondition;
this.alreadyReceived = new HashSet<>(); // TODO consider an implementation with less overhead
this.stop = false;
}

@Override
Expand All @@ -45,13 +48,14 @@ public final void render(MapView map, MapCanvas canvas, Player player) {
if (mayRender(context)) {
render(context);
if (renderOnce)
alreadyReceived.add(player);
alreadyReceived.add(context);
}
}

private boolean mayRender(RenderContext context) {
return (renderForAllPlayers || receivers.contains(context.getPlayer()))
&& (!renderOnce || !alreadyReceived.contains(context.getPlayer()))
return !stop
&&(renderForAllPlayers || receivers.contains(context.getPlayer()))
&& (!renderOnce || !alreadyReceived.contains(context))
&& precondition.test(context);
}

Expand Down Expand Up @@ -97,12 +101,21 @@ public boolean isRenderOnce() {
return renderOnce;
}

/**
* Makes this renderer stop rendering anything, forever.
*/
public void stopRendering() {
this.stop = true;
}

/**
* Renders the map after the preconditions have passed, i.e.:
* <ul>
* <li>The predicate's test was successful</li>
* <li>This renderer applies to the player or</li>
* <li>This renderer renders maps for all players</li>
* <li>This renderer does not only render once or</li>
* <li>This renderer has not rendered for the given context yet</li>
* <li>The predicate's test was successful</li>
* </ul>
*
* @param context the context for this rendering operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapView;

import java.util.Objects;

/**
* A container class for every map rendering operation. It contains the subject
* {@link MapView}, a {@link MapCanvas} as well as the {@link Player} the map is rendered for.
Expand Down Expand Up @@ -60,4 +62,18 @@ public MapCanvas getCanvas() {
public Player getPlayer() {
return player;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RenderContext that = (RenderContext) o;
return mapView.getId() == that.mapView.getId()
&& Objects.equals(player, that.player);
}

@Override
public int hashCode() {
return Objects.hash(mapView.getId(), player);
}
}

0 comments on commit f86b026

Please sign in to comment.