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 * LodestoneTrackerComponent 031 * 032 * @author creatorfromhell 033 * @see <a href="https://minecraft.wiki/w/Data_component_format#lodestone_tracker">Reference</a> 034 * <p> 035 * @since 0.2.0.0 036 */ 037public abstract class LodestoneTrackerComponent<I extends AbstractItemStack<T>, T> implements SerialComponent<I, T> { 038 039 protected String target; 040 protected int[] pos = new int[3]; // x, y, z coordinates 041 protected String dimension; 042 protected boolean tracked = true; 043 044 public LodestoneTrackerComponent() { 045 046 } 047 048 public LodestoneTrackerComponent(final String target) { 049 050 this.target = target; 051 } 052 053 public LodestoneTrackerComponent(final String target, final int[] pos) { 054 055 this.target = target; 056 this.pos = pos; 057 } 058 059 public LodestoneTrackerComponent(final String target, final int[] pos, final String dimension) { 060 061 this.target = target; 062 this.pos = pos; 063 this.dimension = dimension; 064 } 065 066 public LodestoneTrackerComponent(final String target, final int[] pos, final String dimension, final boolean tracked) { 067 068 this.target = target; 069 this.pos = pos; 070 this.dimension = dimension; 071 this.tracked = tracked; 072 } 073 074 @Override 075 public String identifier() { 076 return "lodestone_tracker"; 077 } 078 079 @Override 080 public JSONObject toJSON() { 081 final JSONObject json = new JSONObject(); 082 json.put("target", target); 083 json.put("pos", pos); 084 json.put("dimension", dimension); 085 json.put("tracked", tracked); 086 return json; 087 } 088 089 @Override 090 public void readJSON(final JSONHelper json, final ItemPlatform<I, T, ?> platform) { 091 if(json.has("target")) target = json.getString("target"); 092 if(json.has("pos")) pos = json.getIntArray("pos"); 093 if(json.has("dimension")) dimension = json.getString("dimension"); 094 if(json.has("tracked")) tracked = json.getBoolean("tracked"); 095 } 096 097 @Override 098 public boolean similar(final SerialComponent<?, ?> component) { 099 if(!(component instanceof final LodestoneTrackerComponent<?, ?> other)) return false; 100 return Objects.equals(this.target, other.target) && 101 Objects.equals(this.pos, other.pos) && 102 Objects.equals(this.dimension, other.dimension) && 103 this.tracked == other.tracked; 104 } 105 106 @Override 107 public int hashCode() { 108 return Objects.hash(target, pos, dimension, tracked); 109 } 110 111 public String target() { 112 113 return target; 114 } 115 116 public void target(final String target) { 117 118 this.target = target; 119 } 120 121 public int[] pos() { 122 123 return pos; 124 } 125 126 public void pos(final int[] pos) { 127 128 this.pos = pos; 129 } 130 131 public String dimension() { 132 133 return dimension; 134 } 135 136 public void dimension(final String dimension) { 137 138 this.dimension = dimension; 139 } 140 141 public boolean tracked() { 142 143 return tracked; 144 } 145 146 public void tracked(final boolean tracked) { 147 148 this.tracked = tracked; 149 } 150}