feature: implement road flags as widely as seems reasonable (#3870)

* feature: implement road flags as widely as seems reasonable
 - Closes #3047
 - Any further road flag implementations would be very specific from what I can gather, and thus the gist of #3047 is now implemented

* Add javadoc since tag

Co-authored-by: Alexander Brandes <mc.cache@web.de>

* Create util class for method

Co-authored-by: Alexander Brandes <mc.cache@web.de>
This commit is contained in:
Jordan 2022-11-15 18:07:38 +00:00 committed by GitHub
parent 2147012beb
commit b5bc1988e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 112 additions and 38 deletions

View File

@ -56,6 +56,7 @@ import com.plotsquared.core.plot.flag.types.BlockTypeWrapper;
import com.plotsquared.core.plot.flag.types.BooleanFlag; import com.plotsquared.core.plot.flag.types.BooleanFlag;
import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.PlotFlagUtil;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
import com.plotsquared.core.util.task.TaskTime; import com.plotsquared.core.util.task.TaskTime;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
@ -166,7 +167,7 @@ public class BlockEventListener implements Listener {
} }
Plot plot = location.getOwnedPlot(); Plot plot = location.getOwnedPlot();
if (plot == null) { if (plot == null) {
if (area.isRoadFlags() && !area.getRoadFlag(RedstoneFlag.class)) { if (PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, RedstoneFlag.class, false)) {
event.setNewCurrent(0); event.setNewCurrent(0);
} }
return; return;
@ -1084,7 +1085,10 @@ public class BlockEventListener implements Listener {
return; return;
} }
if (plot == null) { if (plot == null) {
if (!Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_ROAD)) { if (!PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, BlockIgnitionFlag.class, true) && !Permissions.hasPermission(
pp,
Permission.PERMISSION_ADMIN_BUILD_ROAD
)) {
pp.sendMessage( pp.sendMessage(
TranslatableCaption.of("permission.no_permission_event"), TranslatableCaption.of("permission.no_permission_event"),
Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_BUILD_ROAD)) Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_BUILD_ROAD))
@ -1092,7 +1096,10 @@ public class BlockEventListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
} }
} else if (!plot.hasOwner()) { } else if (!plot.hasOwner()) {
if (!Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_UNOWNED)) { if (!PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, BlockIgnitionFlag.class, true) && !Permissions.hasPermission(
pp,
Permission.PERMISSION_ADMIN_BUILD_UNOWNED
)) {
pp.sendMessage( pp.sendMessage(
TranslatableCaption.of("permission.no_permission_event"), TranslatableCaption.of("permission.no_permission_event"),
Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_BUILD_UNOWNED)) Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_BUILD_UNOWNED))

View File

