package fr.ethilvan.bukkit.dimensions.listener;

import org.bukkit.Bukkit;
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.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerRespawnEvent;

import fr.ethilvan.bukkit.api.EthilVan;
import fr.ethilvan.bukkit.api.dimensions.Dimension;
import fr.ethilvan.bukkit.api.dimensions.Dimensions;
import fr.ethilvan.bukkit.api.event.dimensions.DimensionEnterEvent;
import fr.ethilvan.bukkit.api.event.dimensions.DimensionLeaveEvent;
import fr.ethilvan.bukkit.api.event.dimensions.DimensionRespawnEvent;

public class EventAdapter implements Listener {

    @EventHandler(priority = EventPriority.HIGH)
    public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
        Player player = event.getPlayer();

        Dimensions dimensions = EthilVan.getDimensions();
        Dimension from = dimensions.get(event.getFrom());
        Dimension to = dimensions.get(player.getWorld());
        if (from != to) {
            DimensionLeaveEvent leaveEvent =
                    new DimensionLeaveEvent(from, player);
            Bukkit.getPluginManager().callEvent(leaveEvent);
            DimensionEnterEvent enterEvent =
                    new DimensionEnterEvent(to, player, from);
            Bukkit.getPluginManager().callEvent(enterEvent);

            if (enterEvent.getChangeGameMode()) {
                player.setGameMode(to.getConfig().getGameMode());
            }
            if (enterEvent.getChangeInventories()) {
                EthilVan.getInventories().replace(player,
                        inventoryName(player, from),
                        inventoryName(player, to));
            }
        }
    }

    private String inventoryName(Player player, Dimension dimension) {
        return "dimensions." + dimension.getName() + "." + player.getName();
    }

    @EventHandler
    public void onPlayerRespawn(PlayerRespawnEvent event) {
        Dimension dim = EthilVan.getDimensions().get(event.getPlayer());
        DimensionRespawnEvent respawnEvent =
                new DimensionRespawnEvent(dim, event);
        Bukkit.getPluginManager().callEvent(respawnEvent);
    }
}

@@ -318,16 +318,55 @@
 wnEvent;
+%0Aimport org.bukkit.potion.PotionEffect;
 %0A%0Aimport
@@ -1810,32 +1810,267 @@
 ;%0A            %7D%0A
+            if (from.getConfig().hasFlag(%22reset-potion%22)) %7B%0A                for (PotionEffect potion : player.getActivePotionEffects()) %7B%0A                    player.removePotionEffect(potion.getType());%0A                %7D%0A            %7D%0A
         %7D%0A    %7D%0A

Fix players keep potion effect when they leave the dimension which have "reset-potion" flag