Remove invulnerable visitor protection if island is in PVP mode

We now have alerts if a player teleports to a PVP island.

https://github.com/BentoBoxWorld/BentoBox/issues/668
This commit is contained in:
tastybento 2021-09-04 14:36:47 -07:00
parent 23522a297f
commit 7621677c93
3 changed files with 35 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import java.util.Optional;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
@ -18,6 +19,7 @@ import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.metadata.MetaDataValue;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.util.Util;
@ -253,4 +255,22 @@ public abstract class FlagListener implements Listener {
protected IslandWorldManager getIWM() {
return plugin.getIWM();
}
/**
* Check if PVP is allowed here or not
* @param location location where action is taking
* @return true if PVP is allowed, false if not
*/
protected boolean PVPAllowed(Location location) {
return plugin.getIslands().getIslandAt(location).map(i -> i.isAllowed(this.getFlag(location.getWorld()))).orElse(false);
}
protected Flag getFlag(World w) {
return switch (w.getEnvironment()) {
case NETHER -> Flags.PVP_NETHER;
case THE_END -> Flags.PVP_END;
default -> Flags.PVP_OVERWORLD;
};
}
}

View File

@ -7,7 +7,6 @@ import java.util.UUID;
import java.util.WeakHashMap;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Firework;
import org.bukkit.entity.LivingEntity;
@ -56,6 +55,10 @@ public class PVPListener extends FlagListener {
if (e.getEntity().equals(e.getDamager()) || e.getEntity().hasMetadata("NPC")) {
return;
}
// Is PVP allowed here?
if (this.PVPAllowed(e.getEntity().getLocation())) {
return;
}
// Protect visitors
if (e.getCause().equals(DamageCause.ENTITY_ATTACK) && protectedVisitor((Player)e.getEntity())) {
if (e.getDamager() instanceof Player) {
@ -117,6 +120,10 @@ public class PVPListener extends FlagListener {
if (e.getCaught().equals(e.getPlayer()) || e.getCaught().hasMetadata("NPC")) {
return;
}
// Is PVP allowed here?
if (this.PVPAllowed(e.getCaught().getLocation())) {
return;
}
// Protect visitors
if (protectedVisitor((Player)e.getCaught())) {
User.getInstance(e.getPlayer()).notify(Flags.INVINCIBLE_VISITORS.getHintReference());
@ -137,6 +144,10 @@ public class PVPListener extends FlagListener {
public void onSplashPotionSplash(final PotionSplashEvent e) {
if (e.getEntity().getShooter() instanceof Player && getPlugin().getIWM().inWorld(e.getEntity().getWorld())) {
User user = User.getInstance((Player)e.getEntity().getShooter());
// Is PVP allowed here?
if (this.PVPAllowed(e.getEntity().getLocation())) {
return;
}
// Run through affected entities and cancel the splash for protected players
for (LivingEntity le : e.getAffectedEntities()) {
if (!le.getUniqueId().equals(user.getUniqueId()) && blockPVP(user, le, e, getFlag(e.getEntity().getWorld()))) {
@ -198,14 +209,6 @@ public class PVPListener extends FlagListener {
}
}
private Flag getFlag(World w) {
return switch (w.getEnvironment()) {
case NETHER -> Flags.PVP_NETHER;
case THE_END -> Flags.PVP_END;
default -> Flags.PVP_OVERWORLD;
};
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled=true)
public void onPlayerShootFireworkEvent(final EntityShootBowEvent e) {
// Only care about players shooting fireworks

View File

@ -128,7 +128,9 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|| !getIWM().inWorld(world)
|| e.getEntity().hasMetadata("NPC")
|| !getIWM().getIvSettings(world).contains(e.getCause().name())
|| getIslands().userIsOnIsland(world, User.getInstance(e.getEntity()))) {
|| getIslands().userIsOnIsland(world, User.getInstance(e.getEntity()))
|| PVPAllowed(p.getLocation())
) {
return;
}
// Player is a visitor and should be protected from damage