Add TNT only making Damage in spawning Region

This commit is contained in:
2026-03-16 21:08:26 +01:00
parent 800705d7fd
commit 7c23af3a7d
4 changed files with 53 additions and 0 deletions
@@ -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<Block> 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);
}
@@ -76,6 +76,12 @@ public interface RegionSystem {
@NonNull
Stream<Region> getRegions();
/**
* Only contains Regions of the same Type as the one you inputted.
*/
@NonNull
Stream<Region> 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<Region> getRegions() {
throw new UnsupportedOperationException();
}
@Override
public @NonNull Stream<Region> getConnectedRegions(Region region) {
throw new UnsupportedOperationException();
}
};
}
}
@@ -164,6 +164,7 @@ public class DynamicRegionSystem implements RegionSystem {
.map(DynamicRegion.class::cast);
}
@Override
public Stream<Region> getConnectedRegions(Region region) {
Set<Region> regions = regionTypeMap.get(region.getType());
@@ -75,4 +75,9 @@ public class FixedRegionSystem implements RegionSystem {
public Stream<Region> getRegions() {
return REGION_MAP.values().stream();
}
@Override
public @NonNull Stream<Region> getConnectedRegions(Region region) {
return Stream.empty();
}
}