Skip to content

Commit

Permalink
Fix: don't use broken MoreCodecs.sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Oct 24, 2023
1 parent 5012146 commit 44588f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/lovetropics/donations/TopDonorManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.lovetropics.donations.backend.ltts.DonationRequests;
import com.lovetropics.donations.backend.ltts.json.TopDonor;
import com.mojang.logging.LogUtils;
import net.minecraft.ChatFormatting;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -13,16 +14,15 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraftforge.server.ServerLifecycleHooks;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;

import javax.annotation.Nullable;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public final class TopDonorManager {
private static final Logger LOGGER = LogManager.getLogger(TopDonorManager.class);
private static final Logger LOGGER = LogUtils.getLogger();

private UUID[] lastEntityUuids;

Expand All @@ -35,6 +35,7 @@ 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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import com.lovetropics.donations.backend.ltts.json.FullDonationState;
import com.lovetropics.donations.backend.ltts.json.TopDonor;
import com.lovetropics.donations.backend.ltts.json.WhitelistEvent;
import com.lovetropics.lib.codec.MoreCodecs;
import com.mojang.serialization.Codec;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Supplier;
Expand All @@ -22,10 +22,15 @@ public class DonationRequests {
);

private static final Codec<List<WhitelistEvent>> WHITELIST_EVENTS_CODEC = WhitelistEvent.CODEC.listOf().fieldOf("events").codec();
private static final Codec<List<TopDonor>> TOP_DONORS_CODEC = MoreCodecs.sorted(
TopDonor.CODEC.listOf().fieldOf("donors").codec(),
Comparator.comparingDouble(TopDonor::total).reversed()
);
private static final Comparator<TopDonor> TOP_DONOR_COMPARATOR = Comparator.comparingDouble(TopDonor::total).reversed();
private static final Codec<List<TopDonor>> TOP_DONORS_CODEC = TopDonor.CODEC.listOf().fieldOf("donors").codec().xmap(
topDonors -> {
final List<TopDonor> sorted = new ArrayList<>(topDonors);
sorted.sort(TOP_DONOR_COMPARATOR);
return List.copyOf(sorted);
},
topDonors -> topDonors
);

private final RequestHelper requests;

Expand Down

0 comments on commit 44588f4

Please sign in to comment.