Skip to content

Commit

Permalink
starting scoreboard fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
Faithcaio committed Nov 25, 2023
1 parent ef06dd8 commit 88ee0dd
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 294 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
*/
package org.spongepowered.common.accessor.world.scores;

import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
import net.minecraft.world.scores.DisplaySlot;
import net.minecraft.world.scores.Objective;
import net.minecraft.world.scores.PlayerScores;
import net.minecraft.world.scores.PlayerTeam;
import net.minecraft.world.scores.Score;
import net.minecraft.world.scores.Scoreboard;
import net.minecraft.world.scores.criteria.ObjectiveCriteria;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -39,16 +41,16 @@
@Mixin(Scoreboard.class)
public interface ScoreboardAccessor {

@Accessor("objectivesByName") Map<String, Objective> accessor$objectivesByName();
@Accessor("objectivesByName") Object2ObjectMap<String, Objective> accessor$objectivesByName();

@Accessor("objectivesByCriteria") Map<ObjectiveCriteria, List<Objective>> accessor$objectivesByCriteria();
@Accessor("objectivesByCriteria") Reference2ObjectMap<ObjectiveCriteria, List<Objective>> accessor$objectivesByCriteria();

@Accessor("playerScores") Map<String, Map<Objective, Score>> accessor$playerScores();
@Accessor("playerScores") Map<String, PlayerScores> accessor$playerScores();

@Accessor("displayObjectives") Map<DisplaySlot, Objective> accessor$displayObjectives();

@Accessor("teamsByName") Map<String, PlayerTeam> accessor$teamsByName();
@Accessor("teamsByName") Object2ObjectMap<String, PlayerTeam> accessor$teamsByName();

@Accessor("teamsByPlayer") Map<String, PlayerTeam> accessor$teamsByPlayer();
@Accessor("teamsByPlayer") Object2ObjectMap<String, PlayerTeam> accessor$teamsByPlayer();

}
2 changes: 0 additions & 2 deletions src/accessors/resources/mixins.sponge.accessors.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@
"world.level.storage.LevelStorageSource_LevelStorageAccessAccessor",
"world.level.storage.PlayerDataStorageAccessor",
"world.phys.AABBAccessor",
"world.scores.ObjectiveAccessor",
"world.scores.PlayerTeamAccessor",
"world.scores.ScoreAccessor",
"world.entity.decoration.PaintingAccessor",
"world.scores.ScoreboardAccessor",
"world.level.biome.MobSpawnSettingsAccessor",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.google.common.collect.Maps;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.minecraft.network.chat.numbers.NumberFormat;
import net.minecraft.world.scores.ScoreAccess;
import net.minecraft.world.scores.ScoreHolder;
import net.minecraft.world.scores.criteria.ObjectiveCriteria;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.scoreboard.Score;
Expand All @@ -35,8 +38,6 @@
import org.spongepowered.api.scoreboard.objective.Objective;
import org.spongepowered.api.scoreboard.objective.displaymode.ObjectiveDisplayMode;
import org.spongepowered.api.scoreboard.objective.displaymode.ObjectiveDisplayModes;
import org.spongepowered.common.accessor.world.scores.ObjectiveAccessor;
import org.spongepowered.common.accessor.world.scores.ScoreAccessor;
import org.spongepowered.common.accessor.world.scores.ScoreboardAccessor;
import org.spongepowered.common.adventure.SpongeAdventure;
import org.spongepowered.common.bridge.world.scores.ObjectiveBridge;
Expand All @@ -55,11 +56,13 @@ public final class SpongeObjective implements Objective {

private final String name;
private final Criterion criterion;
private final Map<Component, Score> scores = new HashMap<>();
private final Map<String, Score> scores = new HashMap<>();
private final Map<net.minecraft.world.scores.Scoreboard, net.minecraft.world.scores.Objective> objectives;

private Component displayName;
private ObjectiveDisplayMode displayMode;
private boolean displayAutoUpdate; // TODO make this do stuff
private NumberFormat numberFormat; // TODO make this do stuff

public SpongeObjective(final String name, final Criterion criterion) {
this.name = name;
Expand Down Expand Up @@ -111,36 +114,40 @@ public void storeDisplayMode(final ObjectiveDisplayMode displayMode) {
}

@Override
public Map<Component, Score> scores() {
public Map<String, Score> scores() {
return new HashMap<>(this.scores);
}

@Override
public boolean hasScore(final Component name) {
public boolean hasScore(final String name) {
return this.scores.containsKey(name);
}

@Override
public void addScore(final Score score) throws IllegalArgumentException {
if (this.scores.containsKey(score.name())) {
throw new IllegalArgumentException(String.format("A score with the name %s already exists!",
LegacyComponentSerializer.legacySection().serialize(score.name())));
throw new IllegalArgumentException(String.format("A score with the name %s already exists!", score.name()));
}
this.scores.put(score.name(), score);

final SpongeScore spongeScore = (SpongeScore) score;
for (final net.minecraft.world.scores.Objective objective: this.objectives.values()) {
this.addScoreToScoreboard(((ObjectiveAccessor) objective).accessor$scoreboard(), spongeScore.getScoreFor(objective));
for (final net.minecraft.world.scores.Scoreboard scoreboard : this.objectives.keySet()) {
final net.minecraft.world.scores.Objective mcObjective = scoreboard.getObjective(this.name);
for (final ScoreHolder holder : scoreboard.getTrackedPlayers()) {
final ScoreAccess accessor = scoreboard.getOrCreatePlayerScore(holder, mcObjective);
spongeScore.registerAndUpdate(mcObjective, accessor);
// TODO set values
}
}
}

@Override
public Optional<Score> findScore(final Component name) {
public Optional<Score> findScore(final String name) {
return Optional.ofNullable(this.scores.get(name));
}

@Override
public Score findOrCreateScore(final Component name) {
public Score findOrCreateScore(final String name) {
if (this.scores.containsKey(name)) {
return this.scores.get(name);
}
Expand All @@ -152,41 +159,41 @@ public Score findOrCreateScore(final Component name) {

@Override
public boolean removeScore(final Score spongeScore) {
final String name = ((SpongeScore) spongeScore).legacyName;
final ScoreHolder holder = ((SpongeScore) spongeScore).holder;

if (!this.scores.containsKey(spongeScore.name())) {
return false;
}

for (final net.minecraft.world.scores.Objective objective: this.objectives.values()) {
final net.minecraft.world.scores.Scoreboard scoreboard = ((ObjectiveAccessor) objective).accessor$scoreboard();
final net.minecraft.world.scores.Scoreboard scoreboard = objective.getScoreboard();


final Map<?, ?> map = ((ScoreboardAccessor) scoreboard).accessor$playerScores().get(name);
final Map<?, ?> map = ((ScoreboardAccessor) scoreboard).accessor$playerScores().get(holder);

if (map != null) {
final net.minecraft.world.scores.Score score = (net.minecraft.world.scores.Score) map.remove(objective);


if (map.size() < 1) {
final Map<?, ?> map1 = ((ScoreboardAccessor) scoreboard).accessor$playerScores().remove(name);
final Map<?, ?> map1 = ((ScoreboardAccessor) scoreboard).accessor$playerScores().remove(holder);

if (map1 != null) {
scoreboard.onPlayerRemoved(name);
scoreboard.onPlayerRemoved(holder);
}
} else if (score != null) {
scoreboard.onPlayerScoreRemoved(name, objective);
scoreboard.onPlayerScoreRemoved(holder, objective);
}
}
((SpongeScore) spongeScore).removeScoreFor(objective);
((SpongeScore) spongeScore).unregister(objective);
}

this.scores.remove(spongeScore.name());
return true;
}

@Override
public boolean removeScore(final Component name) {
public boolean removeScore(final String name) {
final Optional<Score> score = this.findScore(name);
return score.filter(this::removeScore).isPresent();
}
Expand All @@ -211,6 +218,12 @@ private void updateDisplayName() {
public void updateScores(final net.minecraft.world.scores.Scoreboard scoreboard) {
final net.minecraft.world.scores.Objective objective = this.getObjectiveFor(scoreboard);

for (final ScoreHolder holder : scoreboard.getTrackedPlayers()) {
// TODO probably need to go through PlayerScores instead
final ScoreAccess access = scoreboard.getOrCreatePlayerScore(holder, objective);

}

for (final Score score: this.scores().values()) {
final SpongeScore spongeScore = (SpongeScore) score;
this.addScoreToScoreboard(scoreboard, spongeScore.getScoreFor(objective));
Expand All @@ -234,7 +247,7 @@ public net.minecraft.world.scores.Objective getObjectiveFor(final net.minecraft.
return this.objectives.get(scoreboard);
}
final net.minecraft.world.scores.Objective objective = new net.minecraft.world.scores.Objective(scoreboard, this.name, (ObjectiveCriteria) this.criterion,
SpongeAdventure.asVanilla(this.displayName), (ObjectiveCriteria.RenderType) (Object) this.displayMode);
SpongeAdventure.asVanilla(this.displayName), (ObjectiveCriteria.RenderType) (Object) this.displayMode, this.displayAutoUpdate, this.numberFormat);
((ObjectiveBridge) objective).bridge$setSpongeObjective(this);
this.objectives.put(scoreboard, objective);
return objective;
Expand All @@ -250,6 +263,22 @@ public Collection<net.minecraft.world.scores.Objective> getObjectives() {
return this.objectives.values();
}

public boolean displayAutoUpdate() {
return displayAutoUpdate;
}

public NumberFormat numberFormat() {
return numberFormat;
}

public void setDisplayAutoUpdate(final boolean displayAutoUpdate) {
this.displayAutoUpdate = displayAutoUpdate;
}

public void setNumberFormat(final NumberFormat numberFormat) {
this.numberFormat = numberFormat;
}

public static final class Builder implements Objective.Builder {

private static final int MAX_NAME_LENGTH = 16;
Expand Down
Loading

0 comments on commit 88ee0dd

Please sign in to comment.