Chat history works! Also fixed a lot

Actually registered the join/leave listener, fixing town colors and nickname mentioning as well
Chat history is finished, it only shows messages that the sender has access to
#82
This commit is contained in:
Norbi Peti 2018-12-27 00:02:47 +01:00
parent f8539a0392
commit 1081f3cf9d
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 49 additions and 25 deletions

1
lombok.config Normal file
View file

@ -0,0 +1 @@
lombok.var.flagUsage = ALLOW

View file

@ -2,6 +2,7 @@ package buttondevteam.chat;
import buttondevteam.chat.commands.YeehawCommand; import buttondevteam.chat.commands.YeehawCommand;
import buttondevteam.chat.commands.ucmds.TownColorCommand; import buttondevteam.chat.commands.ucmds.TownColorCommand;
import buttondevteam.chat.listener.PlayerJoinLeaveListener;
import buttondevteam.chat.listener.PlayerListener; import buttondevteam.chat.listener.PlayerListener;
import buttondevteam.chat.listener.TownyListener; import buttondevteam.chat.listener.TownyListener;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
@ -81,6 +82,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
PluginMain.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials")); PluginMain.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials"));
TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this);
TBMCCoreAPI.RegisterEventsForExceptions(new PlayerJoinLeaveListener(), this);
TBMCCoreAPI.RegisterEventsForExceptions(new TownyListener(), this); TBMCCoreAPI.RegisterEventsForExceptions(new TownyListener(), this);
TBMCChatAPI.AddCommands(this, YeehawCommand.class); TBMCChatAPI.AddCommands(this, YeehawCommand.class);
Console = this.getServer().getConsoleSender(); Console = this.getServer().getConsoleSender();

View file

