protocolsupport.utils.Utils Java Examples

The following examples show how to use protocolsupport.utils.Utils. 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
Source File: ScoreboardTeam.java    From ProtocolSupport with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected void writeToClient() {
	ClientBoundPacketData scoreboardteam = ClientBoundPacketData.create(PacketType.CLIENTBOUND_PLAY_SCOREBOARD_TEAM);
	StringSerializer.writeString(scoreboardteam, version, name);
	MiscSerializer.writeByteEnum(scoreboardteam, mode);
	if ((mode == Mode.CREATE) || (mode == Mode.UPDATE)) {
		StringSerializer.writeString(scoreboardteam, version, LegacyChat.clampLegacyText(displayName.toLegacyText(clientCache.getLocale()), 32));
		Any<String, String> nfix = formatPrefixSuffix();
		StringSerializer.writeString(scoreboardteam, version, nfix.getObj1());
		StringSerializer.writeString(scoreboardteam, version, nfix.getObj2());
		scoreboardteam.writeByte(friendlyFire);
	}
	if ((mode == Mode.CREATE) || (mode == Mode.PLAYERS_ADD) || (mode == Mode.PLAYERS_REMOVE)) {
		ArraySerializer.writeShortTArray(scoreboardteam, players, (to, element) -> StringSerializer.writeString(to, version, Utils.clampString(element, 16)));
	}
	codec.write(scoreboardteam);
}
 
Example #2
Source File: ScoreboardTeam.java    From ProtocolSupport with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected void writeToClient() {
	ClientBoundPacketData scoreboardteam = ClientBoundPacketData.create(PacketType.CLIENTBOUND_PLAY_SCOREBOARD_TEAM);
	StringSerializer.writeVarIntUTF8String(scoreboardteam, name);
	MiscSerializer.writeByteEnum(scoreboardteam, mode);
	if ((mode == Mode.CREATE) || (mode == Mode.UPDATE)) {
		StringSerializer.writeVarIntUTF8String(scoreboardteam, LegacyChat.clampLegacyText(displayName.toLegacyText(clientCache.getLocale()), 32));
		Any<String, String> nfix = formatPrefixSuffix();
		StringSerializer.writeVarIntUTF8String(scoreboardteam, nfix.getObj1());
		StringSerializer.writeVarIntUTF8String(scoreboardteam, nfix.getObj2());
		scoreboardteam.writeByte(friendlyFire);
		StringSerializer.writeVarIntUTF8String(scoreboardteam, nameTagVisibility);
		scoreboardteam.writeByte(format.isColor() ? format.ordinal() : -1);
	}
	if ((mode == Mode.CREATE) || (mode == Mode.PLAYERS_ADD) || (mode == Mode.PLAYERS_REMOVE)) {
		ArraySerializer.writeVarIntTArray(scoreboardteam, players, (to, element) -> StringSerializer.writeVarIntUTF8String(to, Utils.clampString(element, 16)));
	}
	codec.write(scoreboardteam);
}
 
Example #3
Source File: SimpleReadTimeoutHandler.java    From ProtocolSupport with GNU Affero General Public License v3.0 6 votes vote down vote up
private void initialize(final ChannelHandlerContext ctx) {
	this.lastReadTime = System.nanoTime();
	this.timeoutTask = ctx.executor().schedule(new Runnable() {
		@Override
		public void run() {
			if (ctx.channel().isOpen()) {
				long untilTimeout = timeoutTime - (Utils.currentTimeMillisFromNanoTime() - lastReadTime);
				if (untilTimeout <= 0) {
					ctx.fireExceptionCaught(ReadTimeoutException.INSTANCE);
				} else {
					ctx.executor().schedule(this, untilTimeout, TimeUnit.MILLISECONDS);
				}
			}
		}
	}, this.timeoutTime, TimeUnit.MILLISECONDS);
}
 
Example #4
Source File: ChatColor.java    From ProtocolSupport with GNU Affero General Public License v3.0 6 votes vote down vote up
public ChatColor asBasic() {
	if (isBasic()) {
		return this;
	}

	ChatColor similarColor = null;
	long similarColorDiff = Long.MAX_VALUE;
	for (ChatColor basic : BASIC_BY_BUKKIT.values()) {
		long colorDiff = Utils.getColorDiff(getRed(), basic.getRed(), getGreen(), basic.getGreen(), getBlue(), basic.getBlue());
		if (colorDiff <= similarColorDiff) {
			similarColorDiff = colorDiff;
			similarColor = basic;
		}
	}
	return similarColor;
}
 
