| 1 | package net.minecraft.client.resources;
|
| 2 |
|
| 3 | import java.io.BufferedReader;
|
| 4 | import java.io.IOException;
|
| 5 | import java.time.MonthDay;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Locale;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.Minecraft;
|
| 11 | import net.minecraft.client.User;
|
| 12 | import net.minecraft.client.gui.components.SplashRenderer;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.network.chat.Style;
|
| 15 | import net.minecraft.resources.Identifier;
|
| 16 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 17 | import net.minecraft.server.packs.resources.SimplePreparableReloadListener;
|
| 18 | import net.minecraft.util.RandomSource;
|
| 19 | import net.minecraft.util.SpecialDates;
|
| 20 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class SplashManager extends SimplePreparableReloadListener<List<Component>> {
|
| 25 | private static final Style DEFAULT_STYLE = Style.EMPTY.withColor(-256);
|
| 26 | public static final Component CHRISTMAS = literalSplash("Merry X-mas!");
|
| 27 | public static final Component NEW_YEAR = literalSplash("Happy new year!");
|
| 28 | public static final Component HALLOWEEN = literalSplash("OOoooOOOoooo! Spooky!");
|
| 29 | private static final Identifier SPLASHES_LOCATION = Identifier.withDefaultNamespace("texts/splashes.txt");
|
| 30 | private static final RandomSource RANDOM = RandomSource.create();
|
| 31 | private List<Component> splashes = List.of();
|
| 32 | private final User user;
|
| 33 |
|
| 34 | public SplashManager(final User user) {
|
| 35 | this.user = user;
|
| 36 | }
|
| 37 |
|
| 38 | private static Component literalSplash(final String text) {
|
| 39 | return Component.literal(text).setStyle(DEFAULT_STYLE);
|
| 40 | }
|
| 41 |
|
| 42 | protected List<Component> prepare(final ResourceManager manager, final ProfilerFiller profiler) {
|
| 43 | try (BufferedReader reader = Minecraft.getInstance().getResourceManager().openAsReader(SPLASHES_LOCATION)) {
|
| 44 | return reader.lines().map(String::trim).filter(line -> line.hashCode() != 125780783).map(SplashManager::literalSplash).toList();
|
| 45 | } catch (IOException ignored) {
|
| 46 | return List.of();
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | protected void apply(final List<Component> preparations, final ResourceManager manager, final ProfilerFiller profiler) {
|
| 51 | this.splashes = List.copyOf(preparations);
|
| 52 | }
|
| 53 |
|
| 54 | public @Nullable SplashRenderer getSplash() {
|
| 55 | MonthDay monthDay = SpecialDates.dayNow();
|
| 56 | if (monthDay.equals(SpecialDates.CHRISTMAS)) {
|
| 57 | return SplashRenderer.CHRISTMAS;
|
| 58 | } else if (monthDay.equals(SpecialDates.NEW_YEAR)) {
|
| 59 | return SplashRenderer.NEW_YEAR;
|
| 60 | } else if (monthDay.equals(SpecialDates.HALLOWEEN)) {
|
| 61 | return SplashRenderer.HALLOWEEN;
|
| 62 | } else if (this.splashes.isEmpty()) {
|
| 63 | return null;
|
| 64 | } else {
|
| 65 | return this.user != null && RANDOM.nextInt(this.splashes.size()) == 42
|
| 66 | ? new SplashRenderer(literalSplash(this.user.getName().toUpperCase(Locale.ROOT) + " IS YOU"))
|
| 67 | : new SplashRenderer(this.splashes.get(RANDOM.nextInt(this.splashes.size())));
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 |
|