use of com.comphenix.protocol.wrappers.WrappedChatComponent in project LibsDisguises by libraryaddict.
the class PacketListenerScoreboardTeam method onPacketSending.
@Override
public void onPacketSending(PacketEvent event) {
PacketContainer packet = event.getPacket();
if (NmsVersion.v1_17.isSupported()) {
int type = packet.getIntegers().read(0);
if (type != 0 && type != 2) {
return;
}
}
String name = packet.getStrings().read(0);
if (name == null || !name.startsWith("LD_") || name.equals("LD_NoName") || name.startsWith("LD_Color_")) {
return;
}
DisguiseUtilities.DScoreTeam team = DisguiseUtilities.getTeams().get(name);
if (team == null) {
return;
}
StructureModifier<WrappedChatComponent> chats;
if (NmsVersion.v1_17.isSupported()) {
// Might need to do sanity checks but eh
chats = packet.getOptionalStructures().read(0).get().getChatComponents();
} else {
chats = packet.getChatComponents();
}
BaseComponent[] prefix = DisguiseUtilities.getColoredChat(team.getPrefix());
BaseComponent[] suffix = DisguiseUtilities.getColoredChat(team.getSuffix());
chats.write(1, WrappedChatComponent.fromJson(ComponentSerializer.toString(prefix)));
chats.write(2, WrappedChatComponent.fromJson(ComponentSerializer.toString(suffix)));
}
use of com.comphenix.protocol.wrappers.WrappedChatComponent in project LibsDisguises by libraryaddict.
the class ReflectionManager method convertInvalidMeta.
public static Object convertInvalidMeta(Object value) {
if (value instanceof Optional) {
Optional opt = (Optional) value;
if (!opt.isPresent()) {
return NmsVersion.v1_13.isSupported() ? value : com.google.common.base.Optional.absent();
}
if (nmsReflection != null) {
return nmsReflection.convertOptional(opt.get());
}
Object val = opt.get();
if (val instanceof BlockPosition) {
BlockPosition pos = (BlockPosition) val;
try {
Object obj = blockPositionConstructor.newInstance(pos.getX(), pos.getY(), pos.getZ());
return NmsVersion.v1_13.isSupported() ? Optional.of(obj) : com.google.common.base.Optional.of(obj);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (val instanceof WrappedBlockData) {
try {
Object obj = ((WrappedBlockData) val).getHandle();
return NmsVersion.v1_13.isSupported() ? Optional.of(obj) : com.google.common.base.Optional.of(obj);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (val instanceof ItemStack) {
val = getNmsItem((ItemStack) val);
if (val == null) {
return NmsVersion.v1_13.isSupported() ? Optional.empty() : com.google.common.base.Optional.absent();
} else {
return Optional.of(val);
}
} else if (val instanceof WrappedChatComponent) {
Object obj = ((WrappedChatComponent) val).getHandle();
return NmsVersion.v1_13.isSupported() ? Optional.of(obj) : com.google.common.base.Optional.of(obj);
} else if (!NmsVersion.v1_13.isSupported()) {
return com.google.common.base.Optional.of(val);
} else {
return Optional.of(val);
}
} else if (value instanceof Vector3F) {
Vector3F angle = (Vector3F) value;
if (nmsReflection != null) {
return nmsReflection.convertVec3(angle);
}
try {
return vector3FConstructor.newInstance(angle.getX(), angle.getY(), angle.getZ());
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (value instanceof EulerAngle) {
EulerAngle angle = (EulerAngle) value;
if (nmsReflection != null) {
return nmsReflection.convertVec3(angle);
}
try {
return vector3FConstructor.newInstance((float) angle.getX(), (float) angle.getY(), (float) angle.getZ());
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (value instanceof Direction) {
if (nmsReflection != null) {
return nmsReflection.convertDirection((Direction) value);
}
try {
return enumDirectionFrom.invoke(null, ((Direction) value).ordinal());
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (value instanceof BlockPosition) {
BlockPosition pos = (BlockPosition) value;
if (nmsReflection != null) {
return nmsReflection.getBlockPosition(pos.getX(), pos.getY(), pos.getZ());
}
try {
return blockPositionConstructor.newInstance(pos.getX(), pos.getY(), pos.getZ());
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (value instanceof ItemStack) {
return getNmsItem((ItemStack) value);
} else if (value instanceof Double) {
return ((Double) value).floatValue();
} else if (value instanceof NbtWrapper) {
return ((NbtWrapper) value).getHandle();
} else if (value instanceof WrappedParticle) {
return ((WrappedParticle) value).getHandle();
} else if (value instanceof EntityPose) {
return getNmsEntityPose((EntityPose) value);
} else if (value instanceof VillagerData) {
return getNmsVillagerData((VillagerData) value);
} else if (value instanceof WrappedChatComponent) {
return ((WrappedChatComponent) value).getHandle();
}
return value;
}
use of com.comphenix.protocol.wrappers.WrappedChatComponent in project SwornAPI by dmulloy2.
the class ProtocolLibProvider method sendMessage.
@Override
public boolean sendMessage(Player player, ChatPosition position, BaseComponent... message) {
try {
PacketContainer packet = manager.createPacket(PacketType.Play.Server.CHAT);
// Write our message to the packet
WrappedChatComponent component = WrappedChatComponent.fromJson(ComponentSerializer.toString(message));
packet.getChatComponents().write(0, component);
// Nullify Spigot's components
packet.getModifier().write(1, null);
// Write the position
packet.getBytes().write(0, position != null ? position.getValue() : 1);
// Send the packet
manager.sendServerPacket(player, packet);
return true;
} catch (Throwable ex) {
LogHandler.globalDebug(Util.getUsefulStack(ex, "sending chat packet to {0}", player.getName()));
return false;
}
}
use of com.comphenix.protocol.wrappers.WrappedChatComponent in project ProtocolStringReplacer by Rothes.
the class AbstractTitleListener method process.
protected void process(PacketEvent packetEvent) {
PsrUser user = getEventUser(packetEvent);
if (user == null) {
return;
}
PacketContainer packet = packetEvent.getPacket();
StructureModifier<WrappedChatComponent> wrappedChatComponentStructureModifier = packet.getChatComponents();
WrappedChatComponent wrappedChatComponent = wrappedChatComponentStructureModifier.read(0);
if (wrappedChatComponent != null) {
String json = wrappedChatComponent.getJson();
WrappedChatComponent replaced = getReplacedJsonWrappedComponent(packetEvent, user, listenType, json, filter);
if (replaced != null) {
wrappedChatComponentStructureModifier.write(0, replaced);
}
}
}
use of com.comphenix.protocol.wrappers.WrappedChatComponent in project ProtocolStringReplacer by Rothes.
the class EntityMetadata method process.
protected void process(PacketEvent packetEvent) {
PsrUser user = getEventUser(packetEvent);
if (user == null) {
return;
}
PacketContainer packet = packetEvent.getPacket();
Entity entity;
WrapperPlayServerEntityMetadata wrapperPlayServerEntityMetadata;
try {
entity = packet.getEntityModifier(packetEvent).read(0);
if (entity == null) {
return;
}
wrapperPlayServerEntityMetadata = new WrapperPlayServerEntityMetadata(packet.deepClone());
} catch (RuntimeException e) {
if (exceptionTimes < ProtocolStringReplacer.getInstance().getConfigManager().protocollibSideStackPrintCount) {
e.printStackTrace();
ProtocolStringReplacer.warn("This may be a ProtocolLib side problem.");
exceptionTimes++;
}
return;
}
List<WrappedWatchableObject> metadataList = wrapperPlayServerEntityMetadata.getMetadata();
if (metadataList != null) {
for (WrappedWatchableObject watchableObject : metadataList) {
if (watchableObject.getValue() instanceof Optional<?>) {
Optional<?> value = (Optional<?>) watchableObject.getValue();
if (value.isPresent() && MinecraftReflection.getIChatBaseComponentClass().isInstance(value.get())) {
WrappedChatComponent wrappedChatComponent = WrappedChatComponent.fromHandle(value.get());
if (wrappedChatComponent != null) {
String replacedJson = getReplacedJson(packetEvent, user, listenType, wrappedChatComponent.getJson(), filter);
if (replacedJson != null) {
wrappedChatComponent.setJson(replacedJson);
watchableObject.setValue(Optional.of(wrappedChatComponent.getHandle()));
} else {
return;
}
}
}
} else if (BukkitConverters.getItemStackConverter().getSpecificType().isInstance(watchableObject.getValue())) {
Object value = watchableObject.getValue();
if (BukkitConverters.getItemStackConverter().getSpecificType().isInstance(value)) {
ItemStack itemStack = BukkitConverters.getItemStackConverter().getSpecific(value);
replaceItemStack(packetEvent, user, listenType, itemStack, filter);
}
}
packetEvent.setPacket(wrapperPlayServerEntityMetadata.getHandle());
}
}
}
Aggregations