Skip to content

Commit

Permalink
Merge pull request #255 from Goosius1/nation_siege_immunity
Browse files Browse the repository at this point in the history
Nation Siege Immunity
  • Loading branch information
Goosius1 authored Apr 28, 2021
2 parents 1580960 + b1e019a commit 92315cd
Show file tree
Hide file tree
Showing 20 changed files with 372 additions and 239 deletions.
48 changes: 29 additions & 19 deletions src/main/java/com/gmail/goosius/siegewar/SiegeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.gmail.goosius.siegewar.metadata.SiegeMetaDataController;
import com.gmail.goosius.siegewar.objects.Siege;
import com.gmail.goosius.siegewar.utils.SiegeWarMoneyUtil;
import com.gmail.goosius.siegewar.utils.SiegeWarTimeUtil;
import com.gmail.goosius.siegewar.utils.SiegeWarImmunityUtil;
import com.gmail.goosius.siegewar.utils.SiegeWarTownUtil;
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.TownyUniverse;
Expand Down Expand Up @@ -91,6 +91,7 @@ public static void saveSiege(Siege siege) {
SiegeMetaDataController.setEndTime(town, siege.getScheduledEndTime());
SiegeMetaDataController.setActualEndTime(town, siege.getActualEndTime());
SiegeMetaDataController.setResidentTimedPointContributors(town, siege.getResidentTimedPointContributors());
SiegeMetaDataController.setPrimaryTownGovernments(town, siege.getPrimaryTownGovernments());
town.save();
}

Expand Down Expand Up @@ -228,6 +229,9 @@ public static boolean loadSiege(Siege siege) {
siege.setActualEndTime(SiegeMetaDataController.getActualEndTime(town));

siege.setResidentTimedPointContributors(SiegeMetaDataController.getResidentTimedPointContributors(town));

siege.setPrimaryTownGovernments(SiegeMetaDataController.getPrimaryTownGovernments(town));

return true;
}