@ -4,20 +4,19 @@ import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.ChatMessage; import buttondevteam.lib.chat.ChatMessage;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.experimental.var;
import lombok.val; import lombok.val;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.Arrays; import javax.annotation.Nullable;
import java.util.Comparator; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Stream; import java.util.stream.Stream;
@CommandClass @CommandClass
public class HistoryCommand extends UCommandBase { public class HistoryCommand extends UCommandBase {
private static HashMap<Channel, HistoryEntry[]> messages = new HashMap<>(); private static HashMap<Channel, LinkedList<HistoryEntry>> messages = new HashMap<>();
@Override @Override
public String[] GetHelpText(String alias) { public String[] GetHelpText(String alias) {
@ -29,46 +28,65 @@ public class HistoryCommand extends UCommandBase {
@Override @Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) { public boolean OnCommand(CommandSender sender, String alias, String[] args) {
Function<Map.Entry<Channel, HistoryEntry[]>, Map.Entry<Channel, HistoryEntry[]>> filterThem = e -> { return showHistory(sender, alias, args, this);
}
public static boolean showHistory(CommandSender sender, String alias, String[] args, @Nullable HistoryCommand hc) {
Function<Map.Entry<Channel, LinkedList<HistoryEntry>>, Map.Entry<Channel, LinkedList<HistoryEntry>>> filterThem = e -> {
int score = e.getKey().getMCScore(sender); int score = e.getKey().getMCScore(sender);
HistoryEntry[] he = new HistoryEntry[10]; LinkedList<HistoryEntry> he = new LinkedList<>();
for (int i = 0, j = 0; i < 10; i++) { for (int i = 0; i < 10 && i < e.getValue().size(); i++) {
val cm = e.getValue()[i].chatMessage; val heh = e.getValue().get(i);
if (cm == null) val cm = heh.chatMessage;
break; //Don't have 10 messages yet
if (score == e.getKey().getMCScore(cm.getPermCheck())) if (score == e.getKey().getMCScore(cm.getPermCheck()))
he[j++] = e.getValue()[i]; he.push(heh);
} }
return new HashMap.SimpleEntry<>(e.getKey(), he); return new HashMap.SimpleEntry<>(e.getKey(), he);
}; };
sender.sendMessage("§6---- Chat History ----"); sender.sendMessage("§6---- Chat History ----");
Stream<HistoryEntry> stream; Stream<Map.Entry<Channel, LinkedList<HistoryEntry>>> stream;
if (args.length == 0) { if (args.length == 0) {
stream = messages.entrySet().stream().map(filterThem).flatMap(e -> Arrays.stream(e.getValue())); stream = messages.entrySet().stream();
} else { } else {
Channel ch = Channel.GlobalChat; //TODO: Channel param Optional<Channel> och = Channel.getChannels().stream().filter(chan -> chan.ID.equalsIgnoreCase(args[0])).findAny();
val hes = messages.get(ch); if (!och.isPresent()) {
if (hes == null) sender.sendMessage("§cChannel not found. Use the ID, for example: /" + (hc == null ? "u history" : hc.GetCommandPath()) + " ooc");
return true; return true;
stream = Arrays.stream(hes); }
val hes = messages.get(och.get());
if (hes == null)
stream = Stream.empty();
else
stream = Stream.of(new HashMap.SimpleEntry<>(och.get(), hes));
} }
AtomicBoolean sent = new AtomicBoolean(); AtomicBoolean sent = new AtomicBoolean();
stream.sorted(Comparator.comparingLong(he -> he.timestamp)).forEach(e -> { val arr = stream.map(filterThem).flatMap(e -> e.getValue().stream())
.sorted(Comparator.comparingLong(he -> he.timestamp)).toArray(HistoryEntry[]::new);
for (int i = Math.max(0, arr.length - 10); i < arr.length; i++) {
HistoryEntry e = arr[i];
val cm = e.chatMessage; val cm = e.chatMessage;
sender.sendMessage(cm.getSender().getName() + ": " + cm.getMessage()); sender.sendMessage("[" + e.channel.DisplayName + "] " + cm.getSender().getName() + ": " + cm.getMessage());
sent.set(true); sent.set(true);
}); }
if (!sent.get()) if (!sent.get())
sender.sendMessage("No messages can be found."); sender.sendMessage("No messages can be found.");
return true; return true;
} }
@RequiredArgsConstructor @RequiredArgsConstructor
public static class HistoryEntry { private static class HistoryEntry {
/** /**
* System.nanoTime() * System.nanoTime()
*/ */
private final long timestamp; private final long timestamp;
private final ChatMessage chatMessage; private final ChatMessage chatMessage;
private final Channel channel;
}
public static void addChatMessage(ChatMessage chatMessage, Channel channel) {
var ll = messages.computeIfAbsent(channel, k -> new LinkedList<>()); //<-- TIL
ll.add(new HistoryEntry(System.nanoTime(), chatMessage, channel)); //Adds as last element
while (ll.size() > 10)
ll.remove(); //Removes the first element
} }
} }

View file

@ -5,6 +5,7 @@ import buttondevteam.chat.FlairStates;
import buttondevteam.chat.PlayerJoinTimerTask; import buttondevteam.chat.PlayerJoinTimerTask;
import buttondevteam.chat.PluginMain; import buttondevteam.chat.PluginMain;
import buttondevteam.chat.commands.UnlolCommand; import buttondevteam.chat.commands.UnlolCommand;
import buttondevteam.chat.commands.ucmds.HistoryCommand;
import buttondevteam.lib.chat.Color; import buttondevteam.lib.chat.Color;
import buttondevteam.lib.player.TBMCPlayerJoinEvent; import buttondevteam.lib.player.TBMCPlayerJoinEvent;
import buttondevteam.lib.player.TBMCPlayerLoadEvent; import buttondevteam.lib.player.TBMCPlayerLoadEvent;
@ -64,14 +65,14 @@ public class PlayerJoinLeaveListener implements Listener {
nwithoutformatting = p.getName(); nwithoutformatting = p.getName();
PlayerListener.nicknames.forcePut(nwithoutformatting.toLowerCase(), p.getUniqueId()); PlayerListener.nicknames.forcePut(nwithoutformatting.toLowerCase(), p.getUniqueId());
Bukkit.getScheduler().runTaskLater(PluginMain.Instance, () -> { updatePlayerColors(p, cp); //TO!DO: Doesn't have effect - It can help to register the listener
updatePlayerColors(p, cp); //TODO: Doesn't have effect
}, 5);
if (cp.ChatOnly || p.getGameMode().equals(GameMode.SPECTATOR)) { if (cp.ChatOnly || p.getGameMode().equals(GameMode.SPECTATOR)) {
cp.ChatOnly = false; cp.ChatOnly = false;
p.setGameMode(GameMode.SURVIVAL); p.setGameMode(GameMode.SURVIVAL);
} }
HistoryCommand.showHistory(e.getPlayer(), "u history", new String[0], null);
} }
@EventHandler @EventHandler

View file

@ -3,6 +3,7 @@ package buttondevteam.chat.listener;
import buttondevteam.chat.ChatPlayer; import buttondevteam.chat.ChatPlayer;
import buttondevteam.chat.ChatProcessing; import buttondevteam.chat.ChatProcessing;
import buttondevteam.chat.PluginMain; import buttondevteam.chat.PluginMain;
import buttondevteam.chat.commands.ucmds.HistoryCommand;
import buttondevteam.lib.TBMCChatEvent; import buttondevteam.lib.TBMCChatEvent;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.*; import buttondevteam.lib.chat.*;
@ -258,6 +259,7 @@ public class PlayerListener implements Listener {
try { try {
if (e.isCancelled()) if (e.isCancelled())
return; return;
HistoryCommand.addChatMessage(e.getCm(), e.getChannel());
e.setCancelled(ChatProcessing.ProcessChat(e)); e.setCancelled(ChatProcessing.ProcessChat(e));
} catch (NoClassDefFoundError | Exception ex) { // Weird things can happen } catch (NoClassDefFoundError | Exception ex) { // Weird things can happen
for (Player p : Bukkit.getOnlinePlayers()) for (Player p : Bukkit.getOnlinePlayers())