001package net.tnemc.item.component.impl;
002/*
003 * The New Item Library
004 * Copyright (C) 2022 - 2025 Daniel "creatorfromhell" Vidmar
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020
021import net.tnemc.item.AbstractItemStack;
022import net.tnemc.item.JSONHelper;
023import net.tnemc.item.component.SerialComponent;
024import net.tnemc.item.platform.ItemPlatform;
025import org.json.simple.JSONArray;
026import org.json.simple.JSONObject;
027
028import java.util.ArrayList;
029import java.util.Arrays;
030import java.util.List;
031import java.util.Objects;
032
033/**
034 * WrittableBookContentComponent
035 *
036 * @author creatorfromhell
037 * @see <a href="https://minecraft.wiki/w/Data_component_format#writable_book_content">Reference</a>
038 * <p>
039 * @since 0.2.0.0
040 */
041public abstract class WritableBookContentComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
042
043  protected final List<String> pages = new ArrayList<>();
044
045  public WritableBookContentComponent() {
046
047  }
048
049  public WritableBookContentComponent(final List<String> pages) {
050
051    this.pages.addAll(pages);
052  }
053
054  public WritableBookContentComponent(final String... pages) {
055
056    this.pages.addAll(Arrays.asList(pages));
057  }
058
059  @Override
060  public String identifier() {
061    return "writable_book_content";
062  }
063
064  @Override
065  public JSONObject toJSON() {
066    final JSONObject json = new JSONObject();
067    final JSONArray pagesArray = new JSONArray();
068    pagesArray.addAll(pages);
069    json.put("pages", pagesArray);
070    return json;
071  }
072
073  @Override
074  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
075    pages.clear();
076    pages.addAll(json.getStringList("pages"));
077  }
078
079  @Override
080  public boolean similar(final SerialComponent<?, ?> component) {
081    if(!(component instanceof final WritableBookContentComponent<?, ?> other)) return false;
082    return Objects.equals(this.pages, other.pages);
083  }
084
085  @Override
086  public int hashCode() {
087    return Objects.hash(pages);
088  }
089
090  public List<String> pages() {
091
092    return pages;
093  }
094
095  public void pages(final List<String> pages) {
096
097    this.pages.clear();
098    this.pages.addAll(pages);
099  }
100
101  public void pages(final String... pages) {
102
103    this.pages.clear();
104    this.pages.addAll(Arrays.asList(pages));
105  }
106}