143 lines
5.5 KiB
Java
143 lines
5.5 KiB
Java
package net.minecraft.world.item;
|
|
|
|
import com.google.common.collect.Maps;
|
|
import com.google.common.collect.Iterables;
|
|
import javax.annotation.Nullable;
|
|
import net.minecraft.nbt.CompoundTag;
|
|
import net.minecraft.stats.Stat;
|
|
import net.minecraft.stats.Stats;
|
|
import net.minecraft.world.level.block.LiquidBlock;
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
import net.minecraft.world.phys.HitResult;
|
|
import net.minecraft.world.level.ClipContext;
|
|
import net.minecraft.world.InteractionResultHolder;
|
|
import net.minecraft.world.InteractionHand;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.level.BaseSpawner;
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.core.Direction;
|
|
import java.util.Objects;
|
|
import net.minecraft.world.entity.MobSpawnType;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.block.entity.SpawnerBlockEntity;
|
|
import net.minecraft.world.level.block.Blocks;
|
|
import net.minecraft.world.InteractionResult;
|
|
import net.minecraft.world.entity.EntityType;
|
|
import java.util.Map;
|
|
|
|
public class SpawnEggItem extends Item {
|
|
private static final Map<EntityType<?>, SpawnEggItem> BY_ID;
|
|
private final int color1;
|
|
private final int color2;
|
|
private final EntityType<?> defaultType;
|
|
|
|
public SpawnEggItem(final EntityType<?> akr, final int integer2, final int integer3, final Properties a) {
|
|
super(a);
|
|
this.defaultType = akr;
|
|
this.color1 = integer2;
|
|
this.color2 = integer3;
|
|
SpawnEggItem.BY_ID.put(akr, this);
|
|
}
|
|
|
|
@Override
|
|
public InteractionResult useOn(final UseOnContext bfw) {
|
|
final Level bjt3 = bfw.getLevel();
|
|
if (bjt3.isClientSide) {
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
final ItemStack bek4 = bfw.getItemInHand();
|
|
final BlockPos fk5 = bfw.getClickedPos();
|
|
final Direction fp6 = bfw.getClickedFace();
|
|
final BlockState byg7 = bjt3.getBlockState(fk5);
|
|
final Block bpe8 = byg7.getBlock();
|
|
if (bpe8 == Blocks.SPAWNER) {
|
|
final BlockEntity bwi9 = bjt3.getBlockEntity(fk5);
|
|
if (bwi9 instanceof SpawnerBlockEntity) {
|
|
final BaseSpawner bja10 = ((SpawnerBlockEntity)bwi9).getSpawner();
|
|
final EntityType<?> akr11 = this.getType(bek4.getTag());
|
|
bja10.setEntityId(akr11);
|
|
bwi9.setChanged();
|
|
bjt3.sendBlockUpdated(fk5, byg7, byg7, 3);
|
|
bek4.shrink(1);
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
}
|
|
BlockPos fk6;
|
|
if (byg7.getCollisionShape(bjt3, fk5).isEmpty()) {
|
|
fk6 = fk5;
|
|
}
|
|
else {
|
|
fk6 = fk5.relative(fp6);
|
|
}
|
|
final EntityType<?> akr12 = this.getType(bek4.getTag());
|
|
if (akr12.spawn(bjt3, bek4, bfw.getPlayer(), fk6, MobSpawnType.SPAWN_EGG, true, !Objects.equals(fk5, fk6) && fp6 == Direction.UP) != null) {
|
|
bek4.shrink(1);
|
|
}
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
|
|
@Override
|
|
public InteractionResultHolder<ItemStack> use(final Level bjt, final Player ayg, final InteractionHand ajh) {
|
|
final ItemStack bek5 = ayg.getItemInHand(ajh);
|
|
final HitResult cvf6 = Item.getPlayerPOVHitResult(bjt, ayg, ClipContext.Fluid.SOURCE_ONLY);
|
|
if (cvf6.getType() != HitResult.Type.BLOCK) {
|
|
return InteractionResultHolder.<ItemStack>pass(bek5);
|
|
}
|
|
if (bjt.isClientSide) {
|
|
return InteractionResultHolder.<ItemStack>success(bek5);
|
|
}
|
|
final BlockHitResult cvd7 = (BlockHitResult)cvf6;
|
|
final BlockPos fk8 = cvd7.getBlockPos();
|
|
if (!(bjt.getBlockState(fk8).getBlock() instanceof LiquidBlock)) {
|
|
return InteractionResultHolder.<ItemStack>pass(bek5);
|
|
}
|
|
if (!bjt.mayInteract(ayg, fk8) || !ayg.mayUseItemAt(fk8, cvd7.getDirection(), bek5)) {
|
|
return InteractionResultHolder.<ItemStack>fail(bek5);
|
|
}
|
|
final EntityType<?> akr9 = this.getType(bek5.getTag());
|
|
if (akr9.spawn(bjt, bek5, ayg, fk8, MobSpawnType.SPAWN_EGG, false, false) == null) {
|
|
return InteractionResultHolder.<ItemStack>pass(bek5);
|
|
}
|
|
if (!ayg.abilities.instabuild) {
|
|
bek5.shrink(1);
|
|
}
|
|
ayg.awardStat(Stats.ITEM_USED.get(this));
|
|
return InteractionResultHolder.<ItemStack>success(bek5);
|
|
}
|
|
|
|
public boolean spawnsEntity(@Nullable final CompoundTag jt, final EntityType<?> akr) {
|
|
return Objects.equals(this.getType(jt), akr);
|
|
}
|
|
|
|
public int getColor(final int integer) {
|
|
return (integer == 0) ? this.color1 : this.color2;
|
|
}
|
|
|
|
@Nullable
|
|
public static SpawnEggItem byId(@Nullable final EntityType<?> akr) {
|
|
return SpawnEggItem.BY_ID.get(akr);
|
|
}
|
|
|
|
public static Iterable<SpawnEggItem> eggs() {
|
|
return Iterables.<SpawnEggItem>unmodifiableIterable(SpawnEggItem.BY_ID.values());
|
|
}
|
|
|
|
public EntityType<?> getType(@Nullable final CompoundTag jt) {
|
|
if (jt != null && jt.contains("EntityTag", 10)) {
|
|
final CompoundTag jt2 = jt.getCompound("EntityTag");
|
|
if (jt2.contains("id", 8)) {
|
|
return EntityType.byString(jt2.getString("id")).orElse(this.defaultType);
|
|
}
|
|
}
|
|
return this.defaultType;
|
|
}
|
|
|
|
static {
|
|
BY_ID = Maps.newIdentityHashMap();
|
|
}
|
|
}
|