Skip to content

Commit

Permalink
GH-33 Fix region provider (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
imDMK committed Mar 23, 2024
1 parent 00b3a0b commit 55cfc54
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.internal.platform.WorldGuardPlatform;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.protection.regions.RegionQuery;
import org.bukkit.entity.Player;

import java.util.stream.Stream;
import java.util.Set;
import java.util.stream.Collectors;

public class WorldGuardRegionProvider implements RegionProvider {

Expand All @@ -22,22 +25,21 @@ public WorldGuardRegionProvider(JumpRestriction regionRestriction) {

@Override
public boolean isInAllowedRegion(Player player) {
Stream<String> regions = this.regionRestriction.list().stream();
Set<String> playerRegions = this.getPlayerRegions(player).getRegions().stream()
.map(ProtectedRegion::getId)
.collect(Collectors.toSet());

return switch (this.regionRestriction.type()) {
case BLACKLIST -> regions.anyMatch(region -> !this.isInRegion(player, region));
case WHITELIST -> regions.anyMatch(region -> this.isInRegion(player, region));
};
return this.regionRestriction.isAllowed(playerRegions);
}

public boolean isInRegion(Player player, String regionId) {
public ApplicableRegionSet getPlayerRegions(Player player) {
Location playerLocation = BukkitAdapter.adapt(player.getLocation());

RegionContainer regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer();
WorldGuardPlatform worldGuardPlatform = WorldGuard.getInstance().getPlatform();

RegionContainer regionContainer = worldGuardPlatform.getRegionContainer();
RegionQuery regionQuery = regionContainer.createQuery();
ApplicableRegionSet applicableRegionSet = regionQuery.getApplicableRegions(playerLocation);

return applicableRegionSet.getRegions().stream()
.anyMatch(region -> region.getId().equals(regionId));
return regionQuery.getApplicableRegions(playerLocation);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.imdmk.doublejump.restriction;

import java.util.List;
import java.util.Set;

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

Expand All @@ -10,4 +11,16 @@ public boolean isAllowed(String value) {
case WHITELIST -> this.list.contains(value);
};
}

public boolean isAllowed(Set<String> values) {
if (values.isEmpty() && this.type == JumpRestrictionType.BLACKLIST) {
return true;
}

for (String value : values) {
return this.isAllowed(value);
}

return false;
}
}
1 change: 1 addition & 0 deletions doublejump-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ bukkit {
author = "DMK"
description = "Efficient double jump plugin with many features and configuration possibilities"
website = "https://github.com/imDMK/DoubleJump"
softDepend = listOf("PlaceholderAPI", "WorldGuard")
}

checkstyle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
public class DoubleJump implements DoubleJumpApi {

private final Plugin plugin;
private final Logger logger;
private final Server server;

private final PluginConfiguration pluginConfiguration;
Expand All @@ -90,10 +91,10 @@ public DoubleJump(Plugin plugin) {
DoubleJumpApiProvider.register(this);

Stopwatch stopwatch = Stopwatch.createStarted();
Logger logger = plugin.getLogger();
PluginDescriptionFile pluginDescriptionFile = plugin.getDescription();

this.plugin = plugin;
this.logger = plugin.getLogger();
this.server = plugin.getServer();

/* Configuration */
Expand Down Expand Up @@ -137,7 +138,7 @@ public DoubleJump(Plugin plugin) {
new JumpRefreshListener(this.jumpPlayerService, taskScheduler),
new JumpRegenerationListener(this.pluginConfiguration.jumpSettings, this.notificationSender, this.jumpPlayerManager),
new JumpStreakResetListener(this.server, this.pluginConfiguration.jumpSettings, this.notificationSender, this.jumpPlayerManager),
new UpdateListener(logger, this.pluginConfiguration, this.notificationSender, updateService, taskScheduler)
new UpdateListener(this.logger, this.pluginConfiguration, this.notificationSender, updateService, taskScheduler)
).forEach(listener -> this.server.getPluginManager().registerEvents(listener, plugin));

/* Lite Commands */
Expand All @@ -158,12 +159,14 @@ public DoubleJump(Plugin plugin) {
new JumpPlayerJumpsPlaceholder(pluginDescriptionFile, this.jumpPlayerManager),
new JumpPlayerStreakPlaceholder(pluginDescriptionFile, this.jumpPlayerManager)
).forEach(this.placeholderRegistry::register);

this.logger.info("Hooked PlaceholderAPI!");
}

/* Metrics */
this.metrics = new Metrics((JavaPlugin) plugin, 19387);

logger.info("Enabled plugin in " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + "ms.");
this.logger.info("Enabled plugin in " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + "ms.");
}

public void disable() {
Expand Down Expand Up @@ -217,6 +220,7 @@ private void disableAllowFlightForOnlinePlayers() {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ private void scheduleCheckRestrictions(Player player) {
if (this.jumpRestrictionService.isPassedRestrictions(player, true)) {
this.jumpPlayerService.disable(player);
}
}, 10L);
}, 20L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onPlayerToggleFlight(PlayerToggleFlightEvent event) {
player.setAllowFlight(false);

if (this.jumpRestrictionService.isPassedRestrictions(player, true)) {
this.jumpPlayerManager.remove(player.getUniqueId());
this.jumpPlayerService.disable(player);
return;
}

Expand All @@ -76,8 +76,8 @@ public void onPlayerMove(PlayerMoveEvent event) {

JumpPlayer jumpPlayer = jumpPlayerOptional.get();

if (this.jumpRestrictionService.isPassedRestrictions(player, player, true)) {
this.jumpPlayerManager.remove(player.getUniqueId());
if (this.jumpRestrictionService.isPassedRestrictions(player, true)) {
this.jumpPlayerService.disable(player);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.github.imdmk.doublejump.region.RegionProvider;
import com.github.imdmk.doublejump.text.Formatter;
import com.github.imdmk.doublejump.util.DurationUtil;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -73,8 +72,8 @@ public boolean isPassedRestrictions(Player player, boolean sendNotification) {
return true;
}

GameMode playerGameMode = player.getGameMode();
if (!this.restrictionSettings.gameModeRestriction.isAllowed(playerGameMode.name())) {
String playerGameModeName = player.getGameMode().name();
if (!this.restrictionSettings.gameModeRestriction.isAllowed(playerGameModeName)) {
this.sendNotification(player, this.restrictionSettings.notificationSettings.jumpDisabledGameMode, sendNotification);
return true;
}
Expand All @@ -89,19 +88,19 @@ public boolean isPassedRestrictions(Player player, boolean sendNotification) {
}

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

String targetGameMode = target.getGameMode().name();
if (this.restrictionSettings.gameModeRestriction.isAllowed(targetGameMode)) {
if (!this.restrictionSettings.gameModeRestriction.isAllowed(targetGameMode)) {
this.sendNotification(sender, this.restrictionSettings.notificationSettings.targetHasDisabledGameMode, sendNotification);
return true;
}

String targetWorld = target.getWorld().getName();
if (this.restrictionSettings.worldRestriction.isAllowed(targetWorld)) {
if (!this.restrictionSettings.worldRestriction.isAllowed(targetWorld)) {
this.sendNotification(sender, this.restrictionSettings.notificationSettings.targetInDisabledWorld, sendNotification);
return true;
}
Expand Down

0 comments on commit 55cfc54

Please sign in to comment.