Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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?

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.

  • Author

I am aware of ,get(), but i have absoulutely no clue on how to make the code work on EntityType instead of RegistryObject

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.