Skip to content

Instantly share code, notes, and snippets.

@jamierocks jamierocks/HackScore.java
Last active Apr 14, 2016

Embed
What would you like to do?
package uk.jamierocks.sponge.hack.scoreboard;
import uk.jamierocks.sponge.hack.util.Wrapper;
import com.google.common.collect.Sets;
import org.spongepowered.api.scoreboard.Score;
import org.spongepowered.api.scoreboard.Team;
import org.spongepowered.api.text.Text;
public class HackScore extends Wrapper<Score> {
private Team fakeTeam;
public HackScore(Score handle) {
super(handle);
this.fakeTeam = Team.builder()
.name(handle.getName() + "-team")
.displayName(handle.getName().toBuilder().append(Text.of("Team")).build())
.members(Sets.newHashSet(handle.getName()))
.build();
}
public Team getFakeTeam() {
return this.fakeTeam;
}
public void setDisplayName(Text text) {
this.fakeTeam.setSuffix(text);
}
}
package uk.jamierocks.sponge.hack.scoreboard;
import uk.jamierocks.sponge.hack.util.Wrapper;
import org.spongepowered.api.scoreboard.Scoreboard;
import org.spongepowered.api.scoreboard.critieria.Criteria;
import org.spongepowered.api.scoreboard.displayslot.DisplaySlot;
import org.spongepowered.api.scoreboard.displayslot.DisplaySlots;
import org.spongepowered.api.scoreboard.objective.Objective;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
public class HackScoreboard extends Wrapper<Scoreboard> {
private static final Objective OBJECTIVE = Objective.builder()
.name("hack-obj")
.displayName(Text.builder().append(Text.of("Hack")).color(TextColors.DARK_BLUE).build())
.criterion(Criteria.DUMMY)
.build();
private int lastPosition = 15;
public HackScoreboard() {
super(Scoreboard.builder().build());
this.addDivider(Text.of(TextColors.RESET));
this.getHandle().addObjective(OBJECTIVE);
this.getHandle().updateDisplaySlot(OBJECTIVE, DisplaySlots.SIDEBAR);
}
/**
* Gets the objective from the given {@link DisplaySlot}.
*
* @param slot The display slot.
* @return An objective is their is one for that slot, or {@code null} otherwise.
*/
public Objective getObjective(DisplaySlot slot) {
if (this.getHandle().getObjective(slot).isPresent()) {
return this.getHandle().getObjective(slot).get();
}
return null;
}
public void addScore(HackScore hackScore) {
hackScore.getHandle().setScore(this.getNextPosition());
this.getHandle().registerTeam(hackScore.getFakeTeam());
}
public void addDivider(Text text) {
OBJECTIVE.getOrCreateScore(text).setScore(this.getNextPosition());
}
public int getNextPosition() {
return this.lastPosition--;
}
}
package uk.jamierocks.sponge.hack.util;
public abstract class Wrapper<T> {
private final T handle;
public Wrapper(final T handle) {
this.handle = handle;
}
public T getHandle() {
return this.handle;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.