Skip to content

Instantly share code, notes, and snippets.

@kelvneo
Last active August 29, 2015 13:59
Show Gist options
  • Save kelvneo/10453947 to your computer and use it in GitHub Desktop.
Save kelvneo/10453947 to your computer and use it in GitHub Desktop.
This is a neat way of placing a sidebar. No NMS or CraftBukkit required. It requires Guava though. You may copy the code for your own use. Github Page: https://github.com/deathline75/FrostLib
package com.frosty.frostlib;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import com.frosty.Sidebar;
import com.google.common.base.Predicate;
import com.google.common.collect.Maps;
/**
* Suppressing OfflinePlayer is annoying.
* This class should never be called because its really derpy.
*
* @author Kelvin Neo
* @version 0.0.1
*/
@SuppressWarnings("deprecation")
public class FrostSidebar implements Sidebar {
private Map<String, Integer> values = new ConcurrentHashMap<String, Integer>();
private Scoreboard scoreboard;
private Objective objective;
public FrostSidebar(String objectiveName) {
this(objectiveName, objectiveName);
}
public FrostSidebar(String objectiveName, String title) {
this(objectiveName, title, Bukkit.getScoreboardManager().getNewScoreboard());
}
public FrostSidebar(String objectiveName, Scoreboard existingScoreboard) {
this(objectiveName, objectiveName, existingScoreboard);
}
public FrostSidebar(String objectiveName, String title, Scoreboard existingScoreboard) {
this.scoreboard = existingScoreboard;
if (scoreboard.getObjective(DisplaySlot.SIDEBAR) != null && objectiveName == null) {
this.objective = scoreboard.getObjective(DisplaySlot.SIDEBAR);
} else {
this.objective = scoreboard.registerNewObjective(objectiveName, "dummy");
}
this.objective.setDisplaySlot(DisplaySlot.SIDEBAR);
this.objective.setDisplayName(title);
}
public Scoreboard getScoreboard() {
return this.scoreboard;
}
public Objective getObjective() {
return this.objective;
}
public String[] getValues(int key) {
return Maps.filterValues(values, new FilterIntegers(key)).keySet().toArray(new String[0]);
}
public String getFirstValue(int key) {
return Maps.filterValues(values, new FilterIntegers(key)).keySet().iterator().next();
}
public String getLastValue(int key) {
// TODO Auto-generated method stub
return null;
}
public void setValue(int key, String value) {
OfflinePlayer playerValue = Bukkit.getOfflinePlayer(value);
objective.getScore(playerValue).setScore(key);
this.values.put(value, key);
}
public String getTitle() {
return objective.getDisplayName();
}
public void setTitle(String title) {
this.objective.setDisplayName(title);
}
public void removeValues(int key) {
for (String value : this.values.keySet()) {
if (this.values.get(value) == key) {
OfflinePlayer playerValue = Bukkit.getOfflinePlayer(value);
getScoreboard().resetScores(playerValue);
this.values.remove(value);
}
}
}
public void removeFirstValue(int key) {
for (String value : this.values.keySet()) {
if (this.values.get(value) == key) {
OfflinePlayer playerValue = Bukkit.getOfflinePlayer(value);
getScoreboard().resetScores(playerValue);
this.values.remove(value);
break;
}
}
}
public void removeValue(String value) {
OfflinePlayer playerValue = Bukkit.getOfflinePlayer(value);
getScoreboard().resetScores(playerValue);
this.values.remove(value);
}
public void overrideValue(int key, String value) {
OfflinePlayer playerValue = Bukkit.getOfflinePlayer(value);
this.removeValues(key);
this.values.put(value, key);
getObjective().getScore(playerValue).setScore(key);
}
public int addValue(String value) {
OfflinePlayer playerValue = FrostLib.getOfflinePlayer(value);
int key = values.size();
objective.getScore(playerValue).setScore(key);
this.values.put(value, key);
return key;
}
public int getKey(String value) {
return this.values.get(value);
}
public void overrideFirstValue(int key, String value) {
OfflinePlayer playerValue = FrostLib.getOfflinePlayer(value);
this.removeFirstValue(key);
this.values.put(value, key);
getObjective().getScore(playerValue).setScore(key);
}
public class FilterIntegers implements Predicate<Integer> {
private int number;
public FilterIntegers(int number) {
this.number = number;
}
public boolean apply(Integer arg0) {
return (number == arg0);
}
}
}
package com.frosty;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
/**
* Sidebar instance for FrostLib
* Go ahead and implement the interface. :D
*
* This interface is for simple sidebar management for nice looking sidebars.
* @author Kelvin Neo
*
*/
public interface Sidebar {
/**
* Gets the scoreboard that is used to build the sidebar.
* @return The scoreboard that is used to build the sidebar.
*/
public Scoreboard getScoreboard();
/**
* Gets the objective of the sidebar
* @return The objective of the sidebar
* @deprecated Dangerous to use
*/
@Deprecated
public Objective getObjective();
/**
* Get the values that are linked to the key
* @param key The integer used to find all the values
* @return An array of values related to the key
*/
public String[] getValues(int key);
/**
* Get the first value that are linked to the key
* @param key The integer used to find all the values
* @return The first value related to the key
*/
public String getFirstValue(int key);
/**
* Get the last value that are linked to the key
* @param key The integer used to find all the values
* @return The last value related to the key
*/
public String getLastValue(int key);
/**
* Set a value for the sidebar
* @param value The value to
* @return The key for the value added
*/
public int addValue(String value);
/**
* Set a value for the sidebar
* @param key The integer to refer it to
* @param value The value to
*/
public void setValue(int key, String value);
/**
* Set a value for the sidebar
* @param key The integer to refer it to
* @param value The value to override
*/
public void overrideValue(int key, String value);
/**
* Set a value for the sidebar
* @param key The integer to refer it to
* @param value The value to override
*/
public void overrideFirstValue(int key, String value);
/**
* Remove all the values linked to the key
* @param key The integer to refer it to
*/
public void removeValues(int key);
/**
* Remove the first value linked to the key
* @param key The integer to refer it to
*/
public void removeFirstValue(int key);
/**
* Get the key of the value
* @param value The value to check
* @return The key of the value
*/
public int getKey(String value);
/**
* Remove the value directly
* @param value The value to be removed
*/
public void removeValue(String value);
/**
* Get the title of the objective
* @return The title of the objective
*/
public String getTitle();
/**
* Sets the title of the objective on the sidebar
* @param title The title of the objective
*/
public void setTitle(String title);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment