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; package org.geysermc.connector.entity;
import com.github.steveice10.mc.auth.data.GameProfile; 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.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.CommandPermission; 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.data.PlayerPermission;
import com.nukkitx.protocol.bedrock.packet.AddPlayerPacket; import com.nukkitx.protocol.bedrock.packet.AddPlayerPacket;
import com.nukkitx.protocol.bedrock.packet.MovePlayerPacket; import com.nukkitx.protocol.bedrock.packet.MovePlayerPacket;
@ -39,6 +42,8 @@ import lombok.Setter;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.entity.type.EntityType; import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession; 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 org.geysermc.connector.utils.SkinUtils;
import java.util.UUID; import java.util.UUID;
@ -152,4 +157,27 @@ public class PlayerEntity extends LivingEntity {
public void setPosition(Vector3f position) { public void setPosition(Vector3f position) {
this.position = position.add(0, entityType.getOffset(), 0); 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: case CREATE:
scoreboard.registerNewTeam(packet.getTeamName(), toPlayerSet(packet.getPlayers())) scoreboard.registerNewTeam(packet.getTeamName(), toPlayerSet(packet.getPlayers()))
.setName(MessageUtils.getBedrockMessage(packet.getDisplayName())) .setName(MessageUtils.getBedrockMessage(packet.getDisplayName()))
.setColor(packet.getColor())
.setPrefix(MessageUtils.getBedrockMessage(packet.getPrefix())) .setPrefix(MessageUtils.getBedrockMessage(packet.getPrefix()))
.setSuffix(MessageUtils.getBedrockMessage(packet.getSuffix())); .setSuffix(MessageUtils.getBedrockMessage(packet.getSuffix()));
break; break;
case UPDATE: case UPDATE:
scoreboard.getTeam(packet.getTeamName()) scoreboard.getTeam(packet.getTeamName())
.setName(MessageUtils.getBedrockMessage(packet.getDisplayName())) .setName(MessageUtils.getBedrockMessage(packet.getDisplayName()))
.setColor(packet.getColor())
.setPrefix(MessageUtils.getBedrockMessage(packet.getPrefix())) .setPrefix(MessageUtils.getBedrockMessage(packet.getPrefix()))
.setSuffix(MessageUtils.getBedrockMessage(packet.getSuffix())) .setSuffix(MessageUtils.getBedrockMessage(packet.getSuffix()))
.setUpdateType(UpdateType.UPDATE); .setUpdateType(UpdateType.UPDATE);

View file

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

View file

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