Example #5
Source File: WorldCustomSound.java    From ProtocolSupport with GNU Affero General Public License v3.0 6 votes vote down vote up
public static ClientBoundPacketData create(
	ProtocolVersion version,
	int x, int y, int z,
	String sound, float volume, float pitch
) {
	ClientBoundPacketData worldcustomsound = ClientBoundPacketData.create(PacketType.CLIENTBOUND_PLAY_WORLD_CUSTOM_SOUND);
	sound = SoundRemapper.getSoundName(version, sound);
	if (version.isBeforeOrEq(ProtocolVersion.MINECRAFT_1_5_2)) {
		sound = Utils.clampString(sound, 32);
	}
	StringSerializer.writeString(worldcustomsound, version, sound);
	worldcustomsound.writeInt(x);
	worldcustomsound.writeInt(y);
	worldcustomsound.writeInt(z);
	worldcustomsound.writeFloat(volume);
	worldcustomsound.writeByte((int) (pitch * 63.5));
	return worldcustomsound;
}
 
Example #6
Source File: ScoreboardScore.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void writeToClient() {
	ClientBoundPacketData scoreboardscore = ClientBoundPacketData.create(PacketType.CLIENTBOUND_PLAY_SCOREBOARD_SCORE);
	StringSerializer.writeString(scoreboardscore, version, Utils.clampString(name, 16));
	scoreboardscore.writeByte(mode);
	if (mode != 1) {
		StringSerializer.writeString(scoreboardscore, version, objectiveName);
		scoreboardscore.writeInt(value);
	}
	codec.write(scoreboardscore);
}
 
Example #7
Source File: ThrottleTracker.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean throttle(InetAddress address) {
	synchronized (tracker) {
		long ctime = Utils.currentTimeMillisFromNanoTime();
		LongIterator iterator = tracker.values().iterator();
		while (iterator.hasNext()) {
			if (iterator.nextLong() < ctime) {
				iterator.remove();
			}
		}
		long ret = tracker.put(address, ctime + time);
		return ret != tracker.defaultReturnValue();
	}
}
 
Example #8
Source File: EntityDestroy.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void writeToClient() {
	for (int[] part : Utils.splitArray(entityIds, 120)) {
		ClientBoundPacketData entitydestroy = ClientBoundPacketData.create(PacketType.CLIENTBOUND_PLAY_ENTITY_DESTROY);
		entitydestroy.writeByte(part.length);
		for (int i = 0; i < part.length; i++) {
			entitydestroy.writeInt(part[i]);
		}
		codec.write(entitydestroy);
	}
}
 
Example #9
Source File: TabComplete.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void writeToClient() {
	String prefix = start <= 1 ? "/" : "";
	ClientBoundPacketData tabcomplete = ClientBoundPacketData.create(PacketType.CLIENTBOUND_PLAY_TAB_COMPLETE);
	StringSerializer.writeShortUTF16BEString(tabcomplete, Utils.clampString(String.join("\u0000", Arrays.stream(matches).map(input -> prefix + input.getMatch()).collect(Collectors.toList())), Short.MAX_VALUE));
	codec.write(tabcomplete);
}
 
Example #10
Source File: SpawnNamed.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void writeToClient() {
	ClientBoundPacketData spawnnamed = ClientBoundPacketData.create(PacketType.CLIENTBOUND_PLAY_SPAWN_NAMED);
	spawnnamed.writeInt(entity.getId());
	PlayerListEntry entry = playerlistCache.getEntry(entity.getUUID());
	StringSerializer.writeShortUTF16BEString(spawnnamed, entry != null ? Utils.clampString(entry.getUserName(), 16) : "UNKNOWN");
	spawnnamed.writeInt((int) (x * 32));
	spawnnamed.writeInt((int) (y * 32));
	spawnnamed.writeInt((int) (z * 32));
	spawnnamed.writeByte(yaw);
	spawnnamed.writeByte(pitch);
	spawnnamed.writeShort(0);
	NetworkEntityMetadataSerializer.writeLegacyData(spawnnamed, version, I18NData.DEFAULT_LOCALE, NetworkEntityMetadataList.EMPTY);
	codec.write(spawnnamed);
}
 
