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.ucmds.TownColorCommand;
import buttondevteam.chat.listener.PlayerJoinLeaveListener;
import buttondevteam.chat.listener.PlayerListener;
import buttondevteam.chat.listener.TownyListener;
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"));
TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this);
TBMCCoreAPI.RegisterEventsForExceptions(new PlayerJoinLeaveListener(), this);
TBMCCoreAPI.RegisterEventsForExceptions(new TownyListener(), this);
TBMCChatAPI.AddCommands(this, YeehawCommand.class);
Console = this.getServer().getConsoleSender();

View file

@ -4,20 +4,19 @@ import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.ChatMessage;
import buttondevteam.lib.chat.CommandClass;
import lombok.RequiredArgsConstructor;
import lombok.experimental.var;
import lombok.val;
import org.bukkit.command.CommandSender;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.stream.Stream;
@CommandClass
public class HistoryCommand extends UCommandBase {
private static HashMap<Channel, HistoryEntry[]> messages = new HashMap<>();
private static HashMap<Channel, LinkedList<HistoryEntry>> messages = new HashMap<>();
@Override
public String[] GetHelpText(String alias) {
@ -29,46 +28,65 @@ public class HistoryCommand extends UCommandBase {
@Override
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);
HistoryEntry[] he = new HistoryEntry[10];
for (int i = 0, j = 0; i < 10; i++) {
val cm = e.getValue()[i].chatMessage;
if (cm == null)
break; //Don't have 10 messages yet
LinkedList<HistoryEntry> he = new LinkedList<>();
for (int i = 0; i < 10 && i < e.getValue().size(); i++) {
val heh = e.getValue().get(i);
val cm = heh.chatMessage;
if (score == e.getKey().getMCScore(cm.getPermCheck()))
he[j++] = e.getValue()[i];
he.push(heh);
}
return new HashMap.SimpleEntry<>(e.getKey(), he);
};
sender.sendMessage("§6---- Chat History ----");
Stream<HistoryEntry> stream;
Stream<Map.Entry<Channel, LinkedList<HistoryEntry>>> stream;
if (args.length == 0) {
stream = messages.entrySet().stream().map(filterThem).flatMap(e -> Arrays.stream(e.getValue()));
stream = messages.entrySet().stream();
} else {
Channel ch = Channel.GlobalChat; //TODO: Channel param
val hes = messages.get(ch);
if (hes == null)
Optional<Channel> och = Channel.getChannels().stream().filter(chan -> chan.ID.equalsIgnoreCase(args[0])).findAny();
if (!och.isPresent()) {
sender.sendMessage("§cChannel not found. Use the ID, for example: /" + (hc == null ? "u history" : hc.GetCommandPath()) + " ooc");
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();
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;
sender.sendMessage(cm.getSender().getName() + ": " + cm.getMessage());
sender.sendMessage("[" + e.channel.DisplayName + "] " + cm.getSender().getName() + ": " + cm.getMessage());
sent.set(true);
});
}
if (!sent.get())
sender.sendMessage("No messages can be found.");
return true;
}
@RequiredArgsConstructor
public static class HistoryEntry {
private static class HistoryEntry {
/**
* System.nanoTime()
*/
private final long timestamp;
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.PluginMain;
import buttondevteam.chat.commands.UnlolCommand;
import buttondevteam.chat.commands.ucmds.HistoryCommand;
import buttondevteam.lib.chat.Color;
import buttondevteam.lib.player.TBMCPlayerJoinEvent;
import buttondevteam.lib.player.TBMCPlayerLoadEvent;
@ -64,14 +65,14 @@ public class PlayerJoinLeaveListener implements Listener {
nwithoutformatting = p.getName();
PlayerListener.nicknames.forcePut(nwithoutformatting.toLowerCase(), p.getUniqueId());
Bukkit.getScheduler().runTaskLater(PluginMain.Instance, () -> {
updatePlayerColors(p, cp); //TODO: Doesn't have effect
}, 5);
updatePlayerColors(p, cp); //TO!DO: Doesn't have effect - It can help to register the listener
if (cp.ChatOnly || p.getGameMode().equals(GameMode.SPECTATOR)) {
cp.ChatOnly = false;
p.setGameMode(GameMode.SURVIVAL);
}
HistoryCommand.showHistory(e.getPlayer(), "u history", new String[0], null);
}
@EventHandler

View file

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