Skip to content

Commit

Permalink
Merge pull request #47 from TownyAdvanced/per_chunk_upkeep
Browse files Browse the repository at this point in the history
Set town costs to be per-chunk
  • Loading branch information
Goosius1 committed Jun 29, 2023
2 parents fd40183 + 49962ac commit 43a7c72
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 54 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.townyadvanced</groupId>
<artifactId>TownyProvinces</artifactId>
<version>1.8.0</version>
<version>1.9.0</version>
<name>townyprovinces</name> <!-- Leave lower-cased -->

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.palmergames.bukkit.towny.utils.NameUtil;
import com.palmergames.bukkit.util.ChatTools;
import com.palmergames.util.StringMgmt;
import io.github.townyadvanced.townyprovinces.TownyProvinces;
import io.github.townyadvanced.townyprovinces.data.TownyProvincesDataHolder;
import io.github.townyadvanced.townyprovinces.jobs.dynmap_display.DynmapDisplayTaskController;
import io.github.townyadvanced.townyprovinces.jobs.land_validation.LandValidationJobStatus;
Expand All @@ -17,6 +18,7 @@
import io.github.townyadvanced.townyprovinces.settings.TownyProvincesPermissionNodes;
import io.github.townyadvanced.townyprovinces.settings.TownyProvincesSettings;
import io.github.townyadvanced.townyprovinces.util.FileUtil;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
Expand All @@ -32,7 +34,7 @@ public class TownyProvincesAdminCommand implements TabExecutor {

private static final List<String> adminTabCompletes = Arrays.asList("province","region", "landvalidationjob");
private static final List<String> adminTabCompletesProvince = Arrays.asList("sea","land");
private static final List<String> adminTabCompletesRegion = Arrays.asList("regenerate", "newtowncost", "upkeeptowncost");
private static final List<String> adminTabCompletesRegion = Arrays.asList("regenerate", "newtowncostperchunk", "upkeeptowncostperchunk");
private static final List<String> adminTabCompletesSeaProvincesJob = Arrays.asList("status", "start", "stop", "restart", "pause");

public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
Expand Down Expand Up @@ -116,8 +118,8 @@ private void showHelp(CommandSender sender) {
TownyMessaging.sendMessage(sender, ChatTools.formatCommand("Eg", "/tpra", "province [sea|land] [<x>,<z>]", ""));
TownyMessaging.sendMessage(sender, ChatTools.formatCommand("Eg", "/tpra", "province [sea|land] [<x>,<z>] [<x>,<z>]", ""));
TownyMessaging.sendMessage(sender, ChatTools.formatCommand("Eg", "/tpra", "region [regenerate] [<Region Name>]", ""));
TownyMessaging.sendMessage(sender, ChatTools.formatCommand("Eg", "/tpra", "region [newtowncost] [<Region Name>] [amount]", ""));
TownyMessaging.sendMessage(sender, ChatTools.formatCommand("Eg", "/tpra", "region [upkeeptowncost] [<Region Name>] [amount]", ""));
TownyMessaging.sendMessage(sender, ChatTools.formatCommand("Eg", "/tpra", "region [newtowncostperchunk] [<Region Name>] [amount]", ""));
TownyMessaging.sendMessage(sender, ChatTools.formatCommand("Eg", "/tpra", "region [upkeeptowncostperchunk] [<Region Name>] [amount]", ""));
TownyMessaging.sendMessage(sender, ChatTools.formatCommand("Eg", "/tpra", "landvalidationjob [status|start|stop|restart|pause]", ""));
}

Expand All @@ -142,10 +144,10 @@ private void parseRegionCommand(CommandSender sender, String[] args) {
}
if (args[0].equalsIgnoreCase("regenerate")) {
parseRegionRegenerateCommand(sender, args);
} else if (args[0].equalsIgnoreCase("newtowncost")) {
parseRegionSetNewTownCostCommand(sender, args);
} else if (args[0].equalsIgnoreCase("upkeeptowncost")) {
parseRegionSetTownUpkeepCostCommand(sender, args);
} else if (args[0].equalsIgnoreCase("newtowncostperchunk")) {
Bukkit.getScheduler().runTaskAsynchronously(TownyProvinces.getPlugin(), () ->parseRegionSetNewTownCostCommand(sender, args));
} else if (args[0].equalsIgnoreCase("upkeeptowncostperchunk")) {
Bukkit.getScheduler().runTaskAsynchronously(TownyProvinces.getPlugin(), () -> parseRegionSetTownUpkeepCostCommand(sender, args));
} else {
showHelp(sender);
}
Expand Down Expand Up @@ -308,29 +310,32 @@ private void parseRegionRegenerateCommand(CommandSender sender, String[] args) {
private void parseRegionSetNewTownCostCommand(CommandSender sender, String[] args) {
try {
String givenRegionName = args[1];
int newTownCost = Integer.parseInt(args[2]);
String formattedNewTownCost = TownyEconomyHandler.getFormattedBalance(newTownCost);
double townCostPerChunk = Double.parseDouble(args[2]);
String formattedTownCostPerChunk = TownyEconomyHandler.getFormattedBalance(townCostPerChunk);;
double townCost;
String caseCorrectRegionName = TownyProvincesSettings.getCaseSensitiveRegionName(givenRegionName);

if(givenRegionName.equalsIgnoreCase("all")) {
//Set cost for all provinces, regardless of region
for (Province province : TownyProvincesDataHolder.getInstance().getProvincesSet()) {
province.setNewTownCost(newTownCost);
townCost = townCostPerChunk * province.getListOfCoordsInProvince().size();
province.setNewTownCost(townCost);
province.saveData();
}
DynmapDisplayTaskController.requestHomeBlocksRefresh();
Messaging.sendMsg(sender, Translatable.of("msg_new_town_cost_set_for_all_regions", formattedNewTownCost));
Messaging.sendMsg(sender, Translatable.of("msg_new_town_cost_set_for_all_regions", formattedTownCostPerChunk));

} else if(TownyProvincesSettings.getRegionDefinitions().containsKey(caseCorrectRegionName)) {
//Set cost for just one region
for (Province province : TownyProvincesDataHolder.getInstance().getProvincesSet()) {
if(TownyProvincesSettings.isProvinceInRegion(province, caseCorrectRegionName)) {
province.setNewTownCost(newTownCost);
townCost = townCostPerChunk * province.getListOfCoordsInProvince().size();
province.setNewTownCost(townCost);
province.saveData();
}
}
DynmapDisplayTaskController.requestHomeBlocksRefresh();
Messaging.sendMsg(sender, Translatable.of("msg_new_town_cost_set_for_one_region", caseCorrectRegionName, formattedNewTownCost));
Messaging.sendMsg(sender, Translatable.of("msg_new_town_cost_set_for_one_region", caseCorrectRegionName, formattedTownCostPerChunk));

} else {
Messaging.sendMsg(sender, Translatable.of("msg_err_unknown_region_name"));
Expand All @@ -343,29 +348,32 @@ private void parseRegionSetNewTownCostCommand(CommandSender sender, String[] arg
private void parseRegionSetTownUpkeepCostCommand(CommandSender sender, String[] args) {
try {
String givenRegionName = args[1];
int townCost = Integer.parseInt(args[2]);
String formattedTownCost = TownyEconomyHandler.getFormattedBalance(townCost);
double townCostPerChunk = Double.parseDouble(args[2]);
String formattedTownCostPerChunk = TownyEconomyHandler.getFormattedBalance(townCostPerChunk);;
double townCost;
String caseCorrectRegionName = TownyProvincesSettings.getCaseSensitiveRegionName(givenRegionName);

if(givenRegionName.equalsIgnoreCase("all")) {
//Set cost for all provinces, regardless of region
for (Province province : TownyProvincesDataHolder.getInstance().getProvincesSet()) {
townCost = townCostPerChunk * province.getListOfCoordsInProvince().size();
province.setUpkeepTownCost(townCost);
province.saveData();
}
DynmapDisplayTaskController.requestHomeBlocksRefresh();
Messaging.sendMsg(sender, Translatable.of("msg_upkeep_town_cost_set_for_all_regions", formattedTownCost));
Messaging.sendMsg(sender, Translatable.of("msg_upkeep_town_cost_set_for_all_regions", formattedTownCostPerChunk));

} else if(TownyProvincesSettings.getRegionDefinitions().containsKey(caseCorrectRegionName)) {
//Set cost for just one region
for (Province province : TownyProvincesDataHolder.getInstance().getProvincesSet()) {
if(TownyProvincesSettings.isProvinceInRegion(province, caseCorrectRegionName)) {
townCost = townCostPerChunk * province.getListOfCoordsInProvince().size();
province.setUpkeepTownCost(townCost);
province.saveData();
}
}
DynmapDisplayTaskController.requestHomeBlocksRefresh();
Messaging.sendMsg(sender, Translatable.of("msg_upkeep_town_cost_set_for_one_region", caseCorrectRegionName, formattedTownCost));
Messaging.sendMsg(sender, Translatable.of("msg_upkeep_town_cost_set_for_one_region", caseCorrectRegionName, formattedTownCostPerChunk));

} else {
Messaging.sendMsg(sender, Translatable.of("msg_err_unknown_region_name"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ public static void loadProvince(File regionDefinitionFile) {
TPCoord homeBlock = unpackCoord(fileEntries.get("home_block"));
boolean isSea = Boolean.parseBoolean(fileEntries.get("is_sea"));
boolean isLandValidationRequested = false;
int newTownCost = 0;
int upkeepTownCost = 0;
double newTownCost = 0;
double upkeepTownCost = 0;
if(fileEntries.containsKey("is_land_validation_requested")) {
isLandValidationRequested = Boolean.parseBoolean(fileEntries.get("is_land_validation_requested"));
}
if(fileEntries.containsKey("new_town_cost")) {
newTownCost = Integer.parseInt(fileEntries.get("new_town_cost"));
newTownCost = Double.parseDouble(fileEntries.get("new_town_cost_per_chunk"));
}
if(fileEntries.containsKey("upkeep_town_cost")) {
upkeepTownCost = Integer.parseInt(fileEntries.get("upkeep_town_cost"));
upkeepTownCost = Double.parseDouble(fileEntries.get("upkeep_town_cost_per_chunk"));
}
//Create province
Province province = new Province(homeBlock, isSea, isLandValidationRequested, newTownCost, upkeepTownCost);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class PaintRegionAction {
private final Location bottomRightRegionCorner;
private final int averageProvinceSize;
private final int maxBrushMoves;
private final int newTownCost;
private final int upkeepTownCost;
private final double newTownCostPerChunk;
private final double upkeepTownCostPerChunk;

public PaintRegionAction(String regionName, Map<TPCoord,TPCoord> unclaimedCoordsMap) {
this.regionName = regionName;
Expand All @@ -70,8 +70,8 @@ public PaintRegionAction(String regionName, Map<TPCoord,TPCoord> unclaimedCoords
this.bottomRightRegionCorner = TownyProvincesSettings.getBottomRightCornerLocation(regionName);
this.averageProvinceSize = TownyProvincesSettings.getAverageProvinceSize(regionName);
this.maxBrushMoves = TownyProvincesSettings.getMaxBrushMoves(regionName);
this.newTownCost = TownyProvincesSettings.getNewTownCost(regionName);
this.upkeepTownCost = TownyProvincesSettings.getUpkeepTownCost(regionName);
this.newTownCostPerChunk = TownyProvincesSettings.getNewTownCostPerChunk(regionName);
this.upkeepTownCostPerChunk = TownyProvincesSettings.getUpkeepTownCostPerChunk(regionName);
}

boolean executeAction(boolean deleteExistingProvincesInRegion) {
Expand Down Expand Up @@ -208,7 +208,7 @@ private double calculateRegionAreaSquareMetres() {
int zCoord = coord.getZ();
TPCoord homeBlockCoord = new TPFinalCoord(xCoord, zCoord);
//Create province object
Province province = new Province(homeBlockCoord, isSea, landValidationRequested, newTownCost, upkeepTownCost);
Province province = new Province(homeBlockCoord, isSea, landValidationRequested, 0, 0);
//Validate province homeblock position
if(validatePositionOfProvinceHomeBlock(province)) {
return province;
Expand Down Expand Up @@ -481,6 +481,5 @@ private boolean deleteEmptyProvinces() {
TownyProvinces.info("Empty Provinces Deleted.");
return true;
}



}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.github.townyadvanced.townyprovinces.jobs.province_generation;

import com.palmergames.bukkit.towny.TownySettings;
import com.palmergames.bukkit.towny.object.Translatable;
import io.github.townyadvanced.townyprovinces.TownyProvinces;
import io.github.townyadvanced.townyprovinces.data.DataHandlerUtil;
import io.github.townyadvanced.townyprovinces.data.TownyProvincesDataHolder;
import io.github.townyadvanced.townyprovinces.jobs.dynmap_display.DynmapDisplayTaskController;
import io.github.townyadvanced.townyprovinces.objects.Province;
import io.github.townyadvanced.townyprovinces.objects.TPCoord;
import io.github.townyadvanced.townyprovinces.objects.TPFinalCoord;
import io.github.townyadvanced.townyprovinces.objects.TPFreeCoord;
Expand Down Expand Up @@ -82,6 +84,8 @@ public void executeRegionRegenerationJob() {
TownyProvinces.info("Problem Painting Regions");
return;
}
//Recalculated all prices
recalculateProvincePrices();
//Save data and request full dynmap refresh
DataHandlerUtil.saveAllData();
DynmapDisplayTaskController.requestFullMapRefresh();
Expand All @@ -93,6 +97,22 @@ public void executeRegionRegenerationJob() {
}
TownyProvinces.info("Region regeneration Job Complete"); //TODO - maybe global message?
}


private void recalculateProvincePrices() {
TownyProvinces.info("Recalculating province prices");
for (String regionName: TownyProvincesSettings.getOrderedRegionNames()) {
double newTownCostPerChunk = TownyProvincesSettings.getNewTownCostPerChunk(regionName);
double upkeepTownCostPerChunk = TownyProvincesSettings.getUpkeepTownCostPerChunk(regionName);
for(Province province: TownyProvincesDataHolder.getInstance().getProvincesSet()) {
if (TownyProvincesSettings.isProvinceInRegion(province, regionName)) {
province.setNewTownCost(newTownCostPerChunk * province.getListOfCoordsInProvince().size());
province.setUpkeepTownCost(upkeepTownCostPerChunk * province.getListOfCoordsInProvince().size());
}
}
}
TownyProvinces.info("Province Prices recalculated");
}

public boolean paintAllRegions() {
//Paint all Regions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.palmergames.bukkit.towny.TownySettings;
import com.palmergames.bukkit.towny.event.PlotChangeTypeEvent;
import com.palmergames.bukkit.towny.event.PlotPreChangeTypeEvent;
import com.palmergames.bukkit.towny.event.PreDeleteTownEvent;
import com.palmergames.bukkit.towny.event.PreNewTownEvent;
import com.palmergames.bukkit.towny.event.TownBlockTypeRegisterEvent;
import com.palmergames.bukkit.towny.event.TownPreClaimEvent;
Expand Down Expand Up @@ -168,7 +167,7 @@ public void on(TownUpkeepCalculationEvent event) {
}

@EventHandler(ignoreCancelled = true)
public void onTownClaimAttempt(TownPreClaimEvent event) {
public void onTownPreClaimAttempt(TownPreClaimEvent event) {
if (!TownyProvincesSettings.isTownyProvincesEnabled()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
public class Province {

private final TPCoord homeBlock;
private int newTownCost;
private int upkeepTownCost;
private double newTownCost;
private double upkeepTownCost;
private boolean isSea;
private final String id; //convenience variable. In memory only. Used for dynmap and file operations
private boolean landValidationRequested;
Expand All @@ -21,7 +21,7 @@ public boolean equals(Object object) {
return homeBlock.equals(((Province)object).getHomeBlock());
}

public Province(TPCoord homeBlock, boolean isSea, boolean landValidationRequested, int newTownCost, int upkeepTownCost) {
public Province(TPCoord homeBlock, boolean isSea, boolean landValidationRequested, double newTownCost, double upkeepTownCost) {
this.homeBlock = homeBlock;
this.isSea = isSea;
this.newTownCost = newTownCost;
Expand All @@ -38,19 +38,19 @@ public TPCoord getHomeBlock() {
return homeBlock;
}

public void setNewTownCost(int i) {
this.newTownCost = i;
public void setNewTownCost(double d) {
this.newTownCost = d;
}

public void setUpkeepTownCost(int i) {
this.upkeepTownCost = i;
public void setUpkeepTownCost(double d) {
this.upkeepTownCost = d;
}

public int getNewTownCost() {
public double getNewTownCost() {
return newTownCost;
}

public int getUpkeepTownCost() {
public double getUpkeepTownCost() {
return upkeepTownCost;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,16 @@ public static int calculateProvinceSquareRadius(String regionName) {
return (int)((Math.sqrt(averageProvinceSize)) / 2);
}

public static int getNewTownCost(String regionName) {
public static double getNewTownCostPerChunk(String regionName) {
Map<String,String> regionDefinitions = TownyProvincesSettings.getRegionDefinitions(regionName);
String intString = regionDefinitions.get("new_town_cost");
return Integer.parseInt(intString);
String intString = regionDefinitions.get("new_town_cost_per_chunk");
return Double.parseDouble(intString);
}

public static int getUpkeepTownCost(String regionName) {
public static double getUpkeepTownCostPerChunk(String regionName) {
Map<String,String> regionDefinitions = TownyProvincesSettings.getRegionDefinitions(regionName);
String intString = regionDefinitions.get("upkeep_town_cost");
return Integer.parseInt(intString);
String intString = regionDefinitions.get("upkeep_town_cost_per_chunk");
return Double.parseDouble(intString);
}

public static int getLandProvinceBorderWeight() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public static boolean createRegionDefinitionsFolderAndSampleFiles() {
fileEntries.add("brush_max_move_as_percentage_of_brush_square_radius: 50");
fileEntries.add("brush_min_move_as_percentage_of_brush_max_move: 50");
fileEntries.add("max_brush_moves: 100");
fileEntries.add("new_town_cost: 250");
fileEntries.add("upkeep_town_cost: 10");
fileEntries.add("new_town_cost_per_chunk: 0.8");
fileEntries.add("upkeep_town_cost_per_chunk: 0.08");
String folderPath = TownyProvinces.getPlugin().getDataFolder().toPath().resolve(FileUtil.REGION_DEFINITIONS_FOLDER_PATH).toString();
String filePath = folderPath + "/" + fileName;
saveListIntoFile(fileEntries, filePath);
Expand All @@ -110,8 +110,8 @@ public static boolean createRegionDefinitionsFolderAndSampleFiles() {
fileEntries.add("brush_max_move_as_percentage_of_brush_square_radius: 50");
fileEntries.add("brush_min_move_as_percentage_of_brush_max_move: 50");
fileEntries.add("max_brush_moves: 100");
fileEntries.add("new_town_cost: 300");
fileEntries.add("upkeep_town_cost: 12");
fileEntries.add("new_town_cost_per_chunk: 7.2");
fileEntries.add("upkeep_town_cost_per_chunk: 0.72");
folderPath = TownyProvinces.getPlugin().getDataFolder().toPath().resolve(FileUtil.REGION_DEFINITIONS_FOLDER_PATH).toString();
filePath = folderPath + "/" + fileName;
saveListIntoFile(fileEntries, filePath);
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/lang/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ msg_successfully_regenerated_one_regions: "&bSuccessfully regenerated region: %s
# Set town costs

msg_err_value_must_be_and_integer: "&cThe given value must be an integer."
msg_new_town_cost_set_for_all_regions: "&bNew-Town-Cost set for all regions, to: %s"
msg_new_town_cost_set_for_one_region: "&bNew-Town-Cost set for region %s, to: %s"
msg_upkeep_town_cost_set_for_all_regions: "&bUpkeep-Town-Cost set for all regions, to: %s"
msg_upkeep_town_cost_set_for_one_region: "&bUpkeep-Town-Cost set for region %s, to: %s"
msg_new_town_cost_set_for_all_regions: "&bNew-Town-Cost-Per-Chunk set for all regions, to: %s"
msg_new_town_cost_set_for_one_region: "&bNew-Town-Cost-Per-Chunk set for region %s, to: %s"
msg_upkeep_town_cost_set_for_all_regions: "&bUpkeep-Town-Cost-Per-Chunk set for all regions, to: %s"
msg_upkeep_town_cost_set_for_one_region: "&bUpkeep-Town-Cost-Per-Chunk set for region %s, to: %s"

# Land validation job

Expand Down

0 comments on commit 43a7c72

Please sign in to comment.