package io.musician101.musicianlibrary.java.minecraft.sponge.gui.anvil;

import java.util.function.BiConsumer;
import javax.annotation.Nonnull;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.scheduler.Task;

public class SpongeJumpToPage<P extends PluginContainer> extends SpongeAnvilGUI {

    public SpongeJumpToPage(@Nonnull P plugin, @Nonnull Player player, int maxPage, @Nonnull BiConsumer<Player, Integer> biConsumer) {
        super(player, (p, name) -> {
            int page;
            try {
                page = Integer.parseInt(name);
            }
            catch (NumberFormatException e) {
                return "That is not a number!";
            }

            if (page > maxPage) {
                return "Page cannot exceed " + maxPage;
            }

            Task.builder().delayTicks(1L).execute(() -> biConsumer.accept(player, page)).submit(plugin.getInstance().get());
            return null;
        });
    }
}

@@ -66,16 +66,43 @@
 anvil;%0A%0A
+import java.util.Optional;%0A
 import j
@@ -127,24 +127,24 @@
 BiConsumer;%0A
-
 import javax
@@ -319,16 +319,109 @@
 er.Task;
+%0Aimport org.spongepowered.api.text.Text;%0Aimport org.spongepowered.api.text.format.TextColors;
 %0A%0Apublic
@@ -1000,120 +1000,466 @@
     
-Task.builder().delayTicks(1L).execute(() -%3E biConsumer.accept(player, page)).submit(plugin.getInstance().get());
+Optional instance = plugin.getInstance();%0A            if (instance.isPresent()) %7B%0A                Task.builder().delayTicks(1L).execute(() -%3E biConsumer.accept(player, page)).submit(instance.get());%0A            %7D%0A            else %7B%0A                player.closeInventory();%0A                player.sendMessage(Text.builder(plugin.getId() + %22 does not have a valid instance. This GUI will not work until it does.%22).color(TextColors.RED).build());%0A            %7D%0A
 %0A   

Check if the PluginContainer contains an instance of the plugin.