Java Code Examples for com.comphenix.protocol.utility.MinecraftReflection

The following examples show how to use com.comphenix.protocol.utility.MinecraftReflection. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Update payload contents with a Netty buffer
 *
 * @param contents - new payload content
 */
default void setContentsBuffer(ByteBuf contents)
{
    if (MinecraftReflection.is(MinecraftReflection.getPacketDataSerializerClass(), contents)) {
        getHandle().getModifier().withType(ByteBuf.class).write(0, contents);
    }
    else {
        Object serializer = MinecraftReflection.getPacketDataSerializer(contents);
        getHandle().getModifier().withType(ByteBuf.class).write(0, serializer);
    }
}
 
Example 2
private static String getPacketDescription(PacketContainer packetContainer) throws IllegalAccessException {
	Object packet = packetContainer.getHandle();
	Class<?> clazz = packet.getClass();
	
	// Get the first Minecraft super class
	while (clazz != null && clazz != Object.class &&
			(!MinecraftReflection.isMinecraftClass(clazz) || 
			 Factory.class.isAssignableFrom(clazz))) {
		clazz = clazz.getSuperclass();
	}
	
	return PrettyPrinter.printObject(packet, clazz, MinecraftReflection.getPacketClass(), PrettyPrinter.RECURSE_DEPTH, new ObjectPrinter() {
		@Override
		public boolean print(StringBuilder output, Object value) {
			// Special case
			if (value instanceof byte[]) {
				byte[] data = (byte[]) value;
				
				if (data.length > HEX_DUMP_THRESHOLD) {
					output.append("[");
					HexDumper.defaultDumper().appendTo(output, data);
					output.append("]");
					return true;
				}
			} else if (value != null) {
				EquivalentConverter<Object> converter = findConverter(value.getClass());

				if (converter != null) {
					output.append(converter.getSpecific(value));
					return true;
				}
			}
			return false;
		}
	});
}
 
Example 3
public MetadataHelper() {
	if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_14_R1)) {
		itemSlotIndex = 7;
	} else if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_10_R1)) {
		itemSlotIndex = 6;
	} else if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_9_R1)) {
		itemSlotIndex = 5;
	} else {
		itemSlotIndex = 10;
	}
	
	if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_15_R1)) {
		armorStandStatusIndex = 14;
	} else {
		armorStandStatusIndex = 11;
	}
	
	entityStatusIndex = 0;
	airLevelWatcherIndex = 1;
	customNameIndex = 2;
	customNameVisibleIndex = 3;
	noGravityIndex = 5;
	slimeSizeIndex = 15;
	
	if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_9_R1)) {
		itemSerializer = Registry.get(MinecraftReflection.getItemStackClass());
		intSerializer = Registry.get(Integer.class);
		byteSerializer = Registry.get(Byte.class);
		stringSerializer = Registry.get(String.class);
		booleanSerializer = Registry.get(Boolean.class);
	}
	
	if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) {
		chatComponentSerializer = Registry.get(MinecraftReflection.getIChatBaseComponentClass(), true);
	}
}
 
Example 4
Source Project: helper   Source File: PacketScoreboardTeam.java    License: MIT License 4 votes vote down vote up
private PacketContainer newUpdatePacket() {
    // http://wiki.vg/Protocol#Teams
    PacketContainer packet = new PacketContainer(PacketType.Play.Server.SCOREBOARD_TEAM);

    // set team name - 	String (16)
    packet.getStrings().write(0, getId());

    // set mode - byte
    packet.getIntegers().write(GTEQ_1_13 ? 0 : 1, UpdateType.UPDATE.getCode());

    if (GTEQ_1_13) {
        // set display name - Component
        packet.getChatComponents().write(0, PacketScoreboard.toComponent(getDisplayName()));

        // set prefix - Component
        packet.getChatComponents().write(1, PacketScoreboard.toComponent(getPrefix()));

        // set suffix - Component
        packet.getChatComponents().write(2, PacketScoreboard.toComponent(getSuffix()));
    } else {
        // set display name - String(32)
        packet.getStrings().write(1, getDisplayName());

        // set prefix - String(16)
        packet.getStrings().write(2, getPrefix());

        // set suffix - String(16)
        packet.getStrings().write(3, getSuffix());
    }

    // set friendly flags - byte - Bit mask. 0x01: Allow friendly fire, 0x02: can see invisible entities on same team
    int data = 0;
    if (isAllowFriendlyFire()) {
        data |= 1;
    }
    if (isCanSeeFriendlyInvisibles()) {
        data |= 2;
    }

    packet.getIntegers().write(GTEQ_1_13 ? 1 : 2, data);

    // set nametag visibility - String Enum (32)
    packet.getStrings().write(GTEQ_1_13 ? 1 : 4, getNameTagVisibility().getProtocolName());

    if (SUPPORTS_COLLISION_RULE) {
        // set collision rule - String Enum (32)
        packet.getStrings().write(GTEQ_1_13 ? 2 : 5, getCollisionRule().getProtocolName());
    }

    // set color - byte - For colors, the same Chat colors (0-15). -1 indicates RESET/no color.
    if (GTEQ_1_13) {
        packet.getEnumModifier(ChatColor.class, MinecraftReflection.getMinecraftClass("EnumChatFormat")).write(0, getColor());
    } else {
        packet.getIntegers().write(0, COLOR_CODES.getOrDefault(getColor(), -1));
    }

    return packet;
}