Skip to content

Instantly share code, notes, and snippets.

@MineTheCube
Created September 9, 2017 00:18
Show Gist options
  • Save MineTheCube/d54b63de1bf1c486c938bd5b182f3e65 to your computer and use it in GitHub Desktop.
Save MineTheCube/d54b63de1bf1c486c938bd5b182f3e65 to your computer and use it in GitHub Desktop.
Create, update and remove scoreboard teams using packets
package fr.onecraft.packets;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import java.util.Collection;
public class TeamPacket {
public enum Action {
CREATE_TEAM,
REMOVE_TEAM,
UPDATE_TEAM,
ADD_PLAYERS,
REMOVE_PLAYERS;
public int getValue() {
return ordinal();
}
}
public enum NameTagVisibility {
ALWAYS,
HIDE_FOR_OTHER_TEAMS,
HIDE_FOR_OWN_TEAM,
NEVER;
private static final String[] values = { "always", "hideForOtherTeams", "hideForOwnTeam", "never" };
public String getValue() {
return values[ordinal()];
}
}
public enum CollisionRule {
ALWAYS,
PUSH_OTHER_TEAMS,
PUSH_OWN_TEAM,
NEVER;
private static final String[] values = { "always", "pushOtherTeams", "pushOwnTeam", "never" };
public String getValue() {
return values[ordinal()];
}
}
public static final int ALLOW_FRIENDLY_FIRE = 0x01;
public static final int CAN_SEE_INVISIBLE = 0x02;
public static PacketContainer createTeamPacket(TeamPacket team, Action action, Collection<String> players) {
PacketContainer packet = new PacketContainer(PacketType.Play.Server.SCOREBOARD_TEAM);
packet.getStrings().write(0, team.getName()); // Team Name
packet.getIntegers().write(1, action.getValue()); // Action (this is not first byte!)
if (action == Action.CREATE_TEAM || action == Action.UPDATE_TEAM) {
packet.getStrings().write(1, team.getDisplayName());
packet.getStrings().write(2, team.getPrefix());
packet.getStrings().write(3, team.getSuffix());
packet.getStrings().write(4, team.getNameTagVisibility().getValue());
packet.getStrings().write(5, team.getCollisionRule().getValue());
packet.getIntegers().write(0, team.getFriendlyFlags());
packet.getIntegers().write(2, team.getColor());
}
if (action == Action.CREATE_TEAM || action == Action.ADD_PLAYERS || action == Action.REMOVE_PLAYERS) {
packet.getSpecificModifier(Collection.class).write(0, players);
}
return packet;
}
private final String name;
private final String displayName;
private final String prefix;
private final String suffix;
private final int friendlyFlags;
private final NameTagVisibility nameTagVisibility;
private final CollisionRule collisionRule;
private final int color;
public TeamPacket(String name, String prefix, String suffix) {
this(name, name, prefix, suffix, 0, NameTagVisibility.ALWAYS, CollisionRule.ALWAYS, -1);
}
public TeamPacket(String name, String displayName, String prefix, String suffix, int friendlyFlags, NameTagVisibility nameTagVisibility, CollisionRule collisionRule, int color) {
this.name = name;
this.displayName = displayName;
this.prefix = prefix;
this.suffix = suffix;
this.friendlyFlags = friendlyFlags;
this.nameTagVisibility = nameTagVisibility;
this.collisionRule = collisionRule;
this.color = color;
}
public String getName() {
return name;
}
public String getDisplayName() {
return displayName;
}
public String getPrefix() {
return prefix;
}
public String getSuffix() {
return suffix;
}
public int getFriendlyFlags() {
return friendlyFlags;
}
public NameTagVisibility getNameTagVisibility() {
return nameTagVisibility;
}
public CollisionRule getCollisionRule() {
return collisionRule;
}
public int getColor() {
return color;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment