Search in sources :

Example 86 with ItemStack

use of net.minecraft.server.v1_15_R1.ItemStack in project CitizensBooks by NicoNekoDev.

the class DistributionHandler method applyPlaceholders.

@SuppressWarnings({ "ConstantConditions", "unchecked" })
@Override
public ItemStack applyPlaceholders(Player player, ItemStack book, NPC npc) throws IllegalAccessException {
    Validate.notNull(book, "The ItemStack is null! This is not an error with CitizensBooks," + " so please don't report it. Make sure the plugins that uses CitizensBooks as dependency are correctly configured.");
    Validate.isTrue(book.getType() == Material.WRITTEN_BOOK, "The ItemStack is not a written book! This is not an error with CitizensBooks," + " so please don't report it. Make sure the plugins that uses CitizensBooks as dependency are correctly configured.");
    BookMeta bookMeta = (BookMeta) book.getItemMeta();
    List<String> pages = bookMeta.hasPages() ? super.papiReplaceStrList.apply(player, ((List<IChatBaseComponent>) this.pagesField.get(bookMeta)).stream().map(IChatBaseComponent.ChatSerializer::a).toList(), Optional.ofNullable(npc)) : new ArrayList<>();
    String author = bookMeta.hasAuthor() ? super.papiReplaceStr.apply(player, bookMeta.getAuthor(), Optional.ofNullable(npc)) : "Server";
    String title = bookMeta.hasTitle() ? super.papiReplaceStr.apply(player, bookMeta.getTitle(), Optional.ofNullable(npc)) : "Title";
    ItemStack newBook = new ItemStack(Material.WRITTEN_BOOK);
    bookMeta.setAuthor(author);
    bookMeta.setTitle(title);
    this.pagesField.set(bookMeta, pages.stream().map(IChatBaseComponent.ChatSerializer::a).toList());
    newBook.setItemMeta(bookMeta);
    return newBook;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) IChatBaseComponent(net.minecraft.server.v1_15_R1.IChatBaseComponent) ItemStack(org.bukkit.inventory.ItemStack) BookMeta(org.bukkit.inventory.meta.BookMeta)

Example 87 with ItemStack

use of net.minecraft.server.v1_15_R1.ItemStack in project CitizensBooks by NicoNekoDev.

the class DistributionHandler method convertJsonToBook.

@SuppressWarnings("ConstantConditions")
@Override
public ItemStack convertJsonToBook(JsonObject jsonBook) throws IllegalAccessException {
    ItemStack newBook = new ItemStack(Material.WRITTEN_BOOK);
    BookMeta bookMeta = (BookMeta) newBook.getItemMeta();
    JsonPrimitive jsonAuthor = jsonBook.getAsJsonPrimitive("author");
    JsonPrimitive jsonTitle = jsonBook.getAsJsonPrimitive("title");
    JsonArray jsonPages = jsonBook.getAsJsonArray("pages");
    bookMeta.setAuthor(jsonAuthor.isString() ? jsonAuthor.getAsString() : "Server");
    bookMeta.setTitle(jsonTitle.isString() ? jsonTitle.getAsString() : "Title");
    List<IChatBaseComponent> pages = new ArrayList<>();
    for (JsonElement jsonPage : jsonPages) {
        pages.add(IChatBaseComponent.ChatSerializer.a(jsonPage.toString()));
    }
    this.pagesField.set(bookMeta, pages);
    newBook.setItemMeta(bookMeta);
    return newBook;
}
Also used : ArrayList(java.util.ArrayList) IChatBaseComponent(net.minecraft.server.v1_15_R1.IChatBaseComponent) ItemStack(org.bukkit.inventory.ItemStack) BookMeta(org.bukkit.inventory.meta.BookMeta)

Example 88 with ItemStack

use of net.minecraft.server.v1_15_R1.ItemStack in project CitizensBooks by NicoNekoDev.

the class DistributionHandler method convertBookToJson.

@SuppressWarnings({ "ConstantConditions", "unchecked" })
@Override
public JsonObject convertBookToJson(ItemStack book) throws IllegalAccessException {
    BookMeta bookMeta = (BookMeta) book.getItemMeta();
    List<IChatBaseComponent> pages = bookMeta.hasPages() ? (List<IChatBaseComponent>) this.pagesField.get(bookMeta) : new ArrayList<>();
    JsonArray jsonPages = new JsonArray();
    for (IChatBaseComponent page : pages) {
        jsonPages.add(super.gson.fromJson(IChatBaseComponent.ChatSerializer.a(page), JsonElement.class));
    }
    JsonPrimitive jsonAuthor = new JsonPrimitive(bookMeta.hasAuthor() ? bookMeta.getAuthor() : "Server");
    JsonPrimitive jsonTitle = new JsonPrimitive(bookMeta.hasTitle() ? bookMeta.getTitle() : "Title");
    JsonObject jsonBook = new JsonObject();
    jsonBook.add("author", jsonAuthor);
    jsonBook.add("title", jsonTitle);
    jsonBook.add("pages", jsonPages);
    return jsonBook;
}
Also used : IChatBaseComponent(net.minecraft.server.v1_15_R1.IChatBaseComponent) BookMeta(org.bukkit.inventory.meta.BookMeta)

