/
PlayerData.java
138 lines (121 loc) · 3.25 KB
/
PlayerData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package me.aglerr.mobcoins;
import me.aglerr.mclibs.mysql.SQLHelper;
import me.aglerr.mobcoins.database.SQLDatabaseInitializer;
import me.aglerr.mobcoins.objects.NotificationUser;
import me.aglerr.mobcoins.utils.Utils;
import org.jetbrains.annotations.NotNull;
public class PlayerData implements Cloneable {
private final String uuid;
private final String name;
private double coins = 0;
public PlayerData(String uuid, String name) {
this.uuid = uuid;
this.name = name;
}
public PlayerData(String uuid, String name, double coins){
this.uuid = uuid;
this.name = name;
this.coins = coins;
}
/**
* Get Players UUID
*
* @return player's uuid
*/
public String getUUID() {
return uuid;
}
/**
* Get Players Name
*
* @return player's name
*/
public String getName() {
return name;
}
/**
* Get Players Mobcoins Amount
*
* @return player's mobcoins amount
*/
public double getCoins() {
return coins;
}
/**
* Get Players Formatted Mobcoins Amount
* e.x: 1,234.56
*
* @return player's formatted mobcoins amount
*/
public String getCoinsFormatted() {
return Utils.digits(this.coins);
}
/**
* Get Players Rounded Mobcoins Amount
* e.x: 1,234.8 -> 1,235
*
* @return player's rounded mobcoins amount
*/
public int getCoinsRounded(){
return (int) this.coins;
}
/**
* Get Players Short Formatted Mobcoins Amount
* e.x: 1.5K, 5.75K, 25K, 100M
*
* @return player's short formatted mobcoins amount
*/
public String getCoinsShortFormat(){
return Utils.digits(this.coins);
}
/**
* Add coins to player data
*
* @param amount the amount coins that will be added
*/
public void addCoins(double amount){
this.coins = this.coins + amount;
}
/**
* Reduce coins from player data
*
* @param amount the amount coins that will be reduced
*/
public void reduceCoins(double amount){
this.coins = this.coins - amount;
}
/**
* Set amount of coins to player data
*
* @param amount the amount coins that will be set
*/
public void setCoins(double amount){
this.coins = amount;
}
/**
* Save player data to the database (call it async if can)
*/
public void save(NotificationUser notificationUser) {
SQLHelper.executeUpdate("REPLACE INTO `" + SQLDatabaseInitializer.MOBCOINS_TABLE + "` VALUES (" +
"\"" + this.uuid + "\", " +
"\"" + this.name + "\", " +
this.coins + ", " +
"\"" + notificationUser.wrapOptions() + "\"" + ");"
);
}
/**
* Clone the current PlayerData object
* Mainly this method used to compare the mobcoins amount on saving
* Take a look at {@link me.aglerr.mobcoins.managers.managers.PlayerDataManager}
*
* @return cloned PlayerData object
*/
@NotNull
public PlayerData clone() {
try {
return (PlayerData) super.clone();
} catch (CloneNotSupportedException var2) {
throw new Error(var2);
}
}
}