mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-21 12:42:26 +00:00
1.20.2-rc1
This commit is contained in:
12 files changed
+85
-146
No files matched your search
@@ -29,36 +29,32 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ForgeSpawnEggItem extends SpawnEggItem
|
||||
{
|
||||
public class ForgeSpawnEggItem extends SpawnEggItem {
|
||||
private static final List<ForgeSpawnEggItem> MOD_EGGS = new ArrayList<>();
|
||||
private static final Map<EntityType<? extends Mob>, ForgeSpawnEggItem> TYPE_MAP = new IdentityHashMap<>();
|
||||
private final Supplier<? extends EntityType<? extends Mob>> typeSupplier;
|
||||
|
||||
public ForgeSpawnEggItem(Supplier<? extends EntityType<? extends Mob>> type, int backgroundColor, int highlightColor, Properties props)
|
||||
{
|
||||
super((EntityType<? extends Mob>) null, backgroundColor, highlightColor, props);
|
||||
@SuppressWarnings("deprecation")
|
||||
public ForgeSpawnEggItem(Supplier<? extends EntityType<? extends Mob>> type, int backgroundColor, int highlightColor, Properties props) {
|
||||
super((EntityType<? extends Mob>)null, backgroundColor, highlightColor, props);
|
||||
this.typeSupplier = type;
|
||||
|
||||
MOD_EGGS.add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityType<?> getType(@Nullable CompoundTag tag)
|
||||
{
|
||||
public EntityType<?> getType(@Nullable CompoundTag tag) {
|
||||
EntityType<?> type = super.getType(tag);
|
||||
return type != null ? type : typeSupplier.get();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected DispenseItemBehavior createDispenseBehavior()
|
||||
{
|
||||
protected DispenseItemBehavior createDispenseBehavior() {
|
||||
return DEFAULT_DISPENSE_BEHAVIOR;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static SpawnEggItem fromEntityType(@Nullable EntityType<?> type)
|
||||
{
|
||||
public static SpawnEggItem fromEntityType(@Nullable EntityType<?> type) {
|
||||
SpawnEggItem ret = TYPE_MAP.get(type);
|
||||
return ret != null ? ret : SpawnEggItem.byId(type);
|
||||
}
|
||||
@@ -68,39 +64,30 @@ public class ForgeSpawnEggItem extends SpawnEggItem
|
||||
return this.typeSupplier.get();
|
||||
}
|
||||
|
||||
private static final DispenseItemBehavior DEFAULT_DISPENSE_BEHAVIOR = (source, stack) ->
|
||||
{
|
||||
Direction face = source.getBlockState().getValue(DispenserBlock.FACING);
|
||||
private static final DispenseItemBehavior DEFAULT_DISPENSE_BEHAVIOR = (source, stack) -> {
|
||||
Direction face = source.state().getValue(DispenserBlock.FACING);
|
||||
EntityType<?> type = ((SpawnEggItem)stack.getItem()).getType(stack.getTag());
|
||||
|
||||
try
|
||||
{
|
||||
type.spawn(source.getLevel(), stack, null, source.getPos().relative(face), MobSpawnType.DISPENSER, face != Direction.UP, false);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
DispenseItemBehavior.LOGGER.error("Error while dispensing spawn egg from dispenser at {}", source.getPos(), exception);
|
||||
try {
|
||||
type.spawn(source.level(), stack, null, source.pos().relative(face), MobSpawnType.DISPENSER, face != Direction.UP, false);
|
||||
} catch (Exception exception) {
|
||||
DispenseItemBehavior.LOGGER.error("Error while dispensing spawn egg from dispenser at {}", source.pos(), exception);
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
stack.shrink(1);
|
||||
source.getLevel().gameEvent(GameEvent.ENTITY_PLACE, source.getPos(), GameEvent.Context.of(source.getBlockState()));
|
||||
source.level().gameEvent(GameEvent.ENTITY_PLACE, source.pos(), GameEvent.Context.of(source.state()));
|
||||
return stack;
|
||||
};
|
||||
|
||||
@Mod.EventBusSubscriber(modid = "forge", bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
private static class CommonHandler
|
||||
{
|
||||
private static class CommonHandler {
|
||||
@SubscribeEvent
|
||||
public static void onCommonSetup(FMLCommonSetupEvent event)
|
||||
{
|
||||
MOD_EGGS.forEach(egg ->
|
||||
{
|
||||
public static void onCommonSetup(FMLCommonSetupEvent event) {
|
||||
MOD_EGGS.forEach(egg -> {
|
||||
DispenseItemBehavior dispenseBehavior = egg.createDispenseBehavior();
|
||||
if (dispenseBehavior != null)
|
||||
{
|
||||
DispenserBlock.registerBehavior(egg, dispenseBehavior);
|
||||
}
|
||||
|
||||
TYPE_MAP.put(egg.typeSupplier.get(), egg);
|
||||
});
|
||||
@@ -108,13 +95,11 @@ public class ForgeSpawnEggItem extends SpawnEggItem
|
||||
}
|
||||
|
||||
@Mod.EventBusSubscriber(value = Dist.CLIENT, modid = "forge", bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
private static class ColorRegisterHandler
|
||||
{
|
||||
private static class ColorRegisterHandler {
|
||||
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
||||
public static void registerSpawnEggColors(RegisterColorHandlersEvent.Item event)
|
||||
{
|
||||
public static void registerSpawnEggColors(RegisterColorHandlersEvent.Item event) {
|
||||
MOD_EGGS.forEach(egg ->
|
||||
event.getItemColors().register((stack, layer) -> egg.getColor(layer), egg)
|
||||
event.register((stack, layer) -> egg.getColor(layer), egg)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user