-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRTPBCommand.java
More file actions
82 lines (74 loc) · 3.73 KB
/
Copy pathRTPBCommand.java
File metadata and controls
82 lines (74 loc) · 3.73 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
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.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.ResourceOrTagArgument;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.biome.Biome;
import java.util.HashMap;
import java.util.Map;
import static com.gmail.picono435.randomtp.api.RandomTPAPI.*;
public class RTPBCommand {
private static final Map<String, Long> cooldowns = new HashMap<String, Long>();
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext) {
dispatcher.register(Commands.literal("rtpb").requires(source -> hasPermission(source, "randomtp.command.interbiome"))
.then(
Commands.argument("biome", ResourceOrTagArgument.resourceOrTag(commandBuildContext, Registries.BIOME))
.executes(context ->
runCommand(context.getSource().getPlayerOrException(), ResourceOrTagArgument.getResourceOrTag(context, "biome", Registries.BIOME))
)
));
dispatcher.register(Commands.literal("biomertp").requires(source -> hasPermission(source, "randomtp.command.interbiome"))
.then(
Commands.argument("biome", ResourceOrTagArgument.resourceOrTag(commandBuildContext, Registries.BIOME))
.executes(context ->
runCommand(context.getSource().getPlayerOrException(), ResourceOrTagArgument.getResourceOrTag(context, "biome", Registries.BIOME))
)
));
}
public static int runCommand(ServerPlayer p, ResourceOrTagArgument.Result<Biome> biome) {
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());
ResourceKey<Biome> biomeKey = biome.unwrap().left().get().key();
ResourceLocation biomeLocation = biomeKey.location();
if(!inWhitelist(biomeLocation.toString())) {
p.sendSystemMessage(Component.literal(Messages.getBiomeNotAllowed().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{biomeId\\}", biomeLocation.toString()).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, p.serverLevel(), biomeKey);
cooldowns.put(p.getName().getString(), System.currentTimeMillis());
return 1;
}
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);
}
}
}