1
0

Add /tpa and /tpaccept

This commit is contained in:
Ryan Fox 2023-06-13 16:24:46 -07:00
parent 9358086717
commit fa28c8bed1
Signed by: flewkey
GPG Key ID: 94F56ADFD848851E
2 changed files with 104 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import party._2a03.mc.command.HomeCommand;
import party._2a03.mc.command.NameCommand;
import party._2a03.mc.command.SkinCommand;
import party._2a03.mc.command.SpawnCommand;
import party._2a03.mc.command.TpaCommand;
import party._2a03.mc.util.Config;
import party._2a03.mc.util.Database;
@ -61,6 +62,7 @@ public class MinecraftTweaks2a03 implements ModInitializer {
NameCommand.register(dispatcher);
SkinCommand.register(dispatcher);
SpawnCommand.register(dispatcher);
TpaCommand.register(dispatcher);
}
});
}

View File

@ -0,0 +1,102 @@
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;
}
}