/*
 * 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.vanilla.mixin.server.management;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.NetworkManager;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.ServerConfigurationManager;
import net.minecraft.util.IChatComponent;
import org.spongepowered.api.Server;
import org.spongepowered.api.entity.player.Player;
import org.spongepowered.api.event.SpongeEventFactory;
import org.spongepowered.api.event.entity.player.PlayerJoinEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.common.Sponge;
import org.spongepowered.common.text.SpongeTexts;

import javax.annotation.Nullable;

@Mixin(ServerConfigurationManager.class)
public abstract class MixinServerConfigurationManager {

    @Shadow private MinecraftServer mcServer;
    @Nullable private IChatComponent joinMessage;

    @Redirect(method = "initializeConnectionToPlayer", at = @At(value = "INVOKE",
            target = "Lnet/minecraft/server/management/ServerConfigurationManager;sendChatMsg(Lnet/minecraft/util/IChatComponent;)V"))
    public void initializeConnectionToPlayerRedirectSendChatMsg(ServerConfigurationManager this$0, IChatComponent component) {
        this.joinMessage = component;
    }

    @Inject(method = "initializeConnectionToPlayer", at = @At("RETURN"))
    public void initializeConnectionToPlayerInject(NetworkManager netManager, EntityPlayerMP player, CallbackInfo ci) {
        PlayerJoinEvent event = SpongeEventFactory.createPlayerJoin(Sponge.getGame(), (Player) player, SpongeTexts.toText(this.joinMessage));
        this.joinMessage = null;
        Sponge.getGame().getEventManager().post(event);

        // Send (possibly changed) join message
        ((Server) this.mcServer).broadcastMessage(event.getJoinMessage());
    }

    @Inject(method = "recreatePlayerEntity", at = @At("RETURN"))
    public void onRecreatePlayerEntity(EntityPlayerMP playerIn, int dimension, boolean conqueredEnd, CallbackInfoReturnable<EntityPlayerMP> ci) {
        // TODO Bed locations
        // TODO Player respawn location
        Sponge.getGame().getEventManager().post(SpongeEventFactory.createPlayerRespawn(Sponge.getGame(), (Player) playerIn, false, ((Player) playerIn).getLocation()));
    }

}

@@ -1756,24 +1756,93 @@
 rJoinEvent;%0A
+import org.spongepowered.api.event.entity.player.PlayerRespawnEvent;%0A
 import org.s
@@ -2217,16 +2217,84 @@
 rnable;%0A
+import org.spongepowered.asm.mixin.injection.callback.LocalCapture;%0A
 import o
@@ -3542,32 +3542,423 @@
 sage());%0A    %7D%0A%0A
+    /**%0A     * Invoke before %3Ccode%3Ereturn entityplayermp1;%3C/code%3E (line 513 of source) to fire %7B@link org.spongepowered.api.event.entity.player.PlayerRespawnEvent%7D.%0A     * @param playerIn Injected player param%0A     * @param dimension Injected dimension param%0A     * @param conqueredEnd Injected conquered end flag%0A     * @param ci Info to provide mixin on how to handle the callback%0A     */%0A
     @Inject(meth
@@ -4044,16 +4044,19 @@
 erEntity
+End
 (EntityP
@@ -4167,118 +4167,41 @@
     
-// TODO Bed locations%0A        // TODO
+final
  Player
- r
+R
 espawn
- location%0A        Sponge.getGame().getEventManager().post(
+Event event = 
 Spon
@@ -4276,13 +4276,41 @@
 In, 
-false
+playerIn.getBedLocation() != null
 , ((
@@ -4341,16 +4341,140 @@
 ation())
+;%0A        Sponge.getGame().getEventManager().post(event);%0A        ((Player) playerIn).setLocation(event.getRespawnLocation()
 );%0A    %7D

Handle respawn position set in respawn as well as if its a bed.