Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to make town and nation ranks require a town or nation level. #7698

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2030,8 +2030,8 @@ public boolean saveResident(Resident resident) {

if (resident.hasTown()) {
list.add("town=" + resident.getTownOrNull().getName());
list.add("town-ranks=" + StringMgmt.join(resident.getTownRanks(), ","));
list.add("nation-ranks=" + StringMgmt.join(resident.getNationRanks(), ","));
list.add("town-ranks=" + StringMgmt.join(resident.getTownRanksForSaving(), ","));
list.add("nation-ranks=" + StringMgmt.join(resident.getNationRanksForSaving(), ","));
}

// Friends
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2312,8 +2312,8 @@ public synchronized boolean saveResident(Resident resident) {
if (!TownySettings.getDefaultResidentAbout().equals(resident.getAbout()))
res_hm.put("about", resident.getAbout());
res_hm.put("town", resident.hasTown() ? resident.getTown().getName() : "");
res_hm.put("town-ranks", resident.hasTown() ? StringMgmt.join(resident.getTownRanks(), "#") : "");
res_hm.put("nation-ranks", resident.hasTown() ? StringMgmt.join(resident.getNationRanks(), "#") : "");
res_hm.put("town-ranks", resident.hasTown() ? StringMgmt.join(resident.getTownRanksForSaving(), "#") : "");
res_hm.put("nation-ranks", resident.hasTown() ? StringMgmt.join(resident.getNationRanksForSaving(), "#") : "");
res_hm.put("friends", StringMgmt.join(resident.getFriends(), "#"));
res_hm.put("protectionStatus", resident.getPermissions().toString().replaceAll(",", "#"));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.palmergames.bukkit.towny.event.nation;

import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

import com.palmergames.bukkit.towny.object.Nation;

public class NationLevelDecreaseEvent extends Event {

private static final HandlerList handlers = new HandlerList();
private final Nation nation;

public NationLevelDecreaseEvent(Nation nation) {
super(!Bukkit.getServer().isPrimaryThread());
this.nation = nation;
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

/**
* The nation which has had its Nation_Level decrease.
*
* @return nation which has had its Nation_Level decrease.
*/
public Nation getNation() {
return nation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.palmergames.bukkit.towny.event.nation;

import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

import com.palmergames.bukkit.towny.object.Nation;

public class NationLevelIncreaseEvent extends Event {

private static final HandlerList handlers = new HandlerList();
private final Nation nation;

public NationLevelIncreaseEvent(Nation nation) {
super(!Bukkit.getServer().isPrimaryThread());
this.nation = nation;
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

/**
* The nation which has had its Nation_Level increase.
*
* @return nation which has had its Nation_Level increase.
*/
public Nation getNation() {
return nation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.palmergames.bukkit.towny.event.town;

import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

import com.palmergames.bukkit.towny.object.Town;

public class TownLevelDecreaseEvent extends Event {

private static final HandlerList handlers = new HandlerList();
private final Town town;

public TownLevelDecreaseEvent(Town town) {
super(!Bukkit.getServer().isPrimaryThread());
this.town = town;
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

/**
* The town which has had its Town_Level decrease.
*
* @return town which has had its Town_Level decrease.
*/
public Town getTown() {
return town;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.palmergames.bukkit.towny.event.town;

import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

import com.palmergames.bukkit.towny.object.Town;

public class TownLevelIncreaseEvent extends Event {

private static final HandlerList handlers = new HandlerList();
private final Town town;

public TownLevelIncreaseEvent(Town town) {
super(!Bukkit.getServer().isPrimaryThread());
this.town = town;
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

/**
* The town which has had its Town_Level increase.
*
* @return town which has had its Town_Level increase.
*/
public Town getTown() {
return town;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
import com.palmergames.bukkit.towny.event.TownPreAddResidentEvent;
import com.palmergames.bukkit.towny.event.TownRemoveResidentEvent;
import com.palmergames.bukkit.towny.event.damage.TownyPlayerDamagePlayerEvent;
import com.palmergames.bukkit.towny.event.nation.NationLevelDecreaseEvent;
import com.palmergames.bukkit.towny.event.nation.NationLevelIncreaseEvent;
import com.palmergames.bukkit.towny.event.nation.NationPreTownLeaveEvent;
import com.palmergames.bukkit.towny.event.town.TownLevelDecreaseEvent;
import com.palmergames.bukkit.towny.event.town.TownLevelIncreaseEvent;
import com.palmergames.bukkit.towny.event.town.TownPreUnclaimCmdEvent;
import com.palmergames.bukkit.towny.event.town.TownPreUnclaimEvent;
import com.palmergames.bukkit.towny.exceptions.TownyException;
import com.palmergames.bukkit.towny.object.CellSurface;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.PlayerCache;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.SpawnType;
Expand All @@ -35,11 +40,13 @@
import com.palmergames.bukkit.towny.object.Translatable;
import com.palmergames.bukkit.towny.object.Translation;
import com.palmergames.bukkit.towny.object.WorldCoord;
import com.palmergames.bukkit.towny.permissions.TownyPerms;
import com.palmergames.bukkit.towny.utils.BorderUtil;
import com.palmergames.bukkit.towny.utils.ChunkNotificationUtil;
import com.palmergames.bukkit.towny.utils.PlayerCacheUtil;
import com.palmergames.bukkit.towny.utils.ProximityUtil;
import com.palmergames.bukkit.towny.utils.SpawnUtil;
import com.palmergames.bukkit.util.BukkitTools;
import com.palmergames.bukkit.util.Colors;
import com.palmergames.bukkit.util.DrawSmokeTaskFactory;
import com.palmergames.util.TimeMgmt;
Expand Down Expand Up @@ -258,14 +265,27 @@ public void onTownClaim(TownClaimEvent event) {
* Used to warn towns when they've lost a resident, so they know they're at risk
* of having claims stolen in the takeoverclaim feature.
*
* Used for town_level and nation_level decrease events.
*
* @param event TownRemoveResidentEvent.
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onTownLosesResident(TownRemoveResidentEvent event) {
Town town = event.getTown();
if (town.getLevelNumber() < TownySettings.getTownLevelFromGivenInt(town.getNumResidents() + 1, town)) {
BukkitTools.fireEvent(new TownLevelDecreaseEvent(town));
}
if (town.hasNation()) {
Nation nation = town.getNationOrNull();
if (nation.getLevelNumber() < TownySettings.getNationLevelFromGivenInt(nation.getNumResidents() + 1)) {
BukkitTools.fireEvent(new NationLevelDecreaseEvent(nation));
}
}

if (!TownySettings.isOverClaimingAllowingStolenLand())
return;
if (event.getTown().isOverClaimed())
TownyMessaging.sendPrefixedTownMessage(event.getTown(), Translatable.literal(Colors.Red).append(Translatable.of("msg_warning_your_town_is_overclaimed")));
if (town.isOverClaimed())
TownyMessaging.sendPrefixedTownMessage(town, Translatable.literal(Colors.Red).append(Translatable.of("msg_warning_your_town_is_overclaimed")));
}

/**
Expand Down Expand Up @@ -312,10 +332,21 @@ public void onResidentPreJoinTown(TownPreAddResidentEvent event) {

@EventHandler(ignoreCancelled = true)
public void onResidentJoinTown(TownAddResidentEvent event) {
Town town = event.getTown();

if (town.getLevelNumber() > TownySettings.getTownLevelFromGivenInt(town.getNumResidents() - 1, town)) {
BukkitTools.fireEvent(new TownLevelIncreaseEvent(town));
}
if (town.hasNation()) {
Nation nation = town.getNationOrNull();
if (nation.getLevelNumber() > TownySettings.getNationLevelFromGivenInt(nation.getNumResidents() - 1)) {
BukkitTools.fireEvent(new NationLevelIncreaseEvent(nation));
}
}

if (!TownySettings.isPromptingNewResidentsToTownSpawn() || !TownySettings.getBoolean(ConfigNodes.SPAWNING_ALLOW_TOWN_SPAWN))
return;

Town town = event.getTown();
Player player = event.getResident().getPlayer();
Town playerLocationTown = Optional.ofNullable(player).map(p -> TownyAPI.getInstance().getTown(p.getLocation())).orElse(null);

Expand Down Expand Up @@ -362,4 +393,48 @@ private void attemptPlayerCacheReset(Player player, WorldCoord worldCoord) {
return;
Towny.getPlugin().resetCache(player);
}

/*
* Watch for town and nation level increasing/decreasing and reassign permissions in case the players have level-requirement permissions.
*/

@EventHandler
public void onTownLevelIncrease(TownLevelIncreaseEvent event) {
if (!TownyPerms.ranksWithTownLevelRequirementPresent())
return;
event.getTown().getResidents()
.stream()
.filter(Resident::isOnline)
.forEach(r -> TownyPerms.assignPermissions(r, r.getPlayer()));
}

@EventHandler
public void onTownLevelDecrease(TownLevelDecreaseEvent event) {
if (!TownyPerms.ranksWithTownLevelRequirementPresent())
return;
event.getTown().getResidents()
.stream()
.filter(Resident::isOnline)
.forEach(r -> TownyPerms.assignPermissions(r, r.getPlayer()));
}

@EventHandler
public void onNationLevelIncrease(NationLevelIncreaseEvent event) {
if (!TownyPerms.ranksWithNationLevelRequirementPresent())
return;
event.getNation().getResidents()
.stream()
.filter(Resident::isOnline)
.forEach(r -> TownyPerms.assignPermissions(r, r.getPlayer()));
}

@EventHandler
public void onNationLevelDecrease(NationLevelDecreaseEvent event) {
if (!TownyPerms.ranksWithNationLevelRequirementPresent())
return;
event.getNation().getResidents()
.stream()
.filter(Resident::isOnline)
.forEach(r -> TownyPerms.assignPermissions(r, r.getPlayer()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -593,9 +593,26 @@ public boolean hasTownRank(String rank) {
}

public List<String> getTownRanks() {
if (!townRanks.isEmpty() && hasTown()) {
ArrayList<String> out = new ArrayList<>();
for (String rank : new ArrayList<>(townRanks)) {
int requiredTownLevelForRank = TownyPerms.getRankTownLevelReq(rank);
if (requiredTownLevelForRank == 0 || getTownOrNull().getLevelNumber() >= requiredTownLevelForRank)
out.add(rank);
}
// Return out modified list of ranks, so that the player will not lose ranks
// they've been assigned when their town goes down in a level temporarily. This
// ensures they save and load their proper list of ranks.
return Collections.unmodifiableList(out);
}
return Collections.unmodifiableList(townRanks);
}


@ApiStatus.Internal
public List<String> getTownRanksForSaving() {
return Collections.unmodifiableList(townRanks);
}

public boolean removeTownRank(String rank) {

if (hasTownRank(rank)) {
Expand Down Expand Up @@ -646,6 +663,23 @@ public boolean hasNationRank(String rank) {
}

public List<String> getNationRanks() {
if (!nationRanks.isEmpty() && hasNation()) {
ArrayList<String> out = new ArrayList<>();
for (String rank : new ArrayList<>(nationRanks)) {
int requiredNationLevelForRank = TownyPerms.getRankTownLevelReq(rank);
if (requiredNationLevelForRank == 0 || getNationOrNull().getLevelNumber() >= requiredNationLevelForRank)
out.add(rank);
}
// Return out modified list of ranks, so that the player will not lose ranks
// they've been assigned when their nation goes down in a level temporarily. This
// ensures they save and load their proper list of ranks.
return Collections.unmodifiableList(out);
}
return Collections.unmodifiableList(nationRanks);
}

@ApiStatus.Internal
public List<String> getNationRanksForSaving() {
return Collections.unmodifiableList(nationRanks);
}

Expand Down
Loading
Loading