Skip to content

Commit

Permalink
Ensure sponge:human is always registered last, to avoid holes in nume…
Browse files Browse the repository at this point in the history
…ric ids from client pov
  • Loading branch information
Yeregorix committed Feb 20, 2025
1 parent 3f13a78 commit 1a07c8b
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 50 deletions.
18 changes: 0 additions & 18 deletions forge/src/main/java/org/spongepowered/forge/SpongeForgeMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
package org.spongepowered.forge;

import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.event.server.ServerAboutToStartEvent;
Expand All @@ -38,8 +36,6 @@
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegisterEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.api.Client;
Expand Down Expand Up @@ -72,7 +68,6 @@ public SpongeForgeMod() {
// modBus: add all FML events with it
modBus.addListener(this::onCommonSetup);
modBus.addListener(this::onClientSetup);
modBus.addListener(this::onRegister);
modBus.addListener(this::onEntityAttributeCreationEvent);

// annotation events, for non-FML things
Expand Down Expand Up @@ -137,19 +132,6 @@ public void onServerStoppingEvent(final ServerStoppingEvent event) {
lifecycle.callStoppingEngineEvent((Server) event.getServer());
}

public void onRegister(RegisterEvent event) {
if (event.getRegistryKey() == ForgeRegistries.Keys.ENTITY_TYPES) {
SpongeEntityTypes.HUMAN = EntityType.Builder.of(HumanEntity::new, MobCategory.MISC)
.noSave()
.sized(0.6F, 1.8F)
.clientTrackingRange(org.spongepowered.common.util.Constants.Entity.Player.TRACKING_RANGE)
.updateInterval(2)
.build("sponge:human")
;

event.register(ForgeRegistries.Keys.ENTITY_TYPES, helper -> helper.register(HumanEntity.KEY, SpongeEntityTypes.HUMAN));
}
}
public void onEntityAttributeCreationEvent(final EntityAttributeCreationEvent event) {
event.put(SpongeEntityTypes.HUMAN, HumanEntity.createAttributes());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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.forge.mixin.core.minecraftforge.registries;

import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.entity.EntityType;
import net.minecraftforge.registries.GameData;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.common.entity.SpongeEntityTypes;

@Mixin(GameData.class)
public class GameDataMixin_Forge {

@Inject(method = "postRegisterEvents", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/ModLoader;postEventWrapContainerInModOrder(Lnet/minecraftforge/eventbus/api/Event;)V", shift = At.Shift.AFTER))
private static void forge$registerSpongeTypesLast(final CallbackInfo ci, @Nullable @Local final Registry<?> vanillaRegistry) {
if (vanillaRegistry != null && Registries.ENTITY_TYPE.equals(vanillaRegistry.key())) {
SpongeEntityTypes.register((Registry<EntityType<?>>) vanillaRegistry);
}
}
}
1 change: 1 addition & 0 deletions forge/src/mixins/resources/mixins.spongeforge.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"minecraftforge.event.world.BlockEventMixin_Forge",
"minecraftforge.internal.BrandingControlMixin_Forge",
"minecraftforge.registries.ForgeRegistryMixin_Forge",
"minecraftforge.registries.GameDataMixin_Forge",
"minecraftforge.registries.RegistryManagerMixin_Forge",
"network.ConnectionMixin_Forge",
"network.protocol.common.ClientboundCustomPayloadPacketMixin_Forge",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
package org.spongepowered.neoforge;

import net.minecraft.client.Minecraft;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
Expand All @@ -38,7 +35,6 @@
import net.neoforged.neoforge.event.server.ServerAboutToStartEvent;
import net.neoforged.neoforge.event.server.ServerStartedEvent;
import net.neoforged.neoforge.event.server.ServerStoppingEvent;
import net.neoforged.neoforge.registries.RegisterEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.api.Client;
Expand Down Expand Up @@ -67,7 +63,6 @@ public SpongeNeoMod(IEventBus modBus) {
// modBus: add all FML events with it
modBus.addListener(this::onCommonSetup);
modBus.addListener(this::onClientSetup);
modBus.addListener(this::onRegister);
modBus.addListener(this::onEntityAttributeCreationEvent);

// annotation events, for non-FML things
Expand Down Expand Up @@ -132,19 +127,6 @@ public void onServerStoppingEvent(final ServerStoppingEvent event) {
lifecycle.callStoppingEngineEvent((Server) event.getServer());
}

public void onRegister(RegisterEvent event) {
if (event.getRegistryKey() == Registries.ENTITY_TYPE) {
SpongeEntityTypes.HUMAN = EntityType.Builder.of(HumanEntity::new, MobCategory.MISC)
.noSave()
.sized(0.6F, 1.8F)
.clientTrackingRange(org.spongepowered.common.util.Constants.Entity.Player.TRACKING_RANGE)
.updateInterval(2)
.build("sponge:human");

event.register(Registries.ENTITY_TYPE, helper -> helper.register(HumanEntity.KEY, SpongeEntityTypes.HUMAN));
}
}

public void onEntityAttributeCreationEvent(final EntityAttributeCreationEvent event) {
event.put(SpongeEntityTypes.HUMAN, HumanEntity.createAttributes());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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.neoforge.mixin.core.neoforge.registries;

import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.entity.EntityType;
import net.neoforged.neoforge.registries.GameData;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.common.entity.SpongeEntityTypes;

@Mixin(GameData.class)
public class GameDataMixin_Neo {

@Inject(method = "postRegisterEvents", at = @At(value = "INVOKE", target = "Lnet/neoforged/fml/ModLoader;postEventWrapContainerInModOrder(Lnet/neoforged/bus/api/Event;)V", shift = At.Shift.AFTER))
private static void neo$registerSpongeTypesLast(final CallbackInfo ci, @Local final Registry<?> registry) {
if (Registries.ENTITY_TYPE.equals(registry.key())) {
SpongeEntityTypes.register((Registry<EntityType<?>>) registry);
}
}
}
1 change: 1 addition & 0 deletions neoforge/src/mixins/resources/mixins.spongeneo.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"neoforge.event.world.BlockEvent_BreakEventMixin_Neo",
"neoforge.event.world.BlockEventMixin_Neo",
"neoforge.internal.BrandingControlMixin_Neo",
"neoforge.registries.GameDataMixin_Neo",
"neoforge.registries.RegistryManagerMixin_Neo",
"network.ConnectionMixin_Neo",
"server.BootstrapMixin_Neo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,24 @@
*/
package org.spongepowered.common.entity;

import net.minecraft.core.Registry;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import org.slf4j.LoggerFactory;
import org.spongepowered.common.entity.living.human.HumanEntity;
import org.spongepowered.common.util.Constants;

public class SpongeEntityTypes {

public static EntityType<HumanEntity> HUMAN;
public static final EntityType<HumanEntity> HUMAN = EntityType.Builder.of(HumanEntity::new, MobCategory.MISC)
.noSave()
.sized(0.6F, 1.8F)
.clientTrackingRange(Constants.Entity.Player.TRACKING_RANGE)
.updateInterval(2)
.build("sponge:human");

public static void register(Registry<EntityType<?>> registry) {
LoggerFactory.getLogger("SpongeEntityTypes").info("Registering SpongeEntityTypes");
Registry.register(registry, HumanEntity.KEY, SpongeEntityTypes.HUMAN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,19 @@
*/
package org.spongepowered.vanilla.mixin.core.world.entity;

import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.common.entity.SpongeEntityTypes;
import org.spongepowered.common.entity.living.human.HumanEntity;
import org.spongepowered.common.util.Constants;

@Mixin(EntityType.class)
public abstract class EntityTypeMixin_Vanilla {

@Inject(method = "<clinit>", at = @At("TAIL"))
private static void impl$createHumanType(CallbackInfo ci) {
SpongeEntityTypes.HUMAN = Registry.register(BuiltInRegistries.ENTITY_TYPE, HumanEntity.KEY,
EntityType.Builder.of(HumanEntity::new, MobCategory.MISC)
.noSave()
.sized(0.6F, 1.8F)
.clientTrackingRange(Constants.Entity.Player.TRACKING_RANGE)
.updateInterval(2)
.build("sponge:human")
);
private static void impl$registerSpongeTypes(CallbackInfo ci) {
SpongeEntityTypes.register(BuiltInRegistries.ENTITY_TYPE);
}
}

0 comments on commit 1a07c8b

Please sign in to comment.