001package net.tnemc.item.component.helper;
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.JSONHelper;
022import org.json.simple.JSONObject;
023
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029/**
030 * BlockPredicate
031 *
032 * @author creatorfromhell
033 * @since 0.2.0.0
034 */
035
036public class BlockPredicate {
037
038  protected final List<String> blocks = new ArrayList<>();
039  protected final Map<String, String> state = new HashMap<>();
040
041  /**
042   * Converts this block predicate to a JSON object.
043   *
044   * @return The JSONObject representing this block predicate.
045   * @since 0.2.0.0
046   */
047  public JSONObject toJSON() {
048    final JSONObject json = new JSONObject();
049    json.put("blocks", blocks);
050
051    final JSONObject stateJson = new JSONObject();
052    for (final Map.Entry<String, String> entry : state.entrySet()) {
053      stateJson.put(entry.getKey(), entry.getValue());
054    }
055    json.put("state", stateJson);
056
057    return json;
058  }
059
060  /**
061   * Reads JSON data and populates this block predicate.
062   *
063   * @param json The JSONHelper instance of the JSON data.
064   * @since 0.2.0.0
065   */
066  public void readJSON(final JSONHelper json) {
067    blocks.clear();
068    blocks.addAll(json.getStringList("blocks"));
069
070    state.clear();
071    final JSONObject stateJson = json.getJSON("state");
072    if(stateJson != null) {
073      for (final Object key : stateJson.keySet()) {
074        state.put(key.toString(), stateJson.get(key).toString());
075      }
076    }
077  }
078
079  @Override
080  public boolean equals(final Object obj) {
081    if(!(obj instanceof final BlockPredicate other)) return false;
082
083    return blocks.equals(other.blocks) && state.equals(other.state);
084  }
085
086  @Override
087  public int hashCode() {
088    return blocks.hashCode() + state.hashCode();
089  }
090
091  @Override
092  public BlockPredicate clone() throws CloneNotSupportedException {
093
094    final BlockPredicate copy = new BlockPredicate();
095    copy.blocks.addAll(this.blocks);
096    copy.state.putAll(this.state);
097    return copy;
098  }
099}