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 * WrittenBookContentComponent 035 * 036 * @author creatorfromhell 037 * @see <a href="https://minecraft.wiki/w/Data_component_format#">Reference</a> 038 * <p> 039 * @since 0.2.0.0 040 */ 041public abstract class WrittenBookContentComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 042 043 protected final List<String> pages = new ArrayList<>(); 044 protected String title; 045 protected String author; 046 protected int generation; 047 protected boolean resolved; 048 049 public WrittenBookContentComponent() { 050 051 } 052 053 public WrittenBookContentComponent(final String title) { 054 055 this.title = title; 056 } 057 058 public WrittenBookContentComponent(final String title, final String author) { 059 060 this.title = title; 061 this.author = author; 062 } 063 064 public WrittenBookContentComponent(final String title, final String author, final int generation) { 065 066 this.title = title; 067 this.author = author; 068 this.generation = generation; 069 } 070 071 public WrittenBookContentComponent(final String title, final String author, final int generation, 072 final boolean resolved) { 073 074 this.title = title; 075 this.author = author; 076 this.generation = generation; 077 this.resolved = resolved; 078 } 079 080 public WrittenBookContentComponent(final String title, final String author, final int generation, 081 final boolean resolved, final List<String> pages) { 082 083 this.title = title; 084 this.author = author; 085 this.generation = generation; 086 this.resolved = resolved; 087 this.pages.addAll(pages); 088 } 089 090 @Override 091 public String identifier() { 092 return "written_book_content"; 093 } 094 095 @Override 096 public JSONObject toJSON() { 097 final JSONObject json = new JSONObject(); 098 final JSONArray pagesArray = new JSONArray(); 099 pagesArray.addAll(pages); 100 json.put("pages", pagesArray); 101 json.put("title", title); 102 json.put("author", author); 103 json.put("generation", generation); 104 json.put("resolved", resolved); 105 return json; 106 } 107 108 @Override 109 public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) { 110 pages.clear(); 111 pages.addAll(json.getStringList("pages")); 112 if(json.has("title")) title = json.getString("title"); 113 if(json.has("author")) author = json.getString("author"); 114 if(json.has("generation")) generation = json.getInteger("generation"); 115 if(json.has("resolved")) resolved = json.getBoolean("resolved"); 116 } 117 118 @Override 119 public boolean similar(final SerialComponent<?, ?> component) { 120 if(!(component instanceof final WrittenBookContentComponent<?, ?> other)) return false; 121 return Objects.equals(this.pages, other.pages) && 122 Objects.equals(this.title, other.title) && 123 Objects.equals(this.author, other.author) && 124 this.generation == other.generation && 125 this.resolved == other.resolved; 126 } 127 128 @Override 129 public int hashCode() { 130 return Objects.hash(pages, title, author, generation, resolved); 131 } 132 133 public List<String> pages() { 134 135 return pages; 136 } 137 138 public void pages(final List<String> pages) { 139 140 this.pages.clear(); 141 this.pages.addAll(pages); 142 } 143 144 public void pages(final String... pages) { 145 146 this.pages.clear(); 147 this.pages.addAll(Arrays.asList(pages)); 148 } 149 150 public String title() { 151 152 return title; 153 } 154 155 public void title(final String title) { 156 157 this.title = title; 158 } 159 160 public String author() { 161 162 return author; 163 } 164 165 public void author(final String author) { 166 167 this.author = author; 168 } 169 170 public int generation() { 171 172 return generation; 173 } 174 175 public void generation(final int generation) { 176 177 this.generation = generation; 178 } 179 180 public boolean resolved() { 181 182 return resolved; 183 } 184 185 public void resolved(final boolean resolved) { 186 187 this.resolved = resolved; 188 } 189}