Jump to content

Recommended Posts

Posted

I am trying to make a spawn egg that spawns the wither, I am using a custom ModSpawnEggItem class:
 

package com.plainplaying.test.items;

import io.netty.handler.codec.http2.Http2FrameLogger;
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.common.util.Lazy;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ModSpawnEggItem extends SpawnEggItem {
    protected static final List<ModSpawnEggItem> UNADDED_EGGS = new ArrayList<>();
    private final Lazy<? extends EntityType<?>> entityTypeSupplier;
    public ModSpawnEggItem(final RegistryObject<? extends EntityType<?>> entityTypeSupplier, int primaryColorIn, int secondaryColorIn, Properties builder) {
        super(null, primaryColorIn, secondaryColorIn, builder);
        this.entityTypeSupplier = Lazy.of(entityTypeSupplier::get);
        UNADDED_EGGS.add(this);
    }

    public static void initSpawnEggs(){
        final Map<EntityType<?>, SpawnEggItem> EGGS = ObfuscationReflectionHelper.getPrivateValue(SpawnEggItem.class, null, "field_195987_b");
        DefaultDispenseItemBehavior dispenseBehavior = new DefaultDispenseItemBehavior() {
            @Override
            protected ItemStack dispenseStack(IBlockSource source, ItemStack stack) {
                Direction direction = source.getBlockState().get(DispenserBlock.FACING);
                EntityType<?> type = ((SpawnEggItem) stack.getItem()).getType(stack.getTag());
                if (direction == Direction.WEST){
                    type.spawn(source.getWorld(), stack, null, source.getBlockPos().add(-1,0,0), SpawnReason.DISPENSER,direction!= Direction.UP, false);
                }
                else if (direction == Direction.EAST){
                    type.spawn(source.getWorld(), stack, null, source.getBlockPos().add(1,0,0), SpawnReason.DISPENSER,direction!= Direction.UP, false);
                }
                else if (direction == Direction.NORTH){
                    type.spawn(source.getWorld(), stack, null, source.getBlockPos().add(0,0,-1), SpawnReason.DISPENSER,direction!= Direction.UP, false);
                }
                else if (direction == Direction.SOUTH){
                    type.spawn(source.getWorld(), stack, null, source.getBlockPos().add(0,0,1), SpawnReason.DISPENSER,true, false);
                }
                else if (direction == Direction.DOWN){
                    type.spawn(source.getWorld(), stack, null, source.getBlockPos().add(0,-1,0), SpawnReason.DISPENSER,true, false);
                }else{
                    type.spawn(source.getWorld(), stack, null, source.getBlockPos().add(0,1,0), SpawnReason.DISPENSER,true, false);
                }
                stack.shrink(1);
                return stack;
            }
        };

        for (final SpawnEggItem spawnEgg : UNADDED_EGGS){
            EGGS.put(spawnEgg.getType(null), spawnEgg);
            DispenserBlock.registerDispenseBehavior(spawnEgg, dispenseBehavior);
        }
        UNADDED_EGGS.clear();
    }

    @Override
    public EntityType<?> getType(CompoundNBT nbt) {
        return this.entityTypeSupplier.get();
    }
}

I am creating the spawn egg like this:
 

public static final RegistryObject<SpawnEggItem> WITHER_SPAWN_EGG = ITEMS.register("wither_spawn_egg",
            () -> new ModSpawnEggItem(EntityType.WITHER, 0xF1BF7B, 0x705128,new Item.Properties().group(TestMod.TAB)));

and I get an error saying that I need an RegistryObject instead of EntityType, and I understand why, but I don't know how to fix. I want it to work with registry object, so i though maybe I can use .get() on the registry object, but I don't know how to make ModSpawnEggItem be compatible with EntityType, Can somone help me?

Posted

I personally would juggle the code so that it takes in an entity type instead as I don't think it's possible to access the wither's registry object as it were (I could be wrong though). If you want to have other instances of a class take in a registry object, you could always use .get() when calling the constructor or even have an overloaded constructer that takes in a registry object and calls .get() on it.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.