Skip to content

Commit

Permalink
[1.21.3] Remove DeferredSpawnEggItem (#1678)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt authored Nov 10, 2024
1 parent c8114bf commit 52d6e59
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 147 deletions.
10 changes: 9 additions & 1 deletion patches/net/minecraft/client/color/item/ItemColors.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public static ItemColors createDefault(BlockColors p_92684_) {
ItemColors itemcolors = new ItemColors();
@@ -101,17 +_,20 @@
@@ -101,17 +_,28 @@
(p_359075_, p_359076_) -> p_359076_ == 0 ? -1 : ARGB.opaque(p_359075_.getOrDefault(DataComponents.MAP_COLOR, MapItemColor.DEFAULT).rgb()),
Items.FILLED_MAP
);
Expand All @@ -24,6 +24,14 @@
return itemcolor == null ? -1 : itemcolor.getColor(p_92677_, p_92678_);
}

+ /**
+ * Neo: returns the color handler for the given {@code item}, or {@code null} if one isn't registered.
+ */
+ @org.jetbrains.annotations.Nullable
+ public ItemColor get(Item item) {
+ return itemColors.get(item);
+ }
+
+ /** @deprecated Register via {@link net.neoforged.neoforge.client.event.RegisterColorHandlersEvent.Item} */
+ @Deprecated
public void register(ItemColor p_92690_, ItemLike... p_92691_) {
Expand Down
85 changes: 41 additions & 44 deletions patches/net/minecraft/world/item/SpawnEggItem.java.patch
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
--- a/net/minecraft/world/item/SpawnEggItem.java
+++ b/net/minecraft/world/item/SpawnEggItem.java
@@ -41,11 +_,14 @@
private final int highlightColor;
private final EntityType<?> defaultType;

+ /** @deprecated Forge: Use {@link net.neoforged.neoforge.common.DeferredSpawnEggItem} instead for suppliers */
+ @Deprecated
public SpawnEggItem(EntityType<? extends Mob> p_43207_, int p_43208_, int p_43209_, Item.Properties p_43210_) {
super(p_43210_);
this.defaultType = p_43207_;
this.backgroundColor = p_43208_;
this.highlightColor = p_43209_;
+ if (p_43207_ != null)
BY_ID.put(p_43207_, this);
}

@@ -133,6 +_,8 @@

@Nullable
public static SpawnEggItem byId(@Nullable EntityType<?> p_43214_) {
+ var ret = net.neoforged.neoforge.common.DeferredSpawnEggItem.deferredOnlyById(p_43214_);
+ if (ret != null) return ret;
return BY_ID.get(p_43214_);
}

@@ -142,12 +_,12 @@

public EntityType<?> getType(ItemStack p_330335_) {
CustomData customdata = p_330335_.getOrDefault(DataComponents.ENTITY_DATA, CustomData.EMPTY);
- return !customdata.isEmpty() ? customdata.read(ENTITY_TYPE_FIELD_CODEC).result().orElse(this.defaultType) : this.defaultType;
+ return !customdata.isEmpty() ? customdata.read(ENTITY_TYPE_FIELD_CODEC).result().orElse(getDefaultType()) : getDefaultType();
}

@Override
public FeatureFlagSet requiredFeatures() {
- return this.defaultType.requiredFeatures();
+ return this.getDefaultType().requiredFeatures();
}

public Optional<Mob> spawnOffspringFromSpawnEgg(
@@ -178,5 +_,9 @@
}
@@ -179,4 +_,45 @@
}
}
}
+
+ public static final net.minecraft.core.dispenser.DispenseItemBehavior DEFAULT_DISPENSE_BEHAVIOR = new net.minecraft.core.dispenser.DefaultDispenseItemBehavior() {
+ @Override
+ protected ItemStack execute(net.minecraft.core.dispenser.BlockSource source, ItemStack egg) {
+ Direction direction = source.state().getValue(net.minecraft.world.level.block.DispenserBlock.FACING);
+ EntityType<?> entitytype = ((SpawnEggItem)egg.getItem()).getType(egg);
+
+ try {
+ entitytype.spawn(
+ source.level(), egg, null, source.pos().relative(direction), EntitySpawnReason.DISPENSER, direction != Direction.UP, false
+ );
+ } catch (Exception exception) {
+ LOGGER.error("Error while dispensing spawn egg from dispenser at {}", source.pos(), exception);
+ return ItemStack.EMPTY;
+ }
+
+ egg.shrink(1);
+ source.level().gameEvent(null, GameEvent.ENTITY_PLACE, source.pos());
+ return egg;
+ }
+ };
+
+ /**
+ * {@return the dispense behavior to register by default}
+ */
+ @Nullable
+ protected net.minecraft.core.dispenser.DispenseItemBehavior createDispenseBehavior() {
+ return DEFAULT_DISPENSE_BEHAVIOR;
+ }
+
+ protected EntityType<?> getDefaultType() {
+ return defaultType;
}
+ @net.neoforged.bus.api.SubscribeEvent(priority = net.neoforged.bus.api.EventPriority.LOWEST)
+ private static void registerDispenseBehavior(final net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent event) {
+ event.enqueueWork(() -> eggs().forEach(egg -> {
+ if (!net.minecraft.world.level.block.DispenserBlock.DISPENSER_REGISTRY.containsKey(egg)) {
+ var beh = egg.createDispenseBehavior();
+ if (beh != null) {
+ net.minecraft.world.level.block.DispenserBlock.registerBehavior(egg, beh);
+ }
+ }
+ }));
+ }
}
13 changes: 13 additions & 0 deletions src/main/java/net/neoforged/neoforge/client/ClientNeoForgeMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ARGB;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.material.FluidState;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.EventPriority;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModContainer;
Expand All @@ -22,6 +25,7 @@
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.client.event.ModelEvent;
import net.neoforged.neoforge.client.event.RegisterClientReloadListenersEvent;
import net.neoforged.neoforge.client.event.RegisterColorHandlersEvent;
import net.neoforged.neoforge.client.event.RegisterNamedRenderTypesEvent;
import net.neoforged.neoforge.client.event.RegisterSpriteSourceTypesEvent;
import net.neoforged.neoforge.client.extensions.common.IClientFluidTypeExtensions;
Expand Down Expand Up @@ -157,4 +161,13 @@ public ResourceLocation getFlowingTexture() {
}
}, milkType));
}

