mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2025-12-19 15:09:24 +00:00
Improve count getter
This commit is contained in:
@@ -17,8 +17,6 @@
|
|||||||
|
|
||||||
package net.momirealms.customfishing.bukkit.competition;
|
package net.momirealms.customfishing.bukkit.competition;
|
||||||
|
|
||||||
import com.google.common.io.ByteArrayDataOutput;
|
|
||||||
import com.google.common.io.ByteStreams;
|
|
||||||
import net.momirealms.customfishing.api.BukkitCustomFishingPlugin;
|
import net.momirealms.customfishing.api.BukkitCustomFishingPlugin;
|
||||||
import net.momirealms.customfishing.api.mechanic.action.ActionManager;
|
import net.momirealms.customfishing.api.mechanic.action.ActionManager;
|
||||||
import net.momirealms.customfishing.api.mechanic.competition.CompetitionConfig;
|
import net.momirealms.customfishing.api.mechanic.competition.CompetitionConfig;
|
||||||
@@ -32,6 +30,10 @@ import net.momirealms.customfishing.common.plugin.scheduler.SchedulerTask;
|
|||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@@ -60,6 +62,7 @@ public class BukkitCompetitionManager implements CompetitionManager {
|
|||||||
this.redisPlayerCount = null;
|
this.redisPlayerCount = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void load() {
|
public void load() {
|
||||||
this.interval = 10;
|
this.interval = 10;
|
||||||
this.hasRedis = plugin.getStorageManager().isRedisEnabled();
|
this.hasRedis = plugin.getStorageManager().isRedisEnabled();
|
||||||
@@ -72,9 +75,9 @@ public class BukkitCompetitionManager implements CompetitionManager {
|
|||||||
plugin.debug("Loaded " + commandConfigMap.size() + " competitions");
|
plugin.debug("Loaded " + commandConfigMap.size() + " competitions");
|
||||||
|
|
||||||
if (hasRedis) {
|
if (hasRedis) {
|
||||||
this.redisPlayerCount = this.redisPlayerCount == null ?
|
if (this.redisPlayerCount == null) {
|
||||||
new RedisPlayerCount(this.interval) :
|
this.redisPlayerCount = new RedisPlayerCount(this.interval);
|
||||||
this.redisPlayerCount;
|
}
|
||||||
} else {
|
} else {
|
||||||
if (this.redisPlayerCount != null) {
|
if (this.redisPlayerCount != null) {
|
||||||
this.redisPlayerCount.cancel();
|
this.redisPlayerCount.cancel();
|
||||||
@@ -83,6 +86,7 @@ public class BukkitCompetitionManager implements CompetitionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void unload() {
|
public void unload() {
|
||||||
if (this.timerCheckTask != null)
|
if (this.timerCheckTask != null)
|
||||||
this.timerCheckTask.cancel();
|
this.timerCheckTask.cancel();
|
||||||
@@ -94,13 +98,16 @@ public class BukkitCompetitionManager implements CompetitionManager {
|
|||||||
this.timeConfigMap.clear();
|
this.timeConfigMap.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void disable() {
|
public void disable() {
|
||||||
if (this.timerCheckTask != null)
|
if (this.timerCheckTask != null)
|
||||||
this.timerCheckTask.cancel();
|
this.timerCheckTask.cancel();
|
||||||
if (currentCompetition != null && currentCompetition.isOnGoing())
|
if (currentCompetition != null && currentCompetition.isOnGoing())
|
||||||
this.currentCompetition.stop(false);
|
this.currentCompetition.stop(false);
|
||||||
if (this.redisPlayerCount != null)
|
if (this.redisPlayerCount != null) {
|
||||||
this.redisPlayerCount.cancel();
|
this.redisPlayerCount.cancel();
|
||||||
|
this.redisPlayerCount = null;
|
||||||
|
}
|
||||||
this.commandConfigMap.clear();
|
this.commandConfigMap.clear();
|
||||||
this.timeConfigMap.clear();
|
this.timeConfigMap.clear();
|
||||||
}
|
}
|
||||||
@@ -175,13 +182,18 @@ public class BukkitCompetitionManager implements CompetitionManager {
|
|||||||
start(config);
|
start(config);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
out.writeUTF(serverGroup);
|
DataOutputStream out = new DataOutputStream(byteArrayOutputStream)) {
|
||||||
out.writeUTF("competition");
|
out.writeUTF(serverGroup);
|
||||||
out.writeUTF("start");
|
out.writeUTF("competition");
|
||||||
out.writeUTF(config.id());
|
out.writeUTF("start");
|
||||||
RedisManager.getInstance().publishRedisMessage(Arrays.toString(out.toByteArray()));
|
out.writeUTF(config.id());
|
||||||
return true;
|
RedisManager.getInstance().publishRedisMessage(byteArrayOutputStream.toString(StandardCharsets.UTF_8));
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,12 +238,12 @@ public class BukkitCompetitionManager implements CompetitionManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onlinePlayerCountProvider() {
|
public int onlinePlayerCountProvider() {
|
||||||
int count = Bukkit.getOnlinePlayers().size();
|
|
||||||
if (hasRedis) {
|
if (hasRedis) {
|
||||||
|
int count = 0;
|
||||||
List<UUID> toRemove = new ArrayList<>();
|
List<UUID> toRemove = new ArrayList<>();
|
||||||
for (Map.Entry<UUID, PlayerCount> entry : playerCountMap.entrySet()) {
|
for (Map.Entry<UUID, PlayerCount> entry : playerCountMap.entrySet()) {
|
||||||
PlayerCount playerCount = entry.getValue();
|
PlayerCount playerCount = entry.getValue();
|
||||||
if ((System.currentTimeMillis() - playerCount.time) < interval * 1000L + 1000L) {
|
if ((System.currentTimeMillis() - playerCount.time) < interval * 1000L + 2333L) {
|
||||||
count += playerCount.count;
|
count += playerCount.count;
|
||||||
} else {
|
} else {
|
||||||
toRemove.add(entry.getKey());
|
toRemove.add(entry.getKey());
|
||||||
@@ -240,11 +252,12 @@ public class BukkitCompetitionManager implements CompetitionManager {
|
|||||||
for (UUID uuid : toRemove) {
|
for (UUID uuid : toRemove) {
|
||||||
playerCountMap.remove(uuid);
|
playerCountMap.remove(uuid);
|
||||||
}
|
}
|
||||||
|
return count;
|
||||||
|
} else {
|
||||||
|
return Bukkit.getOnlinePlayers().size();
|
||||||
}
|
}
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updatePlayerCount(UUID uuid, int count) {
|
public void updatePlayerCount(UUID uuid, int count) {
|
||||||
playerCountMap.put(uuid, new PlayerCount(count, System.currentTimeMillis()));
|
playerCountMap.put(uuid, new PlayerCount(count, System.currentTimeMillis()));
|
||||||
@@ -254,17 +267,21 @@ public class BukkitCompetitionManager implements CompetitionManager {
|
|||||||
private final SchedulerTask task;
|
private final SchedulerTask task;
|
||||||
|
|
||||||
public RedisPlayerCount(int interval) {
|
public RedisPlayerCount(int interval) {
|
||||||
task = plugin.getScheduler().asyncRepeating(this, 0, interval, TimeUnit.SECONDS);
|
task = plugin.getScheduler().asyncRepeating(this, interval, interval, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
out.writeUTF(ConfigManager.serverGroup());
|
DataOutputStream out = new DataOutputStream(byteArrayOutputStream)) {
|
||||||
out.writeUTF("online");
|
out.writeUTF(ConfigManager.serverGroup());
|
||||||
out.writeUTF(String.valueOf(identifier));
|
out.writeUTF("online");
|
||||||
out.writeUTF(String.valueOf(Bukkit.getOnlinePlayers().size()));
|
out.writeUTF(String.valueOf(identifier));
|
||||||
RedisManager.getInstance().publishRedisMessage(Arrays.toString(out.toByteArray()));
|
out.writeInt(Bukkit.getOnlinePlayers().size());
|
||||||
|
RedisManager.getInstance().publishRedisMessage(byteArrayOutputStream.toString(StandardCharsets.UTF_8));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cancel() {
|
public void cancel() {
|
||||||
|
|||||||
@@ -17,8 +17,6 @@
|
|||||||
|
|
||||||
package net.momirealms.customfishing.bukkit.storage.method.database.nosql;
|
package net.momirealms.customfishing.bukkit.storage.method.database.nosql;
|
||||||
|
|
||||||
import com.google.common.io.ByteArrayDataInput;
|
|
||||||
import com.google.common.io.ByteStreams;
|
|
||||||
import dev.dejvokep.boostedyaml.YamlDocument;
|
import dev.dejvokep.boostedyaml.YamlDocument;
|
||||||
import dev.dejvokep.boostedyaml.block.implementation.Section;
|
import dev.dejvokep.boostedyaml.block.implementation.Section;
|
||||||
import net.momirealms.customfishing.api.BukkitCustomFishingPlugin;
|
import net.momirealms.customfishing.api.BukkitCustomFishingPlugin;
|
||||||
@@ -32,6 +30,9 @@ import redis.clients.jedis.exceptions.JedisException;
|
|||||||
import redis.clients.jedis.params.XReadParams;
|
import redis.clients.jedis.params.XReadParams;
|
||||||
import redis.clients.jedis.resps.StreamEntry;
|
import redis.clients.jedis.resps.StreamEntry;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -175,7 +176,11 @@ public class RedisManager extends AbstractStorage {
|
|||||||
if (!channel.equals(getStream())) {
|
if (!channel.equals(getStream())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
handleMessage(message);
|
try {
|
||||||
|
handleMessage(message);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, getStream());
|
}, getStream());
|
||||||
}
|
}
|
||||||
@@ -183,8 +188,8 @@ public class RedisManager extends AbstractStorage {
|
|||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleMessage(String message) {
|
private void handleMessage(String message) throws IOException {
|
||||||
ByteArrayDataInput input = ByteStreams.newDataInput(message.getBytes(StandardCharsets.UTF_8));
|
DataInputStream input = new DataInputStream(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)));
|
||||||
String server = input.readUTF();
|
String server = input.readUTF();
|
||||||
if (!ConfigManager.serverGroup().equals(server))
|
if (!ConfigManager.serverGroup().equals(server))
|
||||||
return;
|
return;
|
||||||
@@ -209,7 +214,7 @@ public class RedisManager extends AbstractStorage {
|
|||||||
case "online" -> {
|
case "online" -> {
|
||||||
plugin.getCompetitionManager().updatePlayerCount(
|
plugin.getCompetitionManager().updatePlayerCount(
|
||||||
UUID.fromString(input.readUTF()),
|
UUID.fromString(input.readUTF()),
|
||||||
Integer.parseInt(input.readUTF())
|
input.readInt()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -375,7 +380,11 @@ public class RedisManager extends AbstractStorage {
|
|||||||
for (Map.Entry<String, List<StreamEntry>> message : messages) {
|
for (Map.Entry<String, List<StreamEntry>> message : messages) {
|
||||||
if (message.getKey().equals(getStream())) {
|
if (message.getKey().equals(getStream())) {
|
||||||
var value = message.getValue().get(0).getFields().get("value");
|
var value = message.getValue().get(0).getFields().get("value");
|
||||||
handleMessage(value);
|
try {
|
||||||
|
handleMessage(value);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Project settings
|
# Project settings
|
||||||
# Rule: [major update].[feature update].[bug fix]
|
# Rule: [major update].[feature update].[bug fix]
|
||||||
project_version=2.2.12
|
project_version=2.2.13
|
||||||
config_version=35
|
config_version=35
|
||||||
project_group=net.momirealms
|
project_group=net.momirealms
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user