Use VCL14 for 1.15, player sender mock

Using VanillaCommandListener14 for 1.15 as well
Using a mock for the DiscordPlayerSender too, to reduce the amount of code
Made the mocks stub-only, which should lower memory usage
This commit is contained in:
Norbi Peti 2020-03-11 12:00:20 +01:00
parent 454265cd6f
commit 037ec3b9dd
9 changed files with 41 additions and 1246 deletions

View file

@ -4,6 +4,7 @@ end_of_line = lf
insert_final_newline = false
indent_style = space
indent_size = 4
ij_any_field_annotation_wrap = off
[*.json]
indent_style = space
@ -12,6 +13,8 @@ indent_size = 2
[*.java]
indent_style = tab
tab_width = 4
ij_java_do_not_wrap_after_single_annotation = true
ij_java_field_annotation_wrap = off
[{*.yml, *.yaml}]
indent_style = space

View file

@ -176,6 +176,12 @@
<version>1.14.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc..</groupId>
<artifactId>spigot</artifactId>
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.discord4j/Discord4J -->
<dependency>
<groupId>com.discord4j</groupId>

View file

@ -253,6 +253,7 @@ public abstract class DiscordConnectedPlayer extends DiscordSenderBase implement
e.printStackTrace();
return RETURNS_DEFAULTS.answer(invocation);
}
});
})
.stubOnly();
}
}

View file

@ -51,17 +51,18 @@ public abstract class DiscordSenderBase implements CommandSender {
public void sendMessage(String message) {
try {
final boolean broadcast = new Exception().getStackTrace()[2].getMethodName().contains("broadcast");
//if (broadcast && DiscordPlugin.hooked) - TODO: What should happen if unhooked
if (broadcast)
if (broadcast) //We're catching broadcasts using the Bukkit event
return;
final String sendmsg = DPUtils.sanitizeString(message);
msgtosend += "\n" + sendmsg;
if (sendtask == null)
sendtask = Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin, () -> {
channel.createMessage((!broadcast && user != null ? user.getMention() + "\n" : "") + msgtosend.trim()).subscribe();
sendtask = null;
msgtosend = "";
}, 4); // Waits a 0.2 second to gather all/most of the different messages
synchronized (this) {
msgtosend += "\n" + sendmsg;
if (sendtask == null)
sendtask = Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin, () -> {
channel.createMessage((user != null ? user.getMention() + "\n":"") + msgtosend.trim()).subscribe();
sendtask = null;
msgtosend = "";
}, 4); // Waits a 0.2 second to gather all/most of the different messages
}
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while sending message to DiscordSender", e);
}

View file

@ -349,7 +349,7 @@ public class MCChatListener implements Listener {
String mcpackage = Bukkit.getServer().getClass().getPackage().getName();
if (mcpackage.contains("1_12"))
VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd);
else if (mcpackage.contains("1_14"))
else if (mcpackage.contains("1_14") || mcpackage.contains("1_15"))
VanillaCommandListener14.runBukkitOrVanillaCommand(dsender, cmd);
else
Bukkit.dispatchCommand(dsender, cmd);

View file

@ -55,9 +55,9 @@ class MCListener implements Listener {
if (dp != null) {
DiscordPlugin.dc.getUserById(Snowflake.of(dp.getDiscordID())).flatMap(user -> user.getPrivateChannel().flatMap(chan -> module.chatChannelMono().flatMap(cc -> {
MCChatUtils.addSender(MCChatUtils.OnlineSenders, dp.getDiscordID(),
new DiscordPlayerSender(user, chan, p));
DiscordPlayerSender.create(user, chan, p));
MCChatUtils.addSender(MCChatUtils.OnlineSenders, dp.getDiscordID(),
new DiscordPlayerSender(user, cc, p)); //Stored per-channel
DiscordPlayerSender.create(user, cc, p)); //Stored per-channel
return Mono.empty();
}))).subscribe();
}

View file

@ -31,7 +31,7 @@ public class VCMDWrapper {
String mcpackage = Bukkit.getServer().getClass().getPackage().getName();
if (mcpackage.contains("1_12"))
return bukkitplayer == null ? new VanillaCommandListener<>(player) : new VanillaCommandListener<>(player, bukkitplayer);
else if (mcpackage.contains("1_14"))
else if (mcpackage.contains("1_14") || mcpackage.contains("1_15"))
return bukkitplayer == null ? new VanillaCommandListener14<>(player) : new VanillaCommandListener14<>(player, bukkitplayer);
else
return null;

View file

@ -20,9 +20,8 @@ public class VanillaCommandListener<T extends DiscordSenderBase & IMCPlayer<T>>
/**
* This constructor will only send raw vanilla messages to the sender in plain text.
*
* @param player
* The Discord sender player (the wrapper)
*
* @param player The Discord sender player (the wrapper)
*/
public VanillaCommandListener(T player) {
this.player = player;
@ -31,11 +30,9 @@ public class VanillaCommandListener<T extends DiscordSenderBase & IMCPlayer<T>>
/**
* This constructor will send both raw vanilla messages to the sender in plain text and forward the raw message to the provided player.
*
* @param player
* The Discord sender player (the wrapper)
* @param bukkitplayer
* The Bukkit player to send the raw message to
*
* @param player The Discord sender player (the wrapper)
* @param bukkitplayer The Bukkit player to send the raw message to
*/
public VanillaCommandListener(T player, Player bukkitplayer) {
this.player = player;
@ -79,7 +76,7 @@ public class VanillaCommandListener<T extends DiscordSenderBase & IMCPlayer<T>>
if (!(dsender instanceof IMCPlayer))
throw new ClassCastException(
"dsender needs to implement IMCPlayer to use vanilla commands as it implements Player.");
"dsender needs to implement IMCPlayer to use vanilla commands as it implements Player.");
IMCPlayer<?> sender = (IMCPlayer<?>) dsender; // Don't use val on recursive interfaces :P