支持Minecraft 26.1.1
增加回溯到随机传送前的位置的功能
( 愚人节OTA正式版,那相当有操作了
( AI写代码未必靠谱,但是翻译还是很不错的
This commit is contained in:
TheWhiteDog9487
2026-04-04 14:18:40 +08:00
parent 4137a166ae
commit 6dff863d3f
10 changed files with 220 additions and 29 deletions

View File

@@ -18,11 +18,10 @@ import net.minecraft.core.BlockPos;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.chunk.status.ChunkStatus;
import net.minecraft.world.phys.Vec3;
import org.jspecify.annotations.Nullable;
import java.util.HashSet;
import java.util.Set;
import java.util.SplittableRandom;
import module java.base;
import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;
@@ -68,20 +67,34 @@ public class CommandRegister {
/**
* 执行命令所需权限等级
* @see TeleportCommand
* @see Commands
* @see <a href="https://zh.minecraft.wiki/w/%E6%9D%83%E9%99%90%E7%AD%89%E7%BA%A7">Minecraft Wiki (中文)</a>
* @see <a href="https://minecraft.wiki/w/Permission_level">Minecraft Wiki (English)</a>
*/
final static PermissionCheck PermissionLevel = Commands.LEVEL_GAMEMASTERS;
/**
* 在传送之前记录当前位置,以支持传送回去
*/
static Map<UUID, Vec3> OldPositions = new HashMap<>();
/**
* 命令执行失败时的返回值
* @see <a href="https://zh.minecraft.wiki/w/%E5%91%BD%E4%BB%A4#%E7%BB%93%E6%9E%9C">Minecraft Wiki (中文)</a>
* @see <a href="https://minecraft.wiki/w/Commands">Minecraft Wiki (English)</a>
*/
final static int CommandExecuteFailure = 0;
/**
* 使用Fabric API向游戏内注册命令
* @param Name 根命令名
* <br>
* @see <a href="https://docs.fabricmc.net/zh_cn/develop/commands/basics">Fabric Wiki (新样式,中文)</a>
* @see <a href="https://wiki.fabricmc.net/zh_cn:tutorial:commands">Fabric Wiki (旧样式,中文)</a>
* @see <a href="https://docs.fabricmc.net/develop/commands/basics">Fabric Wiki (New style,English)</a>
* @see <a href="https://wiki.fabricmc.net/tutorial:commands">Fabric Wiki (Old style,English)</a>
* @see <a href="https://docs.fabricmc.net/zh_cn/develop/commands/basics">Fabric Docs (中文)</a>
* @see <a href="https://wiki.fabricmc.net/zh_cn:tutorial:commands">Fabric Wiki (中文)</a>
* @see <a href="https://docs.fabricmc.net/develop/commands/basics">Fabric Docs (English)</a>
* @see <a href="https://wiki.fabricmc.net/tutorial:commands">Fabric Wiki (English)</a>
*/
public static void Register(String Name){
CommandRegistrationCallback.EVENT
.register((dispatcher, registryAccess, environment) ->{
dispatcher.register(literal(Name)
@@ -89,6 +102,25 @@ public class CommandRegister {
.requires(Commands.hasPermission(PermissionLevel))
.executes(context -> ExecuteCommand(
context.getSource(),null,null, null, null, null))
// /rtp back
.then(literal("back")
.requires(Commands.hasPermission(PermissionLevel))
.executes(context -> TeleportBack(
context.getSource(), null)))
// /rtp back <PlayerID(被传送玩家名)>
.then(literal("back")
.then(argument(CommandArgumentName_Target, EntityArgument.entity())
.requires(Commands.hasPermission(PermissionLevel))
.executes(context -> TeleportBack(
context.getSource(),
EntityArgument.getEntity(context,CommandArgumentName_Target)))))
// /rtp <PlayerID(被传送玩家名)> back
.then(argument(CommandArgumentName_Target, EntityArgument.entity())
.then(literal("back")
.requires(Commands.hasPermission(PermissionLevel))
.executes(context -> TeleportBack(
context.getSource(),
EntityArgument.getEntity(context,CommandArgumentName_Target)))))
// /rtp <Radius(半径)>
.then(argument(CommandArgumentName_Radius, IntegerArgumentType.integer(0))
.requires(Commands.hasPermission(PermissionLevel))
@@ -297,7 +329,7 @@ public class CommandRegister {
*/
if (TargetEntity == null) {
Source.sendSuccess(()->{ return Component.translatableWithFallback("error.no_target","不存在被传送目标由非玩家物体执行命令时请显式指定被传送玩家ID"); }, true);
return -1; }
return CommandExecuteFailure; }
int Coordinate_X = 0;
int Coordinate_Z = 0;
if (RegionFrom == null || RegionTo == null) { // ← 按半径和中心取随机
@@ -313,9 +345,8 @@ public class CommandRegister {
catch (IllegalArgumentException e) {
// 半径为零
if (Origin == null) {
Source.sendSuccess(() -> {
return Component.translatableWithFallback("warning.radius_equal_zero_no_target", "由于你设置的随机半径为0并且未设置随机中心点坐标因此什么都不会发生"); }, true);
return -1; }
Source.sendFailure(Component.translatableWithFallback("warning.radius_equal_zero_no_target", "由于你设置的随机半径为0并且未设置随机中心点坐标因此什么都不会发生"));
return CommandExecuteFailure; }
else {
Coordinate_X = (int) Origin.x;
Coordinate_Z = (int) Origin.y;
@@ -349,6 +380,7 @@ public class CommandRegister {
EntityWorld.setBlockAndUpdate(BlockPos, TargetBlock.defaultBlockState());}}}
// if ( String.valueOf(TargetEntity.getWorld().getBiome(new BlockPos(Math.toIntExact(Coordinate_X), Coordinate_Y, Math.toIntExact(Coordinate_Z))).getKey()).equals("minecraft:the_void") ) {
// Coordinate_Y++;}
OldPositions.put(TargetEntity.getUUID(), TargetEntity.position());
TargetEntity.teleportTo((ServerLevel) EntityWorld,Coordinate_X + 0.5, Coordinate_Y, Coordinate_Z + 0.5, new HashSet<>(), TargetEntity.getYRot(), TargetEntity.getXRot(), false);
int finalCoordinate_X = Coordinate_X;
int finalCoordinate_Z = Coordinate_Z;
@@ -356,4 +388,25 @@ public class CommandRegister {
final var FeedbackFallbackString = String.format("已将玩家%s传送到%d %d %d", TargetEntity.getName().getString(), Coordinate_X, Coordinate_Y, Coordinate_Z);
Source.sendSuccess(()->{ return Component.translatableWithFallback("info.success", FeedbackFallbackString, TargetEntity.getName(), finalCoordinate_X, Coordinate_Y, finalCoordinate_Z); },true);
return Command.SINGLE_SUCCESS; }
static int TeleportBack(CommandSourceStack Source, @Nullable Entity TargetEntity) {
TargetEntity = TargetEntity != null ? TargetEntity : Source.getPlayer();
/*
if (TargetEntity == null){
TargetEntity = Source.getPlayer(); }
*/
if (TargetEntity == null) {
Source.sendFailure(Component.translatableWithFallback("error.no_target", "不存在被传送目标由非玩家物体执行命令时请显式指定被传送玩家ID"));
return CommandExecuteFailure; }
var OldPos = OldPositions.get(TargetEntity.getUUID());
if (OldPos == null) {
Source.sendFailure(Component.translatableWithFallback("error.no_old_position", "%s还未使用过随机传送因此无法回溯", TargetEntity.getName().getString()));
return CommandExecuteFailure; }
else {
TargetEntity.teleportTo(( ServerLevel) TargetEntity.level(), OldPos.x, OldPos.y, OldPos.z, new HashSet<>(), TargetEntity.getYRot(), TargetEntity.getXRot(), false);
Entity finalTargetEntity = TargetEntity;
// ↑ "lambda 表达式中使用的变量应为 final 或有效 final"
Source.sendSuccess(() -> Component.translatableWithFallback("info.success.back", "已将玩家%s传送回原位置", finalTargetEntity.getName().getString()), true);
return Command.SINGLE_SUCCESS; } }
}

View File

@@ -19,6 +19,7 @@ public class RandomTeleporter implements ModInitializer {
// Proceed with mild caution.
ResourceReloaderListener.Register();
ServerLifecycleListener.Register();
CommandRegister.Register();
LOGGER.info("RandomTeleporter已写入命令注册回调目标命令将会在该注册的时候被注册");
}

View File

@@ -0,0 +1,9 @@
package xyz.thewhitedog9487;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
public class ServerLifecycleListener {
public static void Register() {
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
CommandRegister.OldPositions.clear();
RandomTeleporter.LOGGER.info("已清空传送历史记录"); } ); } }

View File

@@ -2,7 +2,9 @@
"modmenu.nameTranslation.randomteleporter": "RandomTeleporter",
"modmenu.descriptionTranslation.randomteleporter": "Added two commands for random teleportation",
"info.success": "Teleported %s to %d %d %d",
"info.success.back": "Teleported %s back to original position",
"error.no_target": "There is no teleported target, and the teleported player ID is explicitly specified when executed by a non-player object",
"error.no_old_position": "%s has not used random teleport yet, so it cannot be traced back",
"warning.radius_equal_zero": "Warning: Since you set a random radius of 0, selecting the right height will teleport you directly to %d %d",
"warning.radius_equal_zero_no_target": "Warning: Since you set a random radius of 0 and you don't set a random center point coordinates, nothing happens",
"bilibili": "Bilibili",

View File

@@ -2,7 +2,9 @@
"modmenu.nameTranslation.randomteleporter": "随机传送",
"modmenu.descriptionTranslation.randomteleporter": "增加了两个用于随机传送的命令",
"info.success": "已将玩家%s传送到%d %d %d",
"info.success.back": "已将玩家%s传送回原位置",
"error.no_target": "不存在被传送目标由非玩家物体执行命令时请显式指定被传送玩家ID",
"error.no_old_position": "%s还未使用过随机传送因此无法回溯",
"warning.radius_equal_zero": "警告由于你设置的随机半径为0因此在选择出合适高度之后将直接把你传送至%d %d",
"warning.radius_equal_zero_no_target": "警告由于你设置的随机半径为0并且未设置随机中心点坐标因此什么都不会发生",
"bilibili": "TheWhiteDog9487的哔哩哔哩主页",

View File

@@ -27,10 +27,10 @@
}
],
"depends": {
"fabricloader": ">=0.18.4",
"minecraft": "26.1",
"fabricloader": ">=0.18.6",
"minecraft": "26.1.1",
"java": ">=25",
"fabric-api": ">=0.144.3"
"fabric-api": ">=0.145.3"
},
"suggests": {
"another-mod": "*"
@@ -39,5 +39,5 @@
"modmenu": {
"links": {
"bilibili": "https://space.bilibili.com/401746666"},
"update_checker": true}}
"update_checker": true } }
}