@ -26,6 +26,7 @@ import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.flag.implementations.CopperOxideFlag; import com.plotsquared.core.plot.flag.implementations.CopperOxideFlag;
import com.plotsquared.core.plot.flag.implementations.MiscInteractFlag; import com.plotsquared.core.plot.flag.implementations.MiscInteractFlag;
import com.plotsquared.core.util.PlotFlagUtil;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -91,7 +92,8 @@ public class BlockEventListener117 implements Listener {
} }
Plot plot = location.getOwnedPlot(); Plot plot = location.getOwnedPlot();
if (plot == null || !plot.getFlag(MiscInteractFlag.class)) { if (plot == null && !PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, MiscInteractFlag.class, true) || plot != null && !plot.getFlag(
MiscInteractFlag.class)) {
if (plotPlayer != null) { if (plotPlayer != null) {
if (plot != null) { if (plot != null) {
if (!plot.isAdded(plotPlayer.getUUID())) { if (!plot.isAdded(plotPlayer.getUUID())) {

View File

@ -38,6 +38,7 @@ import com.plotsquared.core.plot.flag.implementations.InvincibleFlag;
import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.PlotFlagUtil;
import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockType;
import org.bukkit.Material; import org.bukkit.Material;
@ -261,7 +262,7 @@ public class EntityEventListener implements Listener {
} }
Plot plot = location.getOwnedPlot(); Plot plot = location.getOwnedPlot();
if (plot == null) { if (plot == null) {
if (area.isRoadFlags() && area.getRoadFlag(InvincibleFlag.class)) { if (PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, InvincibleFlag.class, true)) {
event.setCancelled(true); event.setCancelled(true);
} }
return; return;

View File

@ -44,6 +44,7 @@ import com.plotsquared.core.plot.flag.implementations.ProjectilesFlag;
import com.plotsquared.core.plot.flag.types.BooleanFlag; import com.plotsquared.core.plot.flag.types.BooleanFlag;
import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.PlotFlagUtil;
import net.kyori.adventure.text.minimessage.Template; import net.kyori.adventure.text.minimessage.Template;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -326,14 +327,18 @@ public class PaperListener implements Listener {
return; return;
} }
Location location = BukkitUtil.adapt(entity.getLocation()); Location location = BukkitUtil.adapt(entity.getLocation());
if (!this.plotAreaManager.hasPlotArea(location.getWorldName())) { PlotArea area = location.getPlotArea();
if (area == null) {
return; return;
} }
PlotPlayer<Player> pp = BukkitUtil.adapt((Player) shooter); PlotPlayer<Player> pp = BukkitUtil.adapt((Player) shooter);
Plot plot = location.getOwnedPlot(); Plot plot = location.getOwnedPlot();
if (plot == null) { if (plot == null) {
if (!Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_PROJECTILE_ROAD)) { if (!PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, ProjectilesFlag.class, true) && !Permissions.hasPermission(
pp,
Permission.PERMISSION_ADMIN_PROJECTILE_ROAD
)) {
pp.sendMessage( pp.sendMessage(
TranslatableCaption.of("permission.no_permission_event"), TranslatableCaption.of("permission.no_permission_event"),
Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_PROJECTILE_ROAD)) Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_PROJECTILE_ROAD))

View File

@ -68,6 +68,7 @@ import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.MathMan; import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.PlotFlagUtil;
import com.plotsquared.core.util.PremiumVerification; import com.plotsquared.core.util.PremiumVerification;
import com.plotsquared.core.util.entity.EntityCategories; import com.plotsquared.core.util.entity.EntityCategories;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
@ -889,8 +890,7 @@ public class PlayerEventListener implements Listener {
} }
} else { } else {
PlotArea area = pp.getPlotAreaAbs(); PlotArea area = pp.getPlotAreaAbs();
if (area != null && area.isRoadFlags() && area if (area != null && PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, PreventCreativeCopyFlag.class, true)) {
.getRoadFlag(PreventCreativeCopyFlag.class)) {
final ItemStack newStack = final ItemStack newStack =
new ItemStack(newItem.getType(), newItem.getAmount()); new ItemStack(newItem.getType(), newItem.getAmount());
event.setCursor(newStack); event.setCursor(newStack);
@ -998,8 +998,10 @@ public class PlayerEventListener implements Listener {
Plot plot = location.getPlotAbs(); Plot plot = location.getPlotAbs();
BukkitPlayer pp = BukkitUtil.adapt(e.getPlayer()); BukkitPlayer pp = BukkitUtil.adapt(e.getPlayer());
if (plot == null) { if (plot == null) {
if (!area.isRoadFlags() && !area.getRoadFlag(MiscInteractFlag.class) && !Permissions if (!PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, MiscInteractFlag.class, true) && !Permissions.hasPermission(
.hasPermission(pp, Permission.PERMISSION_ADMIN_INTERACT_ROAD)) { pp,
Permission.PERMISSION_ADMIN_INTERACT_ROAD
)) {
pp.sendMessage( pp.sendMessage(
TranslatableCaption.of("permission.no_permission_event"), TranslatableCaption.of("permission.no_permission_event"),
Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_INTERACT_ROAD)) Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_INTERACT_ROAD))
@ -1009,9 +1011,7 @@ public class PlayerEventListener implements Listener {
} else { } else {
if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) {
if (!Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_OTHER)) { if (!Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_BUILD_OTHER)) {
pp.sendMessage( pp.sendMessage(TranslatableCaption.of("done.building_restricted"));
TranslatableCaption.of("done.building_restricted")
);
e.setCancelled(true); e.setCancelled(true);
return; return;
} }
@ -1594,7 +1594,10 @@ public class PlayerEventListener implements Listener {
BukkitPlayer pp = BukkitUtil.adapt(p); BukkitPlayer pp = BukkitUtil.adapt(p);
Plot plot = area.getPlot(location); Plot plot = area.getPlot(location);
if (plot == null) { if (plot == null) {
if (!Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_DESTROY_VEHICLE_ROAD)) { if (!PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, VehicleBreakFlag.class, true) && !Permissions.hasPermission(
pp,
Permission.PERMISSION_ADMIN_DESTROY_VEHICLE_ROAD
)) {
pp.sendMessage( pp.sendMessage(
TranslatableCaption.of("permission.no_permission_event"), TranslatableCaption.of("permission.no_permission_event"),
Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_DESTROY_VEHICLE_ROAD)) Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_DESTROY_VEHICLE_ROAD))
@ -1642,7 +1645,7 @@ public class PlayerEventListener implements Listener {
} }
Plot plot = location.getOwnedPlot(); Plot plot = location.getOwnedPlot();
if (plot == null) { if (plot == null) {
if (area.isRoadFlags() && !area.getRoadFlag(ItemDropFlag.class)) { if (PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, ItemDropFlag.class, false)) {
event.setCancelled(true); event.setCancelled(true);
} }
return; return;
@ -1668,7 +1671,7 @@ public class PlayerEventListener implements Listener {
} }
Plot plot = location.getOwnedPlot(); Plot plot = location.getOwnedPlot();
if (plot == null) { if (plot == null) {
if (area.isRoadFlags() && area.getRoadFlag(DropProtectionFlag.class)) { if (PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, DropProtectionFlag.class, true)) {
event.setCancelled(true); event.setCancelled(true);
} }
return; return;
@ -1690,19 +1693,17 @@ public class PlayerEventListener implements Listener {
} }
Plot plot = location.getOwnedPlot(); Plot plot = location.getOwnedPlot();
if (plot == null) { if (plot == null) {
if (area.isRoadFlags() && area.getRoadFlag(KeepInventoryFlag.class)) { if (PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, KeepInventoryFlag.class, true)) {
event.setCancelled(true); event.setCancelled(true);
} }
return; return;
} }
if (plot.getFlag(KeepInventoryFlag.class)) {
if (plot.getFlag(KeepInventoryFlag.class)) { if (plot.getFlag(KeepInventoryFlag.class)) {
plot.debug(event.getEntity().getName() + " kept their inventory because of keep-inventory = true"); plot.debug(event.getEntity().getName() + " kept their inventory because of keep-inventory = true");
event.getDrops().clear(); event.getDrops().clear();
event.setKeepInventory(true); event.setKeepInventory(true);
} }
} }
}
@SuppressWarnings("deprecation") // #getLocate is needed for Spigot compatibility @SuppressWarnings("deprecation") // #getLocate is needed for Spigot compatibility
@EventHandler @EventHandler
@ -1725,18 +1726,16 @@ public class PlayerEventListener implements Listener {
} }
Plot plot = location.getOwnedPlot(); Plot plot = location.getOwnedPlot();
if (plot == null) { if (plot == null) {
if (area.isRoadFlags() && area.getRoadFlag(DenyPortalTravelFlag.class)) { if (PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, DenyPortalTravelFlag.class, true)) {
event.setCancelled(true); event.setCancelled(true);
} }
return; return;
} }
if (plot.getFlag(DenyPortalTravelFlag.class)) {
if (plot.getFlag(DenyPortalTravelFlag.class)) { if (plot.getFlag(DenyPortalTravelFlag.class)) {
plot.debug(event.getPlayer().getName() + " did not travel thru a portal because of deny-portal-travel = true"); plot.debug(event.getPlayer().getName() + " did not travel thru a portal because of deny-portal-travel = true");
event.setCancelled(true); event.setCancelled(true);
} }
} }
}
@EventHandler @EventHandler
public void onPortalCreation(PortalCreateEvent event) { public void onPortalCreation(PortalCreateEvent event) {
@ -1772,7 +1771,7 @@ public class PlayerEventListener implements Listener {
} }
Plot plot = location.getOwnedPlot(); Plot plot = location.getOwnedPlot();
if (plot == null) { if (plot == null) {
if (area.isRoadFlags() && area.getRoadFlag(DenyPortalsFlag.class)) { if (PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, DenyPortalsFlag.class, true)) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
@ -1801,17 +1800,15 @@ public class PlayerEventListener implements Listener {
} }
Plot plot = location.getOwnedPlot(); Plot plot = location.getOwnedPlot();
if (plot == null) { if (plot == null) {
if (area.isRoadFlags() && area.getRoadFlag(LecternReadBookFlag.class)) { if (PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, LecternReadBookFlag.class, true)) {
event.setCancelled(true); event.setCancelled(true);
} }
return; return;
} }
if (plot.getFlag(LecternReadBookFlag.class)) {
if (plot.getFlag(LecternReadBookFlag.class)) { if (plot.getFlag(LecternReadBookFlag.class)) {
plot.debug(event.getPlayer().getName() + " could not take the book because of lectern-read-book = true"); plot.debug(event.getPlayer().getName() + " could not take the book because of lectern-read-book = true");
event.setCancelled(true); event.setCancelled(true);
} }
} }
}
} }

