Skip to content

Commit

Permalink
- Add a listener which will reset the player cache when they are
Browse files Browse the repository at this point in the history
inside of a plot or town which has had their plot permissions change.
  • Loading branch information
LlmDl committed Oct 24, 2023
1 parent fe496c1 commit 64002ba
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Towny/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<artifactId>towny</artifactId>
<packaging>jar</packaging>
<version>0.99.6.4</version>
<version>0.99.6.5</version>

<licenses>
<license>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import com.palmergames.bukkit.towny.event.PlayerChangePlotEvent;
import com.palmergames.bukkit.towny.event.SpawnEvent;
import com.palmergames.bukkit.towny.event.TownAddResidentEvent;
import com.palmergames.bukkit.towny.event.TownBlockPermissionChangeEvent;
import com.palmergames.bukkit.towny.event.TownClaimEvent;
import com.palmergames.bukkit.towny.event.TownRemoveResidentEvent;
import com.palmergames.bukkit.towny.event.damage.TownyPlayerDamagePlayerEvent;
import com.palmergames.bukkit.towny.event.nation.NationPreTownLeaveEvent;
import com.palmergames.bukkit.towny.event.town.TownPreUnclaimCmdEvent;
import com.palmergames.bukkit.towny.exceptions.TownyException;
import com.palmergames.bukkit.towny.object.PlayerCache;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.SpawnType;
import com.palmergames.bukkit.towny.object.Town;
Expand All @@ -30,6 +32,7 @@
import com.palmergames.bukkit.towny.object.WorldCoord;
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.SpawnUtil;
import com.palmergames.bukkit.util.Colors;
import com.palmergames.bukkit.util.DrawSmokeTaskFactory;
Expand Down Expand Up @@ -262,4 +265,20 @@ public void onResidentJoinTown(TownAddResidentEvent event) {
TownyMessaging.sendErrorMsg(player, e.getMessage(player));
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onTownBlockPermissionChange(TownBlockPermissionChangeEvent event) {
WorldCoord wc = event.getTownBlock().getWorldCoord();
for (Player player : Bukkit.getOnlinePlayers())
Towny.getPlugin().getScheduler().runAsync(() -> attemptPlayerCacheReset(player, wc));
}

private void attemptPlayerCacheReset(Player player, WorldCoord worldCoord) {
if (!worldCoord.getWorldName().equalsIgnoreCase(player.getWorld().getName()))
return;
PlayerCache cache = Towny.getPlugin().getCache(player);
if (cache == null || PlayerCacheUtil.isOwnerCache(cache) || !cache.getLastTownBlock().equals(worldCoord))
return;
Towny.getPlugin().resetCache(player);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.palmergames.bukkit.towny.utils;

import com.google.common.base.Preconditions;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.TownyMessaging;
Expand All @@ -25,6 +26,7 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

/**
* Groups all the cache status and permissions in one place.
Expand Down Expand Up @@ -482,4 +484,18 @@ private static boolean isAllowedMaterial(TownBlock townBlock, Material material,

return true;
}

/**
* Does the given cache have a TownBlockStatus which is admin, plot_owner or
* town_owner. These TownBlockStatus values will always allow for all
* interactions.
*
* @param cache PlayerCache to check for, do not pass a null cache.
* @return true if the cache's TownBlockStatus is a admin, plot or town owner.
*/
public static boolean isOwnerCache(@NotNull PlayerCache cache) {
Preconditions.checkNotNull(cache, "Cache cannot be null.");
TownBlockStatus status = cache.getStatus();
return status.equals(TownBlockStatus.ADMIN) || status.equals(TownBlockStatus.PLOT_OWNER) || status.equals(TownBlockStatus.TOWN_OWNER);
}
}
4 changes: 3 additions & 1 deletion Towny/src/main/resources/ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9167,4 +9167,6 @@ v0.92.0.11:
- Fills a surrounded section with claims for a town.
- New Permission Node: towny.command.town.claim.fill
- Child node of towny.command.town.claim.*
- No changes to townyperms.yml required.
- No changes to townyperms.yml required.
0.99.6.5:
- Add a listener which will reset the player cache when they are inside of a plot or town which has had their plot permissions change.

0 comments on commit 64002ba

Please sign in to comment.