1.16 updates, fixes

This commit is contained in:
Norbi Peti 2020-06-27 03:00:08 +02:00
parent c688ec9243
commit 7448eb7e3a
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
6 changed files with 44 additions and 23 deletions

View file

@ -1,12 +1,13 @@
package buttondevteam.chat; package buttondevteam.chat;
import buttondevteam.core.component.channel.Channel;
import buttondevteam.lib.ChromaUtils; import buttondevteam.lib.ChromaUtils;
import buttondevteam.lib.TBMCChatEvent; import buttondevteam.lib.TBMCChatEvent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function;
public final class ChatUtils { public final class ChatUtils {
public static final String MCORIGIN = "Minecraft"; //Shouldn't change, like ever - TBMCPlayer.getFolderForType(TBMCPlayer.class) capitalized public static final String MCORIGIN = "Minecraft"; //Shouldn't change, like ever - TBMCPlayer.getFolderForType(TBMCPlayer.class) capitalized
@ -44,16 +45,22 @@ public final class ChatUtils {
/** /**
* Sends a regular (non-Markdown) chat message. Used as a fallback if the chat processing fails. * Sends a regular (non-Markdown) chat message. Used as a fallback if the chat processing fails.
* *
* @param e The chat event * @param e The chat event
* @param modifier A function that alters the message to be displayed to the player
*/ */
public static void sendChatMessage(TBMCChatEvent e, Function<String, String> modifier) { public static void sendChatMessage(TBMCChatEvent e) {
var str = "[" + e.getChannel().DisplayName().get() + "] <" var str = getMessageString(e.getChannel(), e.getSender(), e.getMessage());
+ ChromaUtils.getDisplayName(e.getSender()) + "> " + e.getMessage();
str = modifier.apply(str);
for (Player p : Bukkit.getOnlinePlayers()) for (Player p : Bukkit.getOnlinePlayers())
if (e.shouldSendTo(p)) if (e.shouldSendTo(p))
p.sendMessage(str); p.sendMessage(str);
Bukkit.getConsoleSender().sendMessage(str); Bukkit.getConsoleSender().sendMessage(str);
} }
public static String getMessageString(Channel channel, CommandSender sender, String message) {
return "§c!§r[" + channel.DisplayName().get() + "] <"
+ ChromaUtils.getDisplayName(sender) + "> " + message;
}
public static void sendChatMessage(Channel channel, CommandSender sender, String message, CommandSender to) {
to.sendMessage(getMessageString(channel, sender, message));
}
} }

View file

@ -6,6 +6,7 @@ import lombok.experimental.UtilityClass;
import lombok.val; import lombok.val;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.Array;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -81,7 +82,9 @@ public class VanillaUtils {
val handle = hm.invoke(p); val handle = hm.invoke(p);
val nms = handle.getClass().getPackage().getName(); val nms = handle.getClass().getPackage().getName();
val chatcompcl = Class.forName(nms + ".IChatBaseComponent"); val chatcompcl = Class.forName(nms + ".IChatBaseComponent");
val sendmsg = handle.getClass().getMethod("sendMessage", chatcompcl); //val chatcomarrcl = Class.forName("[L" + chatcompcl.getName() + ";");
val chatcomparr = Array.newInstance(chatcompcl, 1);
val sendmsg = handle.getClass().getMethod("sendMessage", chatcomparr.getClass());
/*val ccucl = Class.forName(nms + ".ChatComponentUtils"); /*val ccucl = Class.forName(nms + ".ChatComponentUtils");
val iclcl = Class.forName(nms + ".ICommandListener"); val iclcl = Class.forName(nms + ".ICommandListener");
@ -97,7 +100,8 @@ public class VanillaUtils {
val hhandle = hm.invoke(pl); val hhandle = hm.invoke(pl);
val deserialized = am.invoke(null, jsonStr); val deserialized = am.invoke(null, jsonStr);
//val filtered = ffdm.invoke(null, hhandle, deserialized, hhandle); //val filtered = ffdm.invoke(null, hhandle, deserialized, hhandle);
sendmsg.invoke(hhandle, deserialized); Array.set(chatcomparr, 0, deserialized);
sendmsg.invoke(hhandle, chatcomparr);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View file

@ -173,7 +173,8 @@ public class ChatProcessing {
return true; return true;
} }
val tc = ComponentManager.getIfEnabled(TownyComponent.class); val tc = ComponentManager.getIfEnabled(TownyComponent.class);
if (tc != null) tc.handleSpiesInit(channel, json, ChatProcessing::toJson); Consumer<Player> spyConsumer = null;
if (tc != null) spyConsumer = tc.handleSpiesInit(channel, json, ChatProcessing::toJson, sender, message);
for (Player p : Bukkit.getOnlinePlayers()) { for (Player p : Bukkit.getOnlinePlayers()) {
final String group; final String group;
if (player != null if (player != null
@ -182,13 +183,15 @@ public class ChatProcessing {
else else
group = VanillaUtils.getGroupIfChatOn(p, e); group = VanillaUtils.getGroupIfChatOn(p, e);
if (senderGroup.equals(group)) if (senderGroup.equals(group))
VanillaUtils.tellRaw(p, jsonstr); if (!VanillaUtils.tellRaw(p, jsonstr))
else if (tc != null) tc.handleSpies(channel, p); p.sendMessage(ChatUtils.getMessageString(channel, sender, message));
else if (tc != null) spyConsumer.accept(p);
//Only sends if didn't send normally //Only sends if didn't send normally
} }
} else } else
for (Player p : Bukkit.getOnlinePlayers()) for (Player p : Bukkit.getOnlinePlayers())
VanillaUtils.tellRaw(p, jsonstr); if (!VanillaUtils.tellRaw(p, jsonstr))
ChatUtils.sendChatMessage(channel, sender, message, p);
} catch (Exception ex) { } catch (Exception ex) {
TBMCCoreAPI.SendException("An error occured while sending a chat message!", ex); TBMCCoreAPI.SendException("An error occured while sending a chat message!", ex);
sender.sendMessage("§cAn error occured while sending the message."); sender.sendMessage("§cAn error occured while sending the message.");

View file

@ -1,5 +1,6 @@
package buttondevteam.chat.components.towny; package buttondevteam.chat.components.towny;
import buttondevteam.chat.ChatUtils;
import buttondevteam.chat.PluginMain; import buttondevteam.chat.PluginMain;
import buttondevteam.chat.VanillaUtils; import buttondevteam.chat.VanillaUtils;
import buttondevteam.chat.components.formatter.formatting.TellrawPart; import buttondevteam.chat.components.formatter.formatting.TellrawPart;
@ -18,6 +19,7 @@ import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -37,8 +39,10 @@ public class TownyComponent extends Component<PluginMain> {
protected void enable() { protected void enable() {
try { try {
try { try {
dataSource = (TownyDataSource) Class.forName("com.palmergames.bukkit.towny.TownyUniverse").getMethod("getDataSource") var tucl = Class.forName("com.palmergames.bukkit.towny.TownyUniverse");
.invoke(null); var tu = tucl.getMethod("getInstance").invoke(null);
dataSource = (TownyDataSource) tucl.getMethod("getDataSource")
.invoke(tu);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
dataSource = (TownyDataSource) Class.forName("com.palmergames.bukkit.towny.object.TownyUniverse").getMethod("getDataSource") dataSource = (TownyDataSource) Class.forName("com.palmergames.bukkit.towny.object.TownyUniverse").getMethod("getDataSource")
.invoke(null); .invoke(null);
@ -60,20 +64,22 @@ public class TownyComponent extends Component<PluginMain> {
TownyAnnouncer.setdown(); TownyAnnouncer.setdown();
} }
public void handleSpiesInit(Channel channel, TellrawPart json, Function<TellrawPart, String> toJson) { public Consumer<Player> handleSpiesInit(Channel channel, TellrawPart json, Function<TellrawPart, String> toJson,
CommandSender sender, String message) {
if (channel.ID.equals(TownChat.ID) || channel.ID.equals(NationChat.ID)) { if (channel.ID.equals(TownChat.ID) || channel.ID.equals(NationChat.ID)) {
((List<TellrawPart>) json.getExtra()).add(0, new TellrawPart("[SPY]")); ((List<TellrawPart>) json.getExtra()).add(0, new TellrawPart("[SPY]"));
jsonstr = toJson.apply(json); String jsonstr = toJson.apply(json);
return p -> handleSpies(channel, p, jsonstr, sender, message);
} }
return p -> {};
} }
private String jsonstr; private void handleSpies(Channel channel, Player p, String jsonstr, CommandSender sender, String message) {
public void handleSpies(Channel channel, Player p) {
if (channel.ID.equals(TownChat.ID) || channel.ID.equals(NationChat.ID)) { if (channel.ID.equals(TownChat.ID) || channel.ID.equals(NationChat.ID)) {
try { try {
if (dataSource.getResident(p.getName()).hasMode("spy")) if (dataSource.getResident(p.getName()).hasMode("spy"))
VanillaUtils.tellRaw(p, jsonstr); if (!VanillaUtils.tellRaw(p, jsonstr))
ChatUtils.sendChatMessage(channel, sender, message, p);
} catch (NotRegisteredException ignored) { } catch (NotRegisteredException ignored) {
} }
} }

View file

@ -87,7 +87,7 @@ public class PlayerListener implements Listener {
if (e.shouldSendTo(p)) if (e.shouldSendTo(p))
p.sendMessage("[" + e.getChannel().DisplayName().get() + "] §cSome features in the message below might be unavailable due to an error."); p.sendMessage("[" + e.getChannel().DisplayName().get() + "] §cSome features in the message below might be unavailable due to an error.");
} }
ChatUtils.sendChatMessage(e, s -> "§c!§r" + s); ChatUtils.sendChatMessage(e);
TBMCCoreAPI.SendException("An error occured while processing a chat message!", ex); TBMCCoreAPI.SendException("An error occured while processing a chat message!", ex);
} }
} }

View file

@ -45,4 +45,5 @@ permissions:
default: false default: false
tbmc.badge.diamond: tbmc.badge.diamond:
description: Gives a cool patron badge. description: Gives a cool patron badge.
default: false default: false
api-version: '1.13'