@ -1,15 +1,15 @@
package xyz.thewhitedog9487 ;
import com.mojang.brigadier.arguments. Long ArgumentType;
import com.mojang.brigadier.arguments. Integer ArgumentType;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback ;
import net.minecraft.block.Blocks ;
import net.minecraft.command.argument.EntityArgumentType ;
import net.minecraft.command.argument.Vec 3 ArgumentType;
import net.minecraft.command.argument.Vec 2 ArgumentType;
import net.minecraft.entity.Entity ;
import net.minecraft.server.command.ServerCommandSource ;
import net.minecraft.text.Text ;
import net.minecraft.util.math.BlockPos ;
import net.minecraft.util.math.Vec 3d ;
import net.minecraft.util.math.Vec 2f ;
import org.jetbrains.annotations.Nullable ;
import java.util.HashSet ;
@ -19,8 +19,11 @@ import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal ;
public class CommandRegister {
final static long WorldBorder = ( long ) 2 . 9e7 ;
static byte PermissionLevel = 2 ;
final static Integer WorldBorder = ( int ) 2 . 9e7 ;
/ / ↑
/ / https : / / zh . minecraft . wiki / w / % E4 % B8 % 96 % E7 % 95 % 8C % E8 % BE % B9 % E7 % 95 % 8C # % E5 % A4 % A7 % E5 % B0 % 8F
/ / https : / / minecraft . wiki / w / World_border # General_information
final static byte PermissionLevel = 2 ;
public static void Register ( String Name ) {
/ / / rtp
CommandRegistrationCallback . EVENT
@ -34,11 +37,11 @@ public class CommandRegister {
CommandRegistrationCallback . EVENT
. register ( ( dispatcher , registryAccess , environment ) - > {
dispatcher . register ( literal ( Name )
. then ( argument ( " Radius(半径) " , LongArgumentType. longArg ( ) )
. then ( argument ( " Radius(半径) " , IntegerArgumentType. integer ( ) )
. requires ( source - > source . hasPermissionLevel ( PermissionLevel ) )
. executes ( context - > execute_command (
context . getSource ( ) ,
LongArgumentType. getLong ( context , " Radius(半径) " ) ,
IntegerArgumentType. getInteger ( context , " Radius(半径) " ) ,
null ,
null ) ) ) ) ; } ) ;
@ -58,12 +61,12 @@ public class CommandRegister {
CommandRegistrationCallback . EVENT
. register ( ( dispatcher , registryAccess , environment ) - > {
dispatcher . register ( literal ( Name )
. then ( argument ( " Radius(半径) " , LongArgumentType. longArg ( ) )
. then ( argument ( " Radius(半径) " , IntegerArgumentType. integer ( ) )
. then ( argument ( " 被传送玩家名(PlayerID) " , EntityArgumentType . entity ( ) )
. requires ( source - > source . hasPermissionLevel ( PermissionLevel ) )
. executes ( context - > execute_command (
context . getSource ( ) ,
LongArgumentType. getLong ( context , " Radius(半径) " ) ,
IntegerArgumentType. getInteger ( context , " Radius(半径) " ) ,
EntityArgumentType . getEntity ( context , " 被传送玩家名(PlayerID) " ) ,
null ) ) ) ) ) ; } ) ;
@ -72,11 +75,11 @@ public class CommandRegister {
. register ( ( dispatcher , registryAccess , environment ) - > {
dispatcher . register ( literal ( Name )
. then ( argument ( " 被传送玩家名(PlayerID) " , EntityArgumentType . entity ( ) )
. then ( argument ( " Radius(半径) " , LongArgumentType. longArg ( ) )
. then ( argument ( " Radius(半径) " , IntegerArgumentType. integer ( ) )
. requires ( source - > source . hasPermissionLevel ( PermissionLevel ) )
. executes ( context - > execute_command (
context . getSource ( ) ,
LongArgumentType. getLong ( context , " Radius(半径) " ) ,
IntegerArgumentType. getInteger ( context , " Radius(半径) " ) ,
EntityArgumentType . getEntity ( context , " 被传送玩家名(PlayerID) " ) ,
null ) ) ) ) ) ; } ) ;
@ -96,100 +99,104 @@ public class CommandRegister {
CommandRegistrationCallback . EVENT
. register ( ( dispatcher , registryAccess , environment ) - > {
dispatcher . register ( literal ( Name )
. then ( argument ( " Radius(半径) " , LongArgumentType. longArg ( ) )
. then ( argument ( " OriginPos(随机中心,坐标) " , Vec 3ArgumentType. vec3 ( ) )
. then ( argument ( " Radius(半径) " , IntegerArgumentType. integer ( ) )
. then ( argument ( " OriginPos(随机中心,坐标) " , Vec 2ArgumentType. vec2 ( ) )
. requires ( source - > source . hasPermissionLevel ( PermissionLevel ) )
. executes ( context - > execute_command (
context . getSource ( ) ,
LongArgumentType. getLong ( context , " Radius(半径) " ) ,
IntegerArgumentType. getInteger ( context , " Radius(半径) " ) ,
null ,
Vec 3ArgumentType. getVec3 ( context , " OriginPos(随机中心,坐标) " ) ) ) ) ) ) ; } ) ;
Vec 2ArgumentType. getVec2 ( context , " OriginPos(随机中心,坐标) " ) ) ) ) ) ) ; } ) ;
/ / / rtp < Radius ( 半径 ) > < 被传送玩家名 ( PlayerID ) > < OriginEntity ( 随机中心 , 实体 ) >
CommandRegistrationCallback . EVENT
. register ( ( dispatcher , registryAccess , environment ) - > {
dispatcher . register ( literal ( Name )
. then ( argument ( " Radius(半径) " , LongArgumentType. longArg ( ) )
. then ( argument ( " Radius(半径) " , IntegerArgumentType. integer ( ) )
. then ( argument ( " 被传送玩家名(PlayerID) " , EntityArgumentType . entity ( ) )
. then ( argument ( " OriginEntity(随机中心,实体) " , EntityArgumentType . entity ( ) )
. requires ( source - > source . hasPermissionLevel ( PermissionLevel ) )
. executes ( context - > execute_command (
context . getSource ( ) ,
LongArgumentType. getLong ( context , " Radius(半径) " ) ,
IntegerArgumentType. getInteger ( context , " Radius(半径) " ) ,
EntityArgumentType . getEntity ( context , " 被传送玩家名(PlayerID) " ) ,
EntityArgumentType . getEntity ( context , " OriginEntity(随机中心,实体) " ) . getPos ( ) ) ) ) ) ) ) ; } ) ;
new Vec2f ( ( float ) EntityArgumentType . getEntity ( context , " OriginEntity(随机中心,实体) " ) . getPos ( ) . x ,
( float ) EntityArgumentType . getEntity ( context , " OriginEntity(随机中心,实体) " ) . getPos ( ) . y ) ) ) ) ) ) ) ; } ) ;
/ / / rtp < Radius ( 半径 ) > < 被传送玩家名 ( PlayerID ) > < OriginPos ( 随机中心 , 坐标 ) >
CommandRegistrationCallback . EVENT
. register ( ( dispatcher , registryAccess , environment ) - > {
dispatcher . register ( literal ( Name )
. then ( argument ( " Radius(半径) " , LongArgumentType. longArg ( ) )
. then ( argument ( " Radius(半径) " , IntegerArgumentType. integer ( ) )
. then ( argument ( " 被传送玩家名(PlayerID) " , EntityArgumentType . entity ( ) )
. then ( argument ( " OriginPos(随机中心,坐标) " , Vec 3ArgumentType. vec3 ( ) )
. then ( argument ( " OriginPos(随机中心,坐标) " , Vec 2ArgumentType. vec2 ( ) )
. requires ( source - > source . hasPermissionLevel ( PermissionLevel ) )
. executes ( context - > execute_command (
context . getSource ( ) ,
LongArgumentType. getLong ( context , " Radius(半径) " ) ,
IntegerArgumentType. getInteger ( context , " Radius(半径) " ) ,
EntityArgumentType . getEntity ( context , " 被传送玩家名(PlayerID) " ) ,
Vec 3ArgumentType. getVec3 ( context , " OriginPos(随机中心,坐标) " ) ) ) ) ) ) ) ; } ) ;
Vec 2ArgumentType. getVec2 ( context , " OriginPos(随机中心,坐标) " ) ) ) ) ) ) ) ; } ) ;
/ / / rtp < 被传送玩家名 ( PlayerID ) > < Radius ( 半径 ) > < OriginEntity ( 随机中心 , 实体 ) >
CommandRegistrationCallback . EVENT
. register ( ( dispatcher , registryAccess , environment ) - > {
dispatcher . register ( literal ( Name )
. then ( argument ( " 被传送玩家名(PlayerID) " , EntityArgumentType . entity ( ) )
. then ( argument ( " Radius(半径) " , LongArgumentType. longArg ( ) )
. then ( argument ( " Radius(半径) " , IntegerArgumentType. integer ( ) )
. then ( argument ( " OriginEntity(随机中心,实体) " , EntityArgumentType . entity ( ) )
. requires ( source - > source . hasPermissionLevel ( PermissionLevel ) )
. executes ( context - > execute_command (
context . getSource ( ) ,
LongArgumentType. getLong ( context , " Radius(半径) " ) ,
IntegerArgumentType. getInteger ( context , " Radius(半径) " ) ,
EntityArgumentType . getEntity ( context , " 被传送玩家名(PlayerID) " ) ,
EntityArgumentType . getEntity ( context , " OriginEntity(随机中心,实体) " ) . getPos ( ) ) ) ) ) ) ) ; } ) ;
new Vec2f ( ( float ) EntityArgumentType . getEntity ( context , " OriginEntity(随机中心,实体) " ) . getPos ( ) . x ,
( float ) EntityArgumentType . getEntity ( context , " OriginEntity(随机中心,实体) " ) . getPos ( ) . y ) ) ) ) ) ) ) ; } ) ;
/ / / rtp < 被传送玩家名 ( PlayerID ) > < Radius ( 半径 ) > < OriginPos ( 随机中心 , 坐标 ) >
CommandRegistrationCallback . EVENT
. register ( ( dispatcher , registryAccess , environment ) - > {
dispatcher . register ( literal ( Name )
. then ( argument ( " 被传送玩家名(PlayerID) " , EntityArgumentType . entity ( ) )
. then ( argument ( " Radius(半径) " , LongArgumentType. longArg ( ) )
. then ( argument ( " OriginPos(随机中心,坐标) " , Vec 3ArgumentType. vec3 ( ) )
. then ( argument ( " Radius(半径) " , IntegerArgumentType. integer ( ) )
. then ( argument ( " OriginPos(随机中心,坐标) " , Vec 2ArgumentType. vec2 ( ) )
. requires ( source - > source . hasPermissionLevel ( PermissionLevel ) )
. executes ( context - > execute_command (
context . getSource ( ) ,
LongArgumentType. getLong ( context , " Radius(半径) " ) ,
IntegerArgumentType. getInteger ( context , " Radius(半径) " ) ,
EntityArgumentType . getEntity ( context , " 被传送玩家名(PlayerID) " ) ,
Vec3ArgumentType . getVec3 ( context , " OriginPos(随机中心,坐标) " ) ) ) ) ) ) ) ; } ) ; }
Vec2ArgumentType . getVec2 ( context , " OriginPos(随机中心,坐标) " ) ) ) ) ) ) ) ; } ) ; }
public static void Register ( ) {
Register ( " 随机传送 " ) ;
Register ( " rtp " ) ; }
static int execute_command ( ServerCommandSource Source , @Nullable Long Radius , @Nullable Entity Player , @Nullable Vec 3d Origin ) {
static int execute_command ( ServerCommandSource Source , @Nullable Integer Radius , @Nullable Entity Player , @Nullable Vec 2f Origin ) {
Entity entity = Player = = null ? Source . getPlayer ( ) : Player ;
if ( entity = = null ) {
Source . sendFeedback ( ( ) - > { return Text . translatable ( " error.not_player " ) ; } , true ) ;
return - 1 ; }
if ( Radius = = null ) { Radius = WorldBorder - ( long ) 1e4 ; }
if ( Radius = = null ) { Radius = ( int ) ( WorldBorder - 1e4 ) ; }
/ / ↑ 远离世界边界
Radius = Math . abs ( Radius ) ;
long Coordinate_X ;
long Coordinate_Z ;
int Coordinate_X ;
int Coordinate_Z ;
if ( Origin = = null ) {
Coordinate_X = new SplittableRandom ( ) . next Long ( - Radius , Radius ) ;
Coordinate_Z = new SplittableRandom ( ) . next Long ( - Radius , Radius ) ; }
Coordinate_X = new SplittableRandom ( ) . next Int ( - Radius , Radius ) ;
Coordinate_Z = new SplittableRandom ( ) . next Int ( - Radius , Radius ) ; }
else {
Coordinate_X = new SplittableRandom ( ) . next Long( Math . round ( Origin . getX ( ) - Radius ) , Math . round ( Origin . getX ( ) + Radius ) ) ;
Coordinate_Z = new SplittableRandom ( ) . next Long( Math . round ( Origin . getZ ( ) - Radius ) , Math . round ( Origin . getZ ( ) + Radius ) ) ; }
Coordinate_X = new SplittableRandom ( ) . next Int( ( int ) Origin . x - Radius , ( int ) Origin . x + Radius ) ;
Coordinate_Z = new SplittableRandom ( ) . next Int( ( int ) Origin . y - Radius , ( int ) Origin . y + Radius ) ; }
int Coordinate_Y = 320 ;
for ( var CurrentBlock = Source . getWorld ( ) . getBlockState ( new BlockPos ( Math. toIntExact ( Coordinate_X) , Coordinate_Y , Math. toIntExact ( Coordinate_Z) ) ) . getBlock ( ) ;
for ( var CurrentBlock = Source . getWorld ( ) . getBlockState ( new BlockPos ( Coordinate_X, Coordinate_Y , Coordinate_Z) ) . getBlock ( ) ;
/ / 从世界顶层往下找 , 直到遇到一个非空气方块
Blocks . AIR = = CurrentBlock | |
Blocks . VOID_AIR = = CurrentBlock | |
Blocks . CAVE_AIR = = CurrentBlock
; CurrentBlock = Source . getWorld ( ) . getBlockState ( new BlockPos ( Math. toIntExact ( Coordinate_X) , Coordinate_Y , Math. toIntExact ( Coordinate_Z) ) ) . getBlock ( ) ) {
; CurrentBlock = Source . getWorld ( ) . getBlockState ( new BlockPos ( Coordinate_X, Coordinate_Y , Coordinate_Z) ) . getBlock ( ) ) {
Coordinate_Y - - ; }
for ( int x = - 1 ; x < = 1 ; x + + ) {
for ( int z = - 1 ; z < = 1 ; z + + ) {
/ / 如果传送到的位置周围一圈是空气 、 水或岩浆 , 将其替换为玻璃
var BlockPos = new BlockPos ( Math. toIntExact ( Coordinate_X - x ) , Coordinate_Y , Math. toIntExact ( Coordinate_Z - z ) ) ;
var BlockPos = new BlockPos ( Coordinate_X - x , Coordinate_Y , Coordinate_Z - z ) ;
var CurrentBlock = Source . getWorld ( ) . getBlockState ( BlockPos ) . getBlock ( ) ;
if ( CurrentBlock = = Blocks . AIR | |
CurrentBlock = = Blocks . VOID_AIR | |
@ -203,6 +210,7 @@ public class CommandRegister {
Coordinate_Y + + ;
/ / ↑ 高一层 , 人别站在土里了
entity . teleport ( Source . getWorld ( ) , Coordinate_X + 0 . 5 , Coordinate_Y , Coordinate_Z + 0 . 5 , new HashSet < > ( ) , entity . getYaw ( ) , entity . getPitch ( ) , false ) ;
Source . sendFeedback ( ( ) - > { return Text . translatable ( " info.success " , entity . getName ( ) , Coordinate_Z , Coordinate_Z , Coordinate_Z ) ; } , true ) ;
int finalCoordinate_Y = Coordinate_Y ;
Source . sendFeedback ( ( ) - > { return Text . translatable ( " info.success " , entity . getName ( ) , Coordinate_X , finalCoordinate_Y , Coordinate_Z ) ; } , true ) ;
return 0 ; }
}