Skip to content

Commit

Permalink
Do a better job of implementing Folia, removing an unneeded Towny (#325)
Browse files Browse the repository at this point in the history
instance being passed around.
  • Loading branch information
LlmDl committed Dec 11, 2023
1 parent 92b258c commit c33ea83
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
8 changes: 3 additions & 5 deletions src/main/java/io/github/townyadvanced/flagwar/FlagWar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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. */
Expand All @@ -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());
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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. */
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit c33ea83

Please sign in to comment.