Skip to content

Commit

Permalink
Fix Title on 1.18
Browse files Browse the repository at this point in the history
  • Loading branch information
Brikster committed Dec 5, 2021
1 parent 6e5c5f5 commit f61cc3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 16 additions & 6 deletions api/src/main/java/ru/mrbrikster/chatty/util/textapi/Title.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.bukkit.entity.Player;
import ru.mrbrikster.chatty.json.FormattedMessage;

import java.lang.reflect.Method;

/**
* Represents a title that appears at the center of the screen.
*
Expand Down Expand Up @@ -95,25 +97,33 @@ public void send(Player player) {
if (subtitle != null)
subtitleComponent = clsChatSerializer.getMethod("a", String.class).invoke(null, subtitle.toString());

Method sendPacketMethod;
try {
sendPacketMethod = playerConnection.getClass().getMethod("sendPacket", clsPacket);
} catch (Exception ignored) {
// 1.18+
sendPacketMethod = playerConnection.getClass().getMethod("a", clsPacket);
}

Class<?> clsSetTitlePacket = NMSUtil.getClass("ClientboundSetTitleTextPacket");
if (clsSetTitlePacket == null) {
// Legacy titles code

Class<?> clsPacketPlayOutTitle = NMSUtil.getClass("PacketPlayOutTitle");
Class<?> clsEnumTitleAction = NMSUtil.getClass("PacketPlayOutTitle$EnumTitleAction");
Object timesPacket = clsPacketPlayOutTitle.getConstructor(int.class, int.class, int.class).newInstance(fadeIn, stay, fadeOut);
playerConnection.getClass().getMethod("sendPacket", clsPacket).invoke(playerConnection, timesPacket);
sendPacketMethod.invoke(playerConnection, timesPacket);

// Play title packet
if (title != null) {
Object titlePacket = clsPacketPlayOutTitle.getConstructor(clsEnumTitleAction, clsIChatBaseComponent).newInstance(clsEnumTitleAction.getField("TITLE").get(null), titleComponent);
playerConnection.getClass().getMethod("sendPacket", clsPacket).invoke(playerConnection, titlePacket);
sendPacketMethod.invoke(playerConnection, titlePacket);
}

// Play subtitle packet
if (subtitle != null) {
Object subtitlePacket = clsPacketPlayOutTitle.getConstructor(clsEnumTitleAction, clsIChatBaseComponent).newInstance(clsEnumTitleAction.getField("SUBTITLE").get(null), subtitleComponent);
playerConnection.getClass().getMethod("sendPacket", clsPacket).invoke(playerConnection, subtitlePacket);
sendPacketMethod.invoke(playerConnection, subtitlePacket);
}
} else {
// New titles code
Expand All @@ -123,18 +133,18 @@ public void send(Player player) {

// Play animation packet
Object animationPacket = clsSetAnimationPacket.getConstructor(int.class, int.class, int.class).newInstance(fadeIn, stay, fadeOut);
playerConnection.getClass().getMethod("sendPacket", clsPacket).invoke(playerConnection, animationPacket);
sendPacketMethod.invoke(playerConnection, animationPacket);

// Play title packet
if (title != null) {
Object titlePacket = clsSetTitlePacket.getConstructor(clsIChatBaseComponent).newInstance(titleComponent);
playerConnection.getClass().getMethod("sendPacket", clsPacket).invoke(playerConnection, titlePacket);
sendPacketMethod.invoke(playerConnection, titlePacket);
}

// Play subtitle packet
if (subtitle != null) {
Object subtitlePacket = clsSetSubtitlePacket.getConstructor(clsIChatBaseComponent).newInstance(subtitleComponent);
playerConnection.getClass().getMethod("sendPacket", clsPacket).invoke(playerConnection, subtitlePacket);
sendPacketMethod.invoke(playerConnection, subtitlePacket);
}
}
} catch (Throwable e) {
Expand Down
2 changes: 0 additions & 2 deletions spigot/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.apache.tools.ant.filters.ReplaceTokens

plugins {
id 'com.github.johnrengelman.shadow' version '6.1.0'
id 'xyz.jpenilla.run-paper' version '1.0.5'
Expand Down

0 comments on commit f61cc3f

Please sign in to comment.