From 5cce86d9247c9b72bf0d58676fdfce140d4f6605 Mon Sep 17 00:00:00 2001 From: ByteExceptionM Date: Thu, 9 Feb 2023 20:51:59 +0100 Subject: [PATCH] feat: Add event firing on remove road entity (#3955) * feat: Add event firing on remove road entity Signed-off-by: ByteExceptionM * chore: Add some more entity vars Signed-off-by: ByteExceptionM * chore: Rename method Signed-off-by: ByteExceptionM * chore: Code cleanup Signed-off-by: ByteExceptionM * chore: Code reformat Signed-off-by: ByteExceptionM * chore: Change iterator removal Signed-off-by: ByteExceptionM * Comply with checkstyle --------- Signed-off-by: ByteExceptionM Co-authored-by: Alexander Brandes --- .../plotsquared/bukkit/BukkitPlatform.java | 31 +++++---- .../plotsquared/core/events/EntityEvent.java | 63 +++++++++++++++++++ .../core/events/RemoveRoadEntityEvent.java | 50 +++++++++++++++ .../core/util/EventDispatcher.java | 8 +++ 4 files changed, 141 insertions(+), 11 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/events/EntityEvent.java create mode 100644 Core/src/main/java/com/plotsquared/core/events/RemoveRoadEntityEvent.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java index 8055bd5cb..c82de570e 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java @@ -71,6 +71,8 @@ import com.plotsquared.core.configuration.Storage; import com.plotsquared.core.configuration.caption.ChatFormatter; import com.plotsquared.core.configuration.file.YamlConfiguration; import com.plotsquared.core.database.DBFunc; +import com.plotsquared.core.events.RemoveRoadEntityEvent; +import com.plotsquared.core.events.Result; import com.plotsquared.core.generator.GeneratorWrapper; import com.plotsquared.core.generator.IndependentPlotGenerator; import com.plotsquared.core.generator.SingleWorldGenerator; @@ -109,6 +111,7 @@ import com.plotsquared.core.uuid.CacheUUIDService; import com.plotsquared.core.uuid.UUIDPipeline; import com.plotsquared.core.uuid.offline.OfflineModeUUIDService; import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.bukkit.BukkitAdapter; import io.papermc.lib.PaperLib; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.text.Component; @@ -810,8 +813,7 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl if (entity.hasMetadata("ps-tmp-teleport")) { continue; } - iterator.remove(); - entity.remove(); + this.removeRoadEntity(entity, iterator); } continue; } @@ -824,8 +826,7 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl if (entity.hasMetadata("ps-tmp-teleport")) { continue; } - iterator.remove(); - entity.remove(); + this.removeRoadEntity(entity, iterator); } } continue; @@ -835,7 +836,7 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl case "DROPPED_ITEM": if (Settings.Enabled_Components.KILL_ROAD_ITEMS && plotArea.getOwnedPlotAbs(BukkitUtil.adapt(entity.getLocation())) == null) { - entity.remove(); + this.removeRoadEntity(entity, iterator); } // dropped item continue; @@ -866,8 +867,7 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl if (entity.hasMetadata("ps-tmp-teleport")) { continue; } - iterator.remove(); - entity.remove(); + this.removeRoadEntity(entity, iterator); } } } @@ -972,8 +972,7 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl if (entity.hasMetadata("ps-tmp-teleport")) { continue; } - iterator.remove(); - entity.remove(); + this.removeRoadEntity(entity, iterator); } } } else { @@ -984,8 +983,7 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl if (entity.hasMetadata("ps-tmp-teleport")) { continue; } - iterator.remove(); - entity.remove(); + this.removeRoadEntity(entity, iterator); } } } @@ -999,6 +997,17 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl }), TaskTime.seconds(1L)); } + private void removeRoadEntity(Entity entity, Iterator entityIterator) { + RemoveRoadEntityEvent event = eventDispatcher.callRemoveRoadEntity(BukkitAdapter.adapt(entity)); + + if (event.getEventResult() == Result.DENY) { + return; + } + + entityIterator.remove(); + entity.remove(); + } + @Override public @Nullable final ChunkGenerator getDefaultWorldGenerator( diff --git a/Core/src/main/java/com/plotsquared/core/events/EntityEvent.java b/Core/src/main/java/com/plotsquared/core/events/EntityEvent.java new file mode 100644 index 000000000..5642342b6 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/events/EntityEvent.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.core.events; + +import com.sk89q.worldedit.entity.Entity; +import org.checkerframework.checker.nullness.qual.NonNull; + +/** + * @since TODO + */ +public abstract class EntityEvent { + + private final Entity entity; + + private String name; + + /** + * @since TODO + */ + public EntityEvent(Entity entity) { + this.entity = entity; + } + + /** + * Obtain the entity involved in the event + * + * @return Entity + * @since TODO + */ + public Entity getEntity() { + return this.entity; + } + + /** + * Obtain the event's class name + * + * @return the event class name + * @since TODO + */ + @NonNull public String getEventName() { + if (this.name == null) { + this.name = this.getClass().getSimpleName(); + } + return this.name; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/events/RemoveRoadEntityEvent.java b/Core/src/main/java/com/plotsquared/core/events/RemoveRoadEntityEvent.java new file mode 100644 index 000000000..aa7155cc7 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/events/RemoveRoadEntityEvent.java @@ -0,0 +1,50 @@ +/* + * 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.events; + +import com.sk89q.worldedit.entity.Entity; + +/** + * @since TODO + */ +public class RemoveRoadEntityEvent extends EntityEvent implements CancellablePlotEvent { + + private Result eventResult; + + /** + * RemoveRoadEntityEvent: Called when an entity on road is removed. + * + * @param entity The entity to remove + * @since TODO + */ + public RemoveRoadEntityEvent(Entity entity) { + super(entity); + } + + @Override + public Result getEventResult() { + return this.eventResult; + } + + @Override + public void setEventResult(Result eventResult) { + this.eventResult = eventResult; + } + +} 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 9318cccd0..f935c50f7 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java +++ b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java @@ -45,6 +45,7 @@ import com.plotsquared.core.events.PlotFlagRemoveEvent; import com.plotsquared.core.events.PlotMergeEvent; import com.plotsquared.core.events.PlotRateEvent; import com.plotsquared.core.events.PlotUnlinkEvent; +import com.plotsquared.core.events.RemoveRoadEntityEvent; import com.plotsquared.core.events.TeleportCause; import com.plotsquared.core.events.post.PostPlayerAutoPlotEvent; import com.plotsquared.core.events.post.PostPlotChangeOwnerEvent; @@ -71,6 +72,7 @@ import com.plotsquared.core.plot.flag.types.BlockTypeWrapper; import com.plotsquared.core.plot.world.SinglePlotArea; 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.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; @@ -296,6 +298,12 @@ public class EventDispatcher { return event; } + public RemoveRoadEntityEvent callRemoveRoadEntity(Entity entity) { + RemoveRoadEntityEvent event = new RemoveRoadEntityEvent(entity); + eventBus.post(event); + return event; + } + public void doJoinTask(final PlotPlayer player) { if (player == null) { return; //possible future warning message to figure out where we are retrieving null