Untitled
unknown
java
a year ago
5.1 kB
11
Indexable
package com.trexmine.bedwars.database.impl.redis;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import com.trexmine.bedwars.BedWars;
import com.trexmine.bedwars.manager.party.Party;
import com.trexmine.bedwars.manager.party.PartyManager;
import com.trexmine.bedwars.utils.functional.ArenaUtils;
import com.trexmine.bedwars.utils.functional.BukkitUtils;
import redis.clients.jedis.*;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
public final class JedisDB extends JedisPubSub {
private final Executor jedisPoolExecutor = Executors.newFixedThreadPool(5);
private JedisPool jedisPool;
public void connect(String host, int port, String password) {
this.jedisPool = new JedisPool(
new HostAndPort(host, port),
DefaultJedisClientConfig.builder()
.password(password)
.build()
);
reloadParties();
listenForMessages();
}
private void listenForMessages() {
CompletableFuture.runAsync(() -> {
try (Jedis jedis = jedisPool.getResource()) {
jedis.subscribe(this, "party_global");
} catch (Exception e) {
e.printStackTrace();
}
}, jedisPoolExecutor);
}
private void reloadParties() {
CompletableFuture.runAsync(() -> {
try (Jedis jedis = jedisPool.getResource()) {
for (String key : jedis.keys("party_leader*")) {
UUID leaderId = UUID.fromString(key.split("_")[2]);
Party party = new Party(leaderId);
jedis.smembers(key)
.stream()
.map(UUID::fromString)
.forEach(party::addPlayer);
PartyManager.cache(party);
if (party.isPrivate()) {
party.setPublic();
BedWars.getInstance()
.getDataBase()
.getPrivateGameOptions(leaderId)
.thenAccept(party::setPrivate);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}, jedisPoolExecutor);
}
@Override
public void onMessage(String channel, String message) {
ByteArrayDataInput dataInput = ByteStreams.newDataInput(message.getBytes());
String command = dataInput.readUTF();
UUID uniqueId = new UUID(dataInput.readLong(), dataInput.readLong());
switch (command) {
case "CREATE": {
Party party = new Party(uniqueId);
party.addPlayer(uniqueId);
PartyManager.cache(party);
break;
}
case "DISBAND": {
PartyManager.remove(uniqueId);
break;
}
case "MEMBER_ADD": {
PartyManager.byLeader(uniqueId).ifPresent(party -> {
party.addPlayer(new UUID(dataInput.readLong(), dataInput.readLong()));
});
break;
}
case "MEMBER_REMOVED": {
PartyManager.byLeader(uniqueId).ifPresent(party -> {
UUID target = new UUID(dataInput.readLong(), dataInput.readLong());
BukkitUtils.getPlayer(target).ifPresent(targetPlayer -> {
ArenaUtils.arenaByMember(targetPlayer).ifPresent(arena -> {
if (arena.getPrivateParty().equals(party)) {
arena.getPlayerInfo(targetPlayer)
.getProfile()
.leaveOnPurpose();
}
});
});
party.removePlayer(target);
});
break;
}
case "PRIVATE_TOGGLE": {
PartyManager.byLeader(uniqueId).ifPresent(party -> {
if (dataInput.readBoolean()) {
party.setPrivate();
} else {
party.setPublic();
}
});
break;
}
}
}
public void sendMessage(String channel, byte[] msg) {
validateConnection();
CompletableFuture.runAsync(() -> {
try (Jedis jedis = jedisPool.getResource()) {
jedis.publish(channel.getBytes(StandardCharsets.UTF_8), msg);
}
}, jedisPoolExecutor);
}
private void validateConnection() {
if (jedisPool == null || jedisPool.isClosed()) {
throw new IllegalStateException("Jedis not connected!");
}
}
}Editor is loading...
Leave a Comment