package team.unstudio.udpl.util;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.BlockPosition;
import com.comphenix.protocol.wrappers.WrappedBlockData;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.nbt.NbtCompound;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;

/**
 * Created by trychen on 17/7/11.
 */
public class SignUtils {
	
    protected static String version = Bukkit.getServer().getBukkitVersion().substring(0, 3);
    private static ProtocolManager manager = ProtocolLibrary.getProtocolManager();
    private static PacketType BLOCK_CHANGE = PacketType.Play.Server.BLOCK_CHANGE;
    private static PacketType UPDATE_SIGN = PacketType.Play.Server.TILE_ENTITY_DATA;
    private static PacketType OPEN_SIGN_ENTITY = PacketType.Play.Server.OPEN_SIGN_EDITOR;

    public static Result open(Player player, String[] lines){
        Location loc = player.getLocation();
        int x = loc.getBlockX();
        int y = 0;
        int z = loc.getBlockZ();
        BlockPosition blockPosition = new BlockPosition(x, y, z);

        if (version.startsWith("1.10.")) {
            try {
                PacketContainer e = manager.createPacket(BLOCK_CHANGE);
                e.getBlockPositionModifier().write(0, blockPosition);
                e.getBlockData().write(0, WrappedBlockData.createData(Material.SIGN_POST));
                manager.sendServerPacket(player, e);
                e = manager.createPacket(UPDATE_SIGN);
                e.getBlockPositionModifier().write(0, blockPosition);
                e.getChatComponentArrays().write(0, new WrappedChatComponent[]{WrappedChatComponent.fromText(lines[0]), WrappedChatComponent.fromText(lines[1]), WrappedChatComponent.fromText(lines[2]), WrappedChatComponent.fromText(lines[3])});
                manager.sendServerPacket(player, e);
                e = manager.createPacket(OPEN_SIGN_ENTITY);
                e.getBlockPositionModifier().write(0, blockPosition);
                manager.sendServerPacket(player, e);
                return Result.success();
            } catch (Exception var11) {
            	return Result.failure(var11);
            }
        } else if (version.startsWith("1.8.")) {
            try {
                PacketContainer e = manager.createPacket(BLOCK_CHANGE);
                e.getBlockPositionModifier().write(0, blockPosition);
                e.getBlockData().write(0, WrappedBlockData.createData(Material.SIGN_POST));
                manager.sendServerPacket(player, e);
                e = manager.createPacket(PacketType.Play.Server.UPDATE_SIGN);
                e.getChatComponentArrays().write(0, new WrappedChatComponent[]{WrappedChatComponent.fromText(lines[0]), WrappedChatComponent.fromText(lines[1]), WrappedChatComponent.fromText(lines[2]), WrappedChatComponent.fromText(lines[3])});
                e.getBlockPositionModifier().write(0, blockPosition);
                manager.sendServerPacket(player, e);
                e = manager.createPacket(OPEN_SIGN_ENTITY);
                e.getBlockPositionModifier().write(0, blockPosition);
                manager.sendServerPacket(player, e);
                return Result.success();
            } catch (Exception var12) {
            	return Result.failure(var12);
            }
        }else {
            try {
                PacketContainer e = manager.createPacket(BLOCK_CHANGE);
                e.getBlockPositionModifier().write(0, blockPosition);
                e.getBlockData().write(0, WrappedBlockData.createData(Material.SIGN_POST));
                manager.sendServerPacket(player, e);
                e = manager.createPacket(UPDATE_SIGN);
                e.getBlockPositionModifier().write(0, blockPosition);
                e.getIntegers().write(0, Integer.valueOf(9));
                NbtCompound nbt = (NbtCompound) e.getNbtModifier().read(0);
                for (int i = 0; i < 4; i++) {
                    nbt.put("Text" + (i + 1), "{\"extra\":[{\"text\":\"" + lines[i] + "\"}],\"text\":\"\"}");
                }
                e.getNbtModifier().write(0, nbt);
                manager.sendServerPacket(player, e);
                e = manager.createPacket(OPEN_SIGN_ENTITY);
                e.getBlockPositionModifier().write(0, blockPosition);
                manager.sendServerPacket(player, e);
                return Result.success();
            } catch (Exception var12) {
            	return Result.failure(var12);
            }
        }
    }
}

@@ -461,35 +461,8 @@
 %0D%0A%0D%0A
-import org.bukkit.Bukkit;%0D%0A
 impo
@@ -603,16 +603,22 @@
 %0Apublic 
+final 
 class Si
@@ -641,15 +641,13 @@
   pr
-otec
+iva
 te
-d
  sta
@@ -671,60 +671,40 @@
 n = 
-Bukkit.getServer().getBukkitVersion().substring(0, 3
+ServerUtils.getMinecraftVersion(
 );%0D%0A
@@ -1046,16 +1046,49 @@
 EDITOR;%0D
+%0A    %0D%0A    private SignUtils()%7B%7D%0D
 %0A%0D%0A    p

Update SignUtils