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;
import buttondevteam.core.component.channel.Channel;
import buttondevteam.lib.ChromaUtils;
import buttondevteam.lib.TBMCChatEvent;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.Optional;
import java.util.function.Function;
public final class ChatUtils {
public static final String MCORIGIN = "Minecraft"; //Shouldn't change, like ever - TBMCPlayer.getFolderForType(TBMCPlayer.class) capitalized
@ -45,15 +46,21 @@ public final class ChatUtils {
* Sends a regular (non-Markdown) chat message. Used as a fallback if the chat processing fails.
*
* @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) {
var str = "[" + e.getChannel().DisplayName().get() + "] <"
+ ChromaUtils.getDisplayName(e.getSender()) + "> " + e.getMessage();
str = modifier.apply(str);
public static void sendChatMessage(TBMCChatEvent e) {
var str = getMessageString(e.getChannel(), e.getSender(), e.getMessage());
for (Player p : Bukkit.getOnlinePlayers())
if (e.shouldSendTo(p))
p.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 org.bukkit.entity.Player;
import java.lang.reflect.Array;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
@ -81,7 +82,9 @@ public class VanillaUtils {
val handle = hm.invoke(p);
val nms = handle.getClass().getPackage().getName();
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 iclcl = Class.forName(nms + ".ICommandListener");
@ -97,7 +100,8 @@ public class VanillaUtils {
val hhandle = hm.invoke(pl);
val deserialized = am.invoke(null, jsonStr);
//val filtered = ffdm.invoke(null, hhandle, deserialized, hhandle);
sendmsg.invoke(hhandle, deserialized);
Array.set(chatcomparr, 0, deserialized);
sendmsg.invoke(hhandle, chatcomparr);
return true;
} catch (Exception e) {
e.printStackTrace();

View file

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

View file

@ -1,5 +1,6 @@
package buttondevteam.chat.components.towny;
import buttondevteam.chat.ChatUtils;
import buttondevteam.chat.PluginMain;
import buttondevteam.chat.VanillaUtils;
import buttondevteam.chat.components.formatter.formatting.TellrawPart;
@ -18,6 +19,7 @@ import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -37,8 +39,10 @@ public class TownyComponent extends Component<PluginMain> {
protected void enable() {
try {
try {
dataSource = (TownyDataSource) Class.forName("com.palmergames.bukkit.towny.TownyUniverse").getMethod("getDataSource")
.invoke(null);
var tucl = Class.forName("com.palmergames.bukkit.towny.TownyUniverse");
var tu = tucl.getMethod("getInstance").invoke(null);
dataSource = (TownyDataSource) tucl.getMethod("getDataSource")
.invoke(tu);
} catch (ClassNotFoundException e) {
dataSource = (TownyDataSource) Class.forName("com.palmergames.bukkit.towny.object.TownyUniverse").getMethod("getDataSource")
.invoke(null);
@ -60,20 +64,22 @@ public class TownyComponent extends Component<PluginMain> {
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)) {
((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;
public void handleSpies(Channel channel, Player p) {
private void handleSpies(Channel channel, Player p, String jsonstr, CommandSender sender, String message) {
if (channel.ID.equals(TownChat.ID) || channel.ID.equals(NationChat.ID)) {
try {
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) {
}
}

View file

@ -87,7 +87,7 @@ public class PlayerListener implements Listener {
if (e.shouldSendTo(p))
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);
}
}

View file

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