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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
package mc.bloostrynetwork.antox11200.bloostryapi.utils.database.redis;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import com.google.gson.Gson;
import mc.bloostrynetwork.antox11200.bloostryapi.Main;
import mc.bloostrynetwork.antox11200.bloostryapi.utils.IntegerUtils;
import mc.bloostrynetwork.antox11200.bloostryapi.utils.console.Console;
import mc.bloostrynetwork.antox11200.bloostryapi.utils.database.RequestDatabase;
import mc.bloostrynetwork.antox11200.bloostryapi.utils.player.BloostryPlayer;
import mc.bloostrynetwork.antox11200.bloostryapi.utils.server.ServerStatus;
import net.minecraft.server.v1_8_R3.ExceptionUnknownCommand;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class RedisClient extends RequestDatabase {
public final String PATH_BLOOSTRYPLAYER = "bloostryPlayers:";
public final String PATH_SERVER_STATUS = "serverStatus:status:";
public final String PATH_SERVER_ONLINE = "serverStatus:playersonline:";
public static final String PREFIX = "§8[§cRedis§8] ";
private String name;
private String adress;
private String password;
private int port;
public JedisPool jedisPool;
public RedisClient(String name, String adress, int port, String password) {
this.name = name;
this.adress = adress;
this.port = port;
this.password = password;
}
public void connection() {
ClassLoader previous = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(RedisClient.class.getClassLoader());
jedisPool = new JedisPool(this.adress, this.port);
Thread.currentThread().setContextClassLoader(previous);
Console.sendMessageInfo(PREFIX + "§aRedis client is connected from server redis :D");
}
public void deconnection() {
this.jedisPool.close();
Console.sendMessageInfo(PREFIX + "§aRedis client is disconnected from server redis \nGood Bye :D");
}
public boolean isConnected() {
if(jedisPool == null)
return false;
if(getJedis() == null)
return false;
if(getJedis().isConnected() && (!(jedisPool.isClosed())))
return true;
return false;
}
/*public JedisCluster getJedisCluster() {
return this.jedisCluster;
}*/
public Jedis getJedis() {
try (Jedis jedis = jedisPool.getResource()){
jedis.auth(this.password);
return jedis;
} catch (Exception e){
Console.sendMessageError("§cJedis can't recover variable !");
return null;
}
}
public String getName() {
return name;
}
public String getAdress() {
return adress;
}
public String getPassword() {
return password;
}
public int getPort() {
return port;
}
// TOUTES LES METHODES CI-DESSOUS SONT DES METHODES EXTENDU DE LA CLASSE
// 'RequestDatabase'
@Override
public boolean hasUser(String name) {
return Main.bloostryAPI.getDatabaseManager().getMySQLClient().hasUser(name);
}
@Override
public boolean hasData(UUID uuid) {
return getJedis().get(PATH_BLOOSTRYPLAYER + uuid.toString()) != null;
}
@Override
public String getUUID(String name) {
return Main.bloostryAPI.getDatabaseManager().getMySQLClient().getUUID(name);
}
@Override
public BloostryPlayer getData(UUID uuid) {
if (!(hasData(uuid)))
return null;
String bloostryPlayerJson = getJedis().get(PATH_BLOOSTRYPLAYER + uuid.toString());
String firstLetter = bloostryPlayerJson.substring(0, 1);
String lastLetter = bloostryPlayerJson.substring(bloostryPlayerJson.length() - 1, bloostryPlayerJson.length());
if (firstLetter.equalsIgnoreCase("'") && lastLetter.equalsIgnoreCase("'")) {
bloostryPlayerJson = bloostryPlayerJson.substring(1, bloostryPlayerJson.length() - 1);
} else if (firstLetter.equalsIgnoreCase("'") && !lastLetter.equalsIgnoreCase("'")) {
bloostryPlayerJson = bloostryPlayerJson.substring(1, bloostryPlayerJson.length());
} else if (!firstLetter.equalsIgnoreCase("'") && lastLetter.equalsIgnoreCase("'")) {
bloostryPlayerJson = bloostryPlayerJson.substring(0, bloostryPlayerJson.length() - 1);
}
bloostryPlayerJson = bloostryPlayerJson.replace("\\", "");
Console.sendMessageDebug(bloostryPlayerJson);
return new Gson().fromJson(bloostryPlayerJson, BloostryPlayer.class);
}
@Override
public void createUser(String name, UUID uuid) {
Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
@Override
public void run() {
Main.bloostryAPI.getDatabaseManager().getMySQLClient().createUser(name, uuid);
}
});
}
@Override
public void createData(UUID uuid, BloostryPlayer bloostryPlayer) {
Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
@Override
public void run() {
String bloostryPlayerJson = new Gson().toJson(bloostryPlayer);
if (!(hasData(uuid))) {
setData(uuid, bloostryPlayer);
} else {
Console.sendMessageDebug(
PREFIX + "§cThis data is not created (" + uuid + "," + bloostryPlayerJson + ")");
}
}
});
}
@Override
public void deleteUser(String name) {
Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
@Override
public void run() {
Main.bloostryAPI.getDatabaseManager().getMySQLClient().deleteUser(name);
}
});
}
@Override
public void deleteData(UUID uuid) {
Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
@Override
public void run() {
if (hasData(uuid)) {
getJedis().del(PATH_BLOOSTRYPLAYER + uuid.toString());
} else {
Console.sendMessageDebug(PREFIX + "§cThis data is not created (" + uuid + ")");
}
}
});
}
@Override
public void setData(UUID uuid, BloostryPlayer bloostryPlayer) {
Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
@Override
public void run() {
String bloostryPlayerJson = new Gson().toJson(bloostryPlayer);
getJedis().set(PATH_BLOOSTRYPLAYER + uuid.toString(), "'" + bloostryPlayerJson + "'");
Console.sendMessageDebug("SET " + PATH_BLOOSTRYPLAYER + uuid.toString() + "'" + bloostryPlayerJson + "'");
}
});
}
public void addPlayerOnline(String serverName) {
Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
@Override
public void run() {
}
});
Integer playersOnline = getPlayersOnline(serverName);
getJedis().set(PATH_SERVER_ONLINE + serverName, ((playersOnline == null ? 0 : playersOnline) + 1) + "");
}
public void removePlayerOnline(String serverName) {
Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
@Override
public void run() {
Integer playersOnline = getPlayersOnline(serverName);
getJedis().set(PATH_SERVER_ONLINE + serverName, ((playersOnline == null ? 0 : playersOnline) - 1) + "");
}
});
}
public Integer getPlayersOnline(String serverName) {
String reponse = getJedis().get(PATH_SERVER_ONLINE + serverName);
if (reponse == null)
return null;
if (!IntegerUtils.isInteger(reponse))
return 0;
return IntegerUtils.getValueOf(reponse);
}
public Integer getPlayersOnlineOnNetwork() {
Set<String> servers = Main.bloostryAPI.getDatabaseManager().getJedis().smembers("serversList");
Integer onlines = 0;
for (String serverName : servers) {
onlines += (getPlayersOnline(serverName) == null ? 0 : getPlayersOnline(serverName));
}
return onlines;
}
public void setServerStatus(String serverName, ServerStatus serverStatus) {
Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
@Override
public void run() {
getJedis().set(PATH_SERVER_STATUS + serverName, serverStatus.getKeyPath());
}
});
}
public ServerStatus getServerStatus(String serverName) {
String status = getJedis().get(PATH_SERVER_STATUS + serverName);
if (status == null)
return null;
return ServerStatus.getServerStatusType(status);
}
} |
Partager