@SubscribeEvent(priority = EventPriority.LOWEST)
static void registerSpawnEggColors(RegisterColorHandlersEvent.Item event) {
SpawnEggItem.eggs().forEach(egg -> {
if (event.getItemColors().get(egg) == null) {
event.register((stack, layer) -> ARGB.opaque(egg.getColor(layer)), egg);
}
});
}
}
100 changes: 0 additions & 100 deletions src/main/java/net/neoforged/neoforge/common/DeferredSpawnEggItem.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import net.minecraft.world.entity.ai.attributes.RangedAttribute;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraft.world.item.crafting.display.SlotDisplay;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.GameRules;
Expand Down Expand Up @@ -588,6 +589,8 @@ public NeoForgeMod(IEventBus modEventBus, Dist dist, ModContainer container) {

modEventBus.register(NeoForgeDataMaps.class);

modEventBus.register(SpawnEggItem.class); // Registers dispenser behaviour for eggs

if (isPRBuild(container.getModInfo().getVersion().toString())) {
isPRBuild = true;
ModLoader.addLoadingIssue(ModLoadingIssue.warning("loadwarning.neoforge.prbuild").withAffectedMod(container.getModInfo()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.minecraft.world.item.MobBucketItem;
import net.minecraft.world.item.PickaxeItem;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraft.world.item.ToolMaterial;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.item.equipment.ArmorMaterials;
Expand All @@ -48,7 +49,6 @@
import net.minecraft.world.level.material.Fluids;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
import net.neoforged.neoforge.client.model.generators.ModelFile;
import net.neoforged.neoforge.common.DeferredSpawnEggItem;
import net.neoforged.testframework.DynamicTest;
import net.neoforged.testframework.annotation.ForEachTest;
import net.neoforged.testframework.annotation.TestHolder;
Expand Down Expand Up @@ -117,7 +117,7 @@ static void forgeSpawnEggTest(final DynamicTest test, final RegistrationHelper r
.withRenderer(() -> PigRenderer::new)
.withLang("Test Pig spawn egg");

final var egg = reg.items().registerItem("test_spawn_egg", props -> new DeferredSpawnEggItem(testEntity, 0x0000FF, 0xFF0000, props) {
final var egg = reg.items().registerItem("test_spawn_egg", props -> new SpawnEggItem(testEntity.get(), 0x0000FF, 0xFF0000, props) {
@Override
public InteractionResult useOn(UseOnContext ctx) {
final var result = super.useOn(ctx);
Expand Down

0 comments on commit 52d6e59

Please sign in to comment.