Compare commits

...
Author SHA1 Message Date
Martin Sulikowski f276a83f50 Experiments 2023-11-13 22:32:37 +01:00
5 changed files with 50 additions and 9 deletions

No files matched your search

@@ -22,6 +22,11 @@ public class DefaultRegionProvider implements RegionProvider {
return this.contains(min, max, location.getX(), location.getZ());
}
@Override
public Region getRegion(Location location) {
return null;
}
public boolean contains(Point min, Point max, double x, double z) {
return x >= min.x() && x < max.x()
&& z >= min.z() && z < max.z();
@@ -0,0 +1,6 @@
package com.eternalcode.combat.region;
import org.bukkit.Location;
public record Region(Location getMin, Location getMax) {
}
@@ -47,14 +47,14 @@ public class RegionController implements Listener {
return;
}
Location playerLocation = player.getLocation().subtract(player.getWorld().getSpawnLocation());
double distance = playerLocation.distance(player.getWorld().getSpawnLocation());
Vector knockback = new Vector(0, 3, 0).multiply(this.pluginConfig.settings.blockedRegionKnockMultiplier / distance);
Vector vector = playerLocation.toVector().add(knockback);
Vector direction = locationFrom.toVector().subtract(locationTo.toVector()).normalize();
Vector knockback = direction.multiply(this.pluginConfig.settings.blockedRegionKnockMultiplier);
player.setVelocity(vector);
player.setVelocity(knockback);
this.announcer.sendMessage(player, this.pluginConfig.messages.cantEnterOnRegion);
event.setCancelled(true);
}
}
}
@@ -2,9 +2,9 @@ package com.eternalcode.combat.region;
import org.bukkit.Location;
@FunctionalInterface
public interface RegionProvider {
boolean isInRegion(Location location);
Region getRegion(Location location);
}
@@ -1,13 +1,17 @@
package com.eternalcode.combat.region;
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.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.protection.regions.RegionQuery;
import org.bukkit.Location;
import org.bukkit.World;
import java.util.List;
import java.util.Optional;
public class WorldGuardRegionProvider implements RegionProvider {
@@ -22,11 +26,37 @@ public class WorldGuardRegionProvider implements RegionProvider {
return this.regions.stream().anyMatch(region -> this.isInCombatRegion(location, region));
}
@Override
public Region getRegion(Location location) {
ApplicableRegionSet applicableRegions = getProtectedRegions(location);
Optional<ProtectedRegion> first = applicableRegions.getRegions().stream().findFirst();
if (first.isEmpty()) {
return null;
}
ProtectedRegion protectedRegion = first.get();
BlockVector3 minimumPoint = protectedRegion.getMinimumPoint();
BlockVector3 maximumPoint = protectedRegion.getMaximumPoint();
World world = location.getWorld();
Location adaptMin = BukkitAdapter.adapt(world, minimumPoint);
Location adaptMax = BukkitAdapter.adapt(world, maximumPoint);
return new Region(adaptMin, adaptMax);
}
private boolean isInCombatRegion(Location location, String regionName) {
RegionContainer regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionQuery regionQuery = regionContainer.createQuery();
ApplicableRegionSet applicableRegions = regionQuery.getApplicableRegions(BukkitAdapter.adapt(location));
ApplicableRegionSet applicableRegions = getProtectedRegions(location);
return applicableRegions.getRegions().stream().anyMatch(region -> region.getId().equalsIgnoreCase(regionName));
}
private static ApplicableRegionSet getProtectedRegions(Location location) {
RegionContainer regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionQuery regionQuery = regionContainer.createQuery();
return regionQuery.getApplicableRegions(BukkitAdapter.adapt(location));
}
}