Skip to content

Commit df7a5b0

Browse files
committed
GH-8: explicitly give the MinecraftServer as part of the context, making single-player bypass mode functional in actor-less scenarios
1 parent 5d639db commit df7a5b0

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import net.neoforged.neoforge.event.server.ServerAboutToStartEvent;
1818
import net.neoforged.neoforge.event.server.ServerStoppingEvent;
1919
import net.neoforged.neoforge.server.permission.events.PermissionGatherEvent;
20+
import org.jetbrains.annotations.Nullable;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
2223
import org.teacon.areacontrol.api.Area;
@@ -40,7 +41,7 @@ public final class AreaControl {
4041

4142
private static final LevelResource SERVER_CONFIG = new LevelResource("serverconfig");
4243

43-
public static Predicate<MinecraftServer> singlePlayerServerChecker;
44+
public static Predicate<@Nullable MinecraftServer> singlePlayerServerChecker;
4445

4546
public AreaControl(ModContainer container, IEventBus modBus) {
4647
VERSION = container.getModInfo().getVersion().toString();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static void vulnerabilityCheck(EntityInvulnerabilityCheckEvent event) {
6666
}
6767
} else {
6868
var entityTypeRegName = BuiltInRegistries.ENTITY_TYPE.getKey(attackTarget.getType());
69-
allow = AreaChecks.checkPropFor(area, damageSrc, AreaProperties.ALLOW_PVE, entityTypeRegName, AreaControlConfig.allowPvE);
69+
allow = AreaChecks.checkPropFor(area, damageSrc, level.getServer(), AreaProperties.ALLOW_PVE, entityTypeRegName, AreaControlConfig.allowPvE);
7070
deniedFeedback = Component.translatable("area_control.notice.pve_disabled", ObjectArrays.EMPTY_ARRAY);
7171
}
7272
if (!allow) {

src/main/java/org/teacon/areacontrol/impl/AreaChecks.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import net.minecraft.network.chat.Component;
66
import net.minecraft.resources.ResourceLocation;
7+
import net.minecraft.server.MinecraftServer;
78
import net.minecraft.server.level.ServerPlayer;
89
import net.minecraft.world.entity.Entity;
910
import net.minecraft.world.entity.player.Player;
@@ -141,16 +142,36 @@ public static boolean checkPossess(Area area, Item item) {
141142
public static boolean checkPropFor(final @Nullable Area area, final @Nullable Entity actor,
142143
final @NotNull String prop, final @Nullable ResourceLocation targetId,
143144
final @Nullable Supplier<@NotNull Boolean> defaultValue) {
145+
return checkPropFor(area, actor, null, prop, targetId, defaultValue);
146+
}
147+
148+
/**
149+
* Recursively checks if an action is allowed in given area
150+
*
151+
* @param area Area to check
152+
* @param actor The entity that carries out the action
153+
* @param currentServer The current server instance we are in
154+
* @param prop The action, represented by a string property
155+
* @param targetId The object on which the action is being carried out.
156+
* @return true if such action is allowed; false otherwise.
157+
*/
158+
public static boolean checkPropFor(final @Nullable Area area, final @Nullable Entity actor,
159+
@Nullable MinecraftServer currentServer,
160+
final @NotNull String prop, final @Nullable ResourceLocation targetId,
161+
final @Nullable Supplier<@NotNull Boolean> defaultValue) {
144162
if (actor != null) {
145-
// If actor is in single-player (without being published to LAN), then skip all checks.
146-
if (AreaControl.singlePlayerServerChecker.test(actor.getServer())) {
147-
return true;
163+
if (currentServer == null) {
164+
currentServer = actor.getServer();
148165
}
149166
// If bypass mode is activated, then skip all checks.
150167
if (AreaControlPlayerTracker.hasBypassModeOnForArea(actor, area)) {
151168
return true;
152169
}
153170
}
171+
// If we are in single-player (without being published to LAN), then skip all checks.
172+
if (AreaControl.singlePlayerServerChecker.test(currentServer)) {
173+
return true;
174+
}
154175
if (targetId != null) {
155176
var objSpecific = AreaProperties.getBoolOptional(area, prop + "." + targetId);
156177
if (objSpecific.isPresent()) {

0 commit comments

Comments
 (0)