-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRTPDCommand.java
More file actions
95 lines (87 loc) · 5.22 KB
/
RTPDCommand.java
File metadata and controls
95 lines (87 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.gmail.picono435.randomtp.commands;
import com.gmail.picono435.randomtp.config.Config;
import com.gmail.picono435.randomtp.config.Messages;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.DimensionArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.portal.TeleportTransition;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import static com.gmail.picono435.randomtp.api.RandomTPAPI.*;
public class RTPDCommand {
private static final Map<String, Long> cooldowns = new HashMap<String, Long>();
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("rtpd").requires(source -> hasPermission(source, "randomtp.command.interdim"))
.then(
Commands.argument("dimension", DimensionArgument.dimension())
.executes(context ->
runCommand(context.getSource().getPlayerOrException(), DimensionArgument.getDimension(context, "dimension"))
)
));
dispatcher.register(Commands.literal("dimensionrtp").requires(source -> hasPermission(source, "randomtp.command.interdim"))
.then(
Commands.argument("dimension", DimensionArgument.dimension())
.executes(context ->
runCommand(context.getSource().getPlayerOrException(), DimensionArgument.getDimension(context, "dimension"))
)
));
}
public static int runCommand(ServerPlayer p, ServerLevel dim) {
try {
if(!checkCooldown(p, cooldowns) && !hasPermission(p, "randomtp.cooldown.exempt")) {
long secondsLeft = getCooldownLeft(p, cooldowns);
Component cooldownmes = Component.literal(Messages.getCooldown().replaceAll("\\{secondsLeft\\}", Long.toString(secondsLeft)).replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("&", "§"));
p.sendSystemMessage(cooldownmes, false);
return 1;
} else {
cooldowns.remove(p.getName().getString());
String dimensionId = dim.dimension().location().getNamespace() + ":" + dim.dimension().location().getPath();
if(!inWhitelist(dimensionId)) {
p.sendSystemMessage(Component.literal(Messages.getDimensionNotAllowed().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{dimensionId\\}", dimensionId).replace('&', '§')), false);
return 1;
}
if(Config.useOriginal()) {
Component finding = Component.literal(Messages.getFinding().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{blockX\\}", "" + (int)p.position().x).replaceAll("\\{blockY\\}", "" + (int)p.position().y).replaceAll("\\{blockZ\\}", "" + (int)p.position().z).replaceAll("&", "§"));
p.sendSystemMessage(finding, false);
randomTeleport(p, dim);
cooldowns.put(p.getName().getString(), System.currentTimeMillis());
return 1;
}
TeleportTransition teleportTransition = new TeleportTransition(dim, p, TeleportTransition.DO_NOTHING);
p.teleport(teleportTransition);
double cal = p.serverLevel().getWorldBorder().getSize()/2;
BigDecimal num = new BigDecimal(cal);
String maxDistance = num.toPlainString();
if(Config.getMaxDistance() == 0) {
String command = "spreadplayers " + p.serverLevel().getWorldBorder().getCenterX() + " " + p.serverLevel().getWorldBorder().getCenterZ() + " " + Config.getMinDistance() + " " + maxDistance + " false " + p.getName().getString().toLowerCase();
p.getServer().getCommands().performCommand(p.getServer().getCommands().getDispatcher().parse(command, p.getServer().createCommandSourceStack()), command);
Component successful = Component.literal(Messages.getSuccessful().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{blockX\\}", "" + (int)p.position().x).replaceAll("\\{blockY\\}", "" + (int)p.position().y).replaceAll("\\{blockZ\\}", "" + (int)p.position().z).replaceAll("&", "§"));
p.sendSystemMessage(successful, false);
} else {
String command = "spreadplayers " + p.serverLevel().getWorldBorder().getCenterX() + " " + p.serverLevel().getWorldBorder().getCenterZ() + " " + Config.getMinDistance() + " " + Config.getMaxDistance() + " false " + p.getName().getString().toLowerCase();
p.getServer().getCommands().performCommand(p.getServer().getCommands().getDispatcher().parse(command, p.getServer().createCommandSourceStack()), command);
Component successful = Component.literal(Messages.getSuccessful().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{blockX\\}", "" + (int)p.position().x).replaceAll("\\{blockY\\}", "" + (int)p.position().y).replaceAll("\\{blockZ\\}", "" + (int)p.position().z).replaceAll("&", "§"));
p.sendSystemMessage(successful, false);
}
cooldowns.put(p.getName().getString(), System.currentTimeMillis());
}
} catch (Exception ex) {
ex.printStackTrace();
}
return 1;
}
private static boolean inWhitelist(String dimension) {
//WHITELIST
if(Config.useWhitelist()) {
return Config.getAllowedDimensions().contains(dimension);
//BLACKLIST
} else {
return !Config.getAllowedDimensions().contains(dimension);
}
}
}