Skip to content

Commit 77f2600

Browse files
committed
Make commands public to ease reuse in other mod
1 parent 007c0c2 commit 77f2600

3 files changed

Lines changed: 37 additions & 36 deletions

File tree

common/src/main/java/com/gmail/picono435/randomtp/commands/RTPBCommand.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.gmail.picono435.randomtp.commands;
22

3-
import com.gmail.picono435.randomtp.api.RandomTPAPI;
43
import com.gmail.picono435.randomtp.config.Config;
54
import com.gmail.picono435.randomtp.config.Messages;
65
import com.mojang.brigadier.CommandDispatcher;
@@ -18,19 +17,21 @@
1817
import java.util.HashMap;
1918
import java.util.Map;
2019

20+
import static com.gmail.picono435.randomtp.api.RandomTPAPI.*;
21+
2122
public class RTPBCommand {
2223

23-
private static Map<String, Long> cooldowns = new HashMap<String, Long>();
24+
private static final Map<String, Long> cooldowns = new HashMap<String, Long>();
2425

2526
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext) {
26-
dispatcher.register(Commands.literal("rtpb").requires(source -> RandomTPAPI.hasPermission(source, "randomtp.command.interbiome"))
27+
dispatcher.register(Commands.literal("rtpb").requires(source -> hasPermission(source, "randomtp.command.interbiome"))
2728
.then(
2829
Commands.argument("biome", ResourceOrTagArgument.resourceOrTag(commandBuildContext, Registries.BIOME))
2930
.executes(context ->
3031
runCommand(context.getSource().getPlayerOrException(), ResourceOrTagArgument.getResourceOrTag(context, "biome", Registries.BIOME))
3132
)
3233
));
33-
dispatcher.register(Commands.literal("biomertp").requires(source -> RandomTPAPI.hasPermission(source, "randomtp.command.interbiome"))
34+
dispatcher.register(Commands.literal("biomertp").requires(source -> hasPermission(source, "randomtp.command.interbiome"))
3435
.then(
3536
Commands.argument("biome", ResourceOrTagArgument.resourceOrTag(commandBuildContext, Registries.BIOME))
3637
.executes(context ->
@@ -39,10 +40,10 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher, Co
3940
));
4041
}
4142

42-
private static int runCommand(ServerPlayer p, ResourceOrTagArgument.Result<Biome> biome) {
43+
public static int runCommand(ServerPlayer p, ResourceOrTagArgument.Result<Biome> biome) {
4344
try {
44-
if(!RandomTPAPI.checkCooldown(p, cooldowns) && !RandomTPAPI.hasPermission(p, "randomtp.cooldown.exempt")) {
45-
long secondsLeft = RandomTPAPI.getCooldownLeft(p, cooldowns);
45+
if(!checkCooldown(p, cooldowns) && !hasPermission(p, "randomtp.cooldown.exempt")) {
46+
long secondsLeft = getCooldownLeft(p, cooldowns);
4647
Component cooldownmes = Component.literal(Messages.getCooldown().replaceAll("\\{secondsLeft\\}", Long.toString(secondsLeft)).replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("&", "§"));
4748
p.sendSystemMessage(cooldownmes, false);
4849
return 1;
@@ -57,7 +58,7 @@ private static int runCommand(ServerPlayer p, ResourceOrTagArgument.Result<Biome
5758
if(Config.useOriginal()) {
5859
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("&", "§"));
5960
p.sendSystemMessage(finding, false);
60-
RandomTPAPI.randomTeleport(p, p.serverLevel(), biomeKey);
61+
randomTeleport(p, p.serverLevel(), biomeKey);
6162
cooldowns.put(p.getName().getString(), System.currentTimeMillis());
6263
return 1;
6364
}

common/src/main/java/com/gmail/picono435/randomtp/commands/RTPCommand.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
package com.gmail.picono435.randomtp.commands;
22

3-
import java.math.BigDecimal;
4-
import java.util.HashMap;
5-
import java.util.Map;
6-
7-
import com.gmail.picono435.randomtp.api.RandomTPAPI;
83
import com.gmail.picono435.randomtp.config.Config;
94
import com.gmail.picono435.randomtp.config.Messages;
105
import com.mojang.brigadier.CommandDispatcher;
11-
126
import net.minecraft.commands.CommandSourceStack;
137
import net.minecraft.commands.Commands;
148
import net.minecraft.network.chat.Component;
159
import net.minecraft.server.level.ServerPlayer;
1610

11+
import java.math.BigDecimal;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import static com.gmail.picono435.randomtp.api.RandomTPAPI.*;
16+
1717
public class RTPCommand {
1818

19-
private static Map<String, Long> cooldowns = new HashMap<String, Long>();
19+
private static final Map<String, Long> cooldowns = new HashMap<String, Long>();
2020

2121
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
22-
dispatcher.register(Commands.literal("rtp").requires(source -> RandomTPAPI.hasPermission(source, "randomtp.command.basic"))
22+
dispatcher.register(Commands.literal("rtp").requires(source -> hasPermission(source, "randomtp.command.basic"))
2323
.executes(context -> runCommand(context.getSource().getPlayerOrException())
2424
));
25-
dispatcher.register(Commands.literal("randomtp").requires(source -> RandomTPAPI.hasPermission(source, "randomtp.command.basic"))
25+
dispatcher.register(Commands.literal("randomtp").requires(source -> hasPermission(source, "randomtp.command.basic"))
2626
.executes(context -> runCommand(context.getSource().getPlayerOrException())
2727
));
2828
}
2929

30-
private static int runCommand(ServerPlayer p) {
30+
public static int runCommand(ServerPlayer p) {
3131
try {
32-
if(!RandomTPAPI.checkCooldown(p, cooldowns) && !RandomTPAPI.hasPermission(p, "randomtp.cooldown.exempt")) {
33-
long secondsLeft = RandomTPAPI.getCooldownLeft(p, cooldowns);
32+
if(!checkCooldown(p, cooldowns) && !hasPermission(p, "randomtp.cooldown.exempt")) {
33+
long secondsLeft = getCooldownLeft(p, cooldowns);
3434
Component cooldownmes = Component.literal(Messages.getCooldown().replaceAll("\\{secondsLeft\\}", Long.toString(secondsLeft)).replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("&", "§"));
3535
p.sendSystemMessage(cooldownmes, false);
3636
return 1;
@@ -40,9 +40,9 @@ private static int runCommand(ServerPlayer p) {
4040
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("&", "§"));
4141
p.sendSystemMessage(finding, false);
4242
if(!Config.getDefaultWorld().equals("playerworld")) {
43-
RandomTPAPI.randomTeleport(p, RandomTPAPI.getWorld(Config.getDefaultWorld(), p.getServer()));
43+
randomTeleport(p, getWorld(Config.getDefaultWorld(), p.getServer()));
4444
} else {
45-
RandomTPAPI.randomTeleport(p, p.serverLevel());
45+
randomTeleport(p, p.serverLevel());
4646
}
4747
cooldowns.put(p.getName().getString(), System.currentTimeMillis());
4848
return 1;

common/src/main/java/com/gmail/picono435/randomtp/commands/RTPDCommand.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
package com.gmail.picono435.randomtp.commands;
22

3-
import java.math.BigDecimal;
4-
import java.util.HashMap;
5-
import java.util.Map;
6-
7-
import com.gmail.picono435.randomtp.api.RandomTPAPI;
83
import com.gmail.picono435.randomtp.config.Config;
94
import com.gmail.picono435.randomtp.config.Messages;
105
import com.mojang.brigadier.CommandDispatcher;
11-
126
import net.minecraft.commands.CommandSourceStack;
137
import net.minecraft.commands.Commands;
148
import net.minecraft.commands.arguments.DimensionArgument;
159
import net.minecraft.network.chat.Component;
1610
import net.minecraft.server.level.ServerLevel;
1711
import net.minecraft.server.level.ServerPlayer;
1812

13+
import java.math.BigDecimal;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
import static com.gmail.picono435.randomtp.api.RandomTPAPI.*;
18+
1919
public class RTPDCommand {
2020

21-
private static Map<String, Long> cooldowns = new HashMap<String, Long>();
21+
private static final Map<String, Long> cooldowns = new HashMap<String, Long>();
2222

2323
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
24-
dispatcher.register(Commands.literal("rtpd").requires(source -> RandomTPAPI.hasPermission(source, "randomtp.command.interdim"))
24+
dispatcher.register(Commands.literal("rtpd").requires(source -> hasPermission(source, "randomtp.command.interdim"))
2525
.then(
2626
Commands.argument("dimension", DimensionArgument.dimension())
2727
.executes(context ->
2828
runCommand(context.getSource().getPlayerOrException(), DimensionArgument.getDimension(context, "dimension"))
2929
)
3030
));
31-
dispatcher.register(Commands.literal("dimensionrtp").requires(source -> RandomTPAPI.hasPermission(source, "randomtp.command.interdim"))
31+
dispatcher.register(Commands.literal("dimensionrtp").requires(source -> hasPermission(source, "randomtp.command.interdim"))
3232
.then(
3333
Commands.argument("dimension", DimensionArgument.dimension())
3434
.executes(context ->
@@ -37,24 +37,24 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
3737
));
3838
}
3939

40-
private static int runCommand(ServerPlayer p, ServerLevel dim) {
40+
public static int runCommand(ServerPlayer p, ServerLevel dim) {
4141
try {
42-
if(!RandomTPAPI.checkCooldown(p, cooldowns) && !RandomTPAPI.hasPermission(p, "randomtp.cooldown.exempt")) {
43-
long secondsLeft = RandomTPAPI.getCooldownLeft(p, cooldowns);
42+
if(!checkCooldown(p, cooldowns) && !hasPermission(p, "randomtp.cooldown.exempt")) {
43+
long secondsLeft = getCooldownLeft(p, cooldowns);
4444
Component cooldownmes = Component.literal(Messages.getCooldown().replaceAll("\\{secondsLeft\\}", Long.toString(secondsLeft)).replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("&", "§"));
4545
p.sendSystemMessage(cooldownmes, false);
4646
return 1;
4747
} else {
4848
cooldowns.remove(p.getName().getString());
4949
String dimensionId = dim.dimension().location().getNamespace() + ":" + dim.dimension().location().getPath();
5050
if(!inWhitelist(dimensionId)) {
51-
p.sendSystemMessage(Component.literal(Messages.getDimensionNotAllowed().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{dimensionId\\}", dimensionId.toString()).replace('&', '§')), false);
51+
p.sendSystemMessage(Component.literal(Messages.getDimensionNotAllowed().replaceAll("\\{playerName\\}", p.getName().getString()).replaceAll("\\{dimensionId\\}", dimensionId).replace('&', '§')), false);
5252
return 1;
5353
}
5454
if(Config.useOriginal()) {
5555
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("&", "§"));
5656
p.sendSystemMessage(finding, false);
57-
RandomTPAPI.randomTeleport(p, dim);
57+
randomTeleport(p, dim);
5858
cooldowns.put(p.getName().getString(), System.currentTimeMillis());
5959
return 1;
6060
}

0 commit comments

Comments
 (0)