From 7c23af3a7d3c000254dcc784e9ef54b04efe2f6c Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Fri, 6 Mar 2026 16:13:41 +0100 Subject: [PATCH] Add TNT only making Damage in spawning Region --- .../features/region/TNTListener.java | 36 +++++++++++++++++++ .../bausystem/region/RegionSystem.java | 11 ++++++ .../bausystem/region/DynamicRegionSystem.java | 1 + .../bausystem/region/FixedRegionSystem.java | 5 +++ 4 files changed, 53 insertions(+) diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java index 813bea81..2eda2b83 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java @@ -21,13 +21,16 @@ package de.steamwar.bausystem.features.region; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.region.Region; +import de.steamwar.bausystem.region.RegionSystem; import de.steamwar.bausystem.region.RegionUtils; import de.steamwar.bausystem.region.flags.Flag; import de.steamwar.bausystem.region.flags.TNTMode; import de.steamwar.bausystem.utils.ScoreboardElement; import de.steamwar.linkage.Linked; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.block.Block; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.entity.TNTPrimed; import org.bukkit.event.EventHandler; @@ -35,12 +38,26 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockExplodeEvent; import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.entity.EntitySpawnEvent; +import org.bukkit.persistence.PersistentDataType; import java.util.List; +import java.util.Objects; +import java.util.UUID; @Linked public class TNTListener implements Listener, ScoreboardElement { + private static final NamespacedKey SPAWN_REGION_ID = Objects.requireNonNull(NamespacedKey.fromString("spawn_region_id", BauSystem.getInstance())); + + @EventHandler + public void onEntitySpawn(EntitySpawnEvent event) { + Entity entity = event.getEntity(); + if (!(entity instanceof TNTPrimed)) return; + Region region = Region.getRegion(entity.getLocation()); + entity.getPersistentDataContainer().set(SPAWN_REGION_ID, PersistentDataType.STRING, region.getID().toString()); + } + private void explode(List blockList, boolean destroy) { blockList.removeIf(block -> { Region region = Region.getRegion(block.getLocation()); @@ -71,6 +88,25 @@ public class TNTListener implements Listener, ScoreboardElement { event.blockList().clear(); return; } + + Entity entity = event.getEntity(); + Region currentRegion = Region.getRegion(entity.getLocation()); + String spawningRegionIdString = entity.getPersistentDataContainer().get(SPAWN_REGION_ID, PersistentDataType.STRING); + UUID spawningRegionId = spawningRegionIdString == null ? null : UUID.fromString(spawningRegionIdString); + + // TNT Only created block damage in the Region it spawned in or in a connected special region! + if (!currentRegion.getID().equals(spawningRegionId)) { + if (!currentRegion.getType().isSpecial()) { + event.blockList().clear(); + return; + } + if (RegionSystem.INSTANCE.getConnectedRegions(currentRegion) + .noneMatch(region -> region.getID().equals(spawningRegionId))) { + event.blockList().clear(); + return; + } + } + explode(event.blockList(), true); } diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionSystem.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionSystem.java index abfa9e27..4853b368 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionSystem.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionSystem.java @@ -76,6 +76,12 @@ public interface RegionSystem { @NonNull Stream getRegions(); + /** + * Only contains Regions of the same Type as the one you inputted. + */ + @NonNull + Stream getConnectedRegions(Region region); + private static RegionSystem init() { if (Core.getVersion() >= 21) { // TODO: Add some kind of detection if the DynamicRegionSystem should be used! @@ -122,6 +128,11 @@ public interface RegionSystem { public Stream getRegions() { throw new UnsupportedOperationException(); } + + @Override + public @NonNull Stream getConnectedRegions(Region region) { + throw new UnsupportedOperationException(); + } }; } } diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/DynamicRegionSystem.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/DynamicRegionSystem.java index 6f29503b..bb853a43 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/DynamicRegionSystem.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/DynamicRegionSystem.java @@ -164,6 +164,7 @@ public class DynamicRegionSystem implements RegionSystem { .map(DynamicRegion.class::cast); } + @Override public Stream getConnectedRegions(Region region) { Set regions = regionTypeMap.get(region.getType()); diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/FixedRegionSystem.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/FixedRegionSystem.java index a5b8d2a1..feb8b22e 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/FixedRegionSystem.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/FixedRegionSystem.java @@ -75,4 +75,9 @@ public class FixedRegionSystem implements RegionSystem { public Stream getRegions() { return REGION_MAP.values().stream(); } + + @Override + public @NonNull Stream getConnectedRegions(Region region) { + return Stream.empty(); + } }