View File

@ -31,6 +31,7 @@ import com.plotsquared.core.plot.PlotHandler;
import com.plotsquared.core.plot.flag.implementations.ProjectilesFlag; import com.plotsquared.core.plot.flag.implementations.ProjectilesFlag;
import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.PlotFlagUtil;
import net.kyori.adventure.text.minimessage.Template; import net.kyori.adventure.text.minimessage.Template;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
@ -97,14 +98,18 @@ public class ProjectileEventListener implements Listener {
return; return;
} }
Location location = BukkitUtil.adapt(entity.getLocation()); Location location = BukkitUtil.adapt(entity.getLocation());
if (!this.plotAreaManager.hasPlotArea(location.getWorldName())) { PlotArea area = location.getPlotArea();
if (area == null) {
return; return;
} }
PlotPlayer<Player> pp = BukkitUtil.adapt((Player) shooter); PlotPlayer<Player> pp = BukkitUtil.adapt((Player) shooter);
Plot plot = location.getOwnedPlot(); Plot plot = location.getOwnedPlot();
if (plot == null) { if (plot == null) {
if (!Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_PROJECTILE_ROAD)) { if (!PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, ProjectilesFlag.class, true) && !Permissions.hasPermission(
pp,
Permission.PERMISSION_ADMIN_PROJECTILE_ROAD
)) {
pp.sendMessage( pp.sendMessage(
TranslatableCaption.of("permission.no_permission_event"), TranslatableCaption.of("permission.no_permission_event"),
Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_PROJECTILE_ROAD)) Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_PROJECTILE_ROAD))
@ -154,6 +159,8 @@ public class ProjectileEventListener implements Listener {
if (plot.isAdded(((Player) shooter).getUniqueId()) || plot.getFlag(ProjectilesFlag.class)) { if (plot.isAdded(((Player) shooter).getUniqueId()) || plot.getFlag(ProjectilesFlag.class)) {
return; return;
} }
} else if (PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, ProjectilesFlag.class, true)) {
return;
} }
entity.remove(); entity.remove();
@ -163,7 +170,10 @@ public class ProjectileEventListener implements Listener {
PlotPlayer<?> pp = BukkitUtil.adapt((Player) shooter); PlotPlayer<?> pp = BukkitUtil.adapt((Player) shooter);
if (plot == null) { if (plot == null) {
if (!Permissions.hasPermission(pp, Permission.PERMISSION_ADMIN_PROJECTILE_UNOWNED)) { if (!PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, ProjectilesFlag.class, true) && !Permissions.hasPermission(
pp,
Permission.PERMISSION_ADMIN_PROJECTILE_UNOWNED
)) {
entity.remove(); entity.remove();
event.setCancelled(true); event.setCancelled(true);
} }
@ -194,7 +204,6 @@ public class ProjectileEventListener implements Listener {
if (sPlot == null || !PlotHandler.sameOwners(plot, sPlot)) { if (sPlot == null || !PlotHandler.sameOwners(plot, sPlot)) {
entity.remove(); entity.remove();
event.setCancelled(true); event.setCancelled(true);
return;
} }
} }
} }

View File

@ -0,0 +1,53 @@
/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.plotsquared.core.util;
import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.flag.PlotFlag;
import java.util.Objects;
/**
* Util class for generic methods relating to plot flags.
*
* @since TODO
*/
public final class PlotFlagUtil {
private PlotFlagUtil() {
//No-op
}
/**
* Check if the value of a {@link PlotFlag} matches the given value. If
* road flags are disabled for the given plot area, returns false.
*
* @param flagClass boolean flag to get value of
* @param value boolean value to check flag value against
* @param <T> The flag value type
* @return true if road flag value matches with road flags enabled
* @since TODO
*/
public static <T> boolean isAreaRoadFlagsAndFlagEquals(
PlotArea area, final Class<? extends PlotFlag<T, ?>> flagClass, T value
) {
return area.isRoadFlags() && Objects.equals(area.getRoadFlag(flagClass), value);
}
}