| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import java.util.Objects;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.server.level.ServerPlayer;
|
| 13 | import net.minecraft.server.permissions.Permissions;
|
| 14 | import net.minecraft.stats.Stats;
|
| 15 | import net.minecraft.world.InteractionHand;
|
| 16 | import net.minecraft.world.InteractionResult;
|
| 17 | import net.minecraft.world.entity.AgeableMob;
|
| 18 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 19 | import net.minecraft.world.entity.EntityType;
|
| 20 | import net.minecraft.world.entity.LivingEntity;
|
| 21 | import net.minecraft.world.entity.Mob;
|
| 22 | import net.minecraft.world.entity.player.Player;
|
| 23 | import net.minecraft.world.item.component.TypedEntityData;
|
| 24 | import net.minecraft.world.item.context.UseOnContext;
|
| 25 | import net.minecraft.world.level.ClipContext;
|
| 26 | import net.minecraft.world.level.Level;
|
| 27 | import net.minecraft.world.level.Spawner;
|
| 28 | import net.minecraft.world.level.block.LiquidBlock;
|
| 29 | import net.minecraft.world.level.block.state.BlockState;
|
| 30 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 31 | import net.minecraft.world.phys.BlockHitResult;
|
| 32 | import net.minecraft.world.phys.HitResult;
|
| 33 | import net.minecraft.world.phys.Vec3;
|
| 34 | import org.jspecify.annotations.Nullable;
|
| 35 |
|
| 36 | public class SpawnEggItem extends Item {
|
| 37 | public SpawnEggItem(final Item.Properties properties) {
|
| 38 | super(properties);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public InteractionResult useOn(final UseOnContext context) {
|
| 43 | Level level = context.getLevel();
|
| 44 | ItemStack itemStack = context.getItemInHand();
|
| 45 | EntityType<?> type = getType(itemStack);
|
| 46 | if (type == null || !type.canSpawn(level)) {
|
| 47 | return InteractionResult.FAIL;
|
| 48 | } else if (!(level instanceof ServerLevel serverLevel)) {
|
| 49 | return InteractionResult.SUCCESS;
|
| 50 | } else {
|
| 51 | BlockPos pos = context.getClickedPos();
|
| 52 | Direction clickedFace = context.getClickedFace();
|
| 53 | BlockState blockState = level.getBlockState(pos);
|
| 54 | if (level.getBlockEntity(pos) instanceof Spawner spawnerHolder) {
|
| 55 | if (!serverLevel.isSpawnerBlockEnabled()) {
|
| 56 | if (context.getPlayer() instanceof ServerPlayer serverPlayer) {
|
| 57 | serverPlayer.sendSystemMessage(Component.translatable("advMode.notEnabled.spawner"));
|
| 58 | }
|
| 59 |
|
| 60 | return InteractionResult.FAIL;
|
| 61 | } else {
|
| 62 | Player player = context.getPlayer();
|
| 63 | TypedEntityData<EntityType<?>> entityData = itemStack.get(DataComponents.ENTITY_DATA);
|
| 64 | if (player != null && entityData != null && level.getServer().getPlayerList().isOp(player.nameAndId())) {
|
| 65 | spawnerHolder.setEntityData(entityData, level.getRandom());
|
| 66 | } else {
|
| 67 | spawnerHolder.setEntityId(type, level.getRandom());
|
| 68 | }
|
| 69 |
|
| 70 | level.sendBlockUpdated(pos, blockState, blockState, 3);
|
| 71 | level.gameEvent(context.getPlayer(), GameEvent.BLOCK_CHANGE, pos);
|
| 72 | itemStack.shrink(1);
|
| 73 | return InteractionResult.SUCCESS;
|
| 74 | }
|
| 75 | } else {
|
| 76 | BlockPos spawnPos;
|
| 77 | if (blockState.getCollisionShape(level, pos).isEmpty()) {
|
| 78 | spawnPos = pos;
|
| 79 | } else {
|
| 80 | spawnPos = pos.relative(clickedFace);
|
| 81 | }
|
| 82 |
|
| 83 | return spawnMob(type, context.getPlayer(), itemStack, serverLevel, spawnPos, true, !Objects.equals(pos, spawnPos) && clickedFace == Direction.UP);
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | private static InteractionResult spawnMob(
|
| 89 | final EntityType<?> type,
|
| 90 | final @Nullable LivingEntity user,
|
| 91 | final ItemStack itemStack,
|
| 92 | final ServerLevel level,
|
| 93 | final BlockPos spawnPos,
|
| 94 | final boolean tryMoveDown,
|
| 95 | final boolean movedUp
|
| 96 | ) {
|
| 97 | if (type.spawn(level, itemStack, user, spawnPos, EntitySpawnReason.SPAWN_ITEM_USE, tryMoveDown, movedUp) != null) {
|
| 98 | itemStack.consume(1, user);
|
| 99 | level.gameEvent(user, GameEvent.ENTITY_PLACE, spawnPos);
|
| 100 | return InteractionResult.SUCCESS;
|
| 101 | } else {
|
| 102 | return InteractionResult.FAIL;
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
|
| 108 | ItemStack itemStack = player.getItemInHand(hand);
|
| 109 | BlockHitResult hitResult = getPlayerPOVHitResult(level, player, ClipContext.Fluid.SOURCE_ONLY);
|
| 110 | if (hitResult.getType() != HitResult.Type.BLOCK) {
|
| 111 | return InteractionResult.PASS;
|
| 112 | }
|
| 113 |
|
| 114 | EntityType<?> type = getType(itemStack);
|
| 115 | if (type == null || !type.canSpawn(level)) {
|
| 116 | return InteractionResult.FAIL;
|
| 117 | }
|
| 118 |
|
| 119 | if (level instanceof ServerLevel serverLevel) {
|
| 120 | BlockPos pos = hitResult.getBlockPos();
|
| 121 | if (!(level.getBlockState(pos).getBlock() instanceof LiquidBlock)) {
|
| 122 | return InteractionResult.PASS;
|
| 123 | }
|
| 124 |
|
| 125 | if (level.mayInteract(player, pos) && player.mayUseItemAt(pos, hitResult.getDirection(), itemStack)) {
|
| 126 | InteractionResult result = spawnMob(type, player, itemStack, serverLevel, pos, false, false);
|
| 127 | if (result == InteractionResult.SUCCESS) {
|
| 128 | player.awardStat(Stats.ITEM_USED.get(this));
|
| 129 | }
|
| 130 |
|
| 131 | return result;
|
| 132 | } else {
|
| 133 | return InteractionResult.FAIL;
|
| 134 | }
|
| 135 | } else {
|
| 136 | return InteractionResult.SUCCESS;
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | public static boolean spawnsEntity(final ItemStack itemStack, final EntityType<?> type) {
|
| 141 | return Objects.equals(getType(itemStack), type);
|
| 142 | }
|
| 143 |
|
| 144 | public static Optional<Holder<Item>> byId(final EntityType<?> type) {
|
| 145 | return BuiltInRegistries.ITEM.componentLookup().findMatching(DataComponents.ENTITY_DATA, c -> c.type() == type).findAny();
|
| 146 | }
|
| 147 |
|
| 148 | public static @Nullable EntityType<?> getType(final ItemStack itemStack) {
|
| 149 | TypedEntityData<EntityType<?>> entityData = itemStack.get(DataComponents.ENTITY_DATA);
|
| 150 | return entityData != null ? entityData.type() : null;
|
| 151 | }
|
| 152 |
|
| 153 | public static Optional<Mob> spawnOffspringFromSpawnEgg(
|
| 154 | final Player player, final Mob parent, final EntityType<? extends Mob> type, final ServerLevel level, final Vec3 pos, final ItemStack spawnEggStack
|
| 155 | ) {
|
| 156 | if (!spawnsEntity(spawnEggStack, type)) {
|
| 157 | return Optional.empty();
|
| 158 | }
|
| 159 |
|
| 160 | Mob offspring;
|
| 161 | if (parent instanceof AgeableMob ageableMob) {
|
| 162 | offspring = ageableMob.getBreedOffspring(level, ageableMob);
|
| 163 | } else {
|
| 164 | offspring = type.create(level, EntitySpawnReason.SPAWN_ITEM_USE);
|
| 165 | }
|
| 166 |
|
| 167 | if (offspring == null) {
|
| 168 | return Optional.empty();
|
| 169 | }
|
| 170 |
|
| 171 | offspring.setBaby(true);
|
| 172 | if (!offspring.isBaby()) {
|
| 173 | return Optional.empty();
|
| 174 | }
|
| 175 |
|
| 176 | offspring.snapTo(pos.x(), pos.y(), pos.z(), 0.0F, 0.0F);
|
| 177 | offspring.applyComponentsFromItemStack(spawnEggStack);
|
| 178 | level.addFreshEntityWithPassengers(offspring);
|
| 179 | spawnEggStack.consume(1, player);
|
| 180 | return Optional.of(offspring);
|
| 181 | }
|
| 182 |
|
| 183 | @Override
|
| 184 | public boolean shouldPrintOpWarning(final ItemStack stack, final @Nullable Player player) {
|
| 185 | if (player != null && player.permissions().hasPermission(Permissions.COMMANDS_GAMEMASTER)) {
|
| 186 | TypedEntityData<EntityType<?>> entityData = stack.get(DataComponents.ENTITY_DATA);
|
| 187 | if (entityData != null) {
|
| 188 | return entityData.type().onlyOpCanSetNbt();
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | return false;
|
| 193 | }
|
| 194 | }
|
| 195 |
|