001package net.tnemc.item.component;
002
003/*
004 * The New Item Library
005 * Copyright (C) 2022 - 2025 Daniel "creatorfromhell" Vidmar
006 *
007 * This program is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 3 of the License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public License
018 * along with this program; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
020 */
021
022import net.tnemc.item.AbstractItemStack;
023import net.tnemc.item.JSONHelper;
024import net.tnemc.item.platform.ItemPlatform;
025import net.tnemc.item.platform.applier.ItemApplicator;
026import net.tnemc.item.platform.check.ItemCheck;
027import net.tnemc.item.platform.serialize.ItemSerializer;
028import org.json.simple.JSONObject;
029
030/**
031 * SerialComponent
032 *
033 * @author creatorfromhell
034 * @since 0.2.0.0
035 */
036public interface SerialComponent<I extends AbstractItemStack<T>, T> extends ItemCheck<T>, ItemApplicator<I, T>, ItemSerializer<I, T> {
037
038
039  /**
040   * Checks if this component applies to the specified item.
041   *
042   * @param item The item to check against.
043   * @return True if this component applies to the item, false otherwise.
044   * @since 0.2.0.0
045   */
046  boolean appliesTo(T item);
047
048  /**
049   * Converts the {@link SerialComponent} to a JSON object.
050   *
051   * @return The JSONObject representing this {@link SerialComponent}.
052   * @since 0.2.0.0
053   */
054  JSONObject toJSON();
055
056  /**
057   * Reads JSON data and converts it back to a {@link SerialComponent} object.
058   *
059   * @param json     The JSONHelper instance of the json data.
060   * @param platform The {@link ItemPlatform platform} instance.
061   * @since 0.2.0.0
062   */
063  void readJSON(JSONHelper json, ItemPlatform<I, T, ?> platform);
064
065  /**
066   * Checks if this component applies to the specified item stack.
067   *
068   * @param original the original item stack
069   * @param check the item stack to check against
070   * @return true if the check passes, false otherwise
071   * @since 0.2.0.0
072   */
073  default boolean applies(final AbstractItemStack<T> original, final AbstractItemStack<T> check) {
074
075    return original.components().containsKey(identifier()) || check.components().containsKey(identifier());
076  }
077
078  /**
079   * @param original the original stack
080   * @param check    the stack to use for the check
081   *
082   * @return True if the check passes, otherwise false.
083   * @since 0.2.0.0
084   */
085  @Override
086  default boolean check(final AbstractItemStack<T> original, final AbstractItemStack<T> check) {
087
088    System.out.println("Checking " + identifier());
089
090    System.out.println("Original contains: " + original.components().containsKey(identifier()));
091    System.out.println("check contains: " + check.components().containsKey(identifier()));
092
093    if(original.components().containsKey(identifier()) && check.components().containsKey(identifier())) {
094      System.out.println("Both stacks contain the check, doing equals");
095
096      final SerialComponent<?, ?> originalComponent = original.components().get(identifier());
097      final SerialComponent<?, ?> checkComponent = check.components().get(identifier());
098      //return originalComponent.equals(checkComponent);
099
100      return original.components().get(identifier()).similar(check.components().get(identifier()));
101    }
102
103    System.out.println("Both components do not contain the check, doing check to make sure neither have it.");
104    return !original.components().containsKey(identifier()) && !check.components().containsKey(identifier());
105  }
106
107  /**
108   *
109   * @param component The SerialComponent to compare for similarity.
110   * @return True if the components are similar, false otherwise.
111   */
112  boolean similar(final SerialComponent<?, ?> component);
113
114  /**
115   * Clones the current {@link SerialComponent} object.
116   * This method creates a deep copy of the current component, including all its properties and components.
117   *
118   * @return A new {@link SerialComponent} object that is a clone of the current component.
119   */
120  //SerialComponent<I, T> cloneComponent();
121}