Skip to content

Commit

Permalink
Add events for towns and nations' level changing.
Browse files Browse the repository at this point in the history
  • Loading branch information
LlmDl committed Jan 6, 2025
1 parent 8d753c8 commit e47945f
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 3 deletions.
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,40 @@ 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) {
event.getTown().getResidents()
.stream()
.filter(Resident::isOnline)
.forEach(r -> TownyPerms.assignPermissions(r, r.getPlayer()));
}

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

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

@EventHandler
public void onNationLevelDecrease(NationLevelDecreaseEvent event) {
event.getNation().getResidents()
.stream()
.filter(Resident::isOnline)
.forEach(r -> TownyPerms.assignPermissions(r, r.getPlayer()));
}
}

0 comments on commit e47945f

Please sign in to comment.