/*
 * This file is part of NpcTraitPack for NucleusFramework, licensed under the MIT License (MIT).
 *
 * Copyright (c) JCThePants (www.jcwhatever.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.jcwhatever.nucleus.npc.traits;

import com.jcwhatever.nucleus.providers.npc.INpc;
import com.jcwhatever.nucleus.providers.npc.traits.NpcTrait;
import com.jcwhatever.nucleus.providers.npc.traits.NpcTraitType;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Vector;

/**
 * Causes the NPC's Y coordinate position to be frozen.
 *
 * <p>Trait is registered with the lookup name "NpcTraitPack:FreezeHeight"</p>
 */
public class FreezeHeightTrait extends NpcTraitType {

    @Override
    public Plugin getPlugin() {
        return NpcTraitPack.getPlugin();
    }

    @Override
    public String getName() {
        return "FreezeHeight";
    }

    @Override
    protected NpcTrait createTrait(INpc npc) {
        return new FreezeHeight(npc, this);
    }

    public static class FreezeHeight extends NpcTrait implements Runnable {

        private boolean _isEnabled;
        private double _y;

        /**
         * Constructor.
         *
         * @param npc  The NPC the trait is for.
         * @param type The parent type that instantiated the trait.
         */
        FreezeHeight(INpc npc, NpcTraitType type) {
            super(npc, type);
        }

        public boolean isEnabled() {
            return _isEnabled;
        }

        public FreezeHeight setEnabled(boolean isEnabled) {
            _isEnabled = isEnabled;

            return this;
        }

        @Override
        public void onAdd() {
            if (getNpc().isSpawned()) {
                Location location = getNpc().getLocation();
                assert location != null;

                _y = location.getY();
            }
        }

        @Override
        public void onSpawn() {
            Location location = getNpc().getLocation();
            assert location != null;

            _y = location.getY();
        }

        @Override
        public void run() {
            if (!_isEnabled || !getNpc().isSpawned())
                return;

            Entity entity = getNpc().getEntity();
            assert entity != null;

            Vector vector = entity.getVelocity();

            Location location = getNpc().getLocation();
            assert location != null;

            if (Double.compare(location.getY(), _y) < 0) {
                vector.setY(0.20);
            }
            else {
                vector.setY(0);
            }

            entity.setVelocity(vector);
        }
    }
}

@@ -2647,16 +2647,36 @@
 abled;%0A%0A
+            setY();%0A
         
@@ -2767,372 +2767,90 @@
     
-if (getNpc().isSpawned()) %7B%0A                Location location = getNpc().getLocation();%0A                assert location != null;%0A%0A                _y = location.getY();%0A            %7D%0A        %7D%0A%0A        @Override%0A        public void onSpawn() %7B%0A            Location location = getNpc().getLocation();%0A            assert location != null;%0A%0A            _y = location.g
+setY();%0A        %7D%0A%0A        @Override%0A        public void onSpawn() %7B%0A            s
 etY(
@@ -3438,21 +3438,256 @@
 );%0A        %7D
+%0A%0A        private void setY() %7B%0A            if (getNpc().isSpawned()) %7B%0A                Location location = getNpc().getLocation();%0A                assert location != null;%0A%0A                _y = location.getY();%0A            %7D%0A        %7D
 %0A    %7D%0A%7D%0A

set freeze coordinates whe FreezeHeightTrait is enabled