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.JSONObject;
026
027import java.util.Objects;
028
029/**
030 * WeaponComponent - as of MC Snapshot 25w02a
031 *
032 * @author creatorfromhell
033 * @see <a href="https://minecraft.wiki/w/Data_component_format#weapon">Reference</a>
034 * <p>
035 * @since 0.2.0.0
036 */
037public abstract class WeaponComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> {
038
039  protected int damagePerAttack = 1;
040  protected boolean canDisableBlocking = false;
041
042  public WeaponComponent() {
043
044  }
045
046  public WeaponComponent(final int damagePerAttack) {
047
048    this.damagePerAttack = damagePerAttack;
049  }
050
051  public WeaponComponent(final boolean canDisableBlocking) {
052
053    this.canDisableBlocking = canDisableBlocking;
054  }
055
056  public WeaponComponent(final int damagePerAttack, final boolean canDisableBlocking) {
057
058    this.damagePerAttack = damagePerAttack;
059    this.canDisableBlocking = canDisableBlocking;
060  }
061
062  /**
063   * @return the type of component this is.
064   * @since 0.2.0.0
065   */
066  @Override
067  public String identifier() {
068
069    return "weapon";
070  }
071
072  /**
073   * Converts the {@link SerialComponent} to a JSON object.
074   *
075   * @return The JSONObject representing this {@link SerialComponent}.
076   * @since 0.2.0.0
077   */
078  @Override
079  public JSONObject toJSON() {
080    final JSONObject json = new JSONObject();
081    json.put("damage_per_attack", damagePerAttack);
082    json.put("can_disable_blocking", canDisableBlocking);
083    return json;
084  }
085
086  @Override
087  public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) {
088    damagePerAttack = json.getInteger("damage_per_attack");
089    canDisableBlocking = json.getBoolean("can_disable_blocking");
090  }
091
092  @Override
093  public boolean similar(final SerialComponent<?, ?> component) {
094    if(!(component instanceof final WeaponComponent<?, ?> other)) return false;
095    return this.damagePerAttack == other.damagePerAttack && this.canDisableBlocking == other.canDisableBlocking;
096  }
097
098  @Override
099  public int hashCode() {
100    return Objects.hash(damagePerAttack, canDisableBlocking);
101  }
102
103  public int damagePerAttack() {
104
105    return damagePerAttack;
106  }
107
108  public void damagePerAttack(final int damagePerAttack) {
109
110    this.damagePerAttack = damagePerAttack;
111  }
112
113  public boolean canDisableBlocking() {
114
115    return canDisableBlocking;
116  }
117
118  public void canDisableBlocking(final boolean canDisableBlocking) {
119
120    this.canDisableBlocking = canDisableBlocking;
121  }
122}