package com.blocklaunch.blwarps.eventhandlers;

import com.blocklaunch.blwarps.BLWarps;
import com.blocklaunch.blwarps.Constants;
import com.blocklaunch.blwarps.Util;
import com.blocklaunch.blwarps.Warp;
import com.blocklaunch.blwarps.data.WarpData;
import com.blocklaunch.blwarps.data.WarpDataManipulatorBuilder;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataTransactionResult;
import org.spongepowered.api.data.value.mutable.ListValue;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.tileentity.ChangeSignEvent;
import org.spongepowered.api.text.Text;

import java.util.Optional;

public class ChangeSignEventHandler {

    private BLWarps plugin;

    public ChangeSignEventHandler(BLWarps plugin) {
        this.plugin = plugin;
    }

    @Listener
    public void signChange(ChangeSignEvent event) throws Exception {
        ListValue<Text> lines = event.getText().lines();

        // Check that the first line is [warp]
        if (lines.get(0).toPlain().equalsIgnoreCase(Constants.WARP_SIGN_PREFIX)) {
            // Second line has to be the name of the warp
            String warpName = lines.get(1).toPlain();
            Optional<Warp> optWarp = this.plugin.getWarpManager().getOne(warpName);
            if (!optWarp.isPresent()) {
                return;
            }
            event.getText().set(Util.generateWarpSignData(optWarp.get()).getValues());

            WarpDataManipulatorBuilder builder = (WarpDataManipulatorBuilder) Sponge.getDataManager().getManipulatorBuilder(WarpData.class).get();
            WarpData data = builder.createFrom(optWarp.get());
            DataTransactionResult result = event.getTargetTile().offer(data);
        }

    }

}

@@ -41,16 +41,128 @@
 dlers;%0A%0A
+import org.spongepowered.api.text.format.TextColors;%0A%0Aimport org.spongepowered.api.entity.living.player.Player;%0A
 import c
@@ -1833,16 +1833,735 @@
 (data);%0A
+%0A            if (!result.isSuccessful()) %7B%0A                // Couldn't offer WarpData to the sign - log in the console and%0A                // warn the possible player that placed the sign%0A                String error =%0A                        %22Failed to offer WarpData %22 + data.toContainer().toString() + %22 to the Sign at %22%0A                                + event.getTargetTile().getLocation().toString();%0A                plugin.getLogger().warn(error);%0A                Optional%3CPlayer%3E optPlayer = event.getCause().first(Player.class);%0A                if (optPlayer.isPresent()) %7B%0A                    optPlayer.get().sendMessage(Text.of(TextColors.RED, Constants.PREFIX, %22 %22, error));%0A                %7D%0A%0A            %7D%0A
         
@@ -2568,12 +2568,11 @@
 %7D%0A%0A    %7D
-%0A
 %0A%7D%0A

Add warning on failure to offer WarpData