Skip to content

Commit

Permalink
GH-30 Improve restrictions (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
imDMK committed Mar 8, 2024
1 parent f7fa47d commit 2ab3cb5
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.github.imdmk.doublejump.jump;

import com.github.imdmk.doublejump.region.RegionProvider;
import com.github.imdmk.doublejump.restriction.JumpRestriction;
import com.github.imdmk.doublejump.util.GameModeUtil;
import org.bukkit.GameMode;
import org.bukkit.World;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -16,19 +15,19 @@ public class JumpPlayerService {
private final RegionProvider regionProvider;
private final JumpPlayerManager playerManager;

private final List<String> disabledWorlds;
private final List<GameMode> disabledGameModes;
private final JumpRestriction worldRestriction;
private final JumpRestriction gameModeRestriction;
private final String doubleJumpUsePermission;

private final boolean jumpsLimitEnabled;
private final int jumpsLimit;
private final Map<String, Integer> jumpsLimitByPermissions;

public JumpPlayerService(RegionProvider regionProvider, JumpPlayerManager playerManager, List<String> disabledWorlds, List<GameMode> disabledGameModes, String doubleJumpUsePermission, boolean jumpsLimitEnabled, int jumpsLimit, Map<String, Integer> jumpsLimitByPermissions) {
public JumpPlayerService(RegionProvider regionProvider, JumpPlayerManager playerManager, JumpRestriction worldRestriction, JumpRestriction gameModeRestriction, String doubleJumpUsePermission, boolean jumpsLimitEnabled, int jumpsLimit, Map<String, Integer> jumpsLimitByPermissions) {
this.regionProvider = regionProvider;
this.playerManager = playerManager;
this.disabledWorlds = disabledWorlds;
this.disabledGameModes = disabledGameModes;
this.worldRestriction = worldRestriction;
this.gameModeRestriction = gameModeRestriction;
this.doubleJumpUsePermission = doubleJumpUsePermission;
this.jumpsLimitEnabled = jumpsLimitEnabled;
this.jumpsLimit = jumpsLimit;
Expand Down Expand Up @@ -118,17 +117,17 @@ public JumpPlayer create(Player player) {
* @return Whether the player can use double jump.
*/
public boolean canUseDoubleJump(Player player) {
if (this.regionProvider.isInRegion(player)) {
if (!this.regionProvider.isInAllowedRegion(player)) {
return false;
}

GameMode playerGameMode = player.getGameMode();
if (this.disabledGameModes.contains(playerGameMode)) {
if (!this.gameModeRestriction.isAllowed(playerGameMode.name())) {
return false;
}

World playerWorld = player.getWorld();
if (this.disabledWorlds.contains(playerWorld.getName())) {
String playerWorldName = player.getWorld().getName();
if (!this.worldRestriction.isAllowed(playerWorldName)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
public interface RegionProvider {

/**
* Checks whether the player is within an area covered by the region restriction.
*
* @return true if the player is in disabled area
* @param player The player whose location is being checked.
* @return true if the player is within an area covered by the restriction, otherwise false.
*/
boolean isInRegion(Player player);
boolean isInAllowedRegion(Player player);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class EmptyRegionProvider implements RegionProvider {

@Override
public boolean isInRegion(Player player) {
return false;
public boolean isInAllowedRegion(Player player) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.imdmk.doublejump.region.impl;

import com.github.imdmk.doublejump.region.RegionProvider;
import com.github.imdmk.doublejump.restriction.JumpRestriction;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldguard.WorldGuard;
Expand All @@ -9,20 +10,20 @@
import com.sk89q.worldguard.protection.regions.RegionQuery;
import org.bukkit.entity.Player;

import java.util.List;

public class WorldGuardRegionProvider implements RegionProvider {

private final List<String> disabledRegions;
private final JumpRestriction regionRestriction;

public WorldGuardRegionProvider(List<String> disabledRegions) {
this.disabledRegions = disabledRegions;
public WorldGuardRegionProvider(JumpRestriction regionRestriction) {
this.regionRestriction = regionRestriction;
}

@Override
public boolean isInRegion(Player player) {
return this.disabledRegions.stream()
.anyMatch(region -> this.isInRegion(player, region));
public boolean isInAllowedRegion(Player player) {
return switch (this.regionRestriction.type()) {
case BLACKLIST -> this.regionRestriction.list().stream().anyMatch(region -> !this.isInRegion(player, region));
case WHITELIST -> this.regionRestriction.list().stream().anyMatch(region -> this.isInRegion(player, region));
};
}

public boolean isInRegion(Player player, String regionId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.imdmk.doublejump.restriction;

import java.util.List;

public record JumpRestriction(JumpRestrictionType type, List<String> list) {

public boolean isAllowed(String value) {
return switch (this.type) {
case BLACKLIST -> !this.list.contains(value);
case WHITELIST -> this.list.contains(value);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.imdmk.doublejump.restriction;

public enum JumpRestrictionType {
WHITELIST, BLACKLIST
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public DoubleJump(Plugin plugin) {
this.jumpPlayerManager = new JumpPlayerManager();

/* Services */
this.jumpPlayerService = new JumpPlayerService(this.regionProvider, this.jumpPlayerManager, this.pluginConfiguration.jumpSettings.restrictionSettings.disabledWorlds, this.pluginConfiguration.jumpSettings.restrictionSettings.disabledGameModes, this.pluginConfiguration.doubleJumpUsePermission, this.pluginConfiguration.jumpSettings.limitSettings.enabled, this.pluginConfiguration.jumpSettings.limitSettings.limit, this.pluginConfiguration.jumpSettings.limitSettings.limitsByPermissions);
this.jumpPlayerService = new JumpPlayerService(this.regionProvider, this.jumpPlayerManager, this.pluginConfiguration.jumpSettings.restrictionSettings.worldRestriction, this.pluginConfiguration.jumpSettings.restrictionSettings.gameModeRestriction, this.pluginConfiguration.doubleJumpUsePermission, this.pluginConfiguration.jumpSettings.limitSettings.enabled, this.pluginConfiguration.jumpSettings.limitSettings.limit, this.pluginConfiguration.jumpSettings.limitSettings.limitsByPermissions);

this.jumpRestrictionService = new JumpRestrictionService(this.pluginConfiguration.jumpSettings, this.pluginConfiguration.jumpSettings.restrictionSettings, this.regionProvider, this.notificationSender);

Expand Down Expand Up @@ -214,7 +214,7 @@ private void disableAllowFlightForOnlinePlayers() {

private RegionProvider hookRegionProvider() {
if (this.server.getPluginManager().isPluginEnabled("WorldGuard")) {
return new WorldGuardRegionProvider(this.pluginConfiguration.jumpSettings.restrictionSettings.disabledRegions);
return new WorldGuardRegionProvider(this.pluginConfiguration.jumpSettings.restrictionSettings.regionRestriction);
}

return new EmptyRegionProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.github.imdmk.doublejump.configuration.transformer.ComponentTransformer;
import com.github.imdmk.doublejump.configuration.transformer.EnchantmentTransformer;
import com.github.imdmk.doublejump.jump.particle.JumpParticleSerializer;
import com.github.imdmk.doublejump.jump.restriction.JumpRestrictionSerializer;
import com.github.imdmk.doublejump.jump.sound.JumpSoundSerializer;
import com.github.imdmk.doublejump.notification.configuration.NotificationTransformer;
import eu.okaeri.configs.ConfigManager;
Expand Down Expand Up @@ -41,6 +42,7 @@ public static <T extends OkaeriConfig> T create(Class<T> config, File dataFolder
registry.register(new ItemStackSerializer());
registry.register(new ItemMetaSerializer());
registry.register(new NotificationTransformer());
registry.register(new JumpRestrictionSerializer());
registry.register(new JumpParticleSerializer());
registry.register(new JumpSoundSerializer());
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.imdmk.doublejump.jump.restriction;

import com.github.imdmk.doublejump.restriction.JumpRestriction;
import com.github.imdmk.doublejump.restriction.JumpRestrictionType;
import eu.okaeri.configs.schema.GenericsDeclaration;
import eu.okaeri.configs.serdes.DeserializationData;
import eu.okaeri.configs.serdes.ObjectSerializer;
import eu.okaeri.configs.serdes.SerializationData;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.List;

public class JumpRestrictionSerializer implements ObjectSerializer<JumpRestriction> {

@Override
public boolean supports(@NonNull Class<? super JumpRestriction> type) {
return JumpRestriction.class.isAssignableFrom(type);
}

@Override
public void serialize(@NonNull JumpRestriction jumpRestriction, @NonNull SerializationData data, @NonNull GenericsDeclaration generics) {
data.add("type", jumpRestriction.type(), JumpRestrictionType.class);
data.addCollection("list", jumpRestriction.list(), String.class);
}

@Override
public JumpRestriction deserialize(@NonNull DeserializationData data, @NonNull GenericsDeclaration generics) {
JumpRestrictionType type = data.get("type", JumpRestrictionType.class);
List<String> list = data.getAsList("list", String.class);

return new JumpRestriction(type, list);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ public boolean isPassedRestrictions(Player player, JumpPlayer jumpPlayer, boolea
}

public boolean isPassedRestrictions(Player player, boolean sendNotification) {
if (this.regionProvider.isInRegion(player)) {
if (!this.regionProvider.isInAllowedRegion(player)) {
this.sendNotification(player, this.restrictionSettings.notificationSettings.jumpDisabledRegion, sendNotification);
return true;
}

GameMode playerGameMode = player.getGameMode();
if (this.restrictionSettings.disabledGameModes.contains(playerGameMode)) {
if (!this.restrictionSettings.gameModeRestriction.isAllowed(playerGameMode.name())) {
this.sendNotification(player, this.restrictionSettings.notificationSettings.jumpDisabledGameMode, sendNotification);
return true;
}

String playerWorldName = player.getWorld().getName();
if (this.restrictionSettings.disabledWorlds.contains(playerWorldName)) {
if (!this.restrictionSettings.worldRestriction.isAllowed(playerWorldName)) {
this.sendNotification(player, this.restrictionSettings.notificationSettings.jumpDisabledWorld, sendNotification);
return true;
}
Expand All @@ -88,19 +88,19 @@ public boolean isPassedRestrictions(Player player, boolean sendNotification) {
}

public boolean isPassedRestrictions(Player player, Player target, boolean sendNotification) {
if (this.regionProvider.isInRegion(target)) {
if (!this.regionProvider.isInAllowedRegion(target)) {
this.sendNotification(player, this.restrictionSettings.notificationSettings.targetInDisabledRegion, sendNotification);
return true;
}

GameMode targetGameMode = target.getGameMode();
if (this.restrictionSettings.disabledGameModes.contains(targetGameMode)) {
if (!this.restrictionSettings.gameModeRestriction.isAllowed(targetGameMode.name())) {
this.sendNotification(player, this.restrictionSettings.notificationSettings.targetHasDisabledGameMode, sendNotification);
return true;
}

String targetWorldName = target.getWorld().getName();
if (this.restrictionSettings.disabledWorlds.contains(targetWorldName)) {
if (!this.restrictionSettings.worldRestriction.isAllowed(targetWorldName)) {
this.sendNotification(player, this.restrictionSettings.notificationSettings.targetInDisabledWorld, sendNotification);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.github.imdmk.doublejump.notification.Notification;
import com.github.imdmk.doublejump.notification.NotificationType;
import com.github.imdmk.doublejump.restriction.JumpRestriction;
import com.github.imdmk.doublejump.restriction.JumpRestrictionType;
import eu.okaeri.configs.OkaeriConfig;
import eu.okaeri.configs.annotation.Comment;
import org.bukkit.GameMode;
Expand All @@ -11,23 +13,16 @@
public class JumpRestrictionSettings extends OkaeriConfig {

@Comment({
"# Names of regions where the player will not be able to double-jump",
"# Restriction of regions where the player will not be able to double-jump",
"# The WorldGuard plugin is required for this feature to work"
})
public List<String> disabledRegions = List.of(
"example-region"
);

@Comment("# Names of worlds where the player will not be able to double-jump")
public List<String> disabledWorlds = List.of(
"example-world"
);

@Comment("# The names of the game modes during which the player will not be able to double-jump")
public List<GameMode> disabledGameModes = List.of(
GameMode.SPECTATOR,
GameMode.CREATIVE
);
public JumpRestriction regionRestriction = new JumpRestriction(JumpRestrictionType.WHITELIST, List.of("example-region"));

@Comment("# Restriction of worlds where the player will not be able to double-jump")
public JumpRestriction worldRestriction = new JumpRestriction(JumpRestrictionType.BLACKLIST, List.of("example-world"));

@Comment("# The restriction of the game modes during which the player will not be able to double-jump")
public JumpRestriction gameModeRestriction = new JumpRestriction(JumpRestrictionType.BLACKLIST, List.of(GameMode.CREATIVE.name()));

@Comment({"#", "# Jump restriction notification settings", "#"})
public JumpRestrictionNotificationSettings notificationSettings = new JumpRestrictionNotificationSettings();
Expand Down

0 comments on commit 2ab3cb5

Please sign in to comment.