Skip to content

Commit

Permalink
Fix top donor state getting out of sync, send null profile ID to the …
Browse files Browse the repository at this point in the history
…datapack for anonymous players
  • Loading branch information
Gegy committed Nov 3, 2023
1 parent 8a11a00 commit 2fed8d8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.Util;
import net.minecraft.core.UUIDUtil;

import java.util.List;
Expand All @@ -15,4 +16,8 @@ public record TopDonor(UUID uuid, Optional<String> minecraftName, List<String> d
Codec.STRING.listOf().optionalFieldOf("display_names", List.of()).forGetter(TopDonor::displayNames),
Codec.DOUBLE.fieldOf("total").forGetter(TopDonor::total)
).apply(i, TopDonor::new));

public boolean isAnonymous() {
return uuid.equals(Util.NIL_UUID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.lovetropics.donations.backend.ltts.json.TopDonor;
import com.mojang.logging.LogUtils;
import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
Expand All @@ -25,8 +26,6 @@
public final class TopDonorManager {
private static final Logger LOGGER = LogUtils.getLogger();

private UUID[] lastEntityUuids;

public void pollTopDonors() {
UUID[] topDonorUuids = DonationConfigs.TOP_DONORS.getTopDonorUuids();

Expand All @@ -35,54 +34,49 @@ public void pollTopDonors() {
}

private void applyTopDonors(List<TopDonor> topDonors) {
UUID[] topDonorUuids = DonationConfigs.TOP_DONORS.getTopDonorUuids();
LOGGER.debug("Applying {} top donators to {} entities", topDonors.size(), topDonorUuids.length);

int newTopDonorLength = Math.min(topDonorUuids.length, topDonors.size());
UUID[] entityIds = DonationConfigs.TOP_DONORS.getTopDonorUuids();
LOGGER.debug("Applying {} top donators to {} entities", topDonors.size(), entityIds.length);

UUID[] entityIds = new UUID[newTopDonorLength];
System.arraycopy(topDonorUuids, 0, entityIds, 0, newTopDonorLength);

for (int i = 0; i < newTopDonorLength; i++) {
for (int i = 0; i < entityIds.length; i++) {
UUID entityId = entityIds[i];
TopDonor donor = topDonors.get(i);
List<String> fallbacks = donor.displayNames();
this.applyToEntity(entityId, donor.minecraftName().orElse(null), fallbacks.isEmpty() ? "Anonymous" : fallbacks.get(fallbacks.size() - 1), donor.total());
}
Entity entity = findEntity(entityId);
if (entity == null) {
continue;
}

UUID[] lastEntityUuids = this.lastEntityUuids;
if (lastEntityUuids != null && lastEntityUuids.length > entityIds.length) {
for (int i = entityIds.length; i < lastEntityUuids.length; i++) {
this.clearEntity(lastEntityUuids[i]);
if (i < topDonors.size()) {
TopDonor donor = topDonors.get(i);
List<String> fallbacks = donor.displayNames();
String minecraftName = donor.minecraftName().orElse(null);
String fallbackName = fallbacks.isEmpty() ? "Anonymous" : fallbacks.get(fallbacks.size() - 1);
applyToEntity(entity, minecraftName, Component.literal(fallbackName), donor.total(), donor.isAnonymous());
} else {
clearEntity(entity);
}
}

this.lastEntityUuids = entityIds;
}

private void applyToEntity(UUID entityId, @Nullable String minecraftName, String fallbackName, double total) {
Entity entity = this.findEntity(entityId);
if (entity == null) return;

private void applyToEntity(Entity entity, @Nullable String minecraftName, Component fallbackName, double total, boolean anonymous) {
CompoundTag data = entity.saveWithoutId(new CompoundTag());
if (minecraftName != null) {
if (anonymous) {
// We look for the null UUID in the datapack
data.putUUID("ProfileID", Util.NIL_UUID);
data.putString("CustomName", Component.Serializer.toJson(fallbackName));
} else if (minecraftName != null) {
data.remove("CustomName");
entity.setCustomName(null);
data.putString("ProfileName", minecraftName);
} else {
data.putString("ProfileName", "");
data.putString("CustomName", "{\"text\":\"" + fallbackName + "\"}");
data.putString("CustomName", Component.Serializer.toJson(fallbackName));
}
Component suffix = Component.literal(" - ").withStyle(ChatFormatting.GRAY)
.append(Component.literal(String.format("$%.2f", total)).withStyle(ChatFormatting.GREEN));
data.putString("NameSuffix", Component.Serializer.toJson(suffix));
entity.load(data);
}

private void clearEntity(UUID entityId) {
Entity entity = this.findEntity(entityId);
if (entity == null) return;

private void clearEntity(Entity entity) {
CompoundTag data = entity.saveWithoutId(new CompoundTag());
data.putString("CustomName", "{\"text\":\"A Future Donator\"}");
data.putString("ProfileName", "");
Expand Down

0 comments on commit 2fed8d8

Please sign in to comment.