103 lines
3.9 KiB
Java
103 lines
3.9 KiB
Java
package party._2a03.mc.command;
|
|
|
|
import com.mojang.brigadier.CommandDispatcher;
|
|
import com.mojang.brigadier.context.CommandContext;
|
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
|
import net.minecraft.command.argument.EntityArgumentType;
|
|
import net.minecraft.server.command.CommandManager;
|
|
import net.minecraft.server.command.ServerCommandSource;
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
|
import net.minecraft.text.Text;
|
|
|
|
import java.util.Hashtable;
|
|
import java.util.UUID;
|
|
|
|
public class TpaCommand {
|
|
private static class TpaRequest {
|
|
private UUID requester;
|
|
private UUID accepter;
|
|
|
|
public TpaRequest(UUID requester, UUID accepter) {
|
|
this.requester = requester;
|
|
this.accepter = accepter;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return requester.toString()+"->"+accepter.toString();
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return (requester.hashCode() << 1) ^ (accepter.hashCode() >> 1);
|
|
}
|
|
}
|
|
|
|
private static Hashtable<Integer, Long> requests = new Hashtable<>();
|
|
|
|
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
|
|
dispatcher.register(CommandManager.literal("tpa")
|
|
.then(CommandManager.argument("target", EntityArgumentType.player())
|
|
.executes(TpaCommand::executeRequest)
|
|
)
|
|
);
|
|
dispatcher.register(CommandManager.literal("tpaccept")
|
|
.then(CommandManager.argument("target", EntityArgumentType.player())
|
|
.executes(TpaCommand::executeAccept)
|
|
)
|
|
);
|
|
}
|
|
|
|
private static int executeRequest(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
|
|
ServerCommandSource source = ctx.getSource();
|
|
ServerPlayerEntity source_player = source.getPlayer();
|
|
ServerPlayerEntity target_player = EntityArgumentType.getPlayer(ctx, "target");
|
|
final String source_name = source_player.getEntityName();
|
|
final String target_name = target_player.getEntityName();
|
|
|
|
if (source_player.getUuid() == target_player.getUuid()) {
|
|
source.sendFeedback(() -> Text.of("Teleported "+source_name+" to "+target_name), false);
|
|
return 0;
|
|
}
|
|
|
|
TpaRequest req = new TpaRequest(source_player.getUuid(), target_player.getUuid());
|
|
requests.put(req.hashCode(), System.currentTimeMillis());
|
|
|
|
source.sendFeedback(() -> Text.of("Requested to teleport to "+target_name), false);
|
|
target_player.sendMessage(Text.of(source_name+" requested to teleport to you"));
|
|
target_player.sendMessage(Text.of("Type /tpaccept "+source_name+" within 30 seconds to accept it"));
|
|
return 1;
|
|
}
|
|
|
|
private static int executeAccept(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
|
|
ServerCommandSource source = ctx.getSource();
|
|
ServerPlayerEntity source_player = source.getPlayer();
|
|
ServerPlayerEntity target_player = EntityArgumentType.getPlayer(ctx, "target");
|
|
final String source_name = source_player.getEntityName();
|
|
final String target_name = target_player.getEntityName();
|
|
|
|
TpaRequest req = new TpaRequest(target_player.getUuid(), source_player.getUuid());
|
|
Long req_time = requests.get(req.hashCode());
|
|
|
|
if (req_time == null) {
|
|
source.sendFeedback(() -> Text.of(target_name+" has not requested to teleport to you"), false);
|
|
return 0;
|
|
}
|
|
if (System.currentTimeMillis() - req_time.longValue() > 30e3) {
|
|
requests.remove(req.hashCode());
|
|
source.sendFeedback(() -> Text.of(target_name+"'s teleport request has expired"), false);
|
|
return 0;
|
|
}
|
|
if (source_player.getAbilities().flying && !target_player.getAbilities().flying) {
|
|
source.sendFeedback(() -> Text.of("Stop flying, dumbass! You'll kill them!"), false);
|
|
return 0;
|
|
}
|
|
|
|
requests.remove(req.hashCode());
|
|
target_player.teleport(source_player.getServerWorld(), source_player.getX(), source_player.getY(), source_player.getZ(), source_player.getYaw(), source_player.getPitch());
|
|
source.sendFeedback(() -> Text.of("Teleported "+target_name+" to "+source_name), false);
|
|
target_player.sendMessage(Text.of(target_name+" has accepted your teleport request"));
|
|
return 1;
|
|
}
|
|
}
|