Added rough share-button for testing, sharing works!

This commit is contained in:
Artemis-the-gr8 2022-07-10 15:55:12 +02:00
parent 347e59220c
commit 608dfcc215
6 changed files with 33 additions and 8 deletions

View File

@ -7,7 +7,7 @@ import com.gmail.artemis.the.gr8.playerstats.commands.TabCompleter;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.listeners.JoinListener;
import com.gmail.artemis.the.gr8.playerstats.msg.MessageWriter;
import com.gmail.artemis.the.gr8.playerstats.statistic.ShareQueue;
import com.gmail.artemis.the.gr8.playerstats.statistic.ShareManager;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
@ -33,9 +33,10 @@ public class Main extends JavaPlugin {
//first get an instance of all the classes that need to be passed along to different classes
ConfigHandler config = new ConfigHandler(this);
MessageWriter messageWriter = new MessageWriter(config);
ShareManager shareManager = new ShareManager(config);
//initialize the threadManager
ThreadManager threadManager = new ThreadManager(adventure(), config, messageWriter);
ThreadManager threadManager = new ThreadManager(adventure(), config, messageWriter, shareManager);
//register all commands and the tabCompleter
PluginCommand statcmd = this.getCommand("statistic");
@ -46,7 +47,7 @@ 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());
if (sharecmd != null) sharecmd.setExecutor(new ShareCommand(adventure(), shareManager));
//register the listener
Bukkit.getPluginManager().registerEvents(new JoinListener(threadManager), this);

View File

@ -28,11 +28,11 @@ public class ThreadManager {
private final HashMap<String, Thread> statThreads;
private static long lastRecordedCalcTime;
public ThreadManager(BukkitAudiences a, ConfigHandler c, MessageWriter m) {
public ThreadManager(BukkitAudiences a, ConfigHandler c, MessageWriter m, ShareManager s) {
adventure = a;
config = c;
messageWriter = m;
shareManager = new ShareManager(config);
shareManager = s;
statThreads = new HashMap<>();
statThreadID = 0;

View File

@ -1,16 +1,27 @@
package com.gmail.artemis.the.gr8.playerstats.commands;
import com.gmail.artemis.the.gr8.playerstats.statistic.ShareManager;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
public class ShareCommand implements CommandExecutor {
private final BukkitAudiences adventure;
private final ShareManager shareManager;
public ShareCommand(BukkitAudiences a, ShareManager s) {
adventure = a;
shareManager = s;
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, String label, String[] args) {
//TODO use UUID code as arg to share the appropriate statResult
return false;
adventure.all().sendMessage(shareManager.getStatResult(sender.getName(), UUID.fromString(args[0])));
return true;
}
}

View File

@ -1,6 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats.msg;
import com.gmail.artemis.the.gr8.playerstats.enums.DebugLevel;
import com.gmail.artemis.the.gr8.playerstats.enums.PluginColor;
import com.gmail.artemis.the.gr8.playerstats.enums.Target;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.enums.Unit;
@ -11,9 +12,12 @@ import com.gmail.artemis.the.gr8.playerstats.models.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import org.bukkit.Bukkit;
import org.bukkit.Statistic;
import org.bukkit.map.MinecraftFont;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.util.*;
@ -192,6 +196,13 @@ public class MessageWriter {
.build();
}
public TextComponent addShareButton(TextComponent component, UUID shareCode) {
return component.append(newline())
.append(text("[SHARE]").color(PluginColor.LIGHT_BLUE.getColor())
.clickEvent(ClickEvent.runCommand("/statshare " + shareCode))
.hoverEvent(HoverEvent.showText(text("CLICK ME").color(PluginColor.LIGHT_GOLD.getColor()))));
}
/** Depending on the config settings, return either a TranslatableComponent representing
the statName (and potential subStatName), or a TextComponent with capitalized English names.*/
private TextComponent getStatNameComponent(StatRequest request) {

View File

@ -26,6 +26,7 @@ public class ShareManager {
isEnabled = config.enableStatSharing();
waitingTime = config.getStatShareWaitingTime();
if (isEnabled) {
resultID = new AtomicInteger();
statResults = new ConcurrentHashMap<>();
shareTimeStamp = new ConcurrentHashMap<>();
}

View File

@ -85,6 +85,7 @@ public class StatThread extends Thread {
}
if (shareManager.isEnabled()) {
UUID shareCode = shareManager.saveStatResult(request.getCommandSender().getName(), statResult);
statResult = messageWriter.addShareButton(statResult, shareCode);
}
adventure.sender(request.getCommandSender()).sendMessage(statResult);
}