|
| 1 | +package pl.olafcio.avoid_impl.net.advancement; |
| 2 | + |
| 3 | +import net.minecraft.advancements.*; |
| 4 | +import net.minecraft.advancements.criterion.ImpossibleTrigger; |
| 5 | +import net.minecraft.core.ClientAsset; |
| 6 | +import net.minecraft.core.registries.BuiltInRegistries; |
| 7 | +import net.minecraft.resources.Identifier; |
| 8 | +import org.jetbrains.annotations.ApiStatus; |
| 9 | +import org.jetbrains.annotations.NotNull; |
| 10 | +import pl.olafcio.avoid.net.advancement.AdvancementType; |
| 11 | +import pl.olafcio.avoid.net.advancement.properties._display; |
| 12 | +import pl.olafcio.avoid.net.advancement.properties._parent; |
| 13 | +import pl.olafcio.avoid.net.advancement.properties._type; |
| 14 | +import pl.olafcio.avoid.net.chat.component.Components; |
| 15 | +import pl.olafcio.avoid.net.id.Identification; |
| 16 | +import pl.olafcio.avoid_impl.net.chat.converter.COToNative; |
| 17 | +import pl.olafcio.avoid_impl.net.id.IdentificationNative; |
| 18 | + |
| 19 | +import java.lang.reflect.Constructor; |
| 20 | +import java.lang.reflect.InvocationTargetException; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.function.Supplier; |
| 26 | + |
| 27 | +@ApiStatus.Experimental |
| 28 | +public final class Advancements { |
| 29 | + @ApiStatus.Internal |
| 30 | + private Advancements() {} |
| 31 | + |
| 32 | + public static final HashMap<Identifier, Advancement> ADVANCEMENTS |
| 33 | + = new HashMap<>(); |
| 34 | + |
| 35 | + public static void register(Identification id, Class<? extends pl.olafcio.avoid.net.advancement.Advancement> advancement) { |
| 36 | + var display = advancement.getDeclaredAnnotation(_display.class); |
| 37 | + var type = advancement.getDeclaredAnnotation(_type.class) |
| 38 | + .value(); |
| 39 | + |
| 40 | + ADVANCEMENTS.put(IdentificationNative.convert(id), new Advancement( |
| 41 | + advancement.isAnnotationPresent(_parent.class) |
| 42 | + ? IdentificationNative.convertOptional( |
| 43 | + advancement.getDeclaredAnnotation(_parent.class) |
| 44 | + .id() |
| 45 | + ) |
| 46 | + : Optional.empty(), |
| 47 | + Optional.of(new DisplayInfo( |
| 48 | + BuiltInRegistries.ITEM.get(IdentificationNative.convert(display.icon())).orElseThrow().value().getDefaultInstance(), |
| 49 | + COToNative.from(Components.Parse.minimessage(display.title())), |
| 50 | + COToNative.from(Components.Parse.minimessage(display.description())), |
| 51 | + IdentificationNative.convertOptional(display.background()) |
| 52 | + .map(ClientAsset.ResourceTexture::new), |
| 53 | + type == AdvancementType.GOAL ? net.minecraft.advancements.AdvancementType.GOAL : |
| 54 | + type == AdvancementType.TASK ? net.minecraft.advancements.AdvancementType.TASK : |
| 55 | + net.minecraft.advancements.AdvancementType.CHALLENGE, |
| 56 | + display.showToast(), |
| 57 | + display.announceChat(), |
| 58 | + display.hidden() |
| 59 | + )), |
| 60 | + AdvancementRewards.EMPTY, |
| 61 | + Map.of("toggle", new Criterion<>(CriteriaTriggers.IMPOSSIBLE, new ImpossibleTrigger.TriggerInstance())), |
| 62 | + AdvancementRequirements.anyOf(Collections.singleton("toggle")), |
| 63 | + false |
| 64 | + )); |
| 65 | + } |
| 66 | +} |
0 commit comments