Skip to content

Commit 3926291

Browse files
authored
Merge pull request #4 from icceey/copilot/fix-d27fee48-07e0-4472-98a3-6d1b45df969d
Add riding entity teleportation support to /tlp command
2 parents b6dd825 + 957840b commit 3926291

2 files changed

Lines changed: 46 additions & 11 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@ A Minecraft Forge mod that provides enhanced teleportation functionality. Easily
55
## Features
66

77
- **Simple Commands**: Easy-to-use commands for teleporting players
8+
- **Riding Entity Teleportation**: When teleporting while riding a living entity (horse, pig, etc.), both player and mount are teleported together
89
- **No Dependencies**: This mod does not require any additional libraries or mods to function
910

1011
## Usage
1112

1213
Run the following commands in-game:
1314
- `/tlp <player>`: Teleports you to the specified player.
1415

16+
### Riding Entity Teleportation
17+
When you use `/tlp` while riding a living entity (such as a horse, pig, donkey, mule, llama, camel, or strider), both you and your mount will be teleported together to the destination. The riding relationship is maintained after teleportation.
18+
19+
**Supported Living Entities:**
20+
- Horses, Donkeys, Mules
21+
- Pigs, Striders
22+
- Llamas, Camels
23+
- Any other rideable living entities
24+
25+
**Note:** Non-living vehicles like boats and minecarts are not teleported - only the player will be teleported in these cases.
26+
1527
**Note**: This mod is not recommended for use on PvP servers as it may provide unfair advantages.
1628

1729
## Compatibility

src/main/java/com/icceey/onlytp/command/TeleportCommand.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import net.minecraft.sounds.SoundEvents;
1818
import net.minecraft.sounds.SoundSource;
1919
import net.minecraft.util.RandomSource;
20+
import net.minecraft.world.entity.LivingEntity;
21+
import net.minecraft.world.entity.RelativeMovement;
2022

2123
import java.util.stream.IntStream;
2224

@@ -74,6 +76,12 @@ private static int execute(CommandContext<CommandSourceStack> context) throws Co
7476
return 0;
7577
}
7678

79+
// 检查玩家是否正在骑乘生物
80+
LivingEntity livingRidingEntity = null;
81+
if (executor.getVehicle() instanceof LivingEntity ridingEntity) {
82+
livingRidingEntity = ridingEntity;
83+
}
84+
7785
// 在原位置播放下界传送门音效
7886
executor.level().playSound(
7987
null, // 发送给所有玩家,无需特定玩家
@@ -89,15 +97,30 @@ private static int execute(CommandContext<CommandSourceStack> context) throws Co
8997
// 在出发点生成末影人粒子效果
9098
spawnTeleportParticles(executor, executor.serverLevel(), ParticleTypes.PORTAL);
9199

92-
// 执行传送
93-
executor.teleportTo(
94-
targetPlayer.serverLevel(),
95-
targetPlayer.getX(),
96-
targetPlayer.getY(),
97-
targetPlayer.getZ(),
98-
targetPlayer.getYRot(),
99-
targetPlayer.getXRot()
100-
);
100+
// 目标坐标
101+
ServerLevel targetLevel = targetPlayer.serverLevel();
102+
double targetX = targetPlayer.getX();
103+
double targetY = targetPlayer.getY();
104+
double targetZ = targetPlayer.getZ();
105+
float targetYRot = targetPlayer.getYRot();
106+
float targetXRot = targetPlayer.getXRot();
107+
108+
// 如果玩家在骑乘状态且骑乘的是生物,先处理骑乘生物的传送
109+
if (livingRidingEntity != null && livingRidingEntity.isAlive()) {
110+
// 让玩家下马(但保持引用)
111+
executor.stopRiding();
112+
113+
// 传送骑乘生物到目标位置
114+
livingRidingEntity.teleportTo(targetLevel, targetX, targetY, targetZ, RelativeMovement.ALL, targetYRot, targetXRot);
115+
}
116+
117+
// 执行玩家传送
118+
executor.teleportTo(targetLevel, targetX, targetY, targetZ, targetYRot, targetXRot);
119+
120+
if (livingRidingEntity != null && livingRidingEntity.isAlive()) {
121+
// 确保骑乘生物在同一个世界且位置正确后,让玩家重新骑上
122+
executor.startRiding(livingRidingEntity, true);
123+
}
101124

102125
// 发送成功消息
103126
source.sendSuccess(() -> translatableWithFallback("commands.onlytp.success", targetPlayer.getGameProfile().getName()), true);
@@ -126,8 +149,8 @@ private static int execute(CommandContext<CommandSourceStack> context) throws Co
126149
/**
127150
* 在玩家周围生成传送粒子效果
128151
*
129-
* @param player 作为粒子生成中心的玩家
130-
* @param level 要在其中生成粒子的世界
152+
* @param player 作为粒子生成中心的玩家
153+
* @param level 要在其中生成粒子的世界
131154
* @param particleType 要生成的粒子类型
132155
*/
133156
private static void spawnTeleportParticles(ServerPlayer player, ServerLevel level, ParticleOptions particleType) {

0 commit comments

Comments
 (0)