From 82f868ae7d60c4848bf14ea0a79bc57e49b20fa6 Mon Sep 17 00:00:00 2001 From: Nicolai <131914029+Fameless9@users.noreply.github.com> Date: Tue, 26 Mar 2024 17:38:55 +0100 Subject: [PATCH] Introduce tile-drop flag (#4371) * added tile-drops flag * added tile-drops flag * added tile-drop flag * updated description * relocated listener --- .../bukkit/listener/PaperListener.java | 15 +++++++ .../bukkit/listener/PlayerEventListener.java | 15 +++++++ .../core/plot/flag/GlobalFlagContainer.java | 2 + .../flag/implementations/TileDropFlag.java | 43 +++++++++++++++++++ Core/src/main/resources/lang/messages_en.json | 1 + 5 files changed, 76 insertions(+) create mode 100644 Core/src/main/java/com/plotsquared/core/plot/flag/implementations/TileDropFlag.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java index 9570cf3c2..6de5f3275 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java @@ -19,6 +19,7 @@ package com.plotsquared.bukkit.listener; import com.destroystokyo.paper.event.block.BeaconEffectEvent; +import com.destroystokyo.paper.event.block.BlockDestroyEvent; import com.destroystokyo.paper.event.entity.EntityPathfindEvent; import com.destroystokyo.paper.event.entity.PlayerNaturallySpawnCreaturesEvent; import com.destroystokyo.paper.event.entity.PreCreatureSpawnEvent; @@ -42,6 +43,7 @@ import com.plotsquared.core.plot.flag.implementations.BeaconEffectsFlag; import com.plotsquared.core.plot.flag.implementations.DoneFlag; import com.plotsquared.core.plot.flag.implementations.FishingFlag; import com.plotsquared.core.plot.flag.implementations.ProjectilesFlag; +import com.plotsquared.core.plot.flag.implementations.TileDropFlag; import com.plotsquared.core.plot.flag.types.BooleanFlag; import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.util.PlotFlagUtil; @@ -84,6 +86,19 @@ public class PaperListener implements Listener { this.plotAreaManager = plotAreaManager; } + @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) + public void onBlockDestroy(final BlockDestroyEvent event) { + Location location = BukkitUtil.adapt(event.getBlock().getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getPlot(location); + if (plot != null) { + event.setWillDrop(plot.getFlag(TileDropFlag.class)); + } + } + @EventHandler public void onEntityPathfind(EntityPathfindEvent event) { if (!Settings.Paper_Components.ENTITY_PATHING) { diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java index 9fba3dff9..6c2da8f45 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java @@ -61,6 +61,7 @@ import com.plotsquared.core.plot.flag.implementations.MiscInteractFlag; import com.plotsquared.core.plot.flag.implementations.PlayerInteractFlag; import com.plotsquared.core.plot.flag.implementations.PreventCreativeCopyFlag; import com.plotsquared.core.plot.flag.implementations.TamedInteractFlag; +import com.plotsquared.core.plot.flag.implementations.TileDropFlag; import com.plotsquared.core.plot.flag.implementations.UntrustedVisitFlag; import com.plotsquared.core.plot.flag.implementations.VehicleBreakFlag; import com.plotsquared.core.plot.flag.implementations.VehicleUseFlag; @@ -107,6 +108,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.entity.EntityPickupItemEvent; import org.bukkit.event.entity.EntityPlaceEvent; import org.bukkit.event.entity.EntityPotionEffectEvent; @@ -236,6 +238,19 @@ public class PlayerEventListener implements Listener { this.plotListener = plotListener; } + @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) + public void onBlockBreak(final BlockBreakEvent event) { + Location location = BukkitUtil.adapt(event.getBlock().getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getPlot(location); + if (plot != null) { + event.setDropItems(plot.getFlag(TileDropFlag.class)); + } + } + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onPlayerDyeSign(PlayerInteractEvent event) { ItemStack itemStack = event.getItem(); 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 2183b008f..bff2b0c8b 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 @@ -102,6 +102,7 @@ import com.plotsquared.core.plot.flag.implementations.SnowMeltFlag; import com.plotsquared.core.plot.flag.implementations.SoilDryFlag; import com.plotsquared.core.plot.flag.implementations.TamedAttackFlag; import com.plotsquared.core.plot.flag.implementations.TamedInteractFlag; +import com.plotsquared.core.plot.flag.implementations.TileDropFlag; import com.plotsquared.core.plot.flag.implementations.TimeFlag; import com.plotsquared.core.plot.flag.implementations.TitlesFlag; import com.plotsquared.core.plot.flag.implementations.UntrustedVisitFlag; @@ -198,6 +199,7 @@ public final class GlobalFlagContainer extends FlagContainer { this.addFlag(SoilDryFlag.SOIL_DRY_FALSE); this.addFlag(TamedAttackFlag.TAMED_ATTACK_FALSE); this.addFlag(TamedInteractFlag.TAMED_INTERACT_FALSE); + this.addFlag(TileDropFlag.TILE_DROP_TRUE); this.addFlag(UntrustedVisitFlag.UNTRUSTED_VISIT_FLAG_TRUE); this.addFlag(VehicleBreakFlag.VEHICLE_BREAK_FALSE); this.addFlag(VehiclePlaceFlag.VEHICLE_PLACE_FALSE); diff --git a/Core/src/main/java/com/plotsquared/core/plot/flag/implementations/TileDropFlag.java b/Core/src/main/java/com/plotsquared/core/plot/flag/implementations/TileDropFlag.java new file mode 100644 index 000000000..459b6f89d --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/plot/flag/implementations/TileDropFlag.java @@ -0,0 +1,43 @@ +/* + * 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 TileDropFlag extends BooleanFlag { + + public static final TileDropFlag TILE_DROP_TRUE = new TileDropFlag(true); + public static final TileDropFlag TILE_DROP_FALSE = new TileDropFlag(false); + + private TileDropFlag(boolean value) { + super(value, TranslatableCaption.of("flags.flag_description_tile_drop")); + } + + @Override + protected TileDropFlag flagOf(@NonNull Boolean value) { + return value ? TILE_DROP_TRUE : TILE_DROP_FALSE; + } + +} diff --git a/Core/src/main/resources/lang/messages_en.json b/Core/src/main/resources/lang/messages_en.json index 06cbd5209..5ea33d6a6 100644 --- a/Core/src/main/resources/lang/messages_en.json +++ b/Core/src/main/resources/lang/messages_en.json @@ -606,6 +606,7 @@ "flags.flag_description_tamed_attack": "Set to `true` to allow guests to attack tamed animals in the plot.", "flags.flag_description_tamed_interact": "Set to `true` to allow guests to interact with tamed animals in the plot.", "flags.flag_description_time": "Set the time in the plot to a fixed value.", + "flags.flag_description_tile_drop": "Set to `false` to prevent blocks from dropping items in the plot.", "flags.flag_description_titles": "Set to `false` to disable plot titles. Can be set to: `none` (to inherit world settings), `true`, or `false`", "flags.flag_description_title": "Set the pop-up title's title and subtitle. Format: /plot flag set title \"A title\" \"The subtitle\"", "flags.flag_description_use": "Define a list of materials players should be able to interact with in the plot.",