From c33ea837b7190429f526f467818eade1cb604cb6 Mon Sep 17 00:00:00 2001 From: LlmDl Date: Mon, 11 Dec 2023 10:02:50 -0600 Subject: [PATCH] Do a better job of implementing Folia, removing an unneeded Towny (#325) instance being passed around. --- .../github/townyadvanced/flagwar/FlagWar.java | 8 +++----- .../flagwar/events/CellAttackEvent.java | 9 ++------- .../flagwar/objects/CellUnderAttack.java | 20 +++++++------------ 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/main/java/io/github/townyadvanced/flagwar/FlagWar.java b/src/main/java/io/github/townyadvanced/flagwar/FlagWar.java index 30554aac8..1d7393439 100644 --- a/src/main/java/io/github/townyadvanced/flagwar/FlagWar.java +++ b/src/main/java/io/github/townyadvanced/flagwar/FlagWar.java @@ -554,7 +554,7 @@ && isThisPlotProtectedByNeighbours(worldCoord)) { calculateFeesAndFines(attackingResident, townBlock, costToPlaceWarFlag); } - if (!kickstartCellAttackEvent(towny, player, block)) { + if (!kickstartCellAttackEvent(player, block)) { return false; } if (TownyEconomyHandler.isActive() && costToPlaceWarFlag.compareTo(BigDecimal.ZERO) > 0) { @@ -653,15 +653,13 @@ private static void payForWarFlag(final Player atkPlayer, final BigDecimal cost) /** * Kickstart a {@link CellAttackEvent}, then return false if it does not get canceled. - * @param towny Instance of {@link Towny}. * @param player Player who should be listed as the attacker. * @param block Block where the flagBaseBlock should have been placed. * @return TRUE if the event was kick-started successfully. FALSE if event canceled. * @throws TownyException if the CellAttackEvent is canceled with a given reason. */ - private static boolean kickstartCellAttackEvent(final Towny towny, final Player player, - final Block block) throws TownyException { - var cellAttackEvent = new CellAttackEvent(towny, player, block); + private static boolean kickstartCellAttackEvent(final Player player, final Block block) throws TownyException { + var cellAttackEvent = new CellAttackEvent(player, block); plugin.getServer().getPluginManager().callEvent(cellAttackEvent); if (cellAttackEvent.isCancelled()) { if (cellAttackEvent.hasReason()) { diff --git a/src/main/java/io/github/townyadvanced/flagwar/events/CellAttackEvent.java b/src/main/java/io/github/townyadvanced/flagwar/events/CellAttackEvent.java index d17787d41..eb33c8c89 100644 --- a/src/main/java/io/github/townyadvanced/flagwar/events/CellAttackEvent.java +++ b/src/main/java/io/github/townyadvanced/flagwar/events/CellAttackEvent.java @@ -25,7 +25,6 @@ import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -import com.palmergames.bukkit.towny.Towny; import org.jetbrains.annotations.NotNull; import java.time.Duration; @@ -37,8 +36,6 @@ public class CellAttackEvent extends Event implements Cancellable { /** Holds the {@link HandlerList} for the {@link CellAttackEvent}. */ private static final HandlerList HANDLERS = new HandlerList(); - /** Holds the {@link Towny} instance. */ - private final Towny plugin; /** Holds the attacking {@link Player}. */ private final Player player; /** Holds the base {@link Block} for the War Flag. */ @@ -63,13 +60,11 @@ public static HandlerList getHandlerList() { /** * Constructs the {@link CellAttackEvent}. - * @param townyInstance the instance of {@link Towny} at runtime. (Use {@link Towny#getPlugin()}.) * @param attacker the attacking {@link Player}. * @param flagBaseBlock the {@link Block} representing the War Flag base/pole. */ - public CellAttackEvent(final Towny townyInstance, final Player attacker, final Block flagBaseBlock) { + public CellAttackEvent(final Player attacker, final Block flagBaseBlock) { super(); - this.plugin = townyInstance; this.player = attacker; this.flagBlock = flagBaseBlock; setPhaseDuration(FlagWarConfig.getFlagPhasesDuration()); @@ -87,7 +82,7 @@ public Block getFlagBlock() { /** @return a new {@link CellUnderAttack} with the Towny instance, attacker, flag base, and attack time stored. */ public CellUnderAttack getData() { - return new CellUnderAttack(plugin, player.getName(), flagBlock, phaseDuration); + return new CellUnderAttack(player.getName(), flagBlock, phaseDuration); } /** diff --git a/src/main/java/io/github/townyadvanced/flagwar/objects/CellUnderAttack.java b/src/main/java/io/github/townyadvanced/flagwar/objects/CellUnderAttack.java index 27a70478c..fe63ffc24 100644 --- a/src/main/java/io/github/townyadvanced/flagwar/objects/CellUnderAttack.java +++ b/src/main/java/io/github/townyadvanced/flagwar/objects/CellUnderAttack.java @@ -22,6 +22,7 @@ import com.gmail.filoghost.holographicdisplays.api.line.TextLine; import com.palmergames.bukkit.towny.object.Coord; import com.palmergames.bukkit.towny.scheduling.ScheduledTask; +import com.palmergames.bukkit.towny.scheduling.TaskScheduler; import io.github.townyadvanced.flagwar.CellAttackThread; import io.github.townyadvanced.flagwar.FlagWar; @@ -34,9 +35,6 @@ import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.inventory.ItemStack; -import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitTask; - import java.time.Duration; import java.util.ArrayList; import java.util.List; @@ -51,8 +49,8 @@ public class CellUnderAttack extends Cell { /** Holds an instance of FlagWar's logger. */ private static final Logger LOGGER = FlagWar.getInstance().getLogger(); - /** Holds an instance of Towny. */ - private final Plugin plugin; + /** The TaskScheduler used to schedule attacks and holograms. */ + private final TaskScheduler scheduler = FlagWar.getFlagWar().getScheduler(); /** Holds the name of the war flag owner. */ private final String nameOfFlagOwner; @@ -73,7 +71,7 @@ public class CellUnderAttack extends Cell { /** A thread used to update the state of the {@link CellUnderAttack} using the Scheduler's repeating task.*/ private ScheduledTask thread; /** A thread used to update the {@link #hologram}'s {@link #timerLine}. */ - private BukkitTask hologramThread; + private ScheduledTask hologramThread; /** Holds the war flag hologram. */ private Hologram hologram; /** Holds the time, in seconds, assuming 20 ticks is 1 second, of the war flag. */ @@ -84,15 +82,13 @@ public class CellUnderAttack extends Cell { /** * Prepares the CellUnderAttack. * - * @param instance A plugin instance. Preferably of Towny or FlagWar. * @param flagOwner Name of the Resident that placed the flag * @param base {@link Block} representing the "flag pole" of the block * @param timerPhase Time (as a long) between Material shifting the flag and beacon. */ - public CellUnderAttack(final Plugin instance, final String flagOwner, final Block base, final Duration timerPhase) { + public CellUnderAttack(final String flagOwner, final Block base, final Duration timerPhase) { super(base.getLocation()); - this.plugin = instance; this.nameOfFlagOwner = flagOwner; this.flagBaseBlock = base; this.flagPhaseID = 0; @@ -364,13 +360,11 @@ public void beginAttack() { final int tps = 20; final int milliTicks = 50; final long ticksFromMs = this.flagPhaseDuration.toMillis() / milliTicks; - thread = FlagWar.getFlagWar().getScheduler() - .runRepeating(() -> new CellAttackThread(this), ticksFromMs, ticksFromMs); + thread = scheduler.runRepeating(() -> new CellAttackThread(this), ticksFromMs, ticksFromMs); if (FlagWarConfig.isHologramEnabled()) { drawHologram(); if (FlagWarConfig.hasTimerLine()) { - hologramThread = plugin.getServer().getScheduler() - .runTaskTimer(plugin, new HologramUpdateThread(this), tps, tps); + hologramThread = scheduler.runRepeating(() -> new HologramUpdateThread(this), tps, tps); } } }