MinecraftScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a mob that can be captured and placed like an item"
/* This mod creates a mob that can be captured and placed like an item in Minecraft Forge. */ /* First, we create the mob entity. This could be any entity you want, here we're creating a custom entity called CapturableMob. */ public class CapturableMob extends MobEntity { /* ... */ } /* Next, we create an item that can capture and place the mob. */ public class MobCaptureItem extends Item { public MobCaptureItem(Properties properties) { super(properties); } /* When you right click an entity with this item, it captures the entity. */ @Override public ActionResultType interactLivingEntity(ItemStack stack, PlayerEntity player, LivingEntity target, Hand hand) { if (!player.level.isClientSide && target instanceof CapturableMob) { /* Replace the item in the player's hand with a new item that represents the captured mob. */ stack.shrink(1); player.addItem(new ItemStack(Items.CAPTURED_MOB)); /* Remove the mob from the world. */ target.remove(); return ActionResultType.SUCCESS; } return ActionResultType.PASS; } /* When you right click a block with this item, it places the mob. */ @Override public ActionResultType useOn(ItemUseContext context) { if (!context.getLevel().isClientSide) { /* Spawn the mob at the clicked position. */ CapturableMob mob = new CapturableMob(context.getLevel()); mob.setPosition(context.getClickedPos().getX(), context.getClickedPos().getY(), context.getClickedPos().getZ()); context.getLevel().addEntity(mob); /* Remove the item from the player's hand. */ context.getItemInHand().shrink(1); return ActionResultType.SUCCESS; } return ActionResultType.PASS; } } /* Finally, we register the item and the mob. */ public static final RegistryObject<Item> CAPTURED_MOB = ITEMS.register("captured_mob", () -> new MobCaptureItem(new Item.Properties().stacksTo(1))); public static final EntityType<CapturableMob> CAPTURABLE_MOB = EntityType.Builder.of(CapturableMob::new, MobCategory.CREATURE).build("capturable_mob"); static { ENTITY_TYPES.register("capturable_mob", () -> CAPTURABLE_MOB); }