Skip to content

Commit

Permalink
1.6.4:
Browse files Browse the repository at this point in the history
  - Adjust minimum Towny version to 0.96.5.5
  - Fix Deprecation warning.
    - Closes #43.
  - Fix being able to unclaim land and keep flight.
    - Closes #42.
  - Fix some teleport commands bypassing flight removal.
    - Closes #38.
  - Prevent PVPListener from removing flight when the attacker is
vanished.
    - Closes #32.
  • Loading branch information
LlmDl committed Dec 17, 2020
1 parent f369e82 commit 41794f5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 47 deletions.
11 changes: 6 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gmail.llmdlio</groupId>
<artifactId>TownyFlight</artifactId>
<version>1.6.3</version>
<version>1.6.4</version>
<name>TownyFlight</name>
<description>A flight plugin for Towny servers.</description>
<properties>
Expand Down Expand Up @@ -40,22 +40,23 @@
</repositories>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.15.2-R0.1-SNAPSHOT</version>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.4-R0.1-SNAPSHOT</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.palmergames.bukkit.towny</groupId>
<artifactId>Towny</artifactId>
<version>0.96.2.17</version>
<version>0.96.5.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>16.0.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
28 changes: 15 additions & 13 deletions src/main/java/com/gmail/llmdlio/townyflight/TownyFlight.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,26 @@
import com.gmail.llmdlio.townyflight.listeners.PlayerJoinListener;
import com.gmail.llmdlio.townyflight.listeners.PlayerLeaveTownListener;
import com.gmail.llmdlio.townyflight.listeners.PlayerPVPListener;
import com.gmail.llmdlio.townyflight.listeners.PlayerTeleportListener;
import com.gmail.llmdlio.townyflight.listeners.TownUnclaimListener;
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.utils.CombatUtil;
import com.palmergames.bukkit.util.Version;

