Skip to content

Commit

Permalink
feat(wrapped): update to support multiple years!
Browse files Browse the repository at this point in the history
  • Loading branch information
ashhhleyyy committed Dec 21, 2024
1 parent 99bde9b commit a0fde1c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
20 changes: 11 additions & 9 deletions src/main/java/xyz/nucleoid/extras/NucleoidExtras.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,17 @@ private static void onServerStopped(MinecraftServer server) {

private static void onPlayerJoin(ServerPlayNetworkHandler handler, PacketSender sender, MinecraftServer server) {
Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.YEAR) == 2023 && calendar.get(Calendar.MONTH) == Calendar.DECEMBER) {
handler.getPlayer().sendMessage(
Text.translatable("text.nucleoid_extras.wrapped.join", handler.getPlayer().getUuidAsString())
.formatted(Formatting.GREEN)
.styled(style -> style.withClickEvent(new ClickEvent(
ClickEvent.Action.OPEN_URL,
"https://stats.nucleoid.xyz/players/" + handler.getPlayer().getUuidAsString() + "/wrapped"
)))
);
for (WrappedEvent event : NucleoidExtrasConfig.get().wrappedEvents()) {
if (event.isDuring(calendar)) {
handler.getPlayer().sendMessage(
Text.translatable("text.nucleoid_extras.wrapped.join", event.year())
.formatted(Formatting.GREEN)
.styled(style -> style.withClickEvent(new ClickEvent(
ClickEvent.Action.OPEN_URL,
"https://stats.nucleoid.xyz/players/" + handler.getPlayer().getUuidAsString() + "/wrapped?year=" + event.year()
)))
);
}
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/main/java/xyz/nucleoid/extras/NucleoidExtrasConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.Identifier;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import net.minecraft.util.Identifier;
import xyz.nucleoid.codecs.MoreCodecs;
import xyz.nucleoid.extras.chat_filter.ChatFilterConfig;
import xyz.nucleoid.extras.command.CommandAliasConfig;
Expand All @@ -27,6 +27,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

public record NucleoidExtrasConfig(
Expand All @@ -40,7 +42,8 @@ public record NucleoidExtrasConfig(
@Nullable URL contributorDataUrl,
ErrorReportingConfig errorReporting,
boolean devServer,
URI httpApi
URI httpApi,
List<WrappedEvent> wrappedEvents
) {
private static final Path PATH = Paths.get("config/nucleoid.json");

Expand All @@ -58,16 +61,17 @@ public record NucleoidExtrasConfig(
MoreCodecs.url("https").optionalFieldOf("contributor_data_url").forGetter(config -> Optional.ofNullable(config.contributorDataUrl())),
ErrorReportingConfig.CODEC.optionalFieldOf("error_reporting", ErrorReportingConfig.NONE).forGetter(NucleoidExtrasConfig::errorReporting),
Codec.BOOL.optionalFieldOf("dev_server", false).forGetter(NucleoidExtrasConfig::devServer),
ExtraCodecs.URI.optionalFieldOf("http_api").forGetter(config -> Optional.ofNullable(config.httpApi()))
).apply(instance, (sidebar, gamePortalOpener, lobbySpawn, integrations, aliases, filter, rules, contributorDataUrl, errorReporting, devServer, httpApiUrl) ->
new NucleoidExtrasConfig(sidebar, gamePortalOpener, lobbySpawn.orElse(null), integrations.orElse(null), aliases.orElse(null), filter.orElse(null), rules.orElse(null), contributorDataUrl.orElse(null), errorReporting, devServer, httpApiUrl.orElse(null))
ExtraCodecs.URI.optionalFieldOf("http_api").forGetter(config -> Optional.ofNullable(config.httpApi())),
WrappedEvent.CODEC.listOf().optionalFieldOf("wrapped_events", Collections.emptyList()).forGetter(NucleoidExtrasConfig::wrappedEvents)
).apply(instance, (sidebar, gamePortalOpener, lobbySpawn, integrations, aliases, filter, rules, contributorDataUrl, errorReporting, devServer, httpApiUrl, wrappedEvents) ->
new NucleoidExtrasConfig(sidebar, gamePortalOpener, lobbySpawn.orElse(null), integrations.orElse(null), aliases.orElse(null), filter.orElse(null), rules.orElse(null), contributorDataUrl.orElse(null), errorReporting, devServer, httpApiUrl.orElse(null), wrappedEvents)
)
);

private static NucleoidExtrasConfig instance;

private NucleoidExtrasConfig() {
this(false, Optional.empty(), null, null, null, null, null, null, ErrorReportingConfig.NONE, false, null);
this(false, Optional.empty(), null, null, null, null, null, null, ErrorReportingConfig.NONE, false, null, Collections.emptyList());
}

@NotNull
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/xyz/nucleoid/extras/WrappedEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package xyz.nucleoid.extras;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import java.util.Calendar;
import java.util.GregorianCalendar;

public record WrappedEvent(
int year,
GregorianCalendar start,
GregorianCalendar end
) {
@SuppressWarnings("MagicConstant")
public static final Codec<GregorianCalendar> CALENDAR_CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.fieldOf("year").forGetter(cal -> cal.get(Calendar.YEAR)),
Codec.INT.fieldOf("month").forGetter(cal -> cal.get(Calendar.MONTH) + 1),
Codec.INT.fieldOf("date").forGetter(cal -> cal.get(Calendar.DAY_OF_MONTH))
).apply(instance, (year, month, dayOfMonth) -> new GregorianCalendar(year, month - 1, dayOfMonth)));

public static final Codec<WrappedEvent> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.fieldOf("year").forGetter(WrappedEvent::year),
CALENDAR_CODEC.fieldOf("start").forGetter(WrappedEvent::start),
CALENDAR_CODEC.fieldOf("end").forGetter(WrappedEvent::end)
).apply(instance, WrappedEvent::new));

public boolean isDuring(Calendar cal) {
return cal.equals(this.start) || cal.equals(this.end) || (cal.after(this.start) && cal.before(this.end));
}
}
2 changes: 1 addition & 1 deletion src/main/resources/data/nucleoid_extras/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@

"text.nucleoid_extras.ui.action.more": "Right-click for more...",

"text.nucleoid_extras.wrapped.join": "Nucleoid Wrapped 2023 is here! Check out yours at https://stats.nucleoid.xyz/players/%s/wrapped",
"text.nucleoid_extras.wrapped.join": "Nucleoid Wrapped %s is here! Click here to open our website and see yours!",

"text.nucleoid_extras.rules.continued_title": "%s (cont.)",
"text.nucleoid_extras.rules.welcome": "Welcome to %s!\n\nWe're building open-source minigames.",
Expand Down

0 comments on commit a0fde1c

Please sign in to comment.