Skip to content

Commit 2bb7769

Browse files
committed
Experimental - new property active_effect - allow/deny having certain mob effects in an area
1 parent 2364cbf commit 2bb7769

5 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/main/java/org/teacon/areacontrol/AreaControlConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class AreaControlConfig {
2121
public static ModConfigSpec.BooleanValue allowCBSelectingFromParent;
2222
public static ModConfigSpec.BooleanValue allowCBSelectingFromChild;
2323
public static ModConfigSpec.BooleanValue allowOpenSafe;
24+
public static ModConfigSpec.BooleanValue allowActiveEffect;
2425

2526
public static ModConfigSpec setup(ModConfigSpec.Builder configSpec) {
2627
disableInSinglePlayer = configSpec.comment("Disable nearly all protection measures when in singleplayer.")
@@ -96,6 +97,8 @@ public static ModConfigSpec setup(ModConfigSpec.Builder configSpec) {
9697
.define("allowCommandBlockUseEntitySelectorToSelectEntitiesFromChildArea", true);
9798
allowOpenSafe = configSpec.comment("Default value for open_safe")
9899
.define("allowOpenSafe", true);
100+
allowActiveEffect = configSpec.comment("Default value for active_effect")
101+
.define("allowActiveEffect", true);
99102
configSpec.pop();
100103
return configSpec.build();
101104
}

src/main/java/org/teacon/areacontrol/AreaControlPlayerTracker.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import net.minecraft.ChatFormatting;
44

5+
import net.minecraft.core.Holder;
56
import net.minecraft.core.registries.BuiltInRegistries;
67
import net.minecraft.network.chat.ClickEvent;
78
import net.minecraft.network.chat.Component;
89
import net.minecraft.network.chat.HoverEvent;
910
import net.minecraft.network.chat.Style;
1011
import net.minecraft.resources.ResourceKey;
1112
import net.minecraft.server.level.ServerPlayer;
13+
import net.minecraft.world.effect.MobEffect;
1214
import net.minecraft.world.entity.Entity;
1315
import net.minecraft.world.entity.player.Player;
1416
import net.minecraft.world.level.Level;
@@ -18,6 +20,7 @@
1820
import net.neoforged.neoforge.event.tick.PlayerTickEvent;
1921
import org.jetbrains.annotations.NotNull;
2022
import org.jetbrains.annotations.Nullable;
23+
import org.jspecify.annotations.NonNull;
2124
import org.slf4j.Logger;
2225
import org.slf4j.LoggerFactory;
2326
import org.slf4j.Marker;
@@ -33,6 +36,7 @@
3336

3437
import java.util.ArrayList;
3538
import java.util.Iterator;
39+
import java.util.List;
3640
import java.util.Set;
3741
import java.util.UUID;
3842

@@ -108,6 +112,18 @@ public static void onPlayerTick(PlayerTickEvent.Pre event) {
108112
player.sendOverlayMessage(Component.translatable("area_control.notice.ride_disabled", riding.getDisplayName()));
109113
player.stopRiding();
110114
}
115+
// Clear disallowed effects
116+
for (Holder<@NonNull MobEffect> effectId : List.copyOf(player.getActiveEffectsMap().keySet())) {
117+
var regKey = effectId.getKey();
118+
if (regKey == null) {
119+
// This is impossible - no effect should lack registry id at runtime, unless it is unregistered.
120+
continue;
121+
}
122+
if (!AreaChecks.checkPropFor(currentArea, player, AreaProperties.ALLOW_ACTIVE_EFFECT, regKey.identifier(), AreaControlConfig.allowActiveEffect)) {
123+
player.removeEffect(effectId);
124+
player.sendOverlayMessage(Component.translatable("area_control.notice.clear_effect", effectId.value().getDisplayName()));
125+
}
126+
}
111127

112128
// 检查玩家的 Bypass 状态并更新。
113129
INSTANCE.updatePlayerExemptionStatus(player, status, prevArea);

src/main/java/org/teacon/areacontrol/api/AreaProperties.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public final class AreaProperties {
3737
public static final String ALLOW_EXPLOSION_AFFECT_ENTITIES = register("explosion_affect_entities");
3838
public static final String ALLOW_FIRE_SPREAD = register("fire_spread");
3939
public static final String ALLOW_OPEN_SAFE = register("open_safe");
40+
public static final String ALLOW_ACTIVE_EFFECT = register("active_effect");
4041

4142
public static final String ALLOW_ENTITY_USE_SELECTOR_FROM_CHILD = register("select_from_child_area_by_entity");
4243
public static final String ALLOW_ENTITY_USE_SELECTOR_FROM_PARENT = register("select_from_parent_area_by_entity");

src/main/resources/assets/area_control/lang/en_us.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
"area_control.notice.ride_disabled": "AreaControl: riding %s is disabled here; turn on bypass mode if you own this area",
147147
"area_control.notice.selector_filtered": "%s entity/entities that were matched by selector are excluded by AreaControl due to area restrictions.",
148148
"area_control.notice.open_safe_disabled": "AreaControl: You cannot open safe here",
149+
"area_control.notice.clear_effect": "AreaControl: this area bans effect '%s' and has been cleared for you",
149150

150151
"area_control.error.must_be_player": "This command requires a real player to execute",
151152
"area_control.error.no_client_ext": "This function requires AreaControl to be installed on client.",

src/main/resources/assets/area_control/lang/zh_cn.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
"area_control.notice.ride_disabled": "AreaControl:该地禁止骑乘 %s;如果你是该地建筑师,请打开 Bypass 模式",
146146
"area_control.notice.selector_filtered": "AreaControl 已根据领地检查排除了 %s 个实体选择器此前选中的实体。",
147147
"area_control.notice.open_safe_disabled": "AreaControl:你不能在这里打开保险箱!",
148+
"area_control.notice.clear_effect": "AreaControl:该领地禁止状态效果「%s」,现已清除。",
148149

149150
"area_control.error.must_be_player": "只有玩家可以执行该命令。",
150151
"area_control.error.no_client_ext": "该功能需要客户端也安装 AreaControl 才能使用。",

0 commit comments

Comments
 (0)