Add support for team prefixes, suffixes, and colors (Fixes #150)

This commit is contained in:
RednedEpic 2020-04-04 01:27:34 -05:00
parent 9b487d7d03
commit 786f137e28
4 changed files with 61 additions and 26 deletions

View file

@ -26,8 +26,11 @@
package org.geysermc.connector.entity;
import com.github.steveice10.mc.auth.data.GameProfile;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
import com.github.steveice10.mc.protocol.data.message.TextMessage;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.CommandPermission;
import com.nukkitx.protocol.bedrock.data.EntityData;
import com.nukkitx.protocol.bedrock.data.PlayerPermission;
import com.nukkitx.protocol.bedrock.packet.AddPlayerPacket;
import com.nukkitx.protocol.bedrock.packet.MovePlayerPacket;
@ -39,6 +42,8 @@ import lombok.Setter;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.scoreboard.Team;
import org.geysermc.connector.utils.MessageUtils;
import org.geysermc.connector.utils.SkinUtils;
import java.util.UUID;
@ -152,4 +157,27 @@ public class PlayerEntity extends LivingEntity {
public void setPosition(Vector3f position) {
this.position = position.add(0, entityType.getOffset(), 0);
}
@Override
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
super.updateBedrockMetadata(entityMetadata, session);
if (entityMetadata.getId() == 2) {
// System.out.println(session.getScoreboardCache().getScoreboard().getObjectives().keySet());
for (Team team : session.getScoreboardCache().getScoreboard().getTeams().values()) {
// session.getConnector().getLogger().info("team name " + team.getName());
// session.getConnector().getLogger().info("team entities " + team.getEntities());
}
String username = this.username;
TextMessage name = (TextMessage) entityMetadata.getValue();
if (name != null) {
username = MessageUtils.getBedrockMessage(name);
}
Team team = session.getScoreboardCache().getScoreboard().getTeamFor(username);
if (team != null) {
// session.getConnector().getLogger().info("team name es " + team.getName() + " with prefix " + team.getPrefix() + " and suffix " + team.getSuffix());
metadata.put(EntityData.NAMETAG, team.getPrefix() + MessageUtils.toChatColor(team.getColor()) + username + team.getSuffix());
}
}
}
}

View file

@ -52,12 +52,14 @@ public class JavaTeamTranslator extends PacketTranslator<ServerTeamPacket> {
case CREATE:
scoreboard.registerNewTeam(packet.getTeamName(), toPlayerSet(packet.getPlayers()))
.setName(MessageUtils.getBedrockMessage(packet.getDisplayName()))
.setColor(packet.getColor())
.setPrefix(MessageUtils.getBedrockMessage(packet.getPrefix()))
.setSuffix(MessageUtils.getBedrockMessage(packet.getSuffix()));
break;
case UPDATE:
scoreboard.getTeam(packet.getTeamName())
.setName(MessageUtils.getBedrockMessage(packet.getDisplayName()))
.setColor(packet.getColor())
.setPrefix(MessageUtils.getBedrockMessage(packet.getPrefix()))
.setSuffix(MessageUtils.getBedrockMessage(packet.getSuffix()))
.setUpdateType(UpdateType.UPDATE);

View file

@ -25,6 +25,7 @@
package org.geysermc.connector.scoreboard;
import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamColor;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import lombok.Getter;
import lombok.Setter;
@ -44,10 +45,10 @@ public class Team {
private UpdateType updateType = UpdateType.ADD;
private String name;
private String prefix;
private TeamColor color;
private String suffix;
private Set<String> entities = new ObjectOpenHashSet<>();
public Team(Scoreboard scoreboard, String id) {
this.scoreboard = scoreboard;
this.id = id;

View file

@ -25,6 +25,7 @@
package org.geysermc.connector.utils;
import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamColor;
import com.github.steveice10.mc.protocol.data.message.ChatColor;
import com.github.steveice10.mc.protocol.data.message.ChatFormat;
import com.github.steveice10.mc.protocol.data.message.Message;
@ -36,20 +37,19 @@ import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MessageUtils {
public static List<String> getTranslationParams(Message[] messages) {
List<String> strings = new ArrayList<String>();
for (int i = 0; i < messages.length; i++) {
if (messages[i] instanceof TranslationMessage) {
TranslationMessage translation = (TranslationMessage) messages[i];
List<String> strings = new ArrayList<>();
for (Message message : messages) {
if (message instanceof TranslationMessage) {
TranslationMessage translation = (TranslationMessage) message;
StringBuilder builder = new StringBuilder("");
builder.append("%");
builder.append(translation.getTranslationKey());
strings.add(builder.toString());
String builder = "%" + translation.getTranslationKey();
strings.add(builder);
if (translation.getTranslationKey().equals("commands.gamemode.success.other")) {
strings.add("");
@ -59,15 +59,12 @@ public class MessageUtils {
strings.add(" - no permission or invalid command!");
}
for (int j = 0; j < getTranslationParams(translation.getTranslationParams()).size(); j++) {
strings.add(getTranslationParams(translation.getTranslationParams()).get(j));
}
strings.addAll(getTranslationParams(translation.getTranslationParams()));
} else {
StringBuilder builder = new StringBuilder("");
builder.append(getFormat(messages[i].getStyle().getFormats()));
builder.append(getColor(messages[i].getStyle().getColor()));
builder.append(getBedrockMessage(messages[i]));
strings.add(builder.toString());
String builder = getFormat(message.getStyle().getFormats()) +
getColor(message.getStyle().getColor()) +
getBedrockMessage(message);
strings.add(builder);
}
}
@ -75,12 +72,8 @@ public class MessageUtils {
}
public static String getTranslationText(TranslationMessage message) {
StringBuilder builder = new StringBuilder("");
builder.append(getFormat(message.getStyle().getFormats()));
builder.append(getColor(message.getStyle().getColor()));
builder.append("%");
builder.append(message.getTranslationKey());
return builder.toString();
return getFormat(message.getStyle().getFormats()) + getColor(message.getStyle().getColor())
+ "%" + message.getTranslationKey();
}
public static String getBedrockMessage(Message message) {
@ -98,7 +91,6 @@ public class MessageUtils {
builder.append(getBedrockMessage(msg));
}
}
return builder.toString();
}
@ -206,7 +198,6 @@ public class MessageUtils {
} catch (Exception ex) {
return false;
}
return true;
}
@ -231,7 +222,20 @@ public class MessageUtils {
formatJson((JsonObject) a.get(i));
}
}
return object;
}
public static String toChatColor(TeamColor teamColor) {
for (ChatColor color : ChatColor.values()) {
if (color.name().equals(teamColor.name())) {
return getColor(color);
}
}
for (ChatFormat format : ChatFormat.values()) {
if (format.name().equals(teamColor.name())) {
return getFormat(Collections.singletonList(format));
}
}
return "";
}
}