Skip to content

Commit

Permalink
Add unclaimEntityTypeDelete to TownyWorld. (#5992)
Browse files Browse the repository at this point in the history
* Add unclaimEntityTypeDelete to TownyWorld.

Allows Towny to delete all Entities of a given type from a TownBlock
when it is unclaimed by a town.

New Config Options:
 - new_world_settings.plot_management.entity_delete.enabled
   - Default: false
 - new_world_settings.plot_management.entity_delete.unclaim_delete
   - Default: "ENDERCRYSTAL"
   - These entities will be deleted upon a plot being unclaimed

Closes #5953.

* Fix typos, add links to Spigot javadoc for reference.

* Change setting TownyWorld's entities to replace if null.
  • Loading branch information
LlmDl authored Jul 19, 2022
1 parent cf9f8f6 commit 9e5ce64
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/com/palmergames/bukkit/config/ConfigNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,21 @@ public enum ConfigNodes {
"",
"# These items will be deleted upon a plot being unclaimed"),

NWS_PLOT_MANAGEMENT_ENTITY_DELETE_HEADER(
"new_world_settings.plot_management.entity_delete",
"",
"",
"# This section is applied to new worlds as default settings when new worlds are detected."),
NWS_PLOT_MANAGEMENT_ENTITY_DELETE_ENABLE(
"new_world_settings.plot_management.entity_delete.enabled",
"false"),
NWS_PLOT_MANAGEMENT_ENTITY_DELETE(
"new_world_settings.plot_management.entity_delete.unclaim_delete",
"ENDER_CRYSTAL",
"",
"# These entities will be deleted upon a plot being unclaimed.",
"# Valid EntityTypes can be found here: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/EntityType.html"),

NWS_PLOT_MANAGEMENT_MAYOR_DELETE_HEADER(
"new_world_settings.plot_management.mayor_plotblock_delete",
"",
Expand Down
8 changes: 8 additions & 0 deletions src/com/palmergames/bukkit/towny/TownySettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,14 @@ public static boolean isForcingFire() {
return getBoolean(ConfigNodes.NWS_FORCE_FIRE_ON);
}

public static boolean isDeletingEntitiesOnUnclaim() {
return getBoolean(ConfigNodes.NWS_PLOT_MANAGEMENT_ENTITY_DELETE_ENABLE);
}

public static List<String> getUnclaimDeleteEntityTypes() {
return getStrArr(ConfigNodes.NWS_PLOT_MANAGEMENT_ENTITY_DELETE);
}

public static boolean isUsingPlotManagementDelete() {

return getBoolean(ConfigNodes.NWS_PLOT_MANAGEMENT_DELETE_ENABLE);
Expand Down
2 changes: 2 additions & 0 deletions src/com/palmergames/bukkit/towny/db/SQL_Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ private static List<String> getWorldColumns() {
columns.add("`unclaimedZoneIgnoreIds` mediumtext NOT NULL");
columns.add("`usingPlotManagementDelete` bool NOT NULL DEFAULT '0'");
columns.add("`plotManagementDeleteIds` mediumtext NOT NULL");
columns.add("`isDeletingEntitiesOnUnclaim` bool NOT NULL DEFAULT '0'");
columns.add("`unclaimDeleteEntityTypes` mediumtext NOT NULL");
columns.add("`usingPlotManagementMayorDelete` bool NOT NULL DEFAULT '0'");
columns.add("`plotManagementMayorDelete` mediumtext NOT NULL");
columns.add("`usingPlotManagementRevert` bool NOT NULL DEFAULT '0'");
Expand Down
2 changes: 2 additions & 0 deletions src/com/palmergames/bukkit/towny/db/TownyDatabaseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ public void removeTownBlock(TownBlock townBlock) {
universe.removeTownBlock(townBlock);
deleteTownBlock(townBlock);

if (townBlock.getWorld().isDeletingEntitiesOnUnclaim())
TownyRegenAPI.addDeleteTownBlockEntityQueue(townBlock.getWorldCoord());

if (townBlock.getWorld().isUsingPlotManagementDelete())
TownyRegenAPI.addDeleteTownBlockIdQueue(townBlock.getWorldCoord());
Expand Down
27 changes: 27 additions & 0 deletions src/com/palmergames/bukkit/towny/db/TownyFlatFileSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,25 @@ public boolean loadWorld(TownyWorld world) {
} catch (Exception ignored) {
}

line = keys.get("isDeletingEntitiesOnUnclaim");
if (line != null)
try {
world.setDeletingEntitiesOnUnclaim(Boolean.parseBoolean(line));
} catch (Exception ignored) {
}

line = keys.get("unclaimDeleteEntityTypes");
if (line != null)
try {
List<String> entityTypes = new ArrayList<>();
for (String s : line.split(","))
if (!s.isEmpty())
entityTypes.add(s);

world.setUnclaimDeleteEntityTypes(entityTypes);
} catch (Exception ignored) {
}

line = keys.get("usingPlotManagementDelete");
if (line != null)
try {
Expand Down Expand Up @@ -2146,6 +2165,14 @@ public boolean saveWorld(TownyWorld world) {
if (world.getPlotManagementDeleteIds() != null)
list.add("plotManagementDeleteIds=" + StringMgmt.join(world.getPlotManagementDeleteIds(), ","));

// EntityType removal on unclaim.
list.add("");
list.add("# The following settings control what EntityTypes are deleted upon a townblock being unclaimed");
list.add("# Valid EntityTypes are listed here: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/EntityType.html");
list.add("isDeletingEntitiesOnUnclaim=" + world.isDeletingEntitiesOnUnclaim());
if (world.getUnclaimDeleteEntityTypes() != null)
list.add("unclaimDeleteEntityTypes=" + StringMgmt.join(world.getUnclaimDeleteEntityTypes(), ","));

// PlotManagement
list.add("");
list.add("# The following settings control what blocks are deleted upon a mayor issuing a '/plot clear' command");
Expand Down
24 changes: 24 additions & 0 deletions src/com/palmergames/bukkit/towny/db/TownySQLSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,25 @@ private boolean loadWorld(ResultSet rs) {
} catch (Exception ignored) {
}

result = rs.getBoolean("isDeletingEntitiesOnUnclaim");
try {
world.setDeletingEntitiesOnUnclaim(result);
} catch (Exception ignored) {
}

line = rs.getString("unclaimDeleteEntityTypes");
if (line != null)
try {
List<String> entityTypes = new ArrayList<>();
search = (line.contains("#")) ? "#" : ",";
for (String split : line.split(search))
if (!split.isEmpty())
entityTypes.add(split);

world.setUnclaimDeleteEntityTypes(entityTypes);
} catch (Exception ignored) {
}

result = rs.getBoolean("usingPlotManagementDelete");
try {
world.setUsingPlotManagementDelete(result);
Expand Down Expand Up @@ -2314,6 +2333,11 @@ public synchronized boolean saveWorld(TownyWorld world) {
if (world.getUnclaimedZoneIgnoreMaterials() != null)
nat_hm.put("unclaimedZoneIgnoreIds", StringMgmt.join(world.getUnclaimedZoneIgnoreMaterials(), "#"));

// Deleting EntityTypes from Townblocks on Unclaim.
nat_hm.put("isDeletingEntitiesOnUnclaim", world.isDeletingEntitiesOnUnclaim());
if (world.getUnclaimDeleteEntityTypes() != null)
nat_hm.put("unclaimDeleteEntityTypes", StringMgmt.join(world.getUnclaimDeleteEntityTypes(), "#"));

// Using PlotManagement Delete
nat_hm.put("usingPlotManagementDelete", world.isUsingPlotManagementDelete());
// Plot Management Delete Ids
Expand Down
22 changes: 22 additions & 0 deletions src/com/palmergames/bukkit/towny/object/TownyWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class TownyWorld extends TownyObject {

private HashMap<String, Town> towns = new HashMap<>();

private boolean isDeletingEntitiesOnUnclaim = TownySettings.isDeletingEntitiesOnUnclaim();
private EnumSet<EntityType> unclaimDeleteEntityTypes = null;

private boolean isUsingPlotManagementDelete = TownySettings.isUsingPlotManagementDelete();
private EnumSet<Material> plotManagementDeleteIds = null;

Expand Down Expand Up @@ -319,6 +322,25 @@ public boolean isUsingPlotManagementDelete() {
return isUsingPlotManagementDelete;
}

public void setDeletingEntitiesOnUnclaim(boolean using) {
isDeletingEntitiesOnUnclaim = using;
}

public boolean isDeletingEntitiesOnUnclaim() {
return isDeletingEntitiesOnUnclaim;
}

public EnumSet<EntityType> getUnclaimDeleteEntityTypes() {
if (unclaimDeleteEntityTypes == null)
setUnclaimDeleteEntityTypes(TownySettings.getUnclaimDeleteEntityTypes());

return unclaimDeleteEntityTypes;
}

public void setUnclaimDeleteEntityTypes(List<String> entityTypes) {
this.unclaimDeleteEntityTypes = TownySettings.toEntityTypeEnumSet(entityTypes);
}

public void setUsingPlotManagementMayorDelete(boolean using) {

isUsingPlotManagementMayorDelete = using;
Expand Down
58 changes: 58 additions & 0 deletions src/com/palmergames/bukkit/towny/regen/TownyRegenAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityExplodeEvent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Hashtable;
Expand All @@ -38,6 +40,9 @@ public class TownyRegenAPI {
// table containing snapshot data of active reversions.
private static Hashtable<String, PlotBlockData> PlotChunks = new Hashtable<>();

// List of all old plots still to be processed for Entity removal
private static List<WorldCoord> deleteTownBlockEntityQueue = new ArrayList<>();

// List of all old plots still to be processed for Block removal
private static List<WorldCoord> deleteTownBlockIdQueue = new ArrayList<>();

Expand Down Expand Up @@ -506,6 +511,59 @@ public static String getPlotKey(WorldCoord wc) {
//
// }

/*
* TownBlock Entity Deleting Queue.
*/

/**
* @return true if there are any chunks being processed.
*/
public static boolean hasDeleteTownBlockEntityQueue() {

return !deleteTownBlockEntityQueue.isEmpty();
}

public static boolean isDeleteTownBlockEntityQueue(WorldCoord plot) {

return deleteTownBlockEntityQueue.contains(plot);
}

public static void addDeleteTownBlockEntityQueue(WorldCoord plot) {
if (!deleteTownBlockEntityQueue.contains(plot))
deleteTownBlockEntityQueue.add(plot);
}

public static WorldCoord getDeleteTownBlockEntityQueue() {

if (!deleteTownBlockEntityQueue.isEmpty()) {
WorldCoord wc = deleteTownBlockEntityQueue.get(0);
deleteTownBlockEntityQueue.remove(0);
return wc;
}
return null;
}


/**
* Deletes all of a specified entity type from a TownBlock
*
* @param worldCoord - WorldCoord for the Town Block
*/
public static void doDeleteTownBlockEntities(WorldCoord worldCoord) {
TownyWorld world = worldCoord.getTownyWorld();
if (!world.isDeletingEntitiesOnUnclaim())
return;
List<Entity> toRemove = new ArrayList<>();
Collection<Entity> entities = worldCoord.getBukkitWorld().getNearbyEntities(worldCoord.getBoundingBox());
for (Entity entity : entities) {
if (world.getUnclaimDeleteEntityTypes().contains(entity.getType()))
toRemove.add(entity);
}

for (Entity entity : toRemove)
entity.remove();
}

/*
* TownBlock Material Deleting Queue.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public void run() {
makeNextPlotSnapshot();
}

// Perform the next plot_management entity_delete
if (TownyRegenAPI.hasDeleteTownBlockEntityQueue())
TownyRegenAPI.doDeleteTownBlockEntities(TownyRegenAPI.getDeleteTownBlockEntityQueue());

// Perform the next plot_management block_delete
if (TownyRegenAPI.hasDeleteTownBlockIdQueue()) {
TownyRegenAPI.doDeleteTownBlockIds(TownyRegenAPI.getDeleteTownBlockIdQueue());
Expand Down

0 comments on commit 9e5ce64

Please sign in to comment.