Started implementing share button. Made ShareCommand, added permission and command to plugin.yml, and made ShareQueue

This commit is contained in:
Artemis-the-gr8 2022-07-07 16:38:03 +02:00
parent 81a068051f
commit 3c3ae71389
5 changed files with 83 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats;
import com.gmail.artemis.the.gr8.playerstats.commands.ReloadCommand;
import com.gmail.artemis.the.gr8.playerstats.commands.ShareCommand;
import com.gmail.artemis.the.gr8.playerstats.commands.StatCommand;
import com.gmail.artemis.the.gr8.playerstats.commands.TabCompleter;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
@ -46,6 +47,8 @@ public class Main extends JavaPlugin {
}
PluginCommand reloadcmd = this.getCommand("statisticreload");
if (reloadcmd != null) reloadcmd.setExecutor(new ReloadCommand(threadManager));
PluginCommand sharecmd = this.getCommand("statisticshare");
if (sharecmd != null) sharecmd.setExecutor(new ShareCommand());
//register the listener
Bukkit.getPluginManager().registerEvents(new JoinListener(threadManager), this);

View File

@ -0,0 +1,14 @@
package com.gmail.artemis.the.gr8.playerstats.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
public class ShareCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,51 @@
package com.gmail.artemis.the.gr8.playerstats.statistic;
import net.kyori.adventure.text.TextComponent;
import org.jetbrains.annotations.Nullable;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.ConcurrentHashMap;
public class ShareQueue {
private final ConcurrentHashMap<String, TextComponent> statResults;
private final ConcurrentHashMap<String, Instant> shareTimestamp;
public ShareQueue() {
statResults = new ConcurrentHashMap<>();
shareTimestamp = new ConcurrentHashMap<>();
}
public void saveStatResults(String senderName, TextComponent statResult) {
statResults.put(senderName, statResult);
}
public boolean senderCanShare(String senderName) {
return senderCanShare(senderName, 0);
}
/** Returns true if the given sender has a statResult that can be shared,
if they have not shared a statResult yet, or if they have passed the timeLimit.
@param senderName name of the commandSender to evaluate
@param timeLimit the waiting time in seconds during which sharing is not allowed*/
public boolean senderCanShare(String senderName, long timeLimit) {
if (timeLimit == 0 || !shareTimestamp.containsKey(senderName)) {
return statResults.containsKey(senderName);
} else {
long seconds = shareTimestamp.get(senderName).until(Instant.now(), ChronoUnit.SECONDS);
return seconds >= timeLimit;
}
}
/** Removes and returns the last statResults for this sender,
and stores the timestamp the results were retrieved on.*/
public @Nullable TextComponent getLastStatResult(String senderName) {
if (statResults.containsKey(senderName)) {
shareTimestamp.put(senderName, Instant.now());
return statResults.remove(senderName);
} else {
return null;
}
}
}

View File

@ -10,6 +10,7 @@ import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import com.google.common.collect.ImmutableList;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -85,8 +86,9 @@ public class StatThread extends Thread {
}
try {
if (selection == Target.TOP) {
adventure.sender(request.getCommandSender()).sendMessage(
messageWriter.formatTopStats(getTopStats(), request));
TextComponent statResult = messageWriter.formatTopStats(getTopStats(), request);
adventure.sender(request.getCommandSender()).sendMessage(statResult);
} else {
adventure.sender(request.getCommandSender()).sendMessage(
messageWriter.formatServerStat(getServerTotal(), request));

View File

@ -11,17 +11,27 @@ commands:
- stats
description: general statistic command
permission: playerstats.stat
statisticshare:
aliases:
- statshare
- statsshare
description: shares last stat lookup in chat
usage: "§b/statshare"
permission: playerstats.share
statisticreload:
aliases:
- statreload
- statsreload
description: reloads the config
usage: "§a/statisticreload"
usage: "§a/statreload"
permission: playerstats.reload
permissions:
playerstats.stat:
description: allows usage of /statistic
default: true
playerstats.share:
description: allows sharing stats in chat
default: true
playerstats.reload:
description: allows usage of /statreload
default: op