| 1 | package net.minecraft.server.bossevents;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.UUID;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import net.minecraft.resources.Identifier;
|
| 10 | import net.minecraft.server.level.ServerPlayer;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.util.Util;
|
| 14 | import net.minecraft.util.datafix.DataFixTypes;
|
| 15 | import net.minecraft.world.level.saveddata.SavedData;
|
| 16 | import net.minecraft.world.level.saveddata.SavedDataType;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | public class CustomBossEvents extends SavedData {
|
| 20 | private static final Codec<Map<Identifier, CustomBossEvent.Packed>> EVENTS_CODEC = Codec.unboundedMap(Identifier.CODEC, CustomBossEvent.Packed.CODEC);
|
| 21 | private static final Codec<CustomBossEvents> CODEC = EVENTS_CODEC.xmap(events -> {
|
| 22 | CustomBossEvents r = new CustomBossEvents();
|
| 23 | events.forEach((id, packed) -> r.events.put(id, CustomBossEvent.load(UUID.randomUUID(), id, packed, r::setDirty)));
|
| 24 | return r;
|
| 25 | }, c -> Util.mapValues(c.events, CustomBossEvent::pack));
|
| 26 | public static final SavedDataType<CustomBossEvents> TYPE = new SavedDataType<>(
|
| 27 | Identifier.withDefaultNamespace("custom_boss_events"), CustomBossEvents::new, CODEC, DataFixTypes.SAVED_DATA_CUSTOM_BOSS_EVENTS
|
| 28 | );
|
| 29 | private final Map<Identifier, CustomBossEvent> events = Maps.newHashMap();
|
| 30 |
|
| 31 | public @Nullable CustomBossEvent get(final Identifier id) {
|
| 32 | return this.events.get(id);
|
| 33 | }
|
| 34 |
|
| 35 | public CustomBossEvent create(final RandomSource random, final Identifier id, final Component name) {
|
| 36 | CustomBossEvent result = new CustomBossEvent(Mth.createInsecureUUID(random), id, name, this::setDirty);
|
| 37 | this.events.put(id, result);
|
| 38 | this.setDirty();
|
| 39 | return result;
|
| 40 | }
|
| 41 |
|
| 42 | public void remove(final CustomBossEvent event) {
|
| 43 | if (this.events.remove(event.customId()) != null) {
|
| 44 | this.setDirty();
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | public Collection<Identifier> getIds() {
|
| 49 | return this.events.keySet();
|
| 50 | }
|
| 51 |
|
| 52 | public Collection<CustomBossEvent> getEvents() {
|
| 53 | return this.events.values();
|
| 54 | }
|
| 55 |
|
| 56 | public void onPlayerConnect(final ServerPlayer player) {
|
| 57 | for (CustomBossEvent event : this.events.values()) {
|
| 58 | event.onPlayerConnect(player);
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | public void onPlayerDisconnect(final ServerPlayer player) {
|
| 63 | for (CustomBossEvent event : this.events.values()) {
|
| 64 | event.onPlayerDisconnect(player);
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|