mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 19:39:22 +00:00
Add ForgeSpawnEggItem to lazily handle EntityTypes (#10626)
This commit is contained in:
1 parent
0e6e6ef2d8
commit
867159643c
8 files changed
+272
-3
No files matched your search
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2021.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common;
|
||||
|
||||
import net.minecraft.block.DispenserBlock;
|
||||
import net.minecraft.dispenser.DefaultDispenseItemBehavior;
|
||||
import net.minecraft.dispenser.IBlockSource;
|
||||
import net.minecraft.entity.EntityType;
|
||||
import net.minecraft.entity.SpawnReason;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.SpawnEggItem;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.util.Direction;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.client.event.ColorHandlerEvent;
|
||||
import net.minecraftforge.eventbus.api.EventPriority;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ForgeSpawnEggItem extends SpawnEggItem
|
||||
{
|
||||
private static final List<ForgeSpawnEggItem> MOD_EGGS = new ArrayList<>();
|
||||
private static final Map<EntityType<?>, ForgeSpawnEggItem> TYPE_MAP = new IdentityHashMap<>();
|
||||
private final Supplier<? extends EntityType<?>> typeSupplier;
|
||||
|
||||
public ForgeSpawnEggItem(Supplier<? extends EntityType<?>> type, int backgroundColor, int highlightColor, Properties props)
|
||||
{
|
||||
super((EntityType<?>) null, backgroundColor, highlightColor, props);
|
||||
this.typeSupplier = type;
|
||||
|
||||
MOD_EGGS.add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityType<?> getType(@Nullable CompoundNBT tag)
|
||||
{
|
||||
EntityType<?> type = super.getType(tag);
|
||||
return type != null ? type : typeSupplier.get();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected DefaultDispenseItemBehavior createDispenseBehavior()
|
||||
{
|
||||
return DEFAULT_DISPENSE_BEHAVIOR;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static SpawnEggItem fromEntityType(@Nullable EntityType<?> type)
|
||||
{
|
||||
SpawnEggItem ret = TYPE_MAP.get(type);
|
||||
return ret != null ? ret : SpawnEggItem.getEgg(type);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static final DefaultDispenseItemBehavior DEFAULT_DISPENSE_BEHAVIOR = new DefaultDispenseItemBehavior()
|
||||
{
|
||||
@Override
|
||||
protected ItemStack dispenseStack(IBlockSource source, ItemStack stack)
|
||||
{
|
||||
Direction face = source.getBlockState().get(DispenserBlock.FACING);
|
||||
EntityType<?> type = ((SpawnEggItem) stack.getItem()).getType(stack.getTag());
|
||||
|
||||
// FORGE: fix potential crash
|
||||
try
|
||||
{
|
||||
type.spawn(source.getWorld(), stack, null, source.getBlockPos().offset(face), SpawnReason.DISPENSER, face != Direction.UP, false);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
DefaultDispenseItemBehavior.LOGGER.error("Error while dispensing spawn egg from dispenser at {}", source.getBlockPos(), exception);
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
stack.shrink(1);
|
||||
return stack;
|
||||
}
|
||||
};
|
||||
|
||||
@Mod.EventBusSubscriber(modid = "forge", bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
private static class CommonHandler
|
||||
{
|
||||
@SubscribeEvent
|
||||
public static void onCommonSetup(FMLCommonSetupEvent event)
|
||||
{
|
||||
MOD_EGGS.forEach(egg ->
|
||||
{
|
||||
DefaultDispenseItemBehavior dispenseBehavior = egg.createDispenseBehavior();
|
||||
if (dispenseBehavior != null)
|
||||
{
|
||||
DispenserBlock.registerDispenseBehavior(egg, dispenseBehavior);
|
||||
}
|
||||
|
||||
TYPE_MAP.put(egg.typeSupplier.get(), egg);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Mod.EventBusSubscriber(value = Dist.CLIENT, modid = "forge", bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
private static class ColorRegisterHandler
|
||||
{
|
||||
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
||||
public static void registerSpawnEggColors(ColorHandlerEvent.Item event)
|
||||
{
|
||||
MOD_EGGS.forEach(egg ->
|
||||
event.getItemColors().register((stack, layer) -> egg.getColor(layer), egg)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,7 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.RayTraceResult;
|
||||
import net.minecraftforge.common.ForgeSpawnEggItem;
|
||||
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
|
||||
|
||||
public interface IForgeEntity extends ICapabilitySerializable<CompoundNBT>
|
||||
@@ -115,7 +116,7 @@ public interface IForgeEntity extends ICapabilitySerializable<CompoundNBT>
|
||||
return new ItemStack(Items.END_CRYSTAL);
|
||||
else
|
||||
{
|
||||
SpawnEggItem egg = SpawnEggItem.getEgg(getEntity().getType());
|
||||
SpawnEggItem egg = ForgeSpawnEggItem.fromEntityType(getEntity().getType());
|
||||
if (egg != null) return new ItemStack(egg);
|
||||
}
|
||||
return ItemStack.EMPTY;
|
||||
|
||||
@@ -40,6 +40,7 @@ net/minecraft/block/WoodType func_227046_a_()Ljava/util/stream/Stream; # getValu
|
||||
net/minecraft/block/WoodType func_227048_b_()Ljava/lang/String; # getName
|
||||
net/minecraft/item/DyeColor func_218388_g()I # getTextColor
|
||||
net/minecraft/item/DyeColor func_196058_b(I)Lnet/minecraft/item/DyeColor; # byFireworkColor
|
||||
net/minecraft/item/SpawnEggItem func_200889_b(Lnet/minecraft/entity/EntityType;)Lnet/minecraft/item/SpawnEggItem; # byId
|
||||
net/minecraft/item/crafting/Ingredient func_193365_a()[Lnet/minecraft/item/ItemStack; # getMatchingStacks
|
||||
net/minecraft/item/crafting/Ingredient func_193369_a([Lnet/minecraft/item/ItemStack;)Lnet/minecraft/item/crafting/Ingredient; # fromStacks
|
||||
net/minecraft/item/crafting/IRecipe func_222128_h()Lnet/minecraft/item/ItemStack; # getIcon
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2021.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.debug.item;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.entity.EntityRendererManager;
|
||||
import net.minecraft.client.renderer.entity.PigRenderer;
|
||||
import net.minecraft.entity.EntityClassification;
|
||||
import net.minecraft.entity.EntityType;
|
||||
import net.minecraft.entity.passive.PigEntity;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemGroup;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.common.ForgeSpawnEggItem;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.RegistryObject;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
@Mod(value = ForgeSpawnEggItemTest.MODID)
|
||||
public class ForgeSpawnEggItemTest
|
||||
{
|
||||
static final String MODID = "forge_spawnegg_test";
|
||||
static final boolean ENABLED = true;
|
||||
|
||||
private static final DeferredRegister<EntityType<?>> ENTITIES = DeferredRegister.create(ForgeRegistries.ENTITIES, MODID);
|
||||
private static final RegistryObject<EntityType<PigEntity>> ENTITY = ENTITIES.register("test_entity", () ->
|
||||
EntityType.Builder.create(PigEntity::new, EntityClassification.CREATURE).size(1, 1).build("test_entity")
|
||||
);
|
||||
|
||||
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
|
||||
private static final RegistryObject<ForgeSpawnEggItem> EGG = ITEMS.register("test_spawn_egg", () ->
|
||||
new ForgeSpawnEggItem(ENTITY, 0x0000FF, 0xFF0000, new Item.Properties().group(ItemGroup.MISC))
|
||||
);
|
||||
|
||||
public ForgeSpawnEggItemTest()
|
||||
{
|
||||
if (ENABLED)
|
||||
{
|
||||
ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
|
||||
ENTITIES.register(FMLJavaModLoadingContext.get().getModEventBus());
|
||||
|
||||
FMLJavaModLoadingContext.get().getModEventBus().register(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Mod.EventBusSubscriber(modid = MODID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
private static class ClientEvents
|
||||
{
|
||||
@SubscribeEvent
|
||||
public static void onClientSetup(final FMLClientSetupEvent event)
|
||||
{
|
||||
if (!ENABLED) { return; }
|
||||
|
||||
EntityRendererManager manager = Minecraft.getInstance().getRenderManager();
|
||||
manager.register(ENTITY.get(), new PigRenderer(manager));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,3 +72,5 @@ loaderVersion="[28,)"
|
||||
modId="player_name_event_test"
|
||||
[[mods]]
|
||||
modId="custom_elytra_test"
|
||||
[[mods]]
|
||||
modId="forge_spawnegg_test"
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"parent": "minecraft:item/template_spawn_egg"
|
||||
}
|
||||
Reference in new issue
Block a user