| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.server.level.ServerPlayer;
|
| 7 | import net.minecraft.sounds.SoundEvents;
|
| 8 | import net.minecraft.sounds.SoundSource;
|
| 9 | import net.minecraft.stats.Stats;
|
| 10 | import net.minecraft.tags.StructureTags;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.world.InteractionHand;
|
| 13 | import net.minecraft.world.InteractionResult;
|
| 14 | import net.minecraft.world.entity.LivingEntity;
|
| 15 | import net.minecraft.world.entity.player.Player;
|
| 16 | import net.minecraft.world.entity.projectile.EyeOfEnder;
|
| 17 | import net.minecraft.world.item.context.UseOnContext;
|
| 18 | import net.minecraft.world.level.ClipContext;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.block.Block;
|
| 21 | import net.minecraft.world.level.block.Blocks;
|
| 22 | import net.minecraft.world.level.block.EndPortalFrameBlock;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.block.state.pattern.BlockPattern;
|
| 25 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 26 | import net.minecraft.world.phys.BlockHitResult;
|
| 27 | import net.minecraft.world.phys.HitResult;
|
| 28 | import net.minecraft.world.phys.Vec3;
|
| 29 |
|
| 30 | public class EnderEyeItem extends Item {
|
| 31 | public EnderEyeItem(final Item.Properties properties) {
|
| 32 | super(properties);
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public InteractionResult useOn(final UseOnContext context) {
|
| 37 | Level level = context.getLevel();
|
| 38 | BlockPos pos = context.getClickedPos();
|
| 39 | BlockState targetState = level.getBlockState(pos);
|
| 40 | if (!targetState.is(Blocks.END_PORTAL_FRAME) || targetState.getValue(EndPortalFrameBlock.HAS_EYE)) {
|
| 41 | return InteractionResult.PASS;
|
| 42 | }
|
| 43 |
|
| 44 | if (level.isClientSide()) {
|
| 45 | return InteractionResult.SUCCESS;
|
| 46 | }
|
| 47 |
|
| 48 | BlockState newState = targetState.setValue(EndPortalFrameBlock.HAS_EYE, true);
|
| 49 | Block.pushEntitiesUp(targetState, newState, level, pos);
|
| 50 | level.setBlock(pos, newState, 2);
|
| 51 | level.updateNeighbourForOutputSignal(pos, Blocks.END_PORTAL_FRAME);
|
| 52 | context.getItemInHand().shrink(1);
|
| 53 | level.levelEvent(1503, pos, 0);
|
| 54 | BlockPattern.BlockPatternMatch match = EndPortalFrameBlock.getOrCreatePortalShape().find(level, pos);
|
| 55 | if (match != null) {
|
| 56 | BlockPos blockPos = match.getFrontTopLeft().offset(-3, 0, -3);
|
| 57 |
|
| 58 | for (int x = 0; x < 3; x++) {
|
| 59 | for (int z = 0; z < 3; z++) {
|
| 60 | BlockPos portalBlockPos = blockPos.offset(x, 0, z);
|
| 61 | level.destroyBlock(portalBlockPos, true, null);
|
| 62 | level.setBlock(portalBlockPos, Blocks.END_PORTAL.defaultBlockState(), 2);
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | level.globalLevelEvent(1038, blockPos.offset(1, 0, 1), 0);
|
| 67 | }
|
| 68 |
|
| 69 | return InteractionResult.SUCCESS;
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public int getUseDuration(final ItemStack itemStack, final LivingEntity user) {
|
| 74 | return 0;
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
|
| 79 | ItemStack itemStack = player.getItemInHand(hand);
|
| 80 | BlockHitResult hitResult = getPlayerPOVHitResult(level, player, ClipContext.Fluid.NONE);
|
| 81 | if (hitResult.getType() == HitResult.Type.BLOCK && level.getBlockState(hitResult.getBlockPos()).is(Blocks.END_PORTAL_FRAME)) {
|
| 82 | return InteractionResult.PASS;
|
| 83 | }
|
| 84 |
|
| 85 | player.startUsingItem(hand);
|
| 86 | if (level instanceof ServerLevel serverLevel) {
|
| 87 | BlockPos nearestMapFeature = serverLevel.findNearestMapStructure(StructureTags.EYE_OF_ENDER_LOCATED, player.blockPosition(), 100, false);
|
| 88 | if (nearestMapFeature == null) {
|
| 89 | return InteractionResult.CONSUME;
|
| 90 | }
|
| 91 |
|
| 92 | EyeOfEnder eyeOfEnder = new EyeOfEnder(level, player.getX(), player.getY(0.5), player.getZ());
|
| 93 | eyeOfEnder.setItem(itemStack);
|
| 94 | eyeOfEnder.signalTo(Vec3.atLowerCornerOf(nearestMapFeature));
|
| 95 | level.gameEvent(GameEvent.PROJECTILE_SHOOT, eyeOfEnder.position(), GameEvent.Context.of(player));
|
| 96 | level.addFreshEntity(eyeOfEnder);
|
| 97 | if (player instanceof ServerPlayer serverPlayer) {
|
| 98 | CriteriaTriggers.USED_ENDER_EYE.trigger(serverPlayer, nearestMapFeature);
|
| 99 | }
|
| 100 |
|
| 101 | float pitch = Mth.lerp(level.getRandom().nextFloat(), 0.33F, 0.5F);
|
| 102 | level.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ENDER_EYE_LAUNCH, SoundSource.NEUTRAL, 1.0F, pitch);
|
| 103 | itemStack.consume(1, player);
|
| 104 | player.awardStat(Stats.ITEM_USED.get(this));
|
| 105 | }
|
| 106 |
|
| 107 | return InteractionResult.SUCCESS_SERVER;
|
| 108 | }
|
| 109 | }
|
| 110 |
|