Skip to content

Commit 6bc0b18

Browse files
authored
Merge pull request #77 from CanadianBaconBoi/dev
Reimplemented Dimension Blacklists for 1.21
2 parents 19b9bd2 + a0e58cb commit 6bc0b18

File tree

5 files changed

+76
-18
lines changed

5 files changed

+76
-18
lines changed

common/src/main/java/dev/ftb/mods/ftbessentials/commands/groups/TeleportingCommands.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private static int spawn(ServerPlayer player) {
110110

111111
//#region RTP
112112
private static int rtp(ServerPlayer player) {
113-
if (!player.hasPermissions(2) && !DimensionFilter.isDimensionOK(player.level().dimension())) {
113+
if (!player.hasPermissions(2) && !DimensionFilter.isRtpDimensionOK(player.level().dimension())) {
114114
player.displayClientMessage(Component.literal("You may not use /rtp in this dimension!").withStyle(ChatFormatting.RED), false);
115115
return 0;
116116
}

common/src/main/java/dev/ftb/mods/ftbessentials/config/FTBEConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public interface FTBEConfig {
7171
.comment("Allows admins to teleport to dimension");
7272
ToggleableConfig JUMP = new ToggleableConfig(TELEPORTATION, "jump")
7373
.comment("Allows admins to jump (teleport) to the focused block");
74+
75+
SNBTConfig TELEPORTATION_BLACKLISTS = TELEPORTATION.addGroup("blacklists")
76+
.comment("Blacklists for all teleport commands",
77+
"Wildcarded dimensions (e.g. 'somemod:*') are supported");
78+
StringListValue TELEPORTATION_BLACKLIST_FROM = TELEPORTATION_BLACKLISTS.addStringList("from", List.of())
79+
.comment("Dimensions players aren't permitted to run teleport commands in.");
80+
StringListValue TELEPORTATION_BLACKLIST_TO = TELEPORTATION_BLACKLISTS.addStringList("to", List.of())
81+
.comment("Dimensions players aren't permitted to teleport into.");
7482

7583
SNBTConfig ADMIN = CONFIG.addGroup("admin").comment("Admin commands for cheating and moderation");
7684
ToggleableConfig HEAL = new ToggleableConfig(ADMIN, "heal")

common/src/main/java/dev/ftb/mods/ftbessentials/util/DimensionFilter.java

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,61 @@
1111
import java.util.function.Predicate;
1212

1313
public class DimensionFilter {
14-
private static WildcardedRLMatcher dimensionMatcherB = null;
15-
private static WildcardedRLMatcher dimensionMatcherW = null;
14+
private static WildcardedRLMatcher rtpDimensionMatcherB = null;
15+
private static WildcardedRLMatcher rtpDimensionMatcherW = null;
1616

17-
public static boolean isDimensionOK(ResourceKey<Level> levelKey) {
17+
private static WildcardedRLMatcher allDimensionMatcherBTo = null;
18+
private static WildcardedRLMatcher allDimensionMatcherBFrom = null;
19+
20+
public static boolean isRtpDimensionOK(ResourceKey<Level> levelKey) {
21+
ResourceLocation name = levelKey.location();
22+
return !getRtpDimensionBlacklist().test(name) && (getRtpDimensionWhitelist().isEmpty() || getRtpDimensionWhitelist().test(name));
23+
}
24+
25+
public static boolean isDimensionOKFrom(ResourceKey<Level> levelKey) {
1826
ResourceLocation name = levelKey.location();
19-
return !getDimensionBlacklist().test(name) && (getDimensionWhitelist().isEmpty() || getDimensionWhitelist().test(name));
27+
return !getAllCommandDimensionBlacklistFrom().test(name);
2028
}
2129

22-
private static WildcardedRLMatcher getDimensionWhitelist() {
23-
if (dimensionMatcherW == null) {
24-
dimensionMatcherW = new WildcardedRLMatcher(FTBEConfig.RTP_DIMENSION_WHITELIST.get());
25-
}
26-
return dimensionMatcherW;
30+
public static boolean isDimensionOKTo(ResourceKey<Level> levelKey) {
31+
ResourceLocation name = levelKey.location();
32+
return !getAllCommandDimensionBlacklistTo().test(name);
2733
}
2834

29-
private static WildcardedRLMatcher getDimensionBlacklist() {
30-
if (dimensionMatcherB == null) {
31-
dimensionMatcherB = new WildcardedRLMatcher(FTBEConfig.RTP_DIMENSION_BLACKLIST.get());
35+
private static WildcardedRLMatcher getRtpDimensionWhitelist() {
36+
if (rtpDimensionMatcherW == null) {
37+
rtpDimensionMatcherW = new WildcardedRLMatcher(FTBEConfig.RTP_DIMENSION_WHITELIST.get());
3238
}
33-
return dimensionMatcherB;
39+
return rtpDimensionMatcherW;
3440
}
3541

42+
private static WildcardedRLMatcher getRtpDimensionBlacklist() {
43+
if (rtpDimensionMatcherB == null) {
44+
rtpDimensionMatcherB = new WildcardedRLMatcher(FTBEConfig.RTP_DIMENSION_BLACKLIST.get());
45+
}
46+
return rtpDimensionMatcherB;
47+
}
48+
49+
private static WildcardedRLMatcher getAllCommandDimensionBlacklistFrom() {
50+
if (allDimensionMatcherBFrom == null) {
51+
allDimensionMatcherBFrom = new WildcardedRLMatcher(FTBEConfig.TELEPORTATION_BLACKLIST_FROM.get());
52+
}
53+
return allDimensionMatcherBFrom;
54+
}
55+
56+
private static WildcardedRLMatcher getAllCommandDimensionBlacklistTo() {
57+
if (allDimensionMatcherBTo == null) {
58+
allDimensionMatcherBTo = new WildcardedRLMatcher(FTBEConfig.TELEPORTATION_BLACKLIST_TO.get());
59+
}
60+
return allDimensionMatcherBTo;
61+
}
62+
3663
public static void clearMatcherCaches() {
37-
dimensionMatcherB = null;
38-
dimensionMatcherW = null;
64+
rtpDimensionMatcherB = null;
65+
rtpDimensionMatcherW = null;
66+
67+
allDimensionMatcherBFrom = null;
68+
allDimensionMatcherBTo = null;
3969
}
4070

4171
private static class WildcardedRLMatcher implements Predicate<ResourceLocation> {

common/src/main/java/dev/ftb/mods/ftbessentials/util/TeleportPos.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import net.minecraft.server.level.ServerLevel;
1212
import net.minecraft.server.level.ServerPlayer;
1313
import net.minecraft.world.entity.Entity;
14+
import net.minecraft.world.entity.player.Player;
1415
import net.minecraft.world.level.Level;
1516

1617
public class TeleportPos {
@@ -47,6 +48,15 @@ public TeleportPos(CompoundTag tag) {
4748
time = tag.getLong("time");
4849
}
4950

51+
public TeleportResult checkDimensionBlacklist(Player player) {
52+
if (!DimensionFilter.isDimensionOKTo(this.dimension)) {
53+
return TeleportResult.DIMENSION_NOT_ALLOWED_TO;
54+
} else if(!DimensionFilter.isDimensionOKFrom(player.level().dimension())) {
55+
return TeleportResult.DIMENSION_NOT_ALLOWED_FROM;
56+
}
57+
return TeleportResult.SUCCESS;
58+
}
59+
5060
public TeleportResult teleport(ServerPlayer player) {
5161
ServerLevel level = player.server.getLevel(dimension);
5262
if (level == null) {
@@ -128,6 +138,10 @@ static TeleportResult failed(Component msg) {
128138
TeleportResult DIMENSION_NOT_FOUND = failed(Component.literal("Dimension not found!"));
129139

130140
TeleportResult UNKNOWN_DESTINATION = failed(Component.literal("Unknown destination!"));
141+
142+
TeleportResult DIMENSION_NOT_ALLOWED_FROM = failed(Component.literal("Teleportation from your dimension is not allowed!"));
143+
144+
TeleportResult DIMENSION_NOT_ALLOWED_TO = failed(Component.literal("Teleportation to this dimension is not allowed!"));
131145

132146
int runCommand(ServerPlayer player);
133147

common/src/main/java/dev/ftb/mods/ftbessentials/util/WarmupCooldownTeleporter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,18 @@ public TeleportResult teleport(ServerPlayer player, Function<ServerPlayer, Telep
6161
return cooldownResult;
6262
}
6363

64+
TeleportPos pos = positionGetter.apply(player);
65+
66+
TeleportResult blacklistedResult = pos.checkDimensionBlacklist(player);
67+
if (!blacklistedResult.isSuccess()) {
68+
return blacklistedResult;
69+
}
70+
6471
CompoundEventResult<Component> result = TeleportEvent.TELEPORT.invoker().teleport(player);
6572
if (result.isFalse()) {
6673
return TeleportResult.failed(result.object());
6774
}
68-
69-
TeleportPos pos = positionGetter.apply(player);
75+
7076
if (!firePlatformTeleportEvent(player, Vec3.atBottomCenterOf(pos.getPos()))) {
7177
return TeleportResult.failed(Component.translatable("ftbessentials.teleport_prevented"));
7278
}

0 commit comments

Comments
 (0)