213 lines
7.6 KiB
Java
213 lines
7.6 KiB
Java
package net.minecraft.server;
|
|
|
|
import net.minecraft.server.level.ServerPlayer;
|
|
import java.util.Iterator;
|
|
import com.google.common.collect.Lists;
|
|
import java.util.List;
|
|
import net.minecraft.network.protocol.game.ClientboundSetObjectivePacket;
|
|
import java.util.Collection;
|
|
import net.minecraft.network.protocol.game.ClientboundSetPlayerTeamPacket;
|
|
import java.util.Arrays;
|
|
import net.minecraft.world.scores.PlayerTeam;
|
|
import net.minecraft.network.protocol.game.ClientboundSetDisplayObjectivePacket;
|
|
import javax.annotation.Nullable;
|
|
import net.minecraft.network.protocol.Packet;
|
|
import net.minecraft.network.protocol.game.ClientboundSetScorePacket;
|
|
import net.minecraft.world.scores.Score;
|
|
import com.google.common.collect.Sets;
|
|
import net.minecraft.world.scores.Objective;
|
|
import java.util.Set;
|
|
import net.minecraft.world.scores.Scoreboard;
|
|
|
|
public class ServerScoreboard extends Scoreboard {
|
|
private final MinecraftServer server;
|
|
private final Set<Objective> trackedObjectives;
|
|
private Runnable[] dirtyListeners;
|
|
|
|
public ServerScoreboard(final MinecraftServer minecraftServer) {
|
|
this.trackedObjectives = Sets.newHashSet();
|
|
this.dirtyListeners = new Runnable[0];
|
|
this.server = minecraftServer;
|
|
}
|
|
|
|
@Override
|
|
public void onScoreChanged(final Score cwh) {
|
|
super.onScoreChanged(cwh);
|
|
if (this.trackedObjectives.contains(cwh.getObjective())) {
|
|
this.server.getPlayerList().broadcastAll(new ClientboundSetScorePacket(Method.CHANGE, cwh.getObjective().getName(), cwh.getOwner(), cwh.getScore()));
|
|
}
|
|
this.setDirty();
|
|
}
|
|
|
|
@Override
|
|
public void onPlayerRemoved(final String string) {
|
|
super.onPlayerRemoved(string);
|
|
this.server.getPlayerList().broadcastAll(new ClientboundSetScorePacket(Method.REMOVE, null, string, 0));
|
|
this.setDirty();
|
|
}
|
|
|
|
@Override
|
|
public void onPlayerScoreRemoved(final String string, final Objective cwf) {
|
|
super.onPlayerScoreRemoved(string, cwf);
|
|
if (this.trackedObjectives.contains(cwf)) {
|
|
this.server.getPlayerList().broadcastAll(new ClientboundSetScorePacket(Method.REMOVE, cwf.getName(), string, 0));
|
|
}
|
|
this.setDirty();
|
|
}
|
|
|
|
@Override
|
|
public void setDisplayObjective(final int integer, @Nullable final Objective cwf) {
|
|
final Objective cwf2 = this.getDisplayObjective(integer);
|
|
super.setDisplayObjective(integer, cwf);
|
|
if (cwf2 != cwf && cwf2 != null) {
|
|
if (this.getObjectiveDisplaySlotCount(cwf2) > 0) {
|
|
this.server.getPlayerList().broadcastAll(new ClientboundSetDisplayObjectivePacket(integer, cwf));
|
|
}
|
|
else {
|
|
this.stopTrackingObjective(cwf2);
|
|
}
|
|
}
|
|
if (cwf != null) {
|
|
if (this.trackedObjectives.contains(cwf)) {
|
|
this.server.getPlayerList().broadcastAll(new ClientboundSetDisplayObjectivePacket(integer, cwf));
|
|
}
|
|
else {
|
|
this.startTrackingObjective(cwf);
|
|
}
|
|
}
|
|
this.setDirty();
|
|
}
|
|
|
|
@Override
|
|
public boolean addPlayerToTeam(final String string, final PlayerTeam cwg) {
|
|
if (super.addPlayerToTeam(string, cwg)) {
|
|
this.server.getPlayerList().broadcastAll(new ClientboundSetPlayerTeamPacket(cwg, Arrays.<String>asList(string), 3));
|
|
this.setDirty();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void removePlayerFromTeam(final String string, final PlayerTeam cwg) {
|
|
super.removePlayerFromTeam(string, cwg);
|
|
this.server.getPlayerList().broadcastAll(new ClientboundSetPlayerTeamPacket(cwg, Arrays.<String>asList(string), 4));
|
|
this.setDirty();
|
|
}
|
|
|
|
@Override
|
|
public void onObjectiveAdded(final Objective cwf) {
|
|
super.onObjectiveAdded(cwf);
|
|
this.setDirty();
|
|
}
|
|
|
|
@Override
|
|
public void onObjectiveChanged(final Objective cwf) {
|
|
super.onObjectiveChanged(cwf);
|
|
if (this.trackedObjectives.contains(cwf)) {
|
|
this.server.getPlayerList().broadcastAll(new ClientboundSetObjectivePacket(cwf, 2));
|
|
}
|
|
this.setDirty();
|
|
}
|
|
|
|
@Override
|
|
public void onObjectiveRemoved(final Objective cwf) {
|
|
super.onObjectiveRemoved(cwf);
|
|
if (this.trackedObjectives.contains(cwf)) {
|
|
this.stopTrackingObjective(cwf);
|
|
}
|
|
this.setDirty();
|
|
}
|
|
|
|
@Override
|
|
public void onTeamAdded(final PlayerTeam cwg) {
|
|
super.onTeamAdded(cwg);
|
|
this.server.getPlayerList().broadcastAll(new ClientboundSetPlayerTeamPacket(cwg, 0));
|
|
this.setDirty();
|
|
}
|
|
|
|
@Override
|
|
public void onTeamChanged(final PlayerTeam cwg) {
|
|
super.onTeamChanged(cwg);
|
|
this.server.getPlayerList().broadcastAll(new ClientboundSetPlayerTeamPacket(cwg, 2));
|
|
this.setDirty();
|
|
}
|
|
|
|
@Override
|
|
public void onTeamRemoved(final PlayerTeam cwg) {
|
|
super.onTeamRemoved(cwg);
|
|
this.server.getPlayerList().broadcastAll(new ClientboundSetPlayerTeamPacket(cwg, 1));
|
|
this.setDirty();
|
|
}
|
|
|
|
public void addDirtyListener(final Runnable runnable) {
|
|
(this.dirtyListeners = Arrays.<Runnable>copyOf(this.dirtyListeners, this.dirtyListeners.length + 1))[this.dirtyListeners.length - 1] = runnable;
|
|
}
|
|
|
|
protected void setDirty() {
|
|
for (final Runnable runnable5 : this.dirtyListeners) {
|
|
runnable5.run();
|
|
}
|
|
}
|
|
|
|
public List<Packet<?>> getStartTrackingPackets(final Objective cwf) {
|
|
final List<Packet<?>> list3 = Lists.newArrayList();
|
|
list3.add(new ClientboundSetObjectivePacket(cwf, 0));
|
|
for (int integer4 = 0; integer4 < 19; ++integer4) {
|
|
if (this.getDisplayObjective(integer4) == cwf) {
|
|
list3.add(new ClientboundSetDisplayObjectivePacket(integer4, cwf));
|
|
}
|
|
}
|
|
for (final Score cwh5 : this.getPlayerScores(cwf)) {
|
|
list3.add(new ClientboundSetScorePacket(Method.CHANGE, cwh5.getObjective().getName(), cwh5.getOwner(), cwh5.getScore()));
|
|
}
|
|
return list3;
|
|
}
|
|
|
|
public void startTrackingObjective(final Objective cwf) {
|
|
final List<Packet<?>> list3 = this.getStartTrackingPackets(cwf);
|
|
for (final ServerPlayer xe5 : this.server.getPlayerList().getPlayers()) {
|
|
for (final Packet<?> lt7 : list3) {
|
|
xe5.connection.send(lt7);
|
|
}
|
|
}
|
|
this.trackedObjectives.add(cwf);
|
|
}
|
|
|
|
public List<Packet<?>> getStopTrackingPackets(final Objective cwf) {
|
|
final List<Packet<?>> list3 = Lists.newArrayList();
|
|
list3.add(new ClientboundSetObjectivePacket(cwf, 1));
|
|
for (int integer4 = 0; integer4 < 19; ++integer4) {
|
|
if (this.getDisplayObjective(integer4) == cwf) {
|
|
list3.add(new ClientboundSetDisplayObjectivePacket(integer4, cwf));
|
|
}
|
|
}
|
|
return list3;
|
|
}
|
|
|
|
public void stopTrackingObjective(final Objective cwf) {
|
|
final List<Packet<?>> list3 = this.getStopTrackingPackets(cwf);
|
|
for (final ServerPlayer xe5 : this.server.getPlayerList().getPlayers()) {
|
|
for (final Packet<?> lt7 : list3) {
|
|
xe5.connection.send(lt7);
|
|
}
|
|
}
|
|
this.trackedObjectives.remove(cwf);
|
|
}
|
|
|
|
public int getObjectiveDisplaySlotCount(final Objective cwf) {
|
|
int integer3 = 0;
|
|
for (int integer4 = 0; integer4 < 19; ++integer4) {
|
|
if (this.getDisplayObjective(integer4) == cwf) {
|
|
++integer3;
|
|
}
|
|
}
|
|
return integer3;
|
|
}
|
|
|
|
public enum Method {
|
|
CHANGE,
|
|
REMOVE;
|
|
}
|
|
}
|