Add support for 1.16, mostly

This commit is contained in:
Norbi Peti 2020-06-27 03:02:32 +02:00
parent 4ecd32f0ad
commit ce71ff2dd6
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
4 changed files with 34 additions and 9 deletions

View file

@ -8,9 +8,11 @@ import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.UUID;
public class PlayerListWatcher { public class PlayerListWatcher {
private static Object plist; private static Object plist;
@ -65,7 +67,13 @@ public class PlayerListWatcher {
val currentPL = server.getClass().getMethod("getPlayerList").invoke(server); val currentPL = server.getClass().getMethod("getPlayerList").invoke(server);
if (up) { if (up) {
val icbcl = Class.forName(nms + ".IChatBaseComponent"); val icbcl = Class.forName(nms + ".IChatBaseComponent");
val sendMessage = server.getClass().getMethod("sendMessage", icbcl); Method sendMessageTemp;
try {
sendMessageTemp = server.getClass().getMethod("sendMessage", icbcl, UUID.class);
} catch (NoSuchMethodException e) {
sendMessageTemp = server.getClass().getMethod("sendMessage", icbcl);
}
val sendMessage = sendMessageTemp;
val cmtcl = Class.forName(nms + ".ChatMessageType"); val cmtcl = Class.forName(nms + ".ChatMessageType");
val systemType = cmtcl.getDeclaredField("SYSTEM").get(null); val systemType = cmtcl.getDeclaredField("SYSTEM").get(null);
val chatType = cmtcl.getDeclaredField("CHAT").get(null); val chatType = cmtcl.getDeclaredField("CHAT").get(null);
@ -74,7 +82,13 @@ public class PlayerListWatcher {
val ccmcl = Class.forName(obc + ".util.CraftChatMessage"); val ccmcl = Class.forName(obc + ".util.CraftChatMessage");
val fixComponent = ccmcl.getMethod("fixComponent", icbcl); val fixComponent = ccmcl.getMethod("fixComponent", icbcl);
val ppoc = Class.forName(nms + ".PacketPlayOutChat"); val ppoc = Class.forName(nms + ".PacketPlayOutChat");
val ppocC = Class.forName(nms + ".PacketPlayOutChat").getConstructor(icbcl, cmtcl); Constructor<?> ppocCTemp;
try {
ppocCTemp = ppoc.getConstructor(icbcl, cmtcl, UUID.class);
} catch (Exception e) {
ppocCTemp = ppoc.getConstructor(icbcl, cmtcl);
}
val ppocC = ppocCTemp;
val sendAll = dplc.getMethod("sendAll", Class.forName(nms + ".Packet")); val sendAll = dplc.getMethod("sendAll", Class.forName(nms + ".Packet"));
Method tpt; Method tpt;
try { try {
@ -83,6 +97,7 @@ public class PlayerListWatcher {
tpt = icbcl.getMethod("getString"); tpt = icbcl.getMethod("getString");
} }
val toPlainText = tpt; val toPlainText = tpt;
val sysb = Class.forName(nms + ".SystemUtils").getField("b");
mock = Mockito.mock(dplc, Mockito.withSettings().defaultAnswer(new Answer<>() { // Cannot call super constructor mock = Mockito.mock(dplc, Mockito.withSettings().defaultAnswer(new Answer<>() { // Cannot call super constructor
@Override @Override
public Object answer(InvocationOnMock invocation) throws Throwable { public Object answer(InvocationOnMock invocation) throws Throwable {
@ -117,11 +132,20 @@ public class PlayerListWatcher {
private void sendMessage(Object chatComponent, boolean system) { private void sendMessage(Object chatComponent, boolean system) {
try { //Converted to use reflection try { //Converted to use reflection
sendMessage.invoke(server, chatComponent); if (sendMessage.getParameterCount() == 2)
sendMessage.invoke(server, chatComponent, sysb.get(null));
else
sendMessage.invoke(server, chatComponent);
Object chatmessagetype = system ? systemType : chatType; Object chatmessagetype = system ? systemType : chatType;
// CraftBukkit start - we run this through our processor first so we can get web links etc // CraftBukkit start - we run this through our processor first so we can get web links etc
this.sendAll(ppocC.newInstance(fixComponent.invoke(null, chatComponent), chatmessagetype)); var comp = fixComponent.invoke(null, chatComponent);
var packet = ppocC.getParameterCount() == 3
? ppocC.newInstance(comp, chatmessagetype, sysb.get(null))
: ppocC.newInstance(comp, chatmessagetype);
this.sendAll(packet);
// CraftBukkit end // CraftBukkit end
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occurred while passing a vanilla message through the player list", e); TBMCCoreAPI.SendException("An error occurred while passing a vanilla message through the player list", e);

View file

@ -376,14 +376,14 @@ public class MCChatListener implements Listener {
VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd); VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd);
else if (mcpackage.contains("1_14")) else if (mcpackage.contains("1_14"))
VanillaCommandListener14.runBukkitOrVanillaCommand(dsender, cmd); VanillaCommandListener14.runBukkitOrVanillaCommand(dsender, cmd);
else if (mcpackage.contains("1_15")) else if (mcpackage.contains("1_15") || mcpackage.contains("1_16"))
VanillaCommandListener15.runBukkitOrVanillaCommand(dsender, cmd); VanillaCommandListener15.runBukkitOrVanillaCommand(dsender, cmd);
else else
Bukkit.dispatchCommand(dsender, cmd); Bukkit.dispatchCommand(dsender, cmd);
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
TBMCCoreAPI.SendException("A class is not found when trying to run command " + cmd + "!", e); TBMCCoreAPI.SendException("A class is not found when trying to run command " + cmd + "!", e);
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occurred when trying to run command " + cmd + "!", e); TBMCCoreAPI.SendException("An error occurred when trying to run command " + cmd + "! Vanilla commands are only supported in some MC versions.", e);
} }
}); });
return true; return true;

View file

@ -3,6 +3,7 @@ package buttondevteam.discordplugin.playerfaker;
import buttondevteam.discordplugin.DPUtils; import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordSenderBase; import buttondevteam.discordplugin.DiscordSenderBase;
import buttondevteam.discordplugin.IMCPlayer; import buttondevteam.discordplugin.IMCPlayer;
import buttondevteam.lib.TBMCCoreAPI;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -36,7 +37,7 @@ public class VCMDWrapper {
ret = new VanillaCommandListener<>(player, bukkitplayer); ret = new VanillaCommandListener<>(player, bukkitplayer);
else if (mcpackage.contains("1_14")) else if (mcpackage.contains("1_14"))
ret = new VanillaCommandListener14<>(player, bukkitplayer); ret = new VanillaCommandListener14<>(player, bukkitplayer);
else if (mcpackage.contains("1_15")) else if (mcpackage.contains("1_15") || mcpackage.contains("1.16"))
ret = VanillaCommandListener15.create(player, bukkitplayer); //bukkitplayer may be null but that's fine ret = VanillaCommandListener15.create(player, bukkitplayer); //bukkitplayer may be null but that's fine
else else
ret = null; ret = null;
@ -45,8 +46,7 @@ public class VCMDWrapper {
return ret; return ret;
} catch (NoClassDefFoundError | Exception e) { } catch (NoClassDefFoundError | Exception e) {
compatWarning(); compatWarning();
if (!(e instanceof NoClassDefFoundError)) TBMCCoreAPI.SendException("Failed to create vanilla command listener", e);
e.printStackTrace();
return null; return null;
} }
} }

View file

@ -8,3 +8,4 @@ softdepend:
commands: commands:
discord: discord:
website: 'https://github.com/TBMCPlugins/Chroma-Discord' website: 'https://github.com/TBMCPlugins/Chroma-Discord'
api-version: '1.13'