Skip to content

Commit b8fa752

Browse files
committed
新增虎头湛金枪
1 parent a21fe39 commit b8fa752

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

src/main/java/xyz/lisbammisakait/item/HutouzhanjinqiangItem.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,76 @@
11
package xyz.lisbammisakait.item;
22

3+
import net.minecraft.block.Blocks;
4+
import net.minecraft.block.enums.BlockHalf;
35
import net.minecraft.entity.LivingEntity;
46
import net.minecraft.item.ItemStack;
57
import net.minecraft.item.SwordItem;
68
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;
714
import net.minecraft.world.World;
815
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+
923

1024
public class HutouzhanjinqiangItem extends SwordItem {
1125
public HutouzhanjinqiangItem(ToolMaterial material, float attackDamage, float attackSpeed, Settings settings) {
1226
super(material, attackDamage, attackSpeed, settings);
13-
1427
}
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);
1537

38+
// 获取攻击者当前的位置
39+
Vec3d currentPosition = attacker.getPos();
40+
// 计算目标位置,即当前位置加上偏移量
41+
Vec3d targetPosition = currentPosition.add(offset);
1642

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+
// }
1774

1875

1976

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package xyz.lisbammisakait.tools;
2+
3+
import net.minecraft.block.Block;
4+
import net.minecraft.block.BlockState;
5+
import net.minecraft.block.Blocks;
6+
import net.minecraft.entity.LivingEntity;
7+
import net.minecraft.network.packet.s2c.play.PositionFlag;
8+
import net.minecraft.server.world.ServerWorld;
9+
import net.minecraft.util.math.BlockPos;
10+
import net.minecraft.world.BlockView;
11+
import xyz.lisbammisakait.RelightTheThreePointStrategy;
12+
13+
import java.util.Arrays;
14+
import java.util.HashSet;
15+
import java.util.Set;
16+
17+
public class SafeTp {
18+
//定义例外方块
19+
private static final Set<Block> ADDITIONAL_PASSABLE_BLOCKS = new HashSet<>(Arrays.asList(
20+
Blocks.LIGHT
21+
));
22+
23+
private static boolean isSafeLocation(BlockView world, BlockPos pos) {
24+
return isPassable(world, pos) && isPassable(world, pos.up());
25+
}
26+
27+
private static boolean isPassable(BlockView world, BlockPos pos) {
28+
BlockState blockState = world.getBlockState(pos);
29+
Block block = blockState.getBlock();
30+
// if (blockState.isAir()) {
31+
// return true;
32+
// }
33+
// 检查方块是否在额外可通过方块集合中
34+
if (ADDITIONAL_PASSABLE_BLOCKS.contains(block)) {
35+
return true;
36+
}
37+
// 检查方块是否为非实心方块
38+
return !blockState.isSolidBlock(world, pos);
39+
}
40+
public static boolean safeTp(LivingEntity TpMan, ServerWorld world, double destX, double destY, double destZ, Set<PositionFlag> flags, float yaw, float pitch, boolean resetCamera) {
41+
if(!isSafeLocation(world, new BlockPos((int) destX, (int) destY, (int) destZ))){
42+
RelightTheThreePointStrategy.LOGGER.info("传送失败,目标位置不安全");
43+
return false;
44+
}
45+
return TpMan.teleport(world, destX, destY, destZ, flags, yaw, pitch, resetCamera);
46+
}
47+
}

0 commit comments

Comments
 (0)