Example #11
Source File: LegacyRelMoveConverter.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
public static RelMove[] getRelMoves(RelMove relMove, int limit) {
	int maxVal = Math.abs(relMove.getX());
	maxVal = Math.max(Math.abs(relMove.getY()), maxVal);
	maxVal = Math.max(Math.abs(relMove.getZ()), maxVal);
	RelMove[] moves = new RelMove[Utils.getSplitCount(maxVal, limit)];
	for (int i = 0; i < moves.length; i++) {
		moves[i] = new RelMove(relMove.decreaseX(limit), relMove.decreaseY(limit), relMove.decreaseZ(limit));
	}
	return moves;
}
 
Example #12
Source File: SpawnNamed.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void writeToClient() {
	ClientBoundPacketData spawnnamed = ClientBoundPacketData.create(PacketType.CLIENTBOUND_PLAY_SPAWN_NAMED);
	VarNumberSerializer.writeVarInt(spawnnamed, entity.getId());
	UUID uuid = entity.getUUID();
	StringSerializer.writeVarIntUTF8String(spawnnamed, version == ProtocolVersion.MINECRAFT_1_7_10 ? uuid.toString() : uuid.toString().replace("-", ""));
	PlayerListEntry entry = playerlistCache.getEntry(uuid);
	if (entry != null) {
		StringSerializer.writeVarIntUTF8String(spawnnamed, Utils.clampString(entry.getUserName(), 16));
		if (version == ProtocolVersion.MINECRAFT_1_7_10) {
			ArraySerializer.writeVarIntTArray(spawnnamed, entry.getProperties(true), (to, property) -> {
				StringSerializer.writeVarIntUTF8String(to, property.getName());
				StringSerializer.writeVarIntUTF8String(to, property.getValue());
				StringSerializer.writeVarIntUTF8String(to, property.getSignature());
			});
		}
	} else {
		StringSerializer.writeVarIntUTF8String(spawnnamed, "UNKNOWN");
		if (version == ProtocolVersion.MINECRAFT_1_7_10) {
			VarNumberSerializer.writeVarInt(spawnnamed, 0);
		}
	}
	spawnnamed.writeInt((int) (x * 32));
	spawnnamed.writeInt((int) (y * 32));
	spawnnamed.writeInt((int) (z * 32));
	spawnnamed.writeByte(yaw);
	spawnnamed.writeByte(pitch);
	spawnnamed.writeShort(0);
	NetworkEntityMetadataSerializer.writeLegacyData(spawnnamed, version, I18NData.DEFAULT_LOCALE, NetworkEntityMetadataList.EMPTY);
	codec.write(spawnnamed);
}
 
Example #13
Source File: MiddleEntityAttributes.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #14
Source File: CustomPayload.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void writeToClient() {
	codec.write(create(version, Utils.clampString(channelsCache.getLegacyName(LegacyCustomPayloadChannelName.toPre13(tag)), 20), data));
}
 
Example #15
Source File: CustomPayload.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void writeToClient() {
	codec.write(create(Utils.clampString(channelsCache.getLegacyName(LegacyCustomPayloadChannelName.toPre13(tag)), 20), data));
}
 
Example #16
Source File: MiddleBlockChangeMulti.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #17
Source File: MiddleEntityAttributes.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #18
Source File: ClientCommand.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void readClientData(ByteBuf clientdata) {
	command = Utils.getFromArrayOrNull(commandsById, VarNumberSerializer.readVarInt(clientdata));
}
 
Example #19
Source File: MiddlePacket.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #20
Source File: MapColorHelper.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
private static long getDiff(IMapColor color1, IMapColor color2) {
	return Utils.getColorDiff(color1.getRed(), color2.getRed(), color1.getGreen(), color2.getGreen(), color1.getBlue(), color2.getBlue());
}
 
Example #21
Source File: ProfileProperty.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #22
Source File: ChatColor.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #23
Source File: HoverAction.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #24
Source File: HoverAction.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #25
Source File: ClickAction.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #26
Source File: Modifier.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #27
Source File: BaseComponent.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #28
Source File: ProtocolSupport.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #29
Source File: NetworkEntityDataCache.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
	return Utils.toStringAllFields(this);
}
 
Example #30
Source File: StringSerializer.java    From ProtocolSupportBungee with GNU Affero General Public License v3.0 4 votes vote down vote up
public static String readShortUTF16BEString(ByteBuf buf) {
	return new String(Utils.readBytes(buf, buf.readUnsignedShort() * 2), StandardCharsets.UTF_16BE);
}