mirror of
https://github.com/TheWhiteDog9487/RandomTeleporter.git
synced 2025-09-16 05:56:59 +00:00
0.1.1
游戏版本1.19.1,功能全部完成
This commit is contained in:
63
src/main/java/xyz/thewhitedog9487/CommandRegister.java
Normal file
63
src/main/java/xyz/thewhitedog9487/CommandRegister.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package xyz.thewhitedog9487;
|
||||
|
||||
import com.mojang.brigadier.arguments.LongArgumentType;
|
||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||
import net.minecraft.block.Blocks;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
|
||||
import java.util.SplittableRandom;
|
||||
|
||||
import static net.minecraft.server.command.CommandManager.argument;
|
||||
import static net.minecraft.server.command.CommandManager.literal;
|
||||
import static xyz.thewhitedog9487.Toolbox.Distance;
|
||||
|
||||
public class CommandRegister {
|
||||
final static long WorldBorder = (long) 2.9e7;
|
||||
static byte Retry = 0;
|
||||
public static void Register(String Name){
|
||||
CommandRegistrationCallback.EVENT
|
||||
.register((dispatcher, registryAccess, environment) ->{
|
||||
dispatcher.register(literal(Name)
|
||||
.requires(source -> source.hasPermissionLevel(4))
|
||||
.executes(context -> execute_command_radius(
|
||||
context.getSource(),null)));});
|
||||
CommandRegistrationCallback.EVENT
|
||||
.register((dispatcher, registryAccess, environment) -> {
|
||||
dispatcher.register(literal(Name)
|
||||
.then(argument("Radius(半径)", LongArgumentType.longArg())
|
||||
.requires(source -> source.hasPermissionLevel(4))
|
||||
.executes(context -> execute_command_radius(
|
||||
context.getSource(),
|
||||
LongArgumentType.getLong(context, "Radius(半径)")))));});}
|
||||
public static void Register(){
|
||||
Register("随机传送");
|
||||
Register("rtp");}
|
||||
static int execute_command_radius(ServerCommandSource Source, Long Radius){
|
||||
if (Source.getPlayer() == null) {
|
||||
Source.sendFeedback(Text.translatable("error.twd.rtp.not_player"), true);
|
||||
return -1;}
|
||||
if (Radius == null){Radius = WorldBorder - (long) 1e4;}
|
||||
Radius = Math.abs(Radius);
|
||||
long Coordinate_X = new SplittableRandom().nextLong(-Radius, Radius);
|
||||
long Coordinate_Z = new SplittableRandom().nextLong(-Radius, Radius);
|
||||
int Coordinate_Y = 320;
|
||||
for (;
|
||||
Blocks.AIR == Source.getWorld().getBlockState(new BlockPos(Coordinate_X, Coordinate_Y, Coordinate_Z)).getBlock() ||
|
||||
Blocks.VOID_AIR == Source.getWorld().getBlockState(new BlockPos(Coordinate_X, Coordinate_Y, Coordinate_Z)).getBlock() ||
|
||||
Blocks.CAVE_AIR == Source.getWorld().getBlockState(new BlockPos(Coordinate_X, Coordinate_Y, Coordinate_Z)).getBlock()
|
||||
;Coordinate_Y--){}
|
||||
Coordinate_Y++;
|
||||
Vec3d Coordinate = new Vec3d(Coordinate_X, Coordinate_Y, Coordinate_Z);
|
||||
if (Radius == WorldBorder && Retry < 126 && Distance(Source.getPlayer().getPos(), Coordinate) < 1e5){
|
||||
Retry++;
|
||||
execute_command_radius(Source, Radius);
|
||||
return 0;}
|
||||
if (Retry >= 126){
|
||||
Source.sendFeedback(Text.translatable("warning.twd.rtp.retry"), true);}
|
||||
Source.getPlayer().teleport(Source.getWorld(), Coordinate_X, Coordinate_Y, Coordinate_Z, 0, 0);
|
||||
Source.sendFeedback(Text.translatable("info.twd.rtp.success", Source.getPlayer().getName(), Coordinate_X, Coordinate_Y, Coordinate_Z),true);
|
||||
return 0;}
|
||||
}
|
22
src/main/java/xyz/thewhitedog9487/RandomTeleporter.java
Normal file
22
src/main/java/xyz/thewhitedog9487/RandomTeleporter.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package xyz.thewhitedog9487;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class RandomTeleporter implements ModInitializer {
|
||||
// This logger is used to write text to the console and the log file.
|
||||
// It is considered best practice to use your mod id as the logger's name.
|
||||
// That way, it's clear which mod wrote info, warnings, and errors.
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger("randomteleporter");
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
// This code runs as soon as Minecraft is in a mod-load-ready state.
|
||||
// However, some things (like resources) may still be uninitialized.
|
||||
// Proceed with mild caution.
|
||||
CommandRegister.Register();
|
||||
LOGGER.info("Hello Fabric world!");
|
||||
}
|
||||
}
|
10
src/main/java/xyz/thewhitedog9487/Toolbox.java
Normal file
10
src/main/java/xyz/thewhitedog9487/Toolbox.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package xyz.thewhitedog9487;
|
||||
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
|
||||
public class Toolbox {
|
||||
static public double Distance(Vec3d A, Vec3d B){
|
||||
return Math.sqrt(
|
||||
Math.pow(A.getX() - B.getX(), 2) +
|
||||
Math.pow(A.getY() - B.getY(), 2));}
|
||||
}
|
15
src/main/java/xyz/thewhitedog9487/mixin/ExampleMixin.java
Normal file
15
src/main/java/xyz/thewhitedog9487/mixin/ExampleMixin.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package xyz.thewhitedog9487.mixin;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(MinecraftServer.class)
|
||||
public class ExampleMixin {
|
||||
@Inject(at = @At("HEAD"), method = "loadWorld")
|
||||
private void init(CallbackInfo info) {
|
||||
// This code is injected into the start of MinecraftServer.loadWorld()V
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user