Skip to content

Commit

Permalink
Expand WarZoneListener (#87)
Browse files Browse the repository at this point in the history
Adds initial implementation for the following events:
- TownyBurnEvent
- TownyExplodingBlocksEvent
- TownyExplosionDamagesEntityEvent
- TownBlockPVPTestEvent

Additionally, creates the `Cell#parse(WorldCoord)` method for convenience.
  • Loading branch information
LlmDl authored Oct 27, 2021
1 parent ffe8625 commit 7d25977
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@
package io.github.townyadvanced.flagwar.listeners;

import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.event.actions.TownyActionEvent;
import com.palmergames.bukkit.towny.event.actions.TownyBuildEvent;
import com.palmergames.bukkit.towny.event.actions.TownyBurnEvent;
import com.palmergames.bukkit.towny.event.actions.TownyDestroyEvent;
import com.palmergames.bukkit.towny.event.actions.TownyExplodingBlocksEvent;
import com.palmergames.bukkit.towny.event.actions.TownyItemuseEvent;
import com.palmergames.bukkit.towny.event.actions.TownySwitchEvent;
import com.palmergames.bukkit.towny.event.damage.TownBlockPVPTestEvent;
import com.palmergames.bukkit.towny.event.damage.TownyExplosionDamagesEntityEvent;
import com.palmergames.bukkit.towny.object.PlayerCache.TownBlockStatus;
import com.palmergames.bukkit.towny.war.common.WarZoneConfig;
import io.github.townyadvanced.flagwar.FlagWarAPI;
import io.github.townyadvanced.flagwar.config.FlagWarConfig;
import io.github.townyadvanced.flagwar.i18n.Translate;
import io.github.townyadvanced.flagwar.objects.Cell;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

Expand Down Expand Up @@ -139,6 +149,86 @@ public void onSwitchUse(final TownySwitchEvent townySwitchEvent) {
townySwitchEvent.setCancelled(false);
}

/**
* When Towny reports a burn event, check if this is a {@link TownBlockStatus} which corresponds to a Cell which
* is under attack, and the Towny WarZoneConfig is configured to allow fires in WarZones.
*
* @param townyBurnEvent the {@link TownyBurnEvent}.
*/
@EventHandler
public void onBurn(final TownyBurnEvent townyBurnEvent) {
if (!FlagWarConfig.isAllowingAttacks()
|| townyBurnEvent.isInWilderness()
|| !WarZoneConfig.isAllowingFireInWarZone()
|| !Cell.parse(townyBurnEvent.getLocation()).isUnderAttack()) {
return;
}
townyBurnEvent.setCancelled(false);
}

/**
* When Towny reports an explosion damaging an entity event, check if this is a {@link TownBlockStatus} which
* corresponds to a Cell which is under attack, and the Towny WarZoneConfig is configured to allow those
* explosions in WarZones.
*
* @param townyExplosionDamagesEntityEvent the {@link TownyExplosionDamagesEntityEvent}
*/
@EventHandler
public void onExplosionDamagingEntity(final TownyExplosionDamagesEntityEvent townyExplosionDamagesEntityEvent) {
if (!FlagWarConfig.isAllowingAttacks()
|| townyExplosionDamagesEntityEvent.isInWilderness()
|| !WarZoneConfig.isAllowingExplosionsInWarZone()
|| !Cell.parse(townyExplosionDamagesEntityEvent.getLocation()).isUnderAttack()) {
return;
}
townyExplosionDamagesEntityEvent.setCancelled(false);
}

/**
* When Towny reports an explosion damaging some blocks, check if this is a {@link TownBlockStatus} which
* corresponds to a Cell which is under attack, and the Towny WarZoneConfig is configured to allow those
* explosions in WarZones.
*
* @param event the {@link TownyExplodingBlocksEvent}.
*/
@EventHandler
public void onExplosionDamagingBlocks(final TownyExplodingBlocksEvent event) {
if (!FlagWarConfig.isAllowingAttacks()
|| !WarZoneConfig.isAllowingExplosionsInWarZone()) {
return;
}
List<Block> toAllow = new ArrayList<Block>();
for (Block block : event.getVanillaBlockList()) {
// Wilderness or not located inside of a Cell which is under attack, skip it.
if (TownyAPI.getInstance().isWilderness(block)
|| !Cell.parse(block.getLocation()).isUnderAttack()) {
continue;
}
// This is an allowed explosion, so add it to our War-allowed list.
toAllow.add(block);
}
// Add all TownyFilteredBlocks to our list, since our list will be used.
toAllow.addAll(event.getTownyFilteredBlockList());

// Return the list of allowed blocks for this block explosion event.
event.setBlockList(toAllow);
}

/**
* When Towny asks if PVP should be enabled in a TownBlock, if it is a {@link Cell} which is under attack,
* we will tell Towny that PVP should be enabled, overriding any Town or TownBlock setting.
*
* @param townBlockPVPTestEvent the {@link TownBlockPVPTestEvent}.
*/
@EventHandler
public void onTownBlockPVPTestEvent(final TownBlockPVPTestEvent townBlockPVPTestEvent) {
if (!FlagWarConfig.isAllowingAttacks()
|| !Cell.parse(townBlockPVPTestEvent.getTownBlock().getWorldCoord()).isUnderAttack()) {
return;
}
townBlockPVPTestEvent.setPvp(true);
}

/**
* Tell the calling method to fail fast if any of the following expressions are true:
* <ul>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/github/townyadvanced/flagwar/objects/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.bukkit.Location;

import com.palmergames.bukkit.towny.object.Coord;
import com.palmergames.bukkit.towny.object.WorldCoord;

/**
* Representation of a Towny plot, based on it's {@link Coord}, used for manipulation in FlagWar.
Expand Down Expand Up @@ -104,6 +105,15 @@ public static Cell parse(final String worldName, final int x, final int z) {
return new Cell(worldName, xResult - (x < 0 && xNeedFix ? 1 : 0), zResult - (z < 0 && zNeedFix ? 1 : 0));
}

/**
* Parse a Towny {@link WorldCoord} into a {@link Cell}.
* @param worldCoord the {@link WorldCoord}.
* @return a new Cell from the given {@link WorldCoord}.
*/
public static Cell parse(final WorldCoord worldCoord) {
return parse(worldCoord.getWorldName(), worldCoord.getX(), worldCoord.getZ());
}

/**
* Gets the {@link org.bukkit.World} from the supplied {@link Location}.
* @param loc the supplied location.
Expand Down

0 comments on commit 7d25977

Please sign in to comment.