Skip to content

Commit

Permalink
- API: Added TownyObjectFormattedNameEvent
Browse files Browse the repository at this point in the history
    - Thrown when a Resident, Town or Nation is returning a
getFormattedName() value.
    - This is used to alter the prefix or postfix that flanks a
TownyObject name, usually a title, surname, townlevel/nationlevel
prefix/postfix.
    - Closes TownyAdvanced#6995.
  • Loading branch information
LlmDl authored and jwkerr committed Oct 9, 2023
1 parent 3734c16 commit 35dd0ef
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.palmergames.bukkit.towny.event;

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

import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyObject;

public class TownyObjectFormattedNameEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final TownyObject object;
private String prefix;
private String postfix;

public TownyObjectFormattedNameEvent(TownyObject object, String prefix, String postfix) {
super(!Bukkit.getServer().isPrimaryThread());
this.object = object;
this.prefix = prefix;
this.postfix = postfix;
}

public String getPrefix() {
return prefix;
}

public void setPrefix(String prefix) {
this.prefix = prefix;
}

public String getPostfix() {
return postfix;
}

public void setPostfix(String postfix) {
this.postfix = postfix;
}

public TownyObject getTownyObject() {
return object;
}

public boolean isResident() {
return object instanceof Resident;
}

public boolean isTown() {
return object instanceof Town;
}

public boolean isNation() {
return object instanceof Nation;
}

public static HandlerList getHandlerList() {
return handlers;
}

@Override
public HandlerList getHandlers() {
return handlers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.palmergames.bukkit.towny.TownyMessaging;
import com.palmergames.bukkit.towny.TownySettings;
import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.towny.event.TownyObjectFormattedNameEvent;
import com.palmergames.bukkit.towny.exceptions.EmptyNationException;
import com.palmergames.bukkit.towny.exceptions.TownyException;
import com.palmergames.bukkit.towny.invites.Invite;
Expand Down Expand Up @@ -547,7 +548,9 @@ public boolean hasKing() {

@Override
public String getFormattedName() {
return TownySettings.getNationPrefix(this) + this.getName().replace("_", " ") + TownySettings.getNationPostfix(this);
TownyObjectFormattedNameEvent event = new TownyObjectFormattedNameEvent(this, TownySettings.getNationPrefix(this), TownySettings.getNationPostfix(this));
BukkitTools.fireEvent(event);
return event.getPrefix() + getName().replace("_", " ") + event.getPostfix();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.palmergames.bukkit.towny.confirmations.Confirmation;
import com.palmergames.bukkit.towny.event.TownAddResidentEvent;
import com.palmergames.bukkit.towny.event.TownRemoveResidentEvent;
import com.palmergames.bukkit.towny.event.TownyObjectFormattedNameEvent;
import com.palmergames.bukkit.towny.event.resident.ResidentToggleModeEvent;
import com.palmergames.bukkit.towny.event.town.TownPreRemoveResidentEvent;
import com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException;
Expand Down Expand Up @@ -808,7 +809,11 @@ public String getFormattedName() {
String postfix = Colors.translateColorCodes(hasSurname() ? " " + getSurname() :
(isKing() && !TownySettings.getKingPostfix(this).isEmpty()) ? TownySettings.getKingPostfix(this) :
(isMayor() && !TownySettings.getMayorPostfix(this).isEmpty()) ? TownySettings.getMayorPostfix(this) : "");
return prefix + getName() + postfix;

TownyObjectFormattedNameEvent event = new TownyObjectFormattedNameEvent(this, prefix, postfix);
BukkitTools.fireEvent(event);

return event.getPrefix() + getName() + event.getPostfix();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.palmergames.bukkit.towny.event.NationRemoveTownEvent;
import com.palmergames.bukkit.towny.event.BonusBlockPurchaseCostCalculationEvent;
import com.palmergames.bukkit.towny.event.TownBlockClaimCostCalculationEvent;
import com.palmergames.bukkit.towny.event.TownyObjectFormattedNameEvent;
import com.palmergames.bukkit.towny.event.town.TownAddAlliedTownEvent;
import com.palmergames.bukkit.towny.event.town.TownAddEnemiedTownEvent;
import com.palmergames.bukkit.towny.event.town.TownConqueredEvent;
Expand Down Expand Up @@ -1424,7 +1425,11 @@ public String getBankAccountPrefix() {
public String getFormattedName() {
String prefix = (this.isCapital() && !TownySettings.getCapitalPrefix(this).isEmpty()) ? TownySettings.getCapitalPrefix(this) : TownySettings.getTownPrefix(this);
String postfix = (this.isCapital() && !TownySettings.getCapitalPostfix(this).isEmpty()) ? TownySettings.getCapitalPostfix(this) : TownySettings.getTownPostfix(this);
return prefix + this.getName().replace("_", " ") + postfix;

TownyObjectFormattedNameEvent event = new TownyObjectFormattedNameEvent(this, prefix, postfix);
BukkitTools.fireEvent(event);

return event.getPrefix() + getName().replace("_", " ") + event.getPostfix();
}

public String getPrefix() {
Expand Down
6 changes: 5 additions & 1 deletion Towny/src/main/resources/ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9080,4 +9080,8 @@ v0.92.0.11:
- Fix tourist-blacklisted commands not being usable in the wilderness.
- Fix seeing an errant permission string when running a /res toggle addon command.
0.99.5.19:
- Update PowerRanks warning to mention their version that fixes the incompatibility.
- Update PowerRanks warning to mention their version that fixes the incompatibility.
- API: Added TownyObjectFormattedNameEvent
- Thrown when a Resident, Town or Nation is returning a getFormattedName() value.
- This is used to alter the prefix or postfix that flanks a TownyObject name, usually a title, surname, townlevel/nationlevel prefix/postfix.
- Closes #6995.

0 comments on commit 35dd0ef

Please sign in to comment.