Made share button work!

This commit is contained in:
Artemis-the-gr8 2022-07-15 17:56:53 +02:00
parent 8054bd42b7
commit 980fbcb43e
4 changed files with 32 additions and 18 deletions

View File

@ -95,11 +95,11 @@ public final class ShareManager {
puts the current time in the shareTimeStamp (ConcurrentHashMap),
puts the shareCode (UUID) in the sharedResults (ArrayBlockingQueue),
and returns the statResult. If no statResult was found, returns null.*/
public @Nullable TextComponent getStatResult(String playerName, UUID identifier) {
if (statResultQueue.containsKey(identifier)) {
public @Nullable TextComponent getStatResult(String playerName, UUID shareCode) {
if (statResultQueue.containsKey(shareCode)) {
shareTimeStamp.put(playerName, Instant.now());
if (!sharedResults.offer(identifier)) { //create a new ArrayBlockingQueue if our queue is full
if (!sharedResults.offer(shareCode)) { //create a new ArrayBlockingQueue if our queue is full
MyLogger.logMsg("500 stat-results have been shared, " +
"creating a new internal queue with the most recent 50 share-code-values and discarding the rest...", DebugLevel.MEDIUM);
ArrayBlockingQueue<UUID> newQueue = new ArrayBlockingQueue<>(500);
@ -112,9 +112,14 @@ public final class ShareManager {
sharedResults = newQueue;
}
sharedResults.offer(identifier);
sharedResults.offer(shareCode);
}
return statResultQueue.remove(identifier).statResult();
StatResult result = statResultQueue.remove(shareCode);
MyLogger.logMsg("StatResult record exists: " + (result != null));
MyLogger.logMsg("Its TextComponent: " + result.statResult());
MyLogger.logMsg("Its ID: " + result.ID());
MyLogger.logMsg("Its uuid: " + result.uuid());
return result.statResult();
} else {
return null;
}

View File

@ -47,8 +47,7 @@ public class ShareCommand implements CommandExecutor {
if (result == null) { //at this point the only possible cause of statResult being null is the request being older than 25 player-requests ago
outputManager.sendFeedbackMsg(sender, StandardMessage.STAT_RESULTS_TOO_OLD);
} else {
//TODO add shared-by signature
adventure.all().sendMessage(result);
outputManager.shareStatResults(sender, result);
}
}
}

View File

@ -189,15 +189,20 @@ public class MessageWriter {
};
}
public Function<UUID, TextComponent> formattedTopStatFunction(@NotNull LinkedHashMap<String, Integer> topStats, @NotNull StatRequest request) {
TextComponent.Builder title = getTopStatsTitle(request, topStats.size());
TextComponent title = getTopStatsTitle(request, topStats.size());
TextComponent list = getTopStatList(topStats, request);
return shareCode -> {
MyLogger.logMsg("Function triggered with shareCode " + shareCode);
TextComponent.Builder topBuilder = Component.text().append(title);
if (shareCode != null) {
title.append(space())
topBuilder
.append(space())
.append(componentFactory.shareButtonComponent(shareCode));
}
return title.append(list).build();
topBuilder.append(list);
MyLogger.logMsg(topBuilder.build() + "");
return topBuilder.build();
};
}
@ -205,14 +210,15 @@ public class MessageWriter {
return componentFactory.sharedButtonComponent(playerName);
}
private TextComponent.Builder getTopStatsTitle(StatRequest request, int statListSize) {
private TextComponent getTopStatsTitle(StatRequest request, int statListSize) {
return Component.text()
.append(newline())
.append(componentFactory.pluginPrefixComponent()).append(space())
.append(componentFactory.titleComponent(config.getTopStatsTitle(), Target.TOP)).append(space())
.append(componentFactory.titleNumberComponent(statListSize)).append(space())
.append(getStatNameComponent(request)) //space is provided by statUnitComponent
.append(getStatUnitComponent(request.getStatistic(), request.getSelection(), request.isConsoleSender()));
.append(getStatUnitComponent(request.getStatistic(), request.getSelection(), request.isConsoleSender()))
.build();
}
private TextComponent getTopStatList(LinkedHashMap<String, Integer> topStats, StatRequest request) {

View File

@ -5,11 +5,14 @@ import com.gmail.artemis.the.gr8.playerstats.ShareManager;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage;
import com.gmail.artemis.the.gr8.playerstats.models.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Statistic;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.EnumMap;
import java.util.LinkedHashMap;
@ -73,12 +76,13 @@ public class OutputManager {
adventure.sender(sender).sendMessage(getWriter(sender).helpMsg());
}
public void shareStatResults(CommandSender sender, UUID shareCode) {
TextComponent statResult = shareManager.getStatResult(sender.getName(), shareCode);
if (statResult != null) {
adventure.filter(commandSender -> !(commandSender == sender)).sendMessage(statResult);
adventure.sender(sender).sendMessage(msg.sharedButton(sender.getName()));
}
public void shareStatResults(CommandSender sender, @NotNull TextComponent statResult) {
MyLogger.logMsg("statResult: " + statResult);
adventure.all()
.filterAudience(player -> !player.get(Identity.NAME)
.orElse("").equalsIgnoreCase(sender.getName())).sendMessage(statResult);
MyLogger.logMsg("SharedButton: " + msg.sharedButton(sender.getName()));
adventure.sender(sender).sendMessage(msg.sharedButton(sender.getName()));
}
public void sendPlayerStat(StatRequest request, int playerStat) {