From 31ae62b62cb510f923c5e77ce9f2b7f40d6a708c Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Sun, 26 Nov 2023 13:36:01 +0100 Subject: [PATCH] Introduce `edit-sign` flag (#4236) --- .../plotsquared/bukkit/BukkitPlatform.java | 4 ++ .../listener/PlayerEventListener1201.java | 63 +++++++++++++++++++ .../core/plot/flag/GlobalFlagContainer.java | 2 + .../flag/implementations/EditSignFlag.java | 41 ++++++++++++ .../core/util/EventDispatcher.java | 8 +++ Core/src/main/resources/lang/messages_en.json | 1 + build.gradle.kts | 2 +- 7 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener1201.java create mode 100644 Core/src/main/java/com/plotsquared/core/plot/flag/implementations/EditSignFlag.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java index c340d373e..ad332176e 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java @@ -37,6 +37,7 @@ import com.plotsquared.bukkit.listener.EntityEventListener; import com.plotsquared.bukkit.listener.EntitySpawnListener; import com.plotsquared.bukkit.listener.PaperListener; import com.plotsquared.bukkit.listener.PlayerEventListener; +import com.plotsquared.bukkit.listener.PlayerEventListener1201; import com.plotsquared.bukkit.listener.ProjectileEventListener; import com.plotsquared.bukkit.listener.ServerListener; import com.plotsquared.bukkit.listener.SingleWorldListener; @@ -359,6 +360,9 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl if (Settings.Enabled_Components.EVENTS) { getServer().getPluginManager().registerEvents(injector().getInstance(PlayerEventListener.class), this); + if ((serverVersion()[1] == 20 && serverVersion()[2] >= 1) || serverVersion()[1] > 20) { + getServer().getPluginManager().registerEvents(injector().getInstance(PlayerEventListener1201.class), this); + } getServer().getPluginManager().registerEvents(injector().getInstance(BlockEventListener.class), this); if (serverVersion()[1] >= 17) { getServer().getPluginManager().registerEvents(injector().getInstance(BlockEventListener117.class), this); diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener1201.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener1201.java new file mode 100644 index 000000000..4dab1198b --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener1201.java @@ -0,0 +1,63 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * 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 . + */ +package com.plotsquared.bukkit.listener; + +import com.plotsquared.bukkit.util.BukkitUtil; +import com.plotsquared.core.location.Location; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotArea; +import com.plotsquared.core.plot.flag.implementations.EditSignFlag; +import com.plotsquared.core.util.PlotFlagUtil; +import org.bukkit.block.Sign; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerSignOpenEvent; + +/** + * For events since 1.20.1 + * @since TODO + */ +public class PlayerEventListener1201 implements Listener { + + @EventHandler(ignoreCancelled = true) + @SuppressWarnings({"removal", "UnstableApiUsage"}) // thanks Paper, thanks Spigot + public void onPlayerSignOpenEvent(PlayerSignOpenEvent event) { + Sign sign = event.getSign(); + Location location = BukkitUtil.adapt(sign.getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = location.getOwnedPlot(); + if (plot == null) { + if (PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, EditSignFlag.class, false)) { + event.setCancelled(true); + } + return; + } + if (plot.isAdded(event.getPlayer().getUniqueId())) { + return; // allow for added players + } + if (!plot.getFlag(EditSignFlag.class)) { + plot.debug(event.getPlayer().getName() + " could not edit the sign because of edit-sign = false"); + event.setCancelled(true); + } + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/plot/flag/GlobalFlagContainer.java b/Core/src/main/java/com/plotsquared/core/plot/flag/GlobalFlagContainer.java index 988367c54..fe2eb2398 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/flag/GlobalFlagContainer.java +++ b/Core/src/main/java/com/plotsquared/core/plot/flag/GlobalFlagContainer.java @@ -42,6 +42,7 @@ import com.plotsquared.core.plot.flag.implementations.DeviceInteractFlag; import com.plotsquared.core.plot.flag.implementations.DisablePhysicsFlag; import com.plotsquared.core.plot.flag.implementations.DoneFlag; import com.plotsquared.core.plot.flag.implementations.DropProtectionFlag; +import com.plotsquared.core.plot.flag.implementations.EditSignFlag; import com.plotsquared.core.plot.flag.implementations.EntityCapFlag; import com.plotsquared.core.plot.flag.implementations.EntityChangeBlockFlag; import com.plotsquared.core.plot.flag.implementations.ExplosionFlag; @@ -153,6 +154,7 @@ public final class GlobalFlagContainer extends FlagContainer { this.addFlag(DeviceInteractFlag.DEVICE_INTERACT_FALSE); this.addFlag(DisablePhysicsFlag.DISABLE_PHYSICS_FALSE); this.addFlag(DropProtectionFlag.DROP_PROTECTION_FALSE); + this.addFlag(EditSignFlag.EDIT_SIGN_FALSE); this.addFlag(EntityChangeBlockFlag.ENTITY_CHANGE_BLOCK_FALSE); this.addFlag(ExplosionFlag.EXPLOSION_FALSE); this.addFlag(ForcefieldFlag.FORCEFIELD_FALSE); diff --git a/Core/src/main/java/com/plotsquared/core/plot/flag/implementations/EditSignFlag.java b/Core/src/main/java/com/plotsquared/core/plot/flag/implementations/EditSignFlag.java new file mode 100644 index 000000000..a4a0f628d --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/plot/flag/implementations/EditSignFlag.java @@ -0,0 +1,41 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * 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 . + */ +package com.plotsquared.core.plot.flag.implementations; + +import com.plotsquared.core.configuration.caption.TranslatableCaption; +import com.plotsquared.core.plot.flag.types.BooleanFlag; +import org.checkerframework.checker.nullness.qual.NonNull; + +/** + * @since TODO + */ +public class EditSignFlag extends BooleanFlag { + public static final EditSignFlag EDIT_SIGN_TRUE = new EditSignFlag(true); + public static final EditSignFlag EDIT_SIGN_FALSE = new EditSignFlag(false); + + private EditSignFlag(final boolean value) { + super(value, TranslatableCaption.of("flags.flag_description_edit_sign")); + } + + @Override + protected EditSignFlag flagOf(@NonNull final Boolean value) { + return value ? EDIT_SIGN_TRUE : EDIT_SIGN_FALSE; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java index 4de989601..128337c20 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java +++ b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java @@ -63,6 +63,7 @@ import com.plotsquared.core.plot.PlotId; import com.plotsquared.core.plot.Rating; import com.plotsquared.core.plot.flag.PlotFlag; import com.plotsquared.core.plot.flag.implementations.DeviceInteractFlag; +import com.plotsquared.core.plot.flag.implementations.EditSignFlag; import com.plotsquared.core.plot.flag.implementations.MiscPlaceFlag; import com.plotsquared.core.plot.flag.implementations.MobPlaceFlag; import com.plotsquared.core.plot.flag.implementations.PlaceFlag; @@ -74,6 +75,7 @@ import com.plotsquared.core.util.task.TaskManager; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.function.pattern.Pattern; +import com.sk89q.worldedit.world.block.BlockCategories; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; import net.kyori.adventure.text.Component; @@ -392,6 +394,12 @@ public class EventDispatcher { if (player.hasPermission(Permission.PERMISSION_ADMIN_INTERACT_OTHER.toString(), false)) { return true; } + // we check for the EditSignFlag in the PlayerSignOpenEvent again, but we must not cancel the interact event + // or send a message if the flag is true + if (BlockCategories.ALL_SIGNS != null && BlockCategories.ALL_SIGNS.contains(blockType) + && plot.getFlag(EditSignFlag.class)) { + return true; + } if (notifyPerms) { player.sendMessage( TranslatableCaption.of("commandconfig.flag_tutorial_usage"), diff --git a/Core/src/main/resources/lang/messages_en.json b/Core/src/main/resources/lang/messages_en.json index cdc8a371b..72e1f72c7 100644 --- a/Core/src/main/resources/lang/messages_en.json +++ b/Core/src/main/resources/lang/messages_en.json @@ -555,6 +555,7 @@ "flags.flag_description_device_interact": "Set to `true` to allow devices to be interacted with in the plot.", "flags.flag_description_disable_physics": "Set to `true` to disable block physics in the plot.", "flags.flag_description_drop_protection": "Set to `true` to prevent dropped items from being picked up by non-members of the plot.", + "flags.flag_description_edit_sign": "Set to `true` to allow editing signs in the plot.", "flags.flag_description_feed": "Specify an interval in seconds and an optional amount by which the players will be fed (amount is 1 by default).", "flags.flag_description_forcefield": "Set to `true` to enable member forcefield in the plot.", "flags.flag_description_grass_grow": "Set to `false` to prevent grass from growing within the plot.", diff --git a/build.gradle.kts b/build.gradle.kts index cfcdea884..92c26e34e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,8 +1,8 @@ import com.diffplug.gradle.spotless.SpotlessPlugin import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin import groovy.json.JsonSlurper -import java.net.URI import xyz.jpenilla.runpaper.task.RunServer +import java.net.URI plugins { java