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.List;
026
027/**
028 * ToolRule
029 *
030 * @author creatorfromhell
031 * @since 0.2.0.0
032 */
033public class ToolRule {
034
035  protected final List<String> materials = new ArrayList<>();
036
037  protected float speed;
038
039  protected boolean drops;
040
041  public ToolRule() {
042
043  }
044
045  public ToolRule(final float speed, final boolean drops) {
046
047    this.speed = speed;
048    this.drops = drops;
049  }
050
051  public JSONObject toJSON() {
052
053    final JSONObject rule = new JSONObject();
054    rule.put("speed", speed);
055    rule.put("drops", drops);
056
057    if(!materials.isEmpty()) {
058      final JSONObject materialsObj = new JSONObject();
059      for(int i = 0; i < materials.size(); i++) {
060        materialsObj.put(i, materials.get(i));
061      }
062      rule.put("materials", materialsObj);
063    }
064    return rule;
065  }
066
067  public static ToolRule readJSON(final JSONHelper json) {
068
069    final ToolRule rule = new ToolRule();
070
071    if(json.has("speed")) {
072      rule.setSpeed(json.getFloat("speed"));
073    }
074
075    if(json.has("drops")) {
076      rule.setDrops(json.getBoolean("drops"));
077    }
078
079    final List<String> materials = new ArrayList<>();
080    if(json.has("materials")) {
081      final JSONObject materialsObj = json.getJSON("materials");
082      materialsObj.forEach((ignore, value)->materials.add(String.valueOf(value)));
083    }
084    rule.setMaterials(materials);
085
086    return rule;
087  }
088
089  public List<String> getMaterials() {
090
091    return materials;
092  }
093
094  public void addMaterials(final List<String> materials) {
095
096    this.materials.addAll(materials);
097  }
098
099  public void setMaterials(final List<String> materials) {
100
101    this.materials.clear();
102    this.materials.addAll(materials);
103  }
104
105  public float getSpeed() {
106
107    return speed;
108  }
109
110  public void setSpeed(final float speed) {
111
112    this.speed = speed;
113  }
114
115  public boolean isDrops() {
116
117    return drops;
118  }
119
120  public void setDrops(final boolean drops) {
121
122    this.drops = drops;
123  }
124
125  @Override
126  public ToolRule clone() throws CloneNotSupportedException {
127
128    final ToolRule copy = new ToolRule();
129    copy.setSpeed(this.speed);
130    copy.setDrops(this.drops);
131    copy.setMaterials(new ArrayList<>(this.materials));
132    return copy;
133  }
134}