This code defines and implements a custom `SpawnEggItem` class, representing...

September 3, 2025 at 12:32 AM

public SpawnEggItem(EntityType<? extends Mob> p_43207_, Item.Properties p_43210_) { super(p_43210_); this.defaultType = p_43207_; BY_ID.put(p_43207_, this); } @Override public InteractionResult useOn(UseOnContext p_43223_) { Level level = p_43223_.getLevel(); if (level.isClientSide) { return InteractionResult.SUCCESS; } else { ItemStack itemstack = p_43223_.getItemInHand(); BlockPos blockpos = p_43223_.getClickedPos(); Direction direction = p_43223_.getClickedFace(); BlockState blockstate = level.getBlockState(blockpos); if (level.getBlockEntity(blockpos) instanceof Spawner spawner) { EntityType<?> entitytype1 = this.getType(level.registryAccess(), itemstack); spawner.setEntityId(entitytype1, level.getRandom()); level.sendBlockUpdated(blockpos, blockstate, blockstate, 3); level.gameEvent(p_43223_.getPlayer(), GameEvent.BLOCK_CHANGE, blockpos); itemstack.shrink(1); return InteractionResult.SUCCESS; } else { BlockPos blockpos1; if (blockstate.getCollisionShape(level, blockpos).isEmpty()) { blockpos1 = blockpos; } else { blockpos1 = blockpos.relative(direction); } EntityType<?> entitytype = this.getType(level.registryAccess(), itemstack); if (entitytype.spawn( (ServerLevel)level, itemstack, p_43223_.getPlayer(), blockpos1, EntitySpawnReason.SPAWN_ITEM_USE, true, !Objects.equals(blockpos, blockpos1) && direction == Direction.UP ) != null) { itemstack.shrink(1); level.gameEvent(p_43223_.getPlayer(), GameEvent.ENTITY_PLACE, blockpos); } return InteractionResult.SUCCESS; } } } @Override public InteractionResult use(Level p_43225_, Player p_43226_, InteractionHand p_43227_) { ItemStack itemstack = p_43226_.getItemInHand(p_43227_); BlockHitResult blockhitresult = getPlayerPOVHitResult(p_43225_, p_43226_, ClipContext.Fluid.SOURCE_ONLY); if (blockhitresult.getType() != HitResult.Type.BLOCK) { return InteractionResult.PASS; } else if (p_43225_ instanceof ServerLevel serverlevel) { BlockPos $$8 = blockhitresult.getBlockPos(); if (!(p_43225_.getBlockState($$8).getBlock() instanceof LiquidBlock)) { return InteractionResult.PASS; } else if (p_43225_.mayInteract(p_43226_, $$8) && p_43226_.mayUseItemAt($$8, blockhitresult.getDirection(), itemstack)) { EntityType<?> entitytype = this.getType(serverlevel.registryAccess(), itemstack); Entity entity = entitytype.spawn(serverlevel, itemstack, p_43226_, $$8, EntitySpawnReason.SPAWN_ITEM_USE, false, false); if (entity == null) { return InteractionResult.PASS; } else { itemstack.consume(1, p_43226_); p_43226_.awardStat(Stats.ITEM_USED.get(this)); p_43225_.gameEvent(p_43226_, GameEvent.ENTITY_PLACE, entity.position()); return InteractionResult.SUCCESS; } } else { return InteractionResult.FAIL; } } else { return InteractionResult.SUCCESS; } } public boolean spawnsEntity(HolderLookup.Provider p_376104_, ItemStack p_331553_, EntityType<?> p_43232_) { return Objects.equals(this.getType(p_376104_, p_331553_), p_43232_); } @Nullable public static SpawnEggItem byId(@Nullable EntityType<?> p_43214_) { return BY_ID.get(p_43214_); } public static Iterable<SpawnEggItem> eggs() { return Iterables.unmodifiableIterable(BY_ID.values()); } public EntityType<?> getType(HolderLookup.Provider p_377295_, ItemStack p_334231_) { CustomData customdata = p_334231_.getOrDefault(DataComponents.ENTITY_DATA, CustomData.EMPTY); if (!customdata.isEmpty()) { EntityType<?> entitytype = customdata.parseEntityType(p_377295_, Registries.ENTITY_TYPE); if (entitytype != null) { return entitytype; } } return this.defaultType; } @Override public FeatureFlagSet requiredFeatures() { return this.defaultType.requiredFeatures(); } public Optional<Mob> spawnOffspringFromSpawnEgg(Player p_43216_, Mob p_43217_, EntityType<? extends Mob> p_43218_, ServerLevel p_43219_, Vec3 p_43220_, ItemStack p_43221_) { if (!this.spawnsEntity(p_43219_.registryAccess(), p_43221_, p_43218_)) { return Optional.empty(); } else { Mob mob; if (p_43217_ instanceof AgeableMob) { mob = ((AgeableMob)p_43217_).getBreedOffspring(p_43219_, (AgeableMob)p_43217_); } else { mob = p_43218_.create(p_43219_, EntitySpawnReason.SPAWN_ITEM_USE); } if (mob == null) { return Optional.empty(); } else { mob.setBaby(true); if (!mob.isBaby()) { return Optional.empty(); } else { mob.moveTo(p_43220_.x(), p_43220_.y(), p_43220_.z(), 0.0F, 0.0F); p_43219_.addFreshEntityWithPassengers(mob); mob.setCustomName(p_43221_.get(DataComponents.CUSTOM_NAME)); p_43221_.consume(1, p_43216_); return Optional.of(mob); } } } } @Override public boolean shouldPrintOpWarning(ItemStack p_378492_, @Nullable Player p_377094_) { if (p_377094_ != null && p_377094_.getPermissionLevel() >= 2) { CustomData customdata = p_378492_.get(DataComponents.ENTITY_DATA); if (customdata != null) { EntityType<?> entitytype = customdata.parseEntityType(p_377094_.level().registryAccess(), Registries.ENTITY_TYPE); return entitytype != null && entitytype.onlyOpCanSetNbt(); } } return false; } }

