-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-184 Refactor Knockback Mechanic and Add Support for Deny Entry flag (
#184) * Initial commit * fix spaces * fix spaces * Add Kamicjusz's changes. Normalize knockback vector, fix message. * Set cooldown for messages * Resolve half of suggestions * Remove delay and add PVP regions blockade. * Improve performance of WorldGuardRegionProvider. * Use lambda * Resolve @vLuckyyy review --------- Co-authored-by: Rollczi <[email protected]>
- Loading branch information
Showing
8 changed files
with
97 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
eternalcombat-api/src/main/java/com/eternalcode/combat/region/Region.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.eternalcode.combat.region; | ||
|
||
import org.bukkit.Location; | ||
|
||
public interface Region { | ||
|
||
Location getCenter(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
eternalcombat-api/src/main/java/com/eternalcode/combat/region/RegionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package com.eternalcode.combat.region; | ||
|
||
import java.util.Optional; | ||
import org.bukkit.Location; | ||
|
||
public interface RegionProvider { | ||
|
||
boolean isInRegion(Location location); | ||
Optional<Region> getRegion(Location location); | ||
|
||
Location getRegionCenter(Location location); | ||
default boolean isInRegion(Location location) { | ||
return getRegion(location).isPresent(); | ||
} | ||
|
||
} |
82 changes: 44 additions & 38 deletions
82
eternalcombat-api/src/main/java/com/eternalcode/combat/region/WorldGuardRegionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,75 @@ | ||
package com.eternalcode.combat.region; | ||
|
||
import com.eternalcode.combat.config.implementation.PluginConfig; | ||
import com.sk89q.worldedit.bukkit.BukkitAdapter; | ||
import com.sk89q.worldedit.math.BlockVector3; | ||
import com.sk89q.worldguard.WorldGuard; | ||
import com.sk89q.worldguard.protection.ApplicableRegionSet; | ||
import com.sk89q.worldguard.protection.flags.Flags; | ||
import com.sk89q.worldguard.protection.flags.StateFlag; | ||
import com.sk89q.worldguard.protection.regions.ProtectedRegion; | ||
import com.sk89q.worldguard.protection.regions.RegionContainer; | ||
import com.sk89q.worldguard.protection.regions.RegionQuery; | ||
import java.util.Optional; | ||
import java.util.TreeSet; | ||
import org.bukkit.Location; | ||
|
||
import java.util.List; | ||
|
||
public class WorldGuardRegionProvider implements RegionProvider { | ||
|
||
private final List<String> regions; | ||
private final RegionContainer regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer(); | ||
private final TreeSet<String> regions = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); | ||
private final PluginConfig pluginConfig; | ||
|
||
public WorldGuardRegionProvider(List<String> regions) { | ||
this.regions = regions; | ||
public WorldGuardRegionProvider(List<String> regions, PluginConfig pluginConfig) { | ||
this.regions.addAll(regions); | ||
this.pluginConfig = pluginConfig; | ||
} | ||
|
||
@Override | ||
public boolean isInRegion(Location location) { | ||
return this.regions.stream().anyMatch(region -> this.isInCombatRegion(location, region)); | ||
} | ||
|
||
@Override | ||
public Location getRegionCenter(Location location) { | ||
RegionContainer regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer(); | ||
RegionQuery regionQuery = regionContainer.createQuery(); | ||
public Optional<Region> getRegion(Location location) { | ||
RegionQuery regionQuery = this.regionContainer.createQuery(); | ||
ApplicableRegionSet applicableRegions = regionQuery.getApplicableRegions(BukkitAdapter.adapt(location)); | ||
|
||
double minX = 0; | ||
double maxX = 0; | ||
double minZ = 0; | ||
double maxZ = 0; | ||
|
||
for (ProtectedRegion region : applicableRegions.getRegions()) { | ||
BlockVector3 min = region.getMinimumPoint(); | ||
BlockVector3 max = region.getMaximumPoint(); | ||
|
||
if (min.getX() < minX) { | ||
minX = min.getX(); | ||
} | ||
if (max.getX() > maxX) { | ||
maxX = max.getX(); | ||
} | ||
if (min.getZ() < minZ) { | ||
minZ = min.getZ(); | ||
} | ||
if (max.getZ() > maxZ) { | ||
maxZ = max.getZ(); | ||
if (!isCombatRegion(region)) { | ||
continue; | ||
} | ||
|
||
return Optional.of(new RegionImpl(location, region)); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
private boolean isCombatRegion(ProtectedRegion region) { | ||
if (this.regions.contains(region.getId())) { | ||
return true; | ||
} | ||
|
||
double x = (maxX - minX) / 2 + minX; | ||
double z = (maxZ - minZ) / 2 + minZ; | ||
if (this.pluginConfig.settings.shouldPreventPvpRegions) { | ||
StateFlag.State flag = region.getFlag(Flags.PVP); | ||
|
||
if (flag != null) { | ||
return flag.equals(StateFlag.State.DENY); | ||
} | ||
} | ||
|
||
return new Location(location.getWorld(), x, location.getY(), z); | ||
return false; | ||
} | ||
|
||
private boolean isInCombatRegion(Location location, String regionName) { | ||
RegionContainer regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer(); | ||
RegionQuery regionQuery = regionContainer.createQuery(); | ||
ApplicableRegionSet applicableRegions = regionQuery.getApplicableRegions(BukkitAdapter.adapt(location)); | ||
private record RegionImpl(Location contextLocation, ProtectedRegion region) implements Region { | ||
@Override | ||
public Location getCenter() { | ||
BlockVector3 min = region.getMinimumPoint(); | ||
BlockVector3 max = region.getMaximumPoint(); | ||
|
||
return applicableRegions.getRegions().stream().anyMatch(region -> region.getId().equalsIgnoreCase(regionName)); | ||
double x = (double) (min.getX() + max.getX()) / 2; | ||
double z = (double) (min.getZ() + max.getZ()) / 2; | ||
|
||
return new Location(contextLocation.getWorld(), x, contextLocation.getY(), z); | ||
} | ||
} | ||
|
||
} |