package net.goldtreeservers.worldguardextraflags.listeners;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityToggleGlideEvent;

import com.sk89q.worldguard.protection.ApplicableRegionSet;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.goldtreeservers.worldguardextraflags.WorldGuardExtraFlagsPlugin;
import net.goldtreeservers.worldguardextraflags.flags.Flags;
import net.goldtreeservers.worldguardextraflags.flags.helpers.ForcedStateFlag.ForcedState;
import net.goldtreeservers.worldguardextraflags.wg.WorldGuardUtils;

@RequiredArgsConstructor
public class EntityListenerOnePointNine implements Listener
{
	@Getter private final WorldGuardExtraFlagsPlugin plugin;
	
	@EventHandler(ignoreCancelled = true)
	public void onEntityToggleGlideEvent(EntityToggleGlideEvent event)
	{
		Entity entity = event.getEntity();
		if (entity instanceof Player)
		{
			Player player = (Player)entity;
			
			ApplicableRegionSet regions = this.plugin.getWorldGuardCommunicator().getRegionContainer().createQuery().getApplicableRegions(player.getLocation());

			ForcedState state = WorldGuardUtils.queryValue(player, player.getWorld(), regions.getRegions(), Flags.GLIDE);
			if (state != ForcedState.ALLOW)
			{
				event.setCancelled(true);
				
				player.setGliding(state == ForcedState.FORCE);
				
				if (state == ForcedState.DENY)
				{
					player.teleport(player.getLocation());
				}
			}
		}
	}
}

@@ -1309,45 +1309,135 @@
 %0A%09%09%09
-if (state != ForcedState.ALLOW
+switch(state)%0A%09%09%09%7B%0A%09%09%09%09case ALLOW:%0A%09%09%09%09%09break;%0A%09%09%09%09case DENY:%0A%09%09%09%09%7B%0A%09%09%09%09%09if (!event.isGliding()
 )%0A%09%09%09
+%09
+%09
 %7B%0A
+%09%09%09%09%09%09return;%0A%09%09%09%09%09%7D%0A%0A%09
 %09%09%09%09
@@ -1470,100 +1470,201 @@
 %09%09%09%09
+%09
 %0A%09%09%09%09
-player.setGliding(state == ForcedState.
+%09//Prevent the player from being allowed to glide by spamming space%0A%09%09%09%09%09player.teleport(player.getLocation());%0A%09%09%09%09%09%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%09case 
 FORCE
-);
+:
 %0A%09%09%09%09
+%7B
 %0A%09%09%09%09
+%09
 if (
-state == ForcedState.DENY
+event.isGliding()
 )%0A
+%09
 %09%09%09%09
@@ -1674,45 +1674,72 @@
 %09%09%09%09
-player.teleport(player.getLocation())
+%09return;%0A%09%09%09%09%09%7D%0A%0A%09%09%09%09%09event.setCancelled(true);%0A%09%09%09%09%09%0A%09%09%09%09%09break
 ;%0A%09%09

Fix edge case with glide flag