package me.ryanhamshire.GriefPrevention;

import org.spongepowered.api.entity.living.player.Player;

public class WelcomeTask implements Runnable {

    private Player player;

    public WelcomeTask(Player player) {
        this.player = player;
    }

    @Override
    public void run() {
        // abort if player has logged out since this task was scheduled
        if (!this.player.isOnline())
            return;

        // offer advice and a helpful link
        GriefPrevention.sendMessage(player, TextMode.Instr, Messages.AvoidGriefClaimLand);
        GriefPrevention.sendMessage(player, TextMode.Instr, Messages.SurvivalBasicsVideo2, DataStore.SURVIVAL_VIDEO_URL);

        // give the player a reference book for later
        if (GriefPrevention.instance.config_claims_supplyPlayerManual) {
            ItemFactory factory = Bukkit.getItemFactory();
            BookMeta meta = (BookMeta) factory.getItemMeta(Material.WRITTEN_BOOK);

            DataStore datastore = GriefPrevention.instance.dataStore;
            meta.setAuthor(datastore.getMessage(Messages.BookAuthor));
            meta.setTitle(datastore.getMessage(Messages.BookTitle));

            StringBuilder page1 = new StringBuilder();
            String URL = datastore.getMessage(Messages.BookLink, DataStore.SURVIVAL_VIDEO_URL);
            String intro = datastore.getMessage(Messages.BookIntro);

            page1.append(URL).append("\n\n");
            page1.append(intro).append("\n\n");
            String editToolName = GriefPrevention.instance.config_claims_modificationTool.name().replace('_', ' ').toLowerCase();
            String infoToolName = GriefPrevention.instance.config_claims_investigationTool.name().replace('_', ' ').toLowerCase();
            String configClaimTools = datastore.getMessage(Messages.BookTools, editToolName, infoToolName);
            page1.append(configClaimTools);
            if (GriefPrevention.instance.config_claims_automaticClaimsForNewPlayersRadius < 0) {
                page1.append(datastore.getMessage(Messages.BookDisabledChestClaims));
            }

            StringBuilder page2 = new StringBuilder(datastore.getMessage(Messages.BookUsefulCommands)).append("\n\n");
            page2.append("/Trust /UnTrust /TrustList\n");
            page2.append("/ClaimsList\n");
            page2.append("/AbandonClaim\n\n");

            page2.append("/IgnorePlayer\n\n");

            page2.append("/SubdivideClaims\n");
            page2.append("/AccessTrust\n");
            page2.append("/ContainerTrust\n");
            page2.append("/PermissionTrust");

            meta.setPages(page1.toString(), page2.toString());

            ItemStack item = new ItemStack(Material.WRITTEN_BOOK);
            item.setItemMeta(meta);
            player.getInventory().addItem(item);
        }

    }

}

@@ -68,35 +68,534 @@
 api.
-entity.living.player.Player
+data.key.Keys;%0Aimport org.spongepowered.api.data.manipulator.mutable.DisplayNameData;%0Aimport org.spongepowered.api.data.manipulator.mutable.item.AuthorData;%0Aimport org.spongepowered.api.data.manipulator.mutable.item.PagedData;%0Aimport org.spongepowered.api.entity.living.player.Player;%0Aimport org.spongepowered.api.item.ItemTypes;%0Aimport org.spongepowered.api.item.inventory.ItemStack;%0Aimport org.spongepowered.api.item.inventory.ItemStackBuilder;%0Aimport org.spongepowered.api.text.Text;%0Aimport org.spongepowered.api.text.Texts
 ;%0A%0Ap
@@ -653,16 +653,22 @@
 private 
+final 
 Player p
@@ -1324,231 +1324,411 @@
 Item
-Factory factory = Bukkit.getItemFactory();%0A            BookMeta meta = (BookMeta) factory.getItemMeta(Material.WRITTEN_BOOK);%0A%0A            DataStore datastore = GriefPrevention.instance.dataStore;%0A            meta.setAuthor
+StackBuilder factory = GriefPrevention.instance.game.getRegistry().createItemBuilder();%0A            DataStore datastore = GriefPrevention.instance.dataStore;%0A            final ItemStack itemStack = factory.itemType(ItemTypes.WRITTEN_BOOK).quantity(1).build();%0A%0A            final AuthorData authorData = itemStack.getOrCreate(AuthorData.class).get();%0A            authorData.set(Keys.BOOK_AUTHOR, Texts.of
 (dat
@@ -1766,16 +1766,17 @@
 Author))
+)
 ;%0A      
@@ -1785,64 +1785,356 @@
     
-meta.setTitle(datastore.getMessage(Messages.BookTitle));
+itemStack.offer(authorData);%0A%0A            final DisplayNameData displayNameData = itemStack.getOrCreate(DisplayNameData.class).get();%0A            displayNameData.set(Keys.DISPLAY_NAME, Texts.of(datastore.getMessage(Messages.BookTitle)));%0A            displayNameData.set(Keys.SHOWS_DISPLAY_NAME, true);%0A            itemStack.offer(displayNameData);%0A
 %0A%0A  
@@ -2528,33 +2528,36 @@
 odificationTool.
-n
+getN
 ame().replace('_
@@ -2670,17 +2670,20 @@
 ionTool.
-n
+getN
 ame().re
@@ -3568,40 +3568,90 @@
 %22);%0A
-%0A            meta.setPages
+            try %7B%0A                final Text page2Text = Texts.legacy().from
 (page
-1
+2
 .toS
@@ -3661,179 +3661,435 @@
 ng()
-, page2.toString());%0A%0A            ItemStack item = new ItemStack(Material.WRITTEN_BOOK);%0A            item.setItemMeta(meta);%0A            player.getInventory().addItem(item
+);%0A                final Text page1Text = Texts.legacy().from(page1.toString());%0A%0A                final PagedData pagedData = itemStack.getOrCreate(PagedData.class).get();%0A                pagedData.set(pagedData.pages().add(page1Text).add(page2Text));%0A                itemStack.offer(pagedData);%0A%0A            %7D catch (Exception e) %7B%0A                e.printStackTrace();%0A            %7D%0A%0A            player.setItemInHand(itemStack
 );%0A 

Make the WelcomeTask work with Data API.