This code defines and implements a custom SpawnEggItem class, representing a modified spawn egg typically used in games like Minecraft to spawn mobs or entities. Below is what the code does in detail:

Class Overview:

The SpawnEggItem class extends a base item class and represents a spawn egg that can spawn specific types of entities (e.g., mobs, creatures) when used. It also implements various overridden methods to define functionality specific to spawn eggs.

Key Features:

  1. Constructor:

    • public SpawnEggItem(EntityType<? extends Mob> p_43207_, Item.Properties p_43210_)
    • The constructor initializes the spawn egg with the entity type it spawns (p_43207_) and its item properties (p_43210_).
    • The constructor adds the entity type (p_43207_) to a BY_ID map, linking entity types to their spawn egg items.
  2. useOn Method (Overridden):

    • Executes logic when the spawn egg is used on a block.
    • Checks if the block is a spawner block; if it is, sets the entity type in the spawner.
    • Otherwise, attempts to spawn the entity at a valid position near the block. The entity's type is determined by the spawn egg's data or its default type.
    • Handles client/server interaction:
      • On the client side, it always returns InteractionResult.SUCCESS.
      • On the server side, it either spawns the entity or modifies the spawner block, consuming one spawn egg item in the process.
  3. use Method (Overridden):

    • Executes logic when the spawn egg is used in more general contexts (e.g., using it directly without targeting a block).
    • Handles interactions with water blocks:
      • Checks if the targeted block is water and if the player has permission to use the spawn egg.
      • Spawns the entity into the water based on the spawn egg's entity type.
      • Updates the game state, reduces the spawn egg stack count, and tracks statistics for the player's action.
  4. Spawn Compatibility:

    • The spawnsEntity method checks if a specific spawn egg can spawn a given entity type, ensuring compatibility.
  5. Lookup Utility Methods:

    • byId and eggs static methods:
      • byId: Retrieves the SpawnEggItem corresponding to a particular EntityType.
      • eggs: Returns an iterable collection of all registered spawn egg items.
  6. Entity Type Resolution:

    • getType determines the EntityType the spawn egg will use to spawn entities. It first checks for custom data associated with the item; if none is found, it defaults to the spawn egg's predefined entity type.
  7. Breeding and Offspring:

    • spawnOffspringFromSpawnEgg attempts to spawn a baby mob as an offspring of a parent mob, checking compatibility with the spawn egg's entity type. It returns the spawned mob (wrapped in Optional) if successful.
  8. Administrative Warnings:

    • The shouldPrintOpWarning method checks if the spawned entity has restricted NBT data (e.g., only operators in the game can spawn such entities). This prevents unauthorized players from misusing custom NBT configurations.
  9. Feature Requirements (Compatibility):

    • The requiredFeatures method ensures that the spawn egg and the corresponding entity type are compatible with any specific game or modded feature flags.

Behavior at a High Level:

  • A player can use the SpawnEggItem on blocks or in-world locations to spawn an entity based on the egg's stored or default entity type.
  • It supports both specific interactions (e.g., with spawner blocks or water) and general interactions (e.g., placing entities in the world or spawning offspring creatures).
  • The class manages compatibility checks, game state updates, and appropriate item stack reduction when used.

Examples of Usage:

  • Spawner Modification:
    • If used on a spawner block, the spawn egg sets the spawner's entity type to match the egg's.
  • Entity Spawning:
    • If used on a block or water source, spawns the corresponding entity at the target location.
  • Egg Compatibility:
    • Checks to ensure that the spawn egg is properly configured to spawn a specific type of entity.

This code is typical for implementing custom spawn egg behavior in games or modding frameworks like Minecraft.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node