public class TownyFlight extends JavaPlugin {
private static Version requiredTownyVersion = Version.fromString("0.96.2.17");
private static Version requiredTownyVersion = Version.fromString("0.96.5.5");

private final PlayerEnterTownListener playerEnterListener = new PlayerEnterTownListener(this);
private final PlayerJoinListener playerJoinListener = new PlayerJoinListener(this);
private final PlayerLeaveTownListener playerLeaveListener = new PlayerLeaveTownListener(this);
private final PlayerPVPListener playerPVPListener = new PlayerPVPListener();
private final TownUnclaimListener townUnclaimListener = new TownUnclaimListener();
private final PlayerFallListener playerFallListener = new PlayerFallListener();
private final PlayerTeleportListener playerTeleportListener = new PlayerTeleportListener();

public static String pluginPrefix;
private static String flightOnMsg;
Expand Down Expand Up @@ -146,6 +149,7 @@ private void registerEvents(){
pluginManager.registerEvents(playerPVPListener, this);
pluginManager.registerEvents(townUnclaimListener, this);
pluginManager.registerEvents(playerFallListener, this);
pluginManager.registerEvents(playerTeleportListener, this);
}

private void unregisterEvents() {
Expand All @@ -155,6 +159,7 @@ private void unregisterEvents() {
HandlerList.unregisterAll(playerPVPListener);
HandlerList.unregisterAll(townUnclaimListener);
HandlerList.unregisterAll(playerFallListener);
HandlerList.unregisterAll(playerTeleportListener);
}

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Expand Down Expand Up @@ -225,14 +230,10 @@ public static boolean canFly(Player player, boolean silent) {
if (!silent) player.sendMessage(pluginPrefix + ChatColor.RED + noPermission + ((showPermissionInMessage) ? "townyflight.command.tfly" : ""));
return false;
}
Resident resident = null;
try {
resident = TownyAPI.getInstance().getDataSource().getResident(player.getName());
} catch (NotRegisteredException e) {
// Sometimes when a player joins for the first time, there can be a canFly test run before Towny has
// the chance to save the player properly.
Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId());
if (resident == null)
return false;
}

if (disableDuringWar && (TownyAPI.getInstance().isWarTime() || warsForTowny(resident))) {
if (!silent) player.sendMessage(pluginPrefix + notDuringWar);
return false;
Expand Down Expand Up @@ -273,11 +274,10 @@ private static boolean warsForTowny(Resident resident) {
* @return true if player is allowed to be flying at their present location.
*/
private static boolean allowedLocation(Player player) {
Resident resident = null;
try {
resident = TownyAPI.getInstance().getDataSource().getResident(player.getName());
} catch (NotRegisteredException ignored) {
}
Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId());
if (resident == null)
return false;

if (TownyAPI.getInstance().isWilderness(player.getLocation()))
return false;

Expand All @@ -304,6 +304,7 @@ private static boolean allowedLocation(Player player) {
* @param forced - whether this is a forced deactivation or not
* @param cause - cause of disabling flight ("", "pvp", "console")
*/
@SuppressWarnings("deprecation")
private static void toggleFlight(Player player, boolean silent, boolean forced, String cause) {
if (player.getAllowFlight()) {
if (!silent) {
Expand Down Expand Up @@ -348,6 +349,7 @@ public void run() {
* @param forced - whether this is a forced deactivation or not
* @param cause - cause of disabling flight ("", "pvp", "console")
*/
@SuppressWarnings("deprecation")
public static void removeFlight(Player player, boolean silent, boolean forced, String cause) {
if (!silent) {
if (forced) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,43 @@ public PlayerPVPListener() {
* Listener to turn off flight if flying player enters PVP combat.
* Runs only if the config.yml's disable_Combat_Prevention is set to true.
*/
@EventHandler(priority = EventPriority.LOWEST)
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void playerPVPEvent (EntityDamageByEntityEvent event) throws NotRegisteredException {
Entity attacker = event.getDamager();
Entity defender = event.getEntity();


if (!TownyAPI.getInstance().isTownyWorld(attacker.getWorld()))
return;

if (attacker instanceof Projectile) {
ProjectileSource shooter = ((Projectile) attacker).getShooter();
if (shooter instanceof Entity)
attacker = (Entity) shooter;
}

if ( !(attacker instanceof Player) || !(defender instanceof Player))
if (!(attacker instanceof Player) || !(defender instanceof Player))
return;

Player attackingPlayer = (Player) attacker;

Player defendingPlayer = (Player) defender;

if (!attackingPlayer.getAllowFlight())
return;

if (attackingPlayer.getGameMode().equals(GameMode.CREATIVE)) {
event.setCancelled(true);
return;
}

if (!attackingPlayer.getAllowFlight())
return;

if (!TownyAPI.getInstance().getDataSource().getWorld(attackingPlayer.getLocation().getWorld().getName()).isUsingTowny())
return;
if (!defendingPlayer.canSee(attackingPlayer)) {
event.setCancelled(true);
return;
}

if (CombatUtil.preventDamageCall(towny, attacker, defender))
return;

if (!event.isCancelled()) {
TownyFlight.removeFlight(attackingPlayer, false, true, "pvp");
event.setCancelled(true);
}
}
TownyFlight.removeFlight(attackingPlayer, false, true, "pvp");
event.setCancelled(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.gmail.llmdlio.townyflight.listeners;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

import com.gmail.llmdlio.townyflight.TownyFlight;

public class PlayerTeleportListener implements Listener {

public PlayerTeleportListener() {
}

@EventHandler(priority = EventPriority.MONITOR)
private void playerTeleports(PlayerTeleportEvent event) {
if (event.getCause() != TeleportCause.PLUGIN || event.getCause() != TeleportCause.COMMAND)
return;

Player player = event.getPlayer();
if (player.hasPermission("townyflight.bypass")
|| !player.getAllowFlight()
|| TownyFlight.canFly(player, true)) {
return;
}

TownyFlight.removeFlight(player, false, true, "");
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.gmail.llmdlio.townyflight.listeners;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

import com.gmail.llmdlio.townyflight.TownyFlight;
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.event.TownUnclaimEvent;
import com.palmergames.bukkit.towny.object.Coord;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
import com.palmergames.bukkit.towny.event.town.TownUnclaimEvent;


public class TownUnclaimListener implements Listener {
Expand All @@ -25,19 +23,15 @@ public TownUnclaimListener() {
*/
@EventHandler(priority = EventPriority.MONITOR)
private void TownUnclaimEvent (TownUnclaimEvent event) {
Town town = event.getTown();
WorldCoord wc = event.getWorldCoord();
World world = event.getWorldCoord().getBukkitWorld();

// Cycle through players of the affected town, because multiple players could be in a plot that is unclaimed.
for (final Player player : TownyAPI.getInstance().getOnlinePlayers(town)) {
if (player.hasPermission("townyflight.bypass"))
for (final Player player : Bukkit.getOnlinePlayers()) {
if (player.hasPermission("townyflight.bypass")
|| !player.getAllowFlight()
|| !player.getWorld().equals(world)
|| TownyFlight.canFly(player, true))
return;
if (!player.getAllowFlight())
return;

WorldCoord pwc = new WorldCoord(player.getWorld().getName(), Coord.parseCoord(player.getLocation()));
if (!pwc.equals(wc))
return;

TownyFlight.removeFlight(player, false, true, "");
}
Expand Down

0 comments on commit 41794f5

Please sign in to comment.