001package net.tnemc.item.persistent.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.persistent.PersistentDataType;
022
023import java.util.Arrays;
024
025/**
026 * PersistentIntArray
027 *
028 * @author creatorfromhell
029 * @since 0.2.0.0
030 */
031public class PersistentIntArray extends PersistentDataType<int[]> {
032
033  public PersistentIntArray(final String namespace, final String key) {
034
035    super(namespace, key);
036  }
037
038  /**
039   * Returns the type of the PersistentDataType.
040   *
041   * @return The type of the PersistentDataType as a String.
042   * @since 0.2.0.0
043   */
044  @Override
045  public String type() {
046
047    return "int-array";
048  }
049
050  /**
051   * Encodes the value of the PersistentDataType into a string representation.
052   *
053   * @return The encoded string representation of the value
054   * @since 0.2.0.0
055   */
056  @Override
057  public String encode() {
058
059    return Arrays.toString(value);
060  }
061
062  /**
063   * Decodes the given encoded string and sets the decoded value.
064   *
065   * @param encoded The string to be decoded
066   * @since 0.2.0.0
067   */
068  @Override
069  public int[] decode(final String encoded) throws IllegalArgumentException {
070
071    final String[] split = encoded.substring(1, encoded.length() - 1).split(", ");
072    final int[] decodedArray = new int[split.length];
073    for(int i = 0; i < split.length; i++) {
074      decodedArray[i] = Integer.parseInt(split[i]);
075    }
076
077    return decodedArray;
078  }
079}