|
1 | 1 | package xyz.lisbammisakait.item; |
2 | 2 |
|
| 3 | +import net.minecraft.block.Blocks; |
| 4 | +import net.minecraft.block.enums.BlockHalf; |
3 | 5 | import net.minecraft.entity.LivingEntity; |
4 | 6 | import net.minecraft.item.ItemStack; |
5 | 7 | import net.minecraft.item.SwordItem; |
6 | 8 | import net.minecraft.item.ToolMaterial; |
| 9 | +import net.minecraft.network.packet.s2c.play.PositionFlag; |
| 10 | +import net.minecraft.server.world.ServerWorld; |
| 11 | +import net.minecraft.util.math.BlockPos; |
| 12 | +import net.minecraft.util.math.Vec3d; |
| 13 | +import net.minecraft.world.BlockView; |
7 | 14 | import net.minecraft.world.World; |
8 | 15 | import xyz.lisbammisakait.RelightTheThreePointStrategy; |
| 16 | +import xyz.lisbammisakait.tools.SafeTp; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.EnumSet; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | + |
9 | 23 |
|
10 | 24 | public class HutouzhanjinqiangItem extends SwordItem { |
11 | 25 | public HutouzhanjinqiangItem(ToolMaterial material, float attackDamage, float attackSpeed, Settings settings) { |
12 | 26 | super(material, attackDamage, attackSpeed, settings); |
13 | | - |
14 | 27 | } |
| 28 | + @Override |
| 29 | + public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) { |
| 30 | + // 计算从攻击者指向被攻击者的方向向量 |
| 31 | + Vec3d direction = target.getPos().subtract(attacker.getPos()); |
| 32 | + // 对方向向量进行归一化,得到单位方向向量 |
| 33 | + Vec3d normalizedDirection = direction.normalize(); |
| 34 | + |
| 35 | + // 计算三格的偏移量 |
| 36 | + Vec3d offset = normalizedDirection.multiply(3); |
15 | 37 |
|
| 38 | + // 获取攻击者当前的位置 |
| 39 | + Vec3d currentPosition = attacker.getPos(); |
| 40 | + // 计算目标位置,即当前位置加上偏移量 |
| 41 | + Vec3d targetPosition = currentPosition.add(offset); |
16 | 42 |
|
| 43 | + // 获取攻击者所在的世界 |
| 44 | + ServerWorld world = (ServerWorld) attacker.getWorld(); |
| 45 | + |
| 46 | + // 这里简单使用空的标志集合,你可以根据需求修改 |
| 47 | + Set<PositionFlag> flags = Collections.emptySet(); |
| 48 | + |
| 49 | + // 计算攻击者面对被攻击者的偏航角和俯仰角 |
| 50 | + double dx = target.getX() - attacker.getX(); |
| 51 | + double dz = target.getZ() - attacker.getZ(); |
| 52 | + double dy = target.getY() - attacker.getY(); |
| 53 | + |
| 54 | + float yaw = (float) (Math.atan2(dz, dx) * 180 / Math.PI) - 90; |
| 55 | + float pitch = (float) -(Math.atan2(dy, Math.sqrt(dx * dx + dz * dz)) * 180 / Math.PI); |
| 56 | + |
| 57 | + // 不重置相机 |
| 58 | + boolean resetCamera = false; |
| 59 | + // 调用 teleport 方法将攻击者传送到目标位置,并设置朝向 |
| 60 | +// attacker.teleport(world, targetPosition.getX(), targetPosition.getY(), targetPosition.getZ(), flags, yaw, pitch, resetCamera); |
| 61 | + SafeTp.safeTp(attacker, world, targetPosition.getX(), targetPosition.getY(), targetPosition.getZ(), flags, yaw, pitch, resetCamera); |
| 62 | + RelightTheThreePointStrategy.LOGGER.info("攻击者传送到目标位置"); |
| 63 | + return super.postHit(stack, target, attacker); |
| 64 | + } |
| 65 | +// private boolean isSafeLocation(BlockView world, BlockPos pos) { |
| 66 | +// if (world.getBlockState(pos).isAir() && world.getBlockState(pos.up()).isAir()) { |
| 67 | +// return true; |
| 68 | +// } |
| 69 | +// if(world.getBlockState(pos).getBlock() == Blocks.LIGHT && world.getBlockState(pos.up()).getBlock() == Blocks.LIGHT){ |
| 70 | +// return true; |
| 71 | +// } |
| 72 | +// return false; |
| 73 | +// } |
17 | 74 |
|
18 | 75 |
|
19 | 76 |
|
|
0 commit comments