Skip to content

Commit

Permalink
Merge pull request #114 from TBMCPlugins/dev
Browse files Browse the repository at this point in the history
Misc. fixes, chat history config, clickable announcements
  • Loading branch information
NorbiPeti authored Feb 1, 2020
2 parents fd3d3df + 2d874d7 commit c66b212
Show file tree
Hide file tree
Showing 33 changed files with 439 additions and 272 deletions.
8 changes: 4 additions & 4 deletions .idea/ButtonChat.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@
<!-- <plugin> <groupId>org.basepom.maven</groupId> <artifactId>duplicate-finder-maven-plugin</artifactId>
<version>1.2.1</version> <executions> <execution> <goals> <goal>check</goal>
</goals> </execution> </executions> </plugin> -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-testCompile</id>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<groupId>buttondevteam</groupId>
Expand Down Expand Up @@ -198,7 +213,7 @@
<dependency>
<groupId>com.palmergames.bukkit.towny</groupId>
<artifactId>Towny</artifactId>
<version>0.95.0.0</version>
<version>0.95.2.0</version>
<scope>provided</scope>
</dependency>
<!-- <dependency> <groupId>au.com.mineauz</groupId> <artifactId>Minigames</artifactId>
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/buttondevteam/chat/PluginMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import buttondevteam.chat.components.towny.TownyComponent;
import buttondevteam.chat.listener.PlayerJoinLeaveListener;
import buttondevteam.chat.listener.PlayerListener;
import buttondevteam.core.MainPlugin;
import buttondevteam.core.component.channel.Channel;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.ButtonPlugin;
Expand All @@ -37,12 +36,12 @@ public class PluginMain extends ButtonPlugin { // Translated to Java: 2015.07.15
public static PluginMain Instance;
public static ConsoleCommandSender Console;

public ConfigData<String> notificationSound() {
return getIConfig().getData("notificationSound", "");
}

public ConfigData<Float> notificationPitch() {
return getIConfig().getData("notificationPitch", 1.0f);
/**
* If enabled, stores and displays the last 10 messages the player can see (public, their town chat etc.)
* Can be used with the Discord plugin so players can see some of the conversation they missed that's visible on Discord anyways.
*/
public ConfigData<Boolean> storeChatHistory() {
return getIConfig().getData("storeChatHistory", true);
}

// Fired when plugin is first enabled
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/buttondevteam/chat/commands/MWikiCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
})
public class MWikiCommand extends ICommand2MC {
@Command2.Subcommand
public boolean def(CommandSender sender, @Command2.OptionalArg String query) {
public boolean def(CommandSender sender, @Command2.OptionalArg @Command2.TextArg String query) {
try {
if (query == null)
sender.sendMessage(new String[] { "§bMinecraft Wiki link: http://minecraft.gamepedia.com/",
"You can also search on it using /mwiki <query>" });
sender.sendMessage(new String[]{"§bMinecraft Wiki link: http://minecraft.gamepedia.com/",
"You can also search on it using /mwiki <query>"});
else
sender.sendMessage("§bMinecraft Wiki link: http://minecraft.gamepedia.com/index.php?search="
+ URLEncoder.encode(query, "UTF-8") + "&title=Special%3ASearch&go=Go");
+ URLEncoder.encode(query, "UTF-8") + "&title=Special%3ASearch&go=Go");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Expand Down
20 changes: 1 addition & 19 deletions src/main/java/buttondevteam/chat/commands/ucmds/HelpCommand.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package buttondevteam.chat.commands.ucmds;

import buttondevteam.chat.PluginMain;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.ArrayList;

@CommandClass(modOnly = false, helpText = {
"Help",
Expand Down Expand Up @@ -36,19 +30,7 @@ public boolean def(CommandSender sender, @Command2.TextArg @Command2.OptionalArg
"- Playernames: Hover over them to get some player info",
"-- Respect: This is the number of paid respects divided by eliglble deaths. This is a reference to CoD:AW's \"Press F to pay respects\""});
else if (topicOrCommand.equalsIgnoreCase("commands")) {
ArrayList<String> text = new ArrayList<String>();
text.add("§6---- Command list ----");
for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values())
if (!cmd.getClass().getAnnotation(CommandClass.class).modOnly() || PluginMain.permission.has(sender, "tbmc.admin"))
if (!cmd.isPlayerOnly() || sender instanceof Player)
if (!cmd.GetCommandPath().contains(" "))
text.add("/" + cmd.GetCommandPath());
else {
final String topcmd = cmd.GetCommandPath().substring(0, cmd.GetCommandPath().indexOf(' '));
if (!text.contains("/" + topcmd))
text.add("/" + topcmd);
}
sender.sendMessage(text.toArray(new String[0]));
sender.sendMessage(getManager().getCommandsText());
} else if (topicOrCommand.equalsIgnoreCase("colors")) {
sender.sendMessage(new String[]{"§6---- Chat colors/formats ----", //
"Tellraw name - Code | Tellraw name - Code", //
Expand Down
32 changes: 21 additions & 11 deletions src/main/java/buttondevteam/chat/commands/ucmds/HistoryCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package buttondevteam.chat.commands.ucmds;

import buttondevteam.chat.PluginMain;
import buttondevteam.core.component.channel.Channel;
import buttondevteam.lib.chat.ChatMessage;
import buttondevteam.lib.chat.Command2;
Expand All @@ -21,14 +22,18 @@ public class HistoryCommand extends UCommandBase {
/**
* Key: ChannelID_groupID
*/
private static HashMap<String, LinkedList<HistoryEntry>> messages = new HashMap<>();
private static final HashMap<String, LinkedList<HistoryEntry>> messages = new HashMap<>();

@Command2.Subcommand
public boolean def(CommandSender sender, @Command2.OptionalArg String channel) {
return showHistory(sender, channel);
}

public static boolean showHistory(CommandSender sender, String channel) {
if (!PluginMain.Instance.storeChatHistory().get()) {
sender.sendMessage("§6Chat history is disabled");
return true;
}
Function<Channel, LinkedList<HistoryEntry>> getThem = ch -> messages.get(ch.ID + "_" + ch.getGroupID(sender)); //If can't see, groupID is null, and that shouldn't be in the map
sender.sendMessage("§6---- Chat History ----");
Stream<Channel> stream;
Expand All @@ -43,13 +48,15 @@ public static boolean showHistory(CommandSender sender, String channel) {
stream = Stream.of(och.get());
}
AtomicBoolean sent = new AtomicBoolean();
val arr = stream.map(getThem).filter(Objects::nonNull).flatMap(Collection::stream)
synchronized (messages) {
val arr = stream.map(getThem).filter(Objects::nonNull).flatMap(Collection::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("[" + e.channel.DisplayName().get() + "] " + cm.getSender().getName() + ": " + cm.getMessage());
sent.set(true);
for (int i = Math.max(0, arr.length - 10); i < arr.length; i++) {
HistoryEntry e = arr[i];
val cm = e.chatMessage;
sender.sendMessage("[" + e.channel.DisplayName().get() + "] " + cm.getSender().getName() + ": " + cm.getMessage());
sent.set(true);
}
}
if (!sent.get())
sender.sendMessage("No messages can be found.");
Expand All @@ -67,11 +74,14 @@ private static class HistoryEntry {
}

public static void addChatMessage(ChatMessage chatMessage, Channel channel) {
if (!PluginMain.Instance.storeChatHistory().get()) return;
val groupID = channel.getGroupID(chatMessage.getPermCheck());
if (groupID == null) return; //Just to be sure
var ll = messages.computeIfAbsent(channel.ID + "_" + groupID, 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
synchronized (messages) {
var ll = messages.computeIfAbsent(channel.ID + "_" + groupID, 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
}
}
}
22 changes: 13 additions & 9 deletions src/main/java/buttondevteam/chat/commands/ucmds/InfoCommand.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package buttondevteam.chat.commands.ucmds;

import buttondevteam.chat.PluginMain;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.player.ChromaGamerBase.InfoTarget;
import buttondevteam.lib.player.TBMCPlayer;
import buttondevteam.lib.player.TBMCPlayerBase;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;

@CommandClass(modOnly = false, helpText = {
Expand All @@ -20,16 +22,18 @@ public boolean def(CommandSender sender, String player) {
sender.sendMessage("The server console.");
return true;
}
try (TBMCPlayer p = TBMCPlayerBase.getFromName(player, TBMCPlayer.class)) {
if (p == null) {
sender.sendMessage("§cThe specified player cannot be found");
return true;
Bukkit.getScheduler().runTaskAsynchronously(PluginMain.Instance, () -> {
try (TBMCPlayer p = TBMCPlayerBase.getFromName(player, TBMCPlayer.class)) {
if (p == null) {
sender.sendMessage("§cThe specified player cannot be found");
return;
}
sender.sendMessage(p.getInfo(InfoTarget.MCCommand));
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while getting player information!", e);
sender.sendMessage("§cError while getting player information!");
}
sender.sendMessage(p.getInfo(InfoTarget.MCCommand));
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while getting player information!", e);
sender.sendMessage("§cError while getting player information!");
}
});
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.ICommand2MC;
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;

@CommandClass(modOnly = false, path = "u")
@OptionallyPlayerCommandClass(playerOnly = false)
public abstract class UCommandBase extends ICommand2MC {
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package buttondevteam.chat.components.announce;

import buttondevteam.chat.commands.ucmds.UCommandBase;
import buttondevteam.chat.components.formatter.ChatProcessing;
import buttondevteam.chat.components.formatter.FormatterComponent;
import buttondevteam.chat.components.formatter.formatting.TellrawEvent;
import buttondevteam.chat.components.formatter.formatting.TellrawPart;
import buttondevteam.core.ComponentManager;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;

@CommandClass(modOnly = true)
@OptionallyPlayerCommandClass(playerOnly = false)
@RequiredArgsConstructor
public class AnnounceCommand extends UCommandBase {
private final AnnouncerComponent component;
Expand All @@ -28,7 +32,7 @@ public boolean add(CommandSender sender, @Command2.TextArg String text) {
@Command2.Subcommand(helpText = {
"Edit announcement",
"This command lets you edit an announcement by its index.",
"Shouldn't be used directly, use either command blocks or click on an announcement in /u announce list (WIP) instead." //TODO: <--
"Shouldn't be used directly, use either command blocks or click on an announcement in /u announce list instead."
})
public boolean edit(CommandSender sender, byte index, @Command2.TextArg String text) {
String finalmessage1 = text.replace('&', '§');
Expand All @@ -49,8 +53,19 @@ public boolean list(CommandSender sender) {
sender.sendMessage("§bList of announce messages:§r");
sender.sendMessage("§bFormat: [index] message§r");
int i = 0;
for (String message : component.announceMessages().get())
sender.sendMessage("[" + i++ + "] " + message);
for (String message : component.announceMessages().get()) {
String msg = "[" + i++ + "] " + message;
//noinspection SuspiciousMethodCalls
if (!ComponentManager.isEnabled(FormatterComponent.class) || !Bukkit.getOnlinePlayers().contains(sender)) {
sender.sendMessage(msg);
continue;
}
String json = ChatProcessing.toJson(new TellrawPart(msg)
.setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, "Click to edit"))
.setClickEvent(TellrawEvent.create(TellrawEvent.ClickAction.SUGGEST_COMMAND,
"/" + getCommandPath() + " edit " + (i - 1) + " " + message.replace('§', '&'))));
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + sender.getName() + " " + json);
}
sender.sendMessage("§bCurrent wait time between announcements: "
+ component.announceTime().get() / 60 / 1000 + " minute(s)§r");
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@
import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.architecture.ConfigData;
import buttondevteam.lib.architecture.ListConfigData;
import buttondevteam.lib.chat.TBMCChatAPI;
import org.bukkit.Bukkit;

import java.util.ArrayList;

/**
* Displays the configured messages at the set interval when someone is online.
*/
public class AnnouncerComponent extends Component<PluginMain> implements Runnable {
public ConfigData<ArrayList<String>> announceMessages() {
return getConfig().getData("announceMessages", new ArrayList<>(0));
/**
* The messages to display to players.
*/
public ListConfigData<String> announceMessages() {
return getConfig().getListData("announceMessages");
}

/**
* The time in milliseconds between the messages. Use /u announce settime to set minutes.
*/
public ConfigData<Integer> announceTime() {
return getConfig().getData("announceTime", 15 * 60 * 1000);
}

private TBMCSystemChatEvent.BroadcastTarget target;

private static int AnnounceMessageIndex = 0;
private int AnnounceMessageIndex = 0;

@Override
public void run() {
Expand All @@ -43,7 +51,7 @@ public void run() {

@Override
protected void enable() {
target= TBMCSystemChatEvent.BroadcastTarget.add("announcements");
target = TBMCSystemChatEvent.BroadcastTarget.add("announcements");
registerCommand(new AnnounceCommand(this));
new Thread(this).start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
import java.util.NoSuchElementException;
import java.util.function.Consumer;

/**
* Allows players to append tableflips and other things to their messages. Everything is configurable here.
*/
public class AppendTextComponent extends Component<PluginMain> {
private Map<String, IHaveConfig> appendTexts;

private ConfigData<String[]> helpText(IHaveConfig config) {
return config.getData("helpText", () -> new String[]{
"Tableflip", //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import buttondevteam.chat.components.formatter.formatting.TellrawPart;
import buttondevteam.core.ComponentManager;
import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.architecture.ComponentMetadata;
import buttondevteam.lib.player.TBMCPlayer;
import buttondevteam.lib.player.TBMCPlayerJoinEvent;
import lombok.val;
Expand All @@ -15,6 +16,10 @@
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;

/**
* Allows players to enter chat-only mode which puts them into spectator mode and disallows everything besides chatting.
*/
@ComponentMetadata(enabledByDefault = false)
public class ChatOnlyComponent extends Component implements Listener {
@Override
protected void enable() {
Expand Down
Loading

0 comments on commit c66b212

Please sign in to comment.