Example 89 with ItemStack

use of net.minecraft.server.v1_15_R1.ItemStack in project SilkSpawners by timbru31.

the class NMSHandler method setNBTEntityID.

@Override
public ItemStack setNBTEntityID(final ItemStack item, final String entity) {
    if (item == null || StringUtils.isBlank(entity)) {
        Bukkit.getLogger().warning("[SilkSpawners] Skipping invalid spawner to set NBT data on.");
        return null;
    }
    String prefixedEntity;
    if (!entity.startsWith("minecraft:")) {
        prefixedEntity = "minecraft:" + entity;
    } else {
        prefixedEntity = entity;
    }
    net.minecraft.server.v1_15_R1.ItemStack itemStack = null;
    final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
    itemStack = CraftItemStack.asNMSCopy(craftStack);
    NBTTagCompound tag = itemStack.getOrCreateTag();
    // Check for SilkSpawners key
    if (!tag.hasKey("SilkSpawners")) {
        tag.set("SilkSpawners", new NBTTagCompound());
    }
    tag.getCompound("SilkSpawners").setString("entity", entity);
    // Check for Vanilla keys
    if (!tag.hasKey("BlockEntityTag")) {
        tag.set("BlockEntityTag", new NBTTagCompound());
    }
    tag = tag.getCompound("BlockEntityTag");
    // EntityId - Deprecated in 1.9
    tag.setString("EntityId", entity);
    tag.setString("id", TileEntityTypes.a(TileEntityTypes.MOB_SPAWNER).getKey());
    // SpawnData
    if (!tag.hasKey("SpawnData")) {
        tag.set("SpawnData", new NBTTagCompound());
    }
    tag.getCompound("SpawnData").setString("id", prefixedEntity);
    if (!tag.hasKey("SpawnPotentials")) {
        tag.set("SpawnPotentials", new NBTTagCompound());
    }
    // SpawnEgg data
    if (!tag.hasKey("EntityTag")) {
        tag.set("EntityTag", new NBTTagCompound());
    }
    tag.getCompound("EntityTag").setString("id", prefixedEntity);
    return CraftItemStack.asCraftMirror(itemStack);
}
Also used : CraftItemStack(org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack) NBTTagCompound(net.minecraft.server.v1_15_R1.NBTTagCompound)

Example 90 with ItemStack

use of net.minecraft.server.v1_15_R1.ItemStack in project SilkSpawners by timbru31.

the class NMSHandler method getVanillaNBTEntityID.

@Override
@Nullable
public String getVanillaNBTEntityID(final ItemStack item) {
    net.minecraft.server.v1_15_R1.ItemStack itemStack = null;
    final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
    itemStack = CraftItemStack.asNMSCopy(craftStack);
    NBTTagCompound tag = itemStack.getTag();
    if (tag == null || !tag.hasKey("BlockEntityTag")) {
        return null;
    }
    tag = tag.getCompound("BlockEntityTag");
    if (tag.hasKey("EntityId")) {
        return tag.getString("EntityId");
    } else if (tag.hasKey("SpawnData") && tag.getCompound("SpawnData").hasKey("id")) {
        return tag.getCompound("SpawnData").getString("id");
    } else if (tag.hasKey("SpawnPotentials") && !tag.getList("SpawnPotentials", 8).isEmpty()) {
        return tag.getList("SpawnPotentials", 8).getCompound(0).getCompound("Entity").getString("id");
    } else {
        return null;
    }
}
Also used : CraftItemStack(org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack) NBTTagCompound(net.minecraft.server.v1_15_R1.NBTTagCompound) Nullable(javax.annotation.Nullable)

Aggregations

ItemStack (net.minecraft.server.v1_8_R3.ItemStack)26 ItemStack (net.minecraft.server.v1_12_R1.ItemStack)24 CraftItemStack (org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack)20 ItemStack (net.minecraft.server.v1_16_R3.ItemStack)19 IconMenuItem (de.Keyle.MyPet.api.gui.IconMenuItem)18 CraftItemStack (org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack)17 CraftItemStack (org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack)16 ItemStack (net.minecraft.server.v1_11_R1.ItemStack)13 ItemStack (net.minecraft.server.v1_7_R4.ItemStack)12 NBTTagCompound (net.minecraft.server.v1_12_R1.NBTTagCompound)11 CraftItemStack (org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack)11 ItemStack (net.minecraft.server.v1_10_R1.ItemStack)10 NBTTagCompound (net.minecraft.server.v1_8_R3.NBTTagCompound)10 TagCompound (de.keyle.knbt.TagCompound)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 NBTTagCompound (net.minecraft.server.v1_16_R3.NBTTagCompound)9 Field (java.lang.reflect.Field)8 ItemStack (net.minecraft.server.v1_13_R2.ItemStack)8 ItemStack (net.minecraft.server.v1_15_R1.ItemStack)8 NBTTagCompound (net.minecraft.server.v1_15_R1.NBTTagCompound)8