From a313aa22c97cf16ae53c59a3df58856c4021fd50 Mon Sep 17 00:00:00 2001 From: Connor Hartley Date: Wed, 17 Aug 2022 02:59:04 +1200 Subject: [PATCH] support additional enderdragon phases added by mods fixing https://github.com/SpongePowered/Sponge/issues/3714 (#3737) --- .../phases/EnderDragonPhaseAccessor.java | 41 +++++++++++++++++++ .../resources/mixins.sponge.accessors.json | 1 + .../registry/VanillaRegistryLoader.java | 14 ++++--- 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/accessors/java/org/spongepowered/common/accessor/world/entity/boss/enderdragon/phases/EnderDragonPhaseAccessor.java diff --git a/src/accessors/java/org/spongepowered/common/accessor/world/entity/boss/enderdragon/phases/EnderDragonPhaseAccessor.java b/src/accessors/java/org/spongepowered/common/accessor/world/entity/boss/enderdragon/phases/EnderDragonPhaseAccessor.java new file mode 100644 index 00000000000..519b68c0d8b --- /dev/null +++ b/src/accessors/java/org/spongepowered/common/accessor/world/entity/boss/enderdragon/phases/EnderDragonPhaseAccessor.java @@ -0,0 +1,41 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.common.accessor.world.entity.boss.enderdragon.phases; + +import net.minecraft.world.entity.boss.enderdragon.phases.EnderDragonPhase; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; +import org.spongepowered.common.UntransformedAccessorError; + +@Mixin(EnderDragonPhase.class) +public interface EnderDragonPhaseAccessor { + + @Accessor("phases") static EnderDragonPhase[] accessor$PHASES() { + throw new UntransformedAccessorError(); + } + + @Accessor("name") String accessor$name(); + +} diff --git a/src/accessors/resources/mixins.sponge.accessors.json b/src/accessors/resources/mixins.sponge.accessors.json index 65c2aa6c15d..07d651977ce 100644 --- a/src/accessors/resources/mixins.sponge.accessors.json +++ b/src/accessors/resources/mixins.sponge.accessors.json @@ -94,6 +94,7 @@ "world.entity.animal.horse.HorseAccessor", "world.entity.animal.horse.LlamaAccessor", "world.entity.animal.horse.TraderLlamaAccessor", + "world.entity.boss.enderdragon.phases.EnderDragonPhaseAccessor", "world.entity.boss.wither.WitherBossAccessor", "world.entity.decoration.ArmorStandAccessor", "world.entity.decoration.HangingEntityAccessor", diff --git a/src/main/java/org/spongepowered/common/registry/VanillaRegistryLoader.java b/src/main/java/org/spongepowered/common/registry/VanillaRegistryLoader.java index 56afa69c2ac..7b2600d007c 100644 --- a/src/main/java/org/spongepowered/common/registry/VanillaRegistryLoader.java +++ b/src/main/java/org/spongepowered/common/registry/VanillaRegistryLoader.java @@ -86,6 +86,7 @@ import org.spongepowered.api.scoreboard.criteria.Criterion; import org.spongepowered.common.accessor.advancements.CriteriaTriggersAccessor; import org.spongepowered.common.accessor.world.entity.animal.MushroomCow_MushroomTypeAccessor; +import org.spongepowered.common.accessor.world.entity.boss.enderdragon.phases.EnderDragonPhaseAccessor; import org.spongepowered.common.accessor.world.item.ArmorMaterialsAccessor; import org.spongepowered.common.accessor.world.level.GameRulesAccessor; import org.spongepowered.common.accessor.world.level.block.entity.BannerPatternAccessor; @@ -114,7 +115,7 @@ private VanillaRegistryLoader(final SpongeRegistryHolder holder) { private void loadInstanceRegistries() { this.holder.createRegistry(RegistryTypes.CRITERION, VanillaRegistryLoader.criterion()); - this.manualName(RegistryTypes.DRAGON_PHASE_TYPE, EnderDragonPhase.getCount(), map -> { + this.manualOrAutomaticName(RegistryTypes.DRAGON_PHASE_TYPE, EnderDragonPhaseAccessor.accessor$PHASES(), map -> { map.put(EnderDragonPhase.HOLDING_PATTERN, "holding_pattern"); map.put(EnderDragonPhase.STRAFE_PLAYER, "strafe_player"); map.put(EnderDragonPhase.LANDING_APPROACH, "landing_approach"); @@ -126,7 +127,7 @@ private void loadInstanceRegistries() { map.put(EnderDragonPhase.CHARGING_PLAYER, "charging_player"); map.put(EnderDragonPhase.DYING, "dying"); map.put(EnderDragonPhase.HOVERING, "hover"); - }); + }, phase -> CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, ((EnderDragonPhaseAccessor) phase).accessor$name())); this.holder.createRegistry(RegistryTypes.FIREWORK_SHAPE, VanillaRegistryLoader.fireworkShape()); this.holder.createRegistry(RegistryTypes.TRIGGER, VanillaRegistryLoader.trigger(), true, (k, trigger) -> CriteriaTriggersAccessor.invoker$register((CriterionTrigger) trigger)); @@ -318,9 +319,12 @@ private > Registry manualName(final RegistryType type } @SuppressWarnings("UnusedReturnValue") - private Registry manualName(final RegistryType type, final int values, final Consumer> byName) { - final Map map = new HashMap<>(values); + private Registry manualOrAutomaticName(final RegistryType type, final I[] values, final Consumer> byName, final Function autoName) { + final Map map = new HashMap<>(values.length); byName.accept(map::put); + for (final I value : values) { + map.computeIfAbsent(value, autoName); + } return this.naming(type, values, map); } @@ -334,7 +338,7 @@ private > Registry naming(final RegistryType type, fi } @SuppressWarnings("UnusedReturnValue") - private > Registry naming(final RegistryType type, final I[] values, final Map byName) { + private Registry naming(final RegistryType type, final I[] values, final Map byName) { return this.naming(type, values.length, byName); }