Skip to content

Commit

Permalink
Fix: make sure that donation scoreboard updates on server start / con…
Browse files Browse the repository at this point in the history
…nection restored
  • Loading branch information
Gegy committed Oct 30, 2023
1 parent 87f1c20 commit 3ee9771
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import net.minecraft.server.MinecraftServer;

public interface DonationListener {
void handleDonation(MinecraftServer server, String name, double amount, DonationState state);
void handleDonation(MinecraftServer server, String name, double amount);
}
23 changes: 19 additions & 4 deletions src/main/java/com/lovetropics/donations/DonationListeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

public class DonationListeners {
private static final Set<DonationListener> LISTENERS = new ReferenceOpenHashSet<>();
private static final Set<DonationStateListener> STATE_LISTENERS = new ReferenceOpenHashSet<>();

static {
LISTENERS.add(DonationListeners::announceDonation);
LISTENERS.add(new DonationScoreboard());
STATE_LISTENERS.add(new DonationScoreboard());
}

private static void announceDonation(final MinecraftServer server, final String name, final double amount, final DonationState state) {
private static void announceDonation(final MinecraftServer server, final String name, final double amount) {
if (name.isBlank()) {
return;
}
Expand All @@ -27,17 +28,31 @@ private static void announceDonation(final MinecraftServer server, final String
}
}

public static void triggerDonation(final MinecraftServer server, final String name, final double amount, final DonationState state) {
public static void updateState(final MinecraftServer server, final DonationState state, final boolean initial) {
for (final DonationStateListener listener : STATE_LISTENERS) {
listener.handleState(server, state, initial);
}
}

public static void triggerDonation(final MinecraftServer server, final String name, final double amount) {
for (final DonationListener listener : LISTENERS) {
listener.handleDonation(server, name, amount, state);
listener.handleDonation(server, name, amount);
}
}

public static void register(final DonationListener listener) {
LISTENERS.add(listener);
}

public static void register(final DonationStateListener listener) {
STATE_LISTENERS.add(listener);
}

public static void unregister(final DonationListener listener) {
LISTENERS.remove(listener);
}

public static void unregister(final DonationStateListener listener) {
STATE_LISTENERS.remove(listener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import net.minecraft.world.scores.Objective;
import net.minecraft.world.scores.criteria.ObjectiveCriteria;

public class DonationScoreboard implements DonationListener {
public class DonationScoreboard implements DonationStateListener {
private static final DonationGroup[] DONATION_GROUPS = DonationGroup.values();
private static final String TOTALS_OBJECTIVE = "donations.totals";
private static final String COUNTS_OBJECTIVE = "donations.counts";

@Override
public void handleDonation(final MinecraftServer server, final String name, final double amount, final DonationState state) {
public void handleState(final MinecraftServer server, final DonationState state, final boolean initial) {
updateScoreboard(server.getScoreboard(), state);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.lovetropics.donations;

import net.minecraft.server.MinecraftServer;

public interface DonationStateListener {
void handleState(MinecraftServer server, DonationState state, boolean initial);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void tick(final TickEvent.ServerTickEvent event) {
final Donation donation = DONATION_QUEUE.poll();
if (donation != null) {
applyFullState(server, donation.fullState(), false);
DonationListeners.triggerDonation(server, donation.getNameShown(), donation.amount(), STATE);
DonationListeners.triggerDonation(server, donation.getNameShown(), donation.amount());
nextDonationPollTick = tick + TICKS_BEFORE_POLL;
}
}
Expand Down Expand Up @@ -99,8 +99,7 @@ public static void initialize(final MinecraftServer server) {

private static void applyFullState(final MinecraftServer server, final FullDonationState data, final boolean initial) {
STATE.apply(data);

MonumentManager.get(server).update(STATE, initial);
DonationListeners.updateState(server, STATE, initial);
}

public static void close(final MinecraftServer server) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setLevel(final Level level) {

public void monitorListener() {
if (this.activeListener == null) {
DonationListener listener = (server, name, amount, totals) -> this.triggerDonation(amount);
DonationListener listener = (server, name, amount) -> this.triggerDonation(amount);
DonationListeners.register(listener);
this.activeListener = listener;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.lovetropics.donations.DonationGroup;
import com.lovetropics.donations.DonationLangKeys;
import com.lovetropics.donations.DonationListeners;
import com.lovetropics.donations.LTDonations;
import com.lovetropics.donations.backend.ltts.DonationRequests;
import com.lovetropics.donations.backend.ltts.json.WhitelistEvent;
import com.lovetropics.donations.monument.MonumentData;
Expand Down Expand Up @@ -128,7 +127,7 @@ public static int simulate(CommandContext<CommandSourceStack> ctx, String name,
if (!name.isEmpty()) {
ctx.getSource().sendSuccess(() -> DonationLangKeys.COMMAND_SIMULATE_DONATION.format(name, NumberFormat.getCurrencyInstance().format(amount)), true);
}
DonationListeners.triggerDonation(ctx.getSource().getServer(), name, amount, LTDonations.state());
DonationListeners.triggerDonation(ctx.getSource().getServer(), name, amount);
return Command.SINGLE_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.lovetropics.donations.monument;

import com.lovetropics.donations.DonationListener;
import com.lovetropics.donations.DonationState;
import com.lovetropics.donations.DonationStateListener;
import com.lovetropics.donations.LTDonations;
import com.mojang.serialization.Codec;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
Expand All @@ -15,7 +15,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MonumentManager extends SavedData implements DonationListener {
public class MonumentManager extends SavedData implements DonationStateListener {
private static final String STORAGE_ID = LTDonations.MODID + "_monuments";

private static final Codec<Map<String, MonumentData>> CODEC = Codec.unboundedMap(Codec.STRING, MonumentData.CODEC);
Expand Down Expand Up @@ -44,8 +44,8 @@ public CompoundTag save(final CompoundTag tag) {
}

@Override
public void handleDonation(final MinecraftServer server, final String name, final double amount, final DonationState state) {
update(state, false);
public void handleState(final MinecraftServer server, final DonationState state, final boolean initial) {
update(state, initial);
}

public void update(final DonationState state, final boolean fast) {
Expand Down

0 comments on commit 3ee9771

Please sign in to comment.