Skip to content

Commit

Permalink
Merge pull request #6608 from TownyAdvanced/api/mayoral_succession_event
Browse files Browse the repository at this point in the history
API: Add town-changing mayor events.
  • Loading branch information
LlmDl committed Apr 14, 2023
2 parents 484ad70 + 11d4f1a commit b6401c8
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/com/palmergames/bukkit/towny/command/TownCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2261,7 +2261,7 @@ public static void townSetMayor(CommandSender sender, String[] split, boolean ad
if (!town.hasResident(split[1]))
throw new TownyException(Translatable.of("msg_err_mayor_doesnt_belong_to_town"));

TownMayorChangeEvent townMayorChangeEvent = new TownMayorChangeEvent(oldMayor, newMayor);
TownMayorChangeEvent townMayorChangeEvent = new TownMayorChangeEvent(sender, oldMayor, newMayor);
if (BukkitTools.isEventCancelled(townMayorChangeEvent) && !admin)
throw new TownyException(townMayorChangeEvent.getCancelMessage());

Expand Down Expand Up @@ -2798,7 +2798,7 @@ public static Town newTown(TownyWorld world, String name, Resident resident, Coo
town.setRegistered(System.currentTimeMillis());
town.setMapColorHexCode(MapUtil.generateRandomTownColourAsHexCode());
resident.setTown(town);
town.setMayor(resident);
town.setMayor(resident, false);
town.setFounder(resident.getName());

// Set the plot permissions to mirror the towns.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.Translation;

import org.bukkit.command.CommandSender;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

/**
* Event that gets fired when a town's mayor changes.
* A Cancellable Event that gets fired when a town's mayor changes by a player
* using an in-game command.
*/
public class TownMayorChangeEvent extends CancellableTownyEvent {
private static final HandlerList HANDLER_LIST = new HandlerList();

private final CommandSender sender;
private final Resident oldMayor;
private final Resident newMayor;

public TownMayorChangeEvent(Resident oldMayor, Resident newMayor) {
public TownMayorChangeEvent(CommandSender sender, Resident oldMayor, Resident newMayor) {
this.sender = sender;
this.oldMayor = oldMayor;
this.newMayor = newMayor;
setCancelMessage(Translation.of("msg_err_command_disable"));
Expand Down Expand Up @@ -52,4 +57,11 @@ public static HandlerList getHandlerList() {
public HandlerList getHandlers() {
return HANDLER_LIST;
}

/**
* @return the CommandSender that ran the /t set mayor command.
*/
public CommandSender getCommandSender() {
return sender;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.palmergames.bukkit.towny.event.town;

import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

/**
* Event that gets fired when a town's mayor has changed.
*/
public class TownMayorChangedEvent extends Event {
private static final HandlerList HANDLER_LIST = new HandlerList();

private final Resident oldMayor;
private final Resident newMayor;

public TownMayorChangedEvent(Resident oldMayor, Resident newMayor) {
super(!Bukkit.getServer().isPrimaryThread());
this.oldMayor = oldMayor;
this.newMayor = newMayor;
}

public Resident getOldMayor() {
return oldMayor;
}

public Resident getNewMayor() {
return newMayor;
}

public Town getTown() {
return TownyAPI.getInstance().getResidentTownOrNull(newMayor);
}

public boolean isNationCapital() {
return getTown().isCapital();
}

public boolean isKingChange() {
return oldMayor.isKing();
}

public static HandlerList getHandlerList() {
return HANDLER_LIST;
}

@NotNull
@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.palmergames.bukkit.towny.event.town;

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

import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Event that gets fired when a town has had their mayor removed by Towny,
* resulting in Towny choosing a Town resident to become mayor.
*
* Towny has chosen the newMayor out of the potentialResidents, but you can
* override who will become the new mayor using this event.
*/
public class TownMayorChosenBySuccessionEvent extends Event {
private static final HandlerList HANDLER_LIST = new HandlerList();

private final Resident oldMayor;
private Resident newMayor;
private List<Resident> potentialResidents;

public TownMayorChosenBySuccessionEvent(Resident oldMayor, Resident newMayor, List<Resident> potentialResidents) {
super(!Bukkit.getServer().isPrimaryThread());
this.oldMayor = oldMayor;
this.newMayor = newMayor;
for (Resident resident : potentialResidents) {
if (resident != oldMayor)
this.potentialResidents.add(resident);
}

}

public Resident getOldMayor() {
return oldMayor;
}

public Resident getNewMayor() {
return newMayor;
}

public void setNewMayor(Resident replacementMayor) {
newMayor = replacementMayor;
}

public Town getTown() {
return TownyAPI.getInstance().getResidentTownOrNull(newMayor);
}

@Nullable
public List<Resident> getPotentialMayors() {
return potentialResidents;
}

public boolean isNationCapital() {
return getTown().isCapital();
}

public boolean isKingChange() {
return oldMayor.isKing();
}

public static HandlerList getHandlerList() {
return HANDLER_LIST;
}

@NotNull
@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}
}
62 changes: 27 additions & 35 deletions src/com/palmergames/bukkit/towny/object/Town.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.palmergames.bukkit.towny.event.town.TownConqueredEvent;
import com.palmergames.bukkit.towny.event.town.TownMapColourLocalCalculationEvent;
import com.palmergames.bukkit.towny.event.town.TownMapColourNationalCalculationEvent;
import com.palmergames.bukkit.towny.event.town.TownMayorChangedEvent;
import com.palmergames.bukkit.towny.event.town.TownMayorChosenBySuccessionEvent;
import com.palmergames.bukkit.towny.event.town.TownRemoveAlliedTownEvent;
import com.palmergames.bukkit.towny.event.town.TownRemoveEnemiedTownEvent;
import com.palmergames.bukkit.towny.event.town.TownUnconquerEvent;
Expand Down Expand Up @@ -192,7 +194,7 @@ public void forceSetMayor(Resident mayor) throws TownyException {
if (!hasResident(mayor))
throw new TownyException(Translation.of("msg_err_mayor_doesnt_belong_to_town"));

setMayor(mayor);
setMayor(mayor, false);
}

/**
Expand All @@ -201,12 +203,19 @@ public void forceSetMayor(Resident mayor) throws TownyException {
* @param mayor - Resident to become mayor.
*/
public void setMayor(Resident mayor) {
setMayor(mayor, true);
}

public void setMayor(Resident mayor, boolean callEvent) {
if (!hasResident(mayor))
return;


if (callEvent)
BukkitTools.fireEvent(new TownMayorChangedEvent(this.mayor, mayor));

this.mayor = mayor;
TownyPerms.assignPermissions(mayor, null);

TownyPerms.assignPermissions(mayor, null);
}

public String getFounder() {
Expand Down Expand Up @@ -806,47 +815,30 @@ private void remove(Resident resident) {
*/
public void findNewMayor() {
for (String rank : TownySettings.getOrderOfMayoralSuccession()) {
if (findNewMayor(rank)) {
if (findNewMayor(getRank(rank))) {
return;
}
}
// No one has the rank to succeed the mayor, choose a resident.
findNewMayorCatchAll();
findNewMayor(getResidents());
}

/**
* Tries to find a new mayor from among the town's residents with the rank specified.
*
* @param rank - the rank being checked for potential mayors
* @return found - whether or not a new mayor was found
*/
private boolean findNewMayor(String rank) {
boolean found = false;
for (Resident newMayor : getRank(rank)) {
if ((newMayor != mayor) && (newMayor.hasTownRank(rank))) { // The latter portion seems redundant.
setMayor(newMayor);
found = true;
break;
}
}
return found;
}

/**
* Tries to find a new mayor from among the town's residents.
* Tries to find a new mayor from ths list of potential residenst.
*
* @return found - whether or not a new mayor was found
* @param potentialResidents the List of Residents that could be mayor.
* @return true if a new mayor is selected.
*/
private boolean findNewMayorCatchAll() {
boolean found = false;
for (Resident newMayor : getResidents()) {
if (newMayor != mayor) {
setMayor(newMayor);
found = true;
break;
}
private boolean findNewMayor(List<Resident> potentialResidents) {
for (Resident newMayor : potentialResidents) {
if (newMayor.equals(mayor))
continue;

TownMayorChosenBySuccessionEvent tmcbse = new TownMayorChosenBySuccessionEvent(mayor, newMayor, potentialResidents);
setMayor(tmcbse.getNewMayor());
return true;
}
return found;
return false;
}

@Override
Expand Down

0 comments on commit b6401c8

Please sign in to comment.