Expand All @@ -236,7 +240,7 @@ public static void removeSiege(Siege siege, SiegeSide refundSideIfSiegeIsActive)
//If siege is active, initiate siege immunity for town, and return war chest
if(siege.getStatus().isActive()) {
siege.setActualEndTime(System.currentTimeMillis());
SiegeWarTimeUtil.activateSiegeImmunityTimers(siege.getTown(), siege);
SiegeWarImmunityUtil.grantSiegeImmunityAfterEndedSiege(siege.getTown(), siege);

//Return warchest only if siege is not revolt
if(siege.getSiegeType() != SiegeType.REVOLT) {
Expand All @@ -256,7 +260,7 @@ else if (refundSideIfSiegeIsActive == SiegeSide.DEFENDERS)
siegedTowns.remove(siege.getTown());
siegedTownNames.remove(siege.getTown().getName());

SiegeWarTownUtil.setTownPvpFlags(town, false);
SiegeWarTownUtil.setPvpFlag(town, false);
CosmeticUtil.removeFakeBeacons(siege);

//Save town
Expand Down Expand Up @@ -391,35 +395,31 @@ public static Map<Siege, Town> getActiveDefensiveSieges(Nation nation) {
}

/**
* This method returns true
* - The given town is in a nation, and
* - One (or more) of the nation's home towns is a siege defender
*
* @param town the town to check
* @return true if one (or more) of the nation's home towns is a siege defender
* @return true if the town has a nation & that nation is fighting a home-defence war
*
* Note: A home defence war is when one or more of the nation's natural towns (ie not occupied foreign towns) is under siege.
*/
public static boolean isAnyHomeTownASiegeDefender(Town town) {
public static boolean isTownsNationFightingAHomeDefenceWar(Town town) {
try {
if(town.hasNation()) {
return isAnyHomeTownASiegeDefender(town.getNation());
return isNationFightingAHomeDefenceWar(town.getNation());
}
} catch (NotRegisteredException ignored) {}
return false;
}

/**
* This method returns true
* - One (or more) of the nation's home towns is a siege defender
*
* @param nation the nation to check
* @return true if one (or more) of the nation's home towns is a siege defender
* @return true if the given nation is fighting a home-defence war
*
* Note: A home defence war is when one or more of the nation's natural towns (ie not occupied foreign towns) is under siege.
*/
public static boolean isAnyHomeTownASiegeDefender(Nation nation) {
public static boolean isNationFightingAHomeDefenceWar(Nation nation) {
for(Siege siege: SiegeController.getSieges()) {
try {
if(siege.getStatus().isActive()
&& siege.getDefender() instanceof Town
&& ((Town)siege.getDefender()).hasNation()
&& siege.getTown().hasNation()
&& siege.getTown().getNation() == nation) {
return true;
}
Expand Down Expand Up @@ -468,8 +468,8 @@ public static void startSiege(Block bannerBlock,
SiegeController.setSiege(targetTown, true);
SiegeController.putTownInSiegeMap(targetTown, siege);

//Set town to true, potentially set the town's nation's towns as well.
SiegeWarTownUtil.setTownPvpFlags(targetTown, true);
//Set pvp to true in the besieged town
SiegeWarTownUtil.setPvpFlag(targetTown, true);

//Send global message;
try {
Expand Down Expand Up @@ -579,4 +579,14 @@ private static void sendGlobalSiegeStartMessage(Siege siege) throws NotRegistere
break;
}
}

public static boolean doesNationHaveAnyHomeDefenceContributionsInActiveSieges(Nation nation) {
for(Siege siege: townSiegeMap.values()) {
if(siege.getStatus().isActive()
&& siege.getPrimaryTownGovernments().containsKey(nation.getUUID())) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,34 +288,16 @@ public void onTownTriesToLeaveNation(NationPreTownLeaveEvent event) {
event.setCancelled(false);
}

/**
* When a besieged town leaves a nation,
* pvp in the remaining towns is returned to the off state.
*/
@EventHandler
public void on(NationTownLeaveEvent event) {
if (SiegeWarSettings.getWarSiegeEnabled()
&& SiegeWarSettings.isAllNationSiegesEnabled()
&& SiegeController.hasActiveSiege(event.getTown())) {

for(Town nationTown: event.getNation().getTowns()) {
if(nationTown != event.getTown() && !nationTown.isNeutral())
SiegeWarTownUtil.setPvpFlag(nationTown, false);
}
}
}

/*
* If nation is under siege, it cannot add new towns
* If nation is fighting a home-defence war it cannot add new towns
*/
@EventHandler
public void on(NationPreAddTownEvent event) {
if (SiegeWarSettings.getWarSiegeEnabled()
&& SiegeWarSettings.isAllNationSiegesEnabled()
&& SiegeController.isAnyHomeTownASiegeDefender(event.getNation())) {
&& SiegeWarSettings.isNationSiegeImmunityEnabled()
&& SiegeController.isNationFightingAHomeDefenceWar(event.getNation())) {
event.setCancelled(true);
event.setCancelMessage(Translation.of("plugin_prefix") + Translation.of("msg_err_siege_affected_home_nation_cannot_recruit"));
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ public void onTownAddResident(TownPreAddResidentEvent event) {
return;
}

if (SiegeWarSettings.isAllNationSiegesEnabled()
&& SiegeController.isAnyHomeTownASiegeDefender(event.getTown())) {
//Cannot recruit if nation is fighting a home-defence war
if (SiegeWarSettings.isNationSiegeImmunityEnabled()
&& SiegeController.isTownsNationFightingAHomeDefenceWar(event.getTown())) {
event.setCancelled(true);
event.setCancelMessage(Translation.of("plugin_prefix") + Translation.of("msg_err_siege_affected_home_nation_town_cannot_recruit"));
return;
Expand Down Expand Up @@ -130,14 +131,6 @@ public void onTownTogglePVP(TownTogglePVPEvent event) {
event.setCancelled(true);
return;
}

//Is the town affected by an all-nation-siege
if(SiegeWarSettings.isAllNationSiegesEnabled()
&& SiegeController.isAnyHomeTownASiegeDefender(event.getTown())) {
event.setCancellationMsg(Translation.of("plugin_prefix") + Translation.of("msg_err_siege_affected_home_nation_town_cannot_toggle_pvp"));
event.setCancelled(true);
return;
}
}
}
}
Expand Down Expand Up @@ -224,9 +217,9 @@ public void onTownClaim(TownPreClaimEvent event) {
return;
}

//If the town is affected by an all-nation-siege, they cannot claim any land
if (SiegeWarSettings.isAllNationSiegesEnabled()
&& SiegeController.isAnyHomeTownASiegeDefender(event.getTown())) {
//If the town is fighting a home-defence war, they cannot claim any land
if (SiegeWarSettings.isNationSiegeImmunityEnabled()
&& SiegeController.isTownsNationFightingAHomeDefenceWar(event.getTown())) {
event.setCancelled(true);
event.setCancelMessage(Translation.of("plugin_prefix") + Translation.of("msg_err_siege_affected_home_nation_town_cannot_claim"));
return;
Expand Down Expand Up @@ -278,9 +271,9 @@ public void onTownUnclaim(TownPreUnclaimCmdEvent event) {
return;
}

//Town affected by all-nation-siege
if (SiegeWarSettings.isAllNationSiegesEnabled()
&& SiegeController.isAnyHomeTownASiegeDefender(event.getTown())) {
//Town fighting a home-defence war
if (SiegeWarSettings.isNationSiegeImmunityEnabled()
&& SiegeController.isTownsNationFightingAHomeDefenceWar(event.getTown())) {
event.setCancelled(true);
event.setCancelMessage(Translation.of("plugin_prefix") + Translation.of("msg_err_siege_affected_home_nation_town_cannot_unclaim"));
return;
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/com/gmail/goosius/siegewar/metadata/MetaDataUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gmail.goosius.siegewar.metadata;

import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyObject;
import com.palmergames.bukkit.towny.object.metadata.BooleanDataField;
import com.palmergames.bukkit.towny.object.metadata.CustomDataField;
import com.palmergames.bukkit.towny.object.metadata.DecimalDataField;
Expand Down Expand Up @@ -30,15 +31,15 @@ static boolean getBoolean(Town town, BooleanDataField bdf) {
return false;
}

static long getLong(Town town, LongDataField ldf) {
CustomDataField<?> cdf = town.getMetadata(ldf.getKey());
static long getLong(TownyObject townyObject, LongDataField ldf) {
CustomDataField<?> cdf = townyObject.getMetadata(ldf.getKey());
if (cdf instanceof LongDataField)
return ((LongDataField) cdf).getValue();
return 0l;
}

static int getInt(Town town, IntegerDataField idf) {
CustomDataField<?> cdf = town.getMetadata(idf.getKey());
static int getInt(TownyObject townyObject, IntegerDataField idf) {
CustomDataField<?> cdf = townyObject.getMetadata(idf.getKey());
if (cdf instanceof IntegerDataField)
return ((IntegerDataField) cdf).getValue();
return 0;
Expand Down Expand Up @@ -69,21 +70,21 @@ static void setBoolean(Town town, BooleanDataField bdf, boolean bool) {
}
}

static void setLong(Town town, LongDataField ldf, long num) {
CustomDataField<?> cdf = town.getMetadata(ldf.getKey());
static void setLong(TownyObject townyObject, LongDataField ldf, long num) {
CustomDataField<?> cdf = townyObject.getMetadata(ldf.getKey());
if (cdf instanceof LongDataField) {
LongDataField value = (LongDataField) cdf;
value.setValue(num);
town.save();
townyObject.save();
}
}
static void setInt(Town town, IntegerDataField idf, int num) {
CustomDataField<?> cdf = town.getMetadata(idf.getKey());

static void setInt(TownyObject townyObject, IntegerDataField idf, int num) {
CustomDataField<?> cdf = townyObject.getMetadata(idf.getKey());
if (cdf instanceof IntegerDataField) {
IntegerDataField value = (IntegerDataField) cdf;
value.setValue(num);
town.save();
townyObject.save();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.metadata.CustomDataField;
import com.palmergames.bukkit.towny.object.metadata.IntegerDataField;
import com.palmergames.bukkit.towny.object.metadata.LongDataField;

public class NationMetaDataController {
@SuppressWarnings("unused")
Expand All @@ -15,6 +16,8 @@ public class NationMetaDataController {
townsGained = "siegewar_totaltownsgained",
townsLost = "siegewar_totaltownslost";

private static final LongDataField pendingSiegeImmunityMillis = new LongDataField("siegewar_pendingSiegeImmunityMillis");

public NationMetaDataController(SiegeWar plugin) {
this.plugin = plugin;
}
Expand Down Expand Up @@ -75,4 +78,26 @@ public static void setTotalTownsGained(Nation nation, int num) {
public static void setTotalTownsLost(Nation nation, int num) {
setIdf(nation, townsLost, num);
}

public static long getPendingSiegeImmunityMillis(Nation nation) {
LongDataField ldf = (LongDataField) pendingSiegeImmunityMillis.clone();
if (nation.hasMeta(ldf.getKey()))
return MetaDataUtil.getLong(nation, ldf);
return 0L;
}

public static void setPendingSiegeImmunityMillis(Nation nation, long num) {
LongDataField ldf = (LongDataField) pendingSiegeImmunityMillis.clone();
if (nation.hasMeta(ldf.getKey()))
MetaDataUtil.setLong(nation, ldf, num);
else
nation.addMetaData(new LongDataField(pendingSiegeImmunityMillis.getKey(), num));
}

public static void removePendingSiegeImmunityMillis(Nation nation) {
LongDataField ldf = (LongDataField) pendingSiegeImmunityMillis.clone();
if (nation.hasMeta(ldf.getKey()))
nation.removeMetaData(ldf);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
*
Expand Down Expand Up @@ -49,7 +50,8 @@ public class SiegeMetaDataController {
private static LongDataField endTime = new LongDataField("siegewar_endTime", 0l);
private static LongDataField actualEndTime = new LongDataField("siegewar_actualEndTime", 0l);
private static StringDataField attackerSiegeContributors = new StringDataField("siegewar_attackerSiegeContributors", "");

private static StringDataField primaryTownGovernments = new StringDataField("siegewar_primaryTownGovernments", "");

public SiegeMetaDataController(SiegeWar plugin) {
this.plugin = plugin;
}
Expand Down Expand Up @@ -360,6 +362,27 @@ public static Map<String, Integer> getResidentTimedPointContributors(Town town)
}
}

public static Map<UUID, Integer> getPrimaryTownGovernments(Town town) {
StringDataField sdf = (StringDataField) primaryTownGovernments.clone();

String dataAsString = null;
if (town.hasMeta(sdf.getKey()))
dataAsString = MetaDataUtil.getString(town, sdf);

if(dataAsString == null || dataAsString.length() == 0) {
return new HashMap<>();
} else {
Map<UUID, Integer> governmentsMap = new HashMap<>();
String[] contributionDataEntries = dataAsString.split(",");
String[] contributionDataPair;
for(String contributionDataEntry: contributionDataEntries) {
contributionDataPair = contributionDataEntry.split(":");
governmentsMap.put(UUID.fromString(contributionDataPair[0]), Integer.parseInt(contributionDataPair[1]));
}
return governmentsMap;
}
}

public static void setResidentTimedPointContributors(Town town, Map<String,Integer> contributorsMap) {
StringBuilder mapAsStringBuilder = new StringBuilder();
boolean firstEntry = true;
Expand All @@ -378,4 +401,23 @@ public static void setResidentTimedPointContributors(Town town, Map<String,Integ
else
town.addMetaData(new StringDataField("siegewar_attackerSiegeContributors", mapAsStringBuilder.toString()));
}

public static void setPrimaryTownGovernments(Town town, Map<UUID,Integer> governmentsMap) {
StringBuilder mapAsStringBuilder = new StringBuilder();
boolean firstEntry = true;
for(Map.Entry<UUID,Integer> governmentEntry: governmentsMap.entrySet()) {
if(firstEntry) {
firstEntry = false;
} else {
mapAsStringBuilder.append(",");
}
mapAsStringBuilder.append(governmentEntry.getKey()).append(":").append(governmentEntry.getValue());
}

StringDataField sdf = (StringDataField) primaryTownGovernments.clone();
if (town.hasMeta(sdf.getKey()))
MetaDataUtil.setString(town, sdf, mapAsStringBuilder.toString());
else
town.addMetaData(new StringDataField(primaryTownGovernments.getKey(), mapAsStringBuilder.toString()));
}
}
Loading

0 comments on commit 92315cd

Please sign in to comment.