Skip to content

Instantly share code, notes, and snippets.

@jamierocks
Last active April 14, 2016 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamierocks/d6c210fd363c8507e20c4eba7dc4f572 to your computer and use it in GitHub Desktop.
Save jamierocks/d6c210fd363c8507e20c4eba7dc4f572 to your computer and use it in GitHub Desktop.
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