This is how I do attributes that are temporary and permanent. It uses an Enum. Please comment if you have suggestions of doing this better.
Features include
- Click button to force chat attribute state or amount
- Display all attributes on a tab interface
- Display top players online related to the attribute
- Integer and boolean
- Very simple, clean, and friendly
Call usage
Code:
player.getAttribute().get(Attribute.EXAMPLE_1);
Code:
package ethos.model.players;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import ethos.util.Misc;
/**
* Handles attributes for players
*
* @author 2012 <http://www.rune-server.org/members/dexter+morgan/>
*/
public class AttributeManager {
/**
* The attribute
*/
private HashMap<Attribute, Integer> attribute = new HashMap<Attribute, Integer>();
/**
* The attribute
*/
public enum Attribute {
// 6986 is the tab line id where the points are displayed
// true means it is saved
// 19000 is the hiscore line id
EXAMPLE_1(6986, true, 19000),
;
/**
* The string id
*/
private int id;
/**
* The hiscore id
*/
private int hiscore;
/**
* Whether to save
*/
private boolean save;
/**
* Sets the id
*
* @param id the id
* @param safe whether is saved in file
* @param hiscore the hiscore id
*/
Attribute(int id, boolean safe, int hiscore) {
this.id = id;
this.save = save;
this.hiscore = hiscore;
}
/**
* Save by default with no id
*/
Attribute() {
this(0, true, 0);
}
/**
* Specify save setting with no id
*/
Attribute(boolean safe) {
this(0, safe, 0);
}
/**
* Specify id with save setting
*/
Attribute(int id, boolean safe) {
this(id, safe, 0);
}
/**
* Save setting with hiscore
*/
Attribute(boolean safe, int hiscore) {
this(0, safe, hiscore);
}
/**
* Sets the id
*
* @return the id
*/
public int getId() {
return id;
}
/**
* Sets the save
*
* @return the save
*/
public boolean isSave() {
return save;
}
/**
* Gets the hiscore
*
* @return the hiscore
*/
public int getHiscore() {
return hiscore;
}
/**
* Gets the attribute by button
*
* @param button the button
* @return the attribute
*/
public static Attribute getForButton(int button) {
return Arrays.stream(values()).filter(c -> c.getHiscore() == button).findFirst()
.orElse(null);
}
}
/**
* Sends the chat
*
* @param player the player
* @param button the button
* @return the chat
*/
public static boolean sendChat(Player player, int button) {
for (Attribute attribute : Attribute.values()) {
if (attribute.getId() - 1 == button) {
String name = Misc.capitalize(attribute.name().toLowerCase());
String suffix = name.endsWith("s") ? "are" : "is";
String string =
"My " + name + " " + suffix + ": " + player.getAttribute().display(attribute);
player.getPA().sendString(attribute.getId(), player.getAttribute().display(attribute));
player.forcedChat(string.replaceAll("_", " "));
return true;
}
}
return false;
}
/**
* Sending the tab
*
* @param player the player
*/
public static void sendTab(Player player) {
for (Attribute points : Attribute.values()) {
if (points.getId() == 0) {
continue;
}
player.getPA().sendString(points.getId(), player.getAttribute().display(points));
}
}
/**
* Gets the top players online
*
* @param key the key
* @param amount the amount
* @param type the type
*/
public static List<Player> sendTopPlayersOnline(Attribute key, int amount, int type) {
List<Player> online = new ArrayList<Player>();
if (PlayerHandler.getPlayerCount() < amount) {
amount = PlayerHandler.getPlayerCount();
}
for (Player players : PlayerHandler.players) {
if (players == null) {
continue;
}
online.add(players);
}
Collections.sort(online, new Comparator<Player>() {
@Override
public int compare(Player o1, Player o2) {
double type1 = o1.getAttribute().get(key);
double type2 = o2.getAttribute().get(key);
if (type == 1) {
if (type1 == type2) {
return 0;
} else if (type1 > type2
&& o1.getAttribute().get(key) > o2.getAttribute().get(key)) {
return 1;
} else {
return -1;
}
} else {
if (type1 == type2) {
return 0;
} else if (type1 > type2) {
return -1;
} else {
return 1;
}
}
}
});
return online;
}
/**
* Displays the attribute
*
* @param attribute the attribute
* @return the attribute
*/
public String display(Attribute attribute) {
return NumberFormat.getInstance().format(get(attribute));
}
/**
* Creates a new attribute
*
* @param key the attribute
*/
public void create(Attribute key) {
attribute.put(key, 0);
}
/**
* Gets the attribute
*
* @param key the attribute
* @return the attribute
*/
public int get(Attribute key) {
return attribute.get(key) == null ? 0 : attribute.get(key);
}
/**
* Checks the attribute
*
* @param key the attribute
* @return the attribute
*/
public boolean check(Attribute key) {
return attribute.get(key) == null ? false : attribute.get(key).intValue() == 1;
}
/**
* Toggles the attribute
*
* @param key the attribute
*/
public void toggle(Attribute key) {
set(key, !check(key));
}
/**
* Sets the attribute
*
* @param key the attribute
* @param state the state
*/
public void set(Attribute key, boolean state) {
if (attribute.get(key) == null) {
create(key);
}
attribute.put(key, state ? 1 : 0);
}
/**
* Sets the attribute
*
* @param key the attribute
* @param value the value
*/
public void set(Attribute key, int value) {
if (attribute.get(key) == null) {
create(key);
}
attribute.put(key, value);
}
/**
* Increases the attribute
*
* @param key the attribute
*/
public void increase(Attribute key) {
increase(key, 1);
}
/**
* Increases by amount
*
* @param key the attribute
* @param value the value
*/
public void increase(Attribute key, int value) {
if (attribute.get(key) == null) {
create(key);
}
attribute.put(key, attribute.get(key) + value);
}
/**
* Decreases the attribute
*
* @param key the attribute
* @return the attribute
*/
public boolean decrease(Attribute key) {
if (attribute.get(key) == null) {
create(key);
}
if (attribute.get(key) < 1) {
return false;
}
set(key, attribute.get(key) - 1);
return true;
}
/**
* Decreases the attribute
*
* @param key the key
* @param amount the amount
*/
public void decrease(Attribute key, long amount) {
for (int i = 1; i <= amount; i++) {
if (!decrease(key)) {
break;
}
}
}
/**
* Gets the attributes
*
* @return the attributes
*/
public HashMap<Attribute, Integer> get() {
return attribute;
}
}
Saving
Code:
for (Attribute points : Attribute.values()) {
if (!points.isSave()) {
continue;
}
int value = player.getPoints().get(points);
String name = points.name();
object.addProperty(name, value);
}
Loading
Code:
for (Attribute points : Attribute.values()) {
if (reader.has(points.name())) {
int value = reader.get(points.name()).getAsInt();
player.getPoints().set(points, value);
}
}
Hiscore can be used such as