minecraft-source/src/net/minecraft/world/item/EnderEyeItem.java

96 lines
4.6 KiB
Java

package net.minecraft.world.item;
import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.projectile.EyeOfEnder;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerLevel;
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.block.state.pattern.BlockPattern;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.EndPortalFrameBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.InteractionResult;
public class EnderEyeItem extends Item {
public EnderEyeItem(final Properties a) {
super(a);
}
@Override
public InteractionResult useOn(final UseOnContext bfw) {
final Level bjt3 = bfw.getLevel();
final BlockPos fk4 = bfw.getClickedPos();
final BlockState byg5 = bjt3.getBlockState(fk4);
if (byg5.getBlock() != Blocks.END_PORTAL_FRAME || byg5.<Boolean>getValue((Property<Boolean>)EndPortalFrameBlock.HAS_EYE)) {
return InteractionResult.PASS;
}
if (bjt3.isClientSide) {
return InteractionResult.SUCCESS;
}
final BlockState byg6 = ((AbstractStateHolder<O, BlockState>)byg5).<Comparable, Boolean>setValue((Property<Comparable>)EndPortalFrameBlock.HAS_EYE, true);
Block.pushEntitiesUp(byg5, byg6, bjt3, fk4);
bjt3.setBlock(fk4, byg6, 2);
bjt3.updateNeighbourForOutputSignal(fk4, Blocks.END_PORTAL_FRAME);
bfw.getItemInHand().shrink(1);
bjt3.levelEvent(1503, fk4, 0);
final BlockPattern.BlockPatternMatch b7 = EndPortalFrameBlock.getOrCreatePortalShape().find(bjt3, fk4);
if (b7 != null) {
final BlockPos fk5 = b7.getFrontTopLeft().offset(-3, 0, -3);
for (int integer9 = 0; integer9 < 3; ++integer9) {
for (int integer10 = 0; integer10 < 3; ++integer10) {
bjt3.setBlock(fk5.offset(integer9, 0, integer10), Blocks.END_PORTAL.defaultBlockState(), 2);
}
}
bjt3.globalLevelEvent(1038, fk5.offset(1, 0, 1), 0);
}
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.NONE);
if (cvf6.getType() == HitResult.Type.BLOCK && bjt.getBlockState(((BlockHitResult)cvf6).getBlockPos()).getBlock() == Blocks.END_PORTAL_FRAME) {
return InteractionResultHolder.<ItemStack>pass(bek5);
}
ayg.startUsingItem(ajh);
if (bjt instanceof ServerLevel) {
final BlockPos fk7 = ((ServerLevel)bjt).getChunkSource().getGenerator().findNearestMapFeature(bjt, "Stronghold", new BlockPos(ayg), 100, false);
if (fk7 != null) {
final EyeOfEnder ayp8 = new EyeOfEnder(bjt, ayg.getX(), ayg.getY(0.5), ayg.getZ());
ayp8.setItem(bek5);
ayp8.signalTo(fk7);
bjt.addFreshEntity(ayp8);
if (ayg instanceof ServerPlayer) {
CriteriaTriggers.USED_ENDER_EYE.trigger((ServerPlayer)ayg, fk7);
}
bjt.playSound(null, ayg.getX(), ayg.getY(), ayg.getZ(), SoundEvents.ENDER_EYE_LAUNCH, SoundSource.NEUTRAL, 0.5f, 0.4f / (EnderEyeItem.random.nextFloat() * 0.4f + 0.8f));
bjt.levelEvent(null, 1003, new BlockPos(ayg), 0);
if (!ayg.abilities.instabuild) {
bek5.shrink(1);
}
ayg.awardStat(Stats.ITEM_USED.get(this));
ayg.swing(ajh, true);
return InteractionResultHolder.<ItemStack>success(bek5);
}
}
return InteractionResultHolder.<ItemStack>consume(bek5);
}
}