/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * 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 org.spongepowered.common.event.tracking.phase.entity;

import net.minecraft.world.server.ServerWorld;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.common.bridge.world.ServerWorldBridge;
import org.spongepowered.common.event.tracking.PhaseTracker;

public final class InvokingTeleporterState extends EntityPhaseState<InvokingTeleporterContext> {

    InvokingTeleporterState() {
    }

    @Override
    public InvokingTeleporterContext createNewContext(final PhaseTracker tracker) {
        return new InvokingTeleporterContext(this, tracker)
            .addBlockCaptures()
            .addEntityCaptures();
    }

    @Override
    public void unwind(final InvokingTeleporterContext context) {
    }

    @Override
    public boolean tracksBlockSpecificDrops(final InvokingTeleporterContext context) {
        return true;
    }

    @Override
    public boolean spawnEntityOrCapture(final InvokingTeleporterContext context, final Entity entity) {
        final ServerWorld worldServer = context.getTargetWorld();
        // Allowed to use the force spawn because it's the same "entity"
        ((ServerWorldBridge) worldServer).bridge$forceSpawnEntity((net.minecraft.entity.Entity) entity);
        return true;
    }

    @Override
    public boolean doesCaptureEntitySpawns() {
        return false;
    }

    @Override
    public boolean tracksEntitySpecificDrops() {
        return true;
    }

    @Override
    public boolean doesDenyChunkRequests() {
        return false;
    }

    @Override
    public void markTeleported(final InvokingTeleporterContext phaseContext) {
        phaseContext.setDidPort(true);
    }
}

@@ -1304,16 +1304,176 @@
 ntity;%0A%0A
+import net.minecraft.entity.effect.LightningBoltEntity;%0Aimport net.minecraft.entity.player.PlayerEntity;%0Aimport net.minecraft.entity.player.ServerPlayerEntity;%0A
 import n
@@ -1551,24 +1551,73 @@
 ity.Entity;%0A
+import org.spongepowered.common.SpongeImplHooks;%0A
 import org.s
@@ -1649,22 +1649,16 @@
 e.world.
-Server
 WorldBri
@@ -1722,16 +1722,66 @@
 Tracker;
+%0Aimport org.spongepowered.common.util.SpongeHooks;
 %0A%0Apublic
@@ -2554,72 +2554,172 @@
     
-// Allowed to use the force spawn because it's the same %22
+if (!((WorldBridge)worldServer).bridge$isFake() && SpongeImplHooks.onServerThread()) %7B%0A            SpongeHooks.logEntitySpawn((net.minecraft.entity.Entity) 
 entity
-%22
+);
 %0A   
@@ -2727,88 +2727,273 @@
     
-((ServerWorldBridge) worldServer).bridge$forceSpawnEntity((net.minecraft.entity.
+%7D%0A        if (entity instanceof ServerPlayerEntity) %7B%0A            worldServer.addNewPlayer((ServerPlayerEntity) entity);%0A            return true;%0A        %7D%0A%0A        if (entity instanceof LightningBoltEntity) %7B%0A            worldServer.addLightningBolt((LightningBolt
 Enti
@@ -3005,32 +3005,36 @@
 ntity);%0A        
+    
 return true;%0A   
@@ -3022,32 +3022,119 @@
    return true;%0A
+        %7D%0A%0A        return worldServer.addEntity((net.minecraft.entity.Entity) entity);%0A
     %7D%0A%0A    @Over

fix InvokingTeleporterState