9
0
mirror of https://github.com/Xiao-MoMi/Custom-Nameplates.git synced 2025-12-26 10:29:09 +00:00
This commit is contained in:
Xiao-MoMi
2023-01-06 13:27:35 +08:00
parent 90e227ce07
commit 39f473d8cd
10 changed files with 69 additions and 118 deletions

View File

@@ -37,6 +37,7 @@ public final class CustomNameplates extends JavaPlugin {
public static CustomNameplates plugin;
public static BukkitAudiences adventure;
public static ProtocolManager protocolManager;
public static String version;
private ResourceManager resourceManager;
private BossBarManager bossBarManager;
@@ -62,6 +63,7 @@ public final class CustomNameplates extends JavaPlugin {
public void onEnable() {
adventure = BukkitAudiences.create(this);
protocolManager = ProtocolLibrary.getProtocolManager();
version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
//Don't delete this
PacketContainer packet = new PacketContainer(PacketType.Play.Server.SCOREBOARD_TEAM);

View File

@@ -87,11 +87,7 @@ public class BossBarSender {
public void show() {
this.isShown = true;
try {
CustomNameplates.protocolManager.sendServerPacket(player, getPacket());
} catch (InvocationTargetException e) {
AdventureUtil.consoleMessage("<red>[CustomNameplates] Failed to display bossbar for " + player.getName());
}
CustomNameplates.protocolManager.sendServerPacket(player, getPacket());
this.bukkitTask = new BukkitRunnable() {
@Override
@@ -114,12 +110,7 @@ public class BossBarSender {
timer_1 = 0;
if (text.update() || force) {
force = false;
try {
CustomNameplates.protocolManager.sendServerPacket(player, getUpdatePacket());
}
catch (InvocationTargetException e){
AdventureUtil.consoleMessage("<red>[CustomNameplates] Failed to update bossbar for " + player.getName());
}
CustomNameplates.protocolManager.sendServerPacket(player, getUpdatePacket());
}
}
}
@@ -174,7 +165,7 @@ public class BossBarSender {
remove.setAccessible(true);
packet.getModifier().write(1, remove.get(null));
CustomNameplates.protocolManager.sendServerPacket(player, packet);
} catch (InvocationTargetException | ClassNotFoundException e){
} catch (ClassNotFoundException e){
AdventureUtil.consoleMessage("<red>[CustomNameplates] Failed to remove bossbar for " + player.getName());
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);

View File

@@ -57,13 +57,7 @@ public class ArmorStandManager {
}
public void mount(Player receiver) {
try {
CustomNameplates.protocolManager.sendServerPacket(receiver, getMountPacket(getArmorStandIDs()));
}
catch (InvocationTargetException e) {
e.printStackTrace();
AdventureUtil.consoleMessage("<red>[CustomNameplates] Failed to mount player");
}
CustomNameplates.protocolManager.sendServerPacket(receiver, getMountPacket(getArmorStandIDs()));
}
private PacketContainer getMountPacket(int[] passengers) {

View File

@@ -20,7 +20,9 @@ package net.momirealms.customnameplates.objects.nameplates;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.WrappedDataValue;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.google.common.collect.Lists;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.momirealms.customnameplates.CustomNameplates;
@@ -34,8 +36,8 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Pose;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@@ -93,60 +95,35 @@ public class FakeArmorStand implements ArmorStand {
if (yOffset == offset) return;
yOffset = offset;
for (Player all : asm.getNearbyPlayers()) {
try {
CustomNameplates.protocolManager.sendServerPacket(all, getTeleportPacket());
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
CustomNameplates.protocolManager.sendServerPacket(all, getTeleportPacket());
}
}
@Override
public void spawn(Player viewer) {
for (PacketContainer packet : getSpawnPackets(viewer)) {
try {
CustomNameplates.protocolManager.sendServerPacket(viewer, packet);
}
catch (InvocationTargetException e){
e.printStackTrace();
}
for (PacketContainer packet : getSpawnPackets()) {
CustomNameplates.protocolManager.sendServerPacket(viewer, packet);
}
}
@Override
public void destroy() {
for (Player all : asm.getNearbyPlayers()) {
try {
CustomNameplates.protocolManager.sendServerPacket(all, destroyPacket);
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
CustomNameplates.protocolManager.sendServerPacket(all, destroyPacket);
}
}
@Override
public void destroy(Player viewer) {
try {
CustomNameplates.protocolManager.sendServerPacket(viewer, destroyPacket);
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
CustomNameplates.protocolManager.sendServerPacket(viewer, destroyPacket);
}
@Override
public void teleport() {
PacketContainer packet = getTeleportPacket();
for (Player all : asm.getNearbyPlayers()) {
try {
CustomNameplates.protocolManager.sendServerPacket(all, packet);
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
CustomNameplates.protocolManager.sendServerPacket(all, packet);
}
}
@@ -155,12 +132,7 @@ public class FakeArmorStand implements ArmorStand {
if (!asm.isNearby(viewer) && viewer != owner) {
asm.spawn(viewer);
} else {
try {
CustomNameplates.protocolManager.sendServerPacket(viewer, getTeleportPacket());
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
CustomNameplates.protocolManager.sendServerPacket(viewer, getTeleportPacket());
}
}
@@ -188,7 +160,6 @@ public class FakeArmorStand implements ArmorStand {
spawn(viewer);
}
//传送包
public PacketContainer getTeleportPacket() {
PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_TELEPORT);
@@ -205,13 +176,18 @@ public class FakeArmorStand implements ArmorStand {
for (Player viewer : asm.getNearbyPlayers()) {
PacketContainer metaPacket = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
metaPacket.getIntegers().write(0, entityId);
metaPacket.getWatchableCollectionModifier().write(0, createDataWatcher(getText().getLatestText(), true).getWatchableObjects());
try {
CustomNameplates.protocolManager.sendServerPacket(viewer, metaPacket);
}
catch (InvocationTargetException e) {
e.printStackTrace();
if (CustomNameplates.version.equals("v1_19_R2")) {
WrappedDataWatcher wrappedDataWatcher = createDataWatcher(getText().getLatestText(), true);
List<WrappedDataValue> wrappedDataValueList = Lists.newArrayList();
wrappedDataWatcher.getWatchableObjects().stream().filter(Objects::nonNull).forEach(entry -> {
final WrappedDataWatcher.WrappedDataWatcherObject dataWatcherObject = entry.getWatcherObject();
wrappedDataValueList.add(new WrappedDataValue(dataWatcherObject.getIndex(), dataWatcherObject.getSerializer(), entry.getRawValue()));
});
metaPacket.getDataValueCollectionModifier().write(0, wrappedDataValueList);
} else {
metaPacket.getWatchableCollectionModifier().write(0, createDataWatcher(getText().getLatestText(), true).getWatchableObjects());
}
CustomNameplates.protocolManager.sendServerPacket(viewer, metaPacket);
}
}
@@ -277,7 +253,7 @@ public class FakeArmorStand implements ArmorStand {
}
//创建生成包
public PacketContainer[] getSpawnPackets(Player viewer) {
public PacketContainer[] getSpawnPackets() {
PacketContainer entityPacket = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY);
entityPacket.getModifier().write(0, entityId);
@@ -291,10 +267,30 @@ public class FakeArmorStand implements ArmorStand {
PacketContainer metaPacket = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
metaPacket.getIntegers().write(0, entityId);
if (this.wrappedChatComponent == null) {
metaPacket.getWatchableCollectionModifier().write(0, createDataWatcher(getText().getLatestText(), true).getWatchableObjects());
if (CustomNameplates.version.equals("v1_19_R2")) {
WrappedDataWatcher wrappedDataWatcher = createDataWatcher(getText().getLatestText(), true);
List<WrappedDataValue> wrappedDataValueList = Lists.newArrayList();
wrappedDataWatcher.getWatchableObjects().stream().filter(Objects::nonNull).forEach(entry -> {
final WrappedDataWatcher.WrappedDataWatcherObject dataWatcherObject = entry.getWatcherObject();
wrappedDataValueList.add(new WrappedDataValue(dataWatcherObject.getIndex(), dataWatcherObject.getSerializer(), entry.getRawValue()));
});
metaPacket.getDataValueCollectionModifier().write(0, wrappedDataValueList);
} else {
metaPacket.getWatchableCollectionModifier().write(0, createDataWatcher(getText().getLatestText(), true).getWatchableObjects());
}
}
else {
metaPacket.getWatchableCollectionModifier().write(0, createDataWatcher("", false).getWatchableObjects());
if (CustomNameplates.version.equals("v1_19_R2")) {
WrappedDataWatcher wrappedDataWatcher = createDataWatcher("", false);
List<WrappedDataValue> wrappedDataValueList = Lists.newArrayList();
wrappedDataWatcher.getWatchableObjects().stream().filter(Objects::nonNull).forEach(entry -> {
final WrappedDataWatcher.WrappedDataWatcherObject dataWatcherObject = entry.getWatcherObject();
wrappedDataValueList.add(new WrappedDataValue(dataWatcherObject.getIndex(), dataWatcherObject.getSerializer(), entry.getRawValue()));
});
metaPacket.getDataValueCollectionModifier().write(0, wrappedDataValueList);
} else {
metaPacket.getWatchableCollectionModifier().write(0, createDataWatcher("", false).getWatchableObjects());
}
}
return new PacketContainer[] {entityPacket, metaPacket};
}

View File

@@ -70,11 +70,6 @@ public class SimpleTeamVisibility implements TeamPacketInterface {
if (NameplateManager.removeTag) internalStructure.getStrings().write(0, "never");
else internalStructure.getStrings().write(0, "always");
internalStructure.getEnumModifier(ChatColor.class, MinecraftReflection.getMinecraftClass("EnumChatFormat")).write(0,ChatColor.WHITE);
try {
CustomNameplates.protocolManager.sendServerPacket(player, packet);
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
CustomNameplates.protocolManager.sendServerPacket(player, packet);
}
}

View File

@@ -82,11 +82,6 @@ public class TeamPrefixSuffix implements TeamPacketInterface {
internalStructure.getChatComponents().write(1, WrappedChatComponent.fromJson(GsonComponentSerializer.gson().serialize(nameplatesTeam.getPrefix())));
internalStructure.getChatComponents().write(2, WrappedChatComponent.fromJson(GsonComponentSerializer.gson().serialize(nameplatesTeam.getSuffix())));
internalStructure.getEnumModifier(ChatColor.class, MinecraftReflection.getMinecraftClass("EnumChatFormat")).write(0, nameplatesTeam.getColor());
try {
CustomNameplates.protocolManager.sendServerPacket(player, packet);
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
CustomNameplates.protocolManager.sendServerPacket(player, packet);
}
}

View File

@@ -21,7 +21,9 @@ import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.WrappedDataValue;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.google.common.collect.Lists;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.momirealms.customnameplates.CustomNameplates;
@@ -66,7 +68,17 @@ public class HoloUtil {
wrappedDataWatcher.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(0, serializer2), mask1);
wrappedDataWatcher.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(15, serializer2), mask2);
packet2.getModifier().write(0,id);
packet2.getWatchableCollectionModifier().write(0, wrappedDataWatcher.getWatchableObjects());
if (CustomNameplates.version.equals("v1_19_R2")) {
List<WrappedDataValue> wrappedDataValueList = Lists.newArrayList();
wrappedDataWatcher.getWatchableObjects().stream().filter(Objects::nonNull).forEach(entry -> {
final WrappedDataWatcher.WrappedDataWatcherObject dataWatcherObject = entry.getWatcherObject();
wrappedDataValueList.add(new WrappedDataValue(dataWatcherObject.getIndex(), dataWatcherObject.getSerializer(), entry.getRawValue()));
});
packet2.getDataValueCollectionModifier().write(0, wrappedDataValueList);
} else {
packet2.getWatchableCollectionModifier().write(0, wrappedDataWatcher.getWatchableObjects());
}
cache.put(player, id);
try {

View File

@@ -1,23 +0,0 @@
/*
* Copyright (C) <2022> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customnameplates.utils;
public class NameplateUtil {
}

View File

@@ -43,13 +43,8 @@ public class TeamManagePacketUtil {
PacketContainer packetToAll = getPlayerTeamPacket(joinPlayer);
for (Player all : Bukkit.getOnlinePlayers()) {
getPlayerTeamPacket(all);
try {
CustomNameplates.protocolManager.sendServerPacket(joinPlayer, getPlayerTeamPacket(all));
if (joinPlayer != all) CustomNameplates.protocolManager.sendServerPacket(all, packetToAll);
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
CustomNameplates.protocolManager.sendServerPacket(joinPlayer, getPlayerTeamPacket(all));
if (joinPlayer != all) CustomNameplates.protocolManager.sendServerPacket(all, packetToAll);
}
}
@@ -68,12 +63,7 @@ public class TeamManagePacketUtil {
packetToAll.getIntegers().write(0,1);
packetToAll.getStrings().write(0, quitPlayer.getName());
for (Player all : Bukkit.getOnlinePlayers()) {
try {
CustomNameplates.protocolManager.sendServerPacket(all, packetToAll);
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
CustomNameplates.protocolManager.sendServerPacket(all, packetToAll);
}
}
}