Updated StatsSet to match newer branches.
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.l2jmobius.commons.util;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
/**
|
||||
* @author UnAfraid
|
||||
*/
|
||||
public class TimeUtil
|
||||
{
|
||||
private static int findIndexOfNonDigit(CharSequence text)
|
||||
{
|
||||
for (int i = 0; i < text.length(); i++)
|
||||
{
|
||||
if (Character.isDigit(text.charAt(i)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses patterns like:
|
||||
* <ul>
|
||||
* <li>1min or 10mins</li>
|
||||
* <li>1day or 10days</li>
|
||||
* <li>1week or 4weeks</li>
|
||||
* <li>1month or 12months</li>
|
||||
* <li>1year or 5years</li>
|
||||
* </ul>
|
||||
* @param datePattern
|
||||
* @return {@link Duration} object converted by the date pattern specified.
|
||||
* @throws IllegalStateException when malformed pattern specified.
|
||||
*/
|
||||
public static Duration parseDuration(String datePattern)
|
||||
{
|
||||
final int index = findIndexOfNonDigit(datePattern);
|
||||
if (index == -1)
|
||||
{
|
||||
throw new IllegalStateException("Incorrect time format given: " + datePattern);
|
||||
}
|
||||
try
|
||||
{
|
||||
final int val = Integer.parseInt(datePattern.substring(0, index));
|
||||
final String type = datePattern.substring(index);
|
||||
final ChronoUnit unit;
|
||||
switch (type.toLowerCase())
|
||||
{
|
||||
case "sec":
|
||||
case "secs":
|
||||
{
|
||||
unit = ChronoUnit.SECONDS;
|
||||
break;
|
||||
}
|
||||
case "min":
|
||||
case "mins":
|
||||
{
|
||||
unit = ChronoUnit.MINUTES;
|
||||
break;
|
||||
}
|
||||
case "hour":
|
||||
case "hours":
|
||||
{
|
||||
unit = ChronoUnit.HOURS;
|
||||
break;
|
||||
}
|
||||
case "day":
|
||||
case "days":
|
||||
{
|
||||
unit = ChronoUnit.DAYS;
|
||||
break;
|
||||
}
|
||||
case "week":
|
||||
case "weeks":
|
||||
{
|
||||
unit = ChronoUnit.WEEKS;
|
||||
break;
|
||||
}
|
||||
case "month":
|
||||
case "months":
|
||||
{
|
||||
unit = ChronoUnit.MONTHS;
|
||||
break;
|
||||
}
|
||||
case "year":
|
||||
case "years":
|
||||
{
|
||||
unit = ChronoUnit.YEARS;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
unit = ChronoUnit.valueOf(type);
|
||||
if (unit == null)
|
||||
{
|
||||
throw new IllegalStateException("Incorrect format: " + type + " !!");
|
||||
}
|
||||
}
|
||||
}
|
||||
return Duration.of(val, unit);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IllegalStateException("Incorrect time format given: " + datePattern + " val: " + datePattern.substring(0, index));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,14 +61,14 @@ public class GlobalVariablesManager extends AbstractVariables
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't restore global variables");
|
||||
LOGGER.warning(getClass().getSimpleName() + ": Couldn't restore global variables.");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
compareAndSetChanges(true, false);
|
||||
}
|
||||
LOGGER.log(Level.INFO, getClass().getSimpleName() + ": Loaded " + getSet().size() + " variables.");
|
||||
LOGGER.info(getClass().getSimpleName() + ": Loaded " + getSet().size() + " variables.");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -106,7 +106,23 @@ public class GlobalVariablesManager extends AbstractVariables
|
||||
{
|
||||
compareAndSetChanges(true, false);
|
||||
}
|
||||
LOGGER.log(Level.INFO, getClass().getSimpleName() + ": Stored " + getSet().size() + " variables.");
|
||||
LOGGER.info(getClass().getSimpleName() + ": Stored " + getSet().size() + " variables.");
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteMe()
|
||||
{
|
||||
try (Connection con = DatabaseFactory.getConnection();
|
||||
Statement del = con.createStatement())
|
||||
{
|
||||
del.execute(DELETE_QUERY);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't delete global variables to database.", e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,15 +16,19 @@
|
||||
*/
|
||||
package org.l2jmobius.gameserver.model;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.l2jmobius.commons.util.TimeUtil;
|
||||
import org.l2jmobius.gameserver.model.holders.MinionHolder;
|
||||
import org.l2jmobius.gameserver.model.holders.SkillHolder;
|
||||
import org.l2jmobius.gameserver.model.interfaces.IParserAdvUtils;
|
||||
@@ -39,13 +43,18 @@ public class StatsSet implements IParserAdvUtils
|
||||
{
|
||||
private static final Logger LOGGER = Logger.getLogger(StatsSet.class.getName());
|
||||
/** Static empty immutable map, used to avoid multiple null checks over the source. */
|
||||
public static final StatsSet EMPTY_STATSET = new StatsSet(Collections.<String, Object> emptyMap());
|
||||
public static final StatsSet EMPTY_STATSET = new StatsSet(Collections.emptyMap());
|
||||
|
||||
private final Map<String, Object> _set;
|
||||
|
||||
public StatsSet()
|
||||
{
|
||||
this(new LinkedHashMap<>());
|
||||
this(ConcurrentHashMap::new);
|
||||
}
|
||||
|
||||
public StatsSet(Supplier<Map<String, Object>> mapFactory)
|
||||
{
|
||||
this(mapFactory.get());
|
||||
}
|
||||
|
||||
public StatsSet(Map<String, Object> map)
|
||||
@@ -66,7 +75,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
* Add a set of couple values in the current set
|
||||
* @param newSet : StatsSet pointing out the list of couples to add in the current set
|
||||
*/
|
||||
public void add(StatsSet newSet)
|
||||
public void merge(StatsSet newSet)
|
||||
{
|
||||
_set.putAll(newSet.getSet());
|
||||
}
|
||||
@@ -89,6 +98,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@Override
|
||||
public boolean getBoolean(String key)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -117,6 +127,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@Override
|
||||
public boolean getBoolean(String key, boolean defaultValue)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -139,6 +150,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@Override
|
||||
public byte getByte(String key)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -161,6 +173,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@Override
|
||||
public byte getByte(String key, byte defaultValue)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -180,8 +193,24 @@ public class StatsSet implements IParserAdvUtils
|
||||
}
|
||||
}
|
||||
|
||||
public short increaseByte(String key, byte increaseWith)
|
||||
{
|
||||
final byte newValue = (byte) (getByte(key) + increaseWith);
|
||||
set(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
public short increaseByte(String key, byte defaultValue, byte increaseWith)
|
||||
{
|
||||
final byte newValue = (byte) (getByte(key, defaultValue) + increaseWith);
|
||||
set(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
public byte[] getByteArray(String key, String splitOn)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
Objects.requireNonNull(splitOn);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -213,6 +242,8 @@ public class StatsSet implements IParserAdvUtils
|
||||
|
||||
public List<Byte> getByteList(String key, String splitOn)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
Objects.requireNonNull(splitOn);
|
||||
final List<Byte> result = new ArrayList<>();
|
||||
for (Byte i : getByteArray(key, splitOn))
|
||||
{
|
||||
@@ -224,6 +255,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@Override
|
||||
public short getShort(String key)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -246,6 +278,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@Override
|
||||
public short getShort(String key, short defaultValue)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -265,9 +298,24 @@ public class StatsSet implements IParserAdvUtils
|
||||
}
|
||||
}
|
||||
|
||||
public short increaseShort(String key, short increaseWith)
|
||||
{
|
||||
final short newValue = (short) (getShort(key) + increaseWith);
|
||||
set(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
public short increaseShort(String key, short defaultValue, short increaseWith)
|
||||
{
|
||||
final short newValue = (short) (getShort(key, defaultValue) + increaseWith);
|
||||
set(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String key)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -292,6 +340,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@Override
|
||||
public int getInt(String key, int defaultValue)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -311,8 +360,24 @@ public class StatsSet implements IParserAdvUtils
|
||||
}
|
||||
}
|
||||
|
||||
public int increaseInt(String key, int increaseWith)
|
||||
{
|
||||
final int newValue = getInt(key) + increaseWith;
|
||||
set(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
public int increaseInt(String key, int defaultValue, int increaseWith)
|
||||
{
|
||||
final int newValue = getInt(key, defaultValue) + increaseWith;
|
||||
set(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
public int[] getIntArray(String key, String splitOn)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
Objects.requireNonNull(splitOn);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -344,6 +409,8 @@ public class StatsSet implements IParserAdvUtils
|
||||
|
||||
public List<Integer> getIntegerList(String key, String splitOn)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
Objects.requireNonNull(splitOn);
|
||||
final List<Integer> result = new ArrayList<>();
|
||||
for (int i : getIntArray(key, splitOn))
|
||||
{
|
||||
@@ -355,6 +422,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@Override
|
||||
public long getLong(String key)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -377,6 +445,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@Override
|
||||
public long getLong(String key, long defaultValue)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -396,9 +465,24 @@ public class StatsSet implements IParserAdvUtils
|
||||
}
|
||||
}
|
||||
|
||||
public long increaseLong(String key, long increaseWith)
|
||||
{
|
||||
final long newValue = getLong(key) + increaseWith;
|
||||
set(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
public long increaseLong(String key, long defaultValue, long increaseWith)
|
||||
{
|
||||
final long newValue = getLong(key, defaultValue) + increaseWith;
|
||||
set(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getFloat(String key)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -421,6 +505,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@Override
|
||||
public float getFloat(String key, float defaultValue)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -440,9 +525,24 @@ public class StatsSet implements IParserAdvUtils
|
||||
}
|
||||
}
|
||||
|
||||
public float increaseFloat(String key, float increaseWith)
|
||||
{
|
||||
final float newValue = getFloat(key) + increaseWith;
|
||||
set(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
public float increaseFloat(String key, float defaultValue, float increaseWith)
|
||||
{
|
||||
final float newValue = getFloat(key, defaultValue) + increaseWith;
|
||||
set(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(String key)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -465,6 +565,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@Override
|
||||
public double getDouble(String key, double defaultValue)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -484,9 +585,24 @@ public class StatsSet implements IParserAdvUtils
|
||||
}
|
||||
}
|
||||
|
||||
public double increaseDouble(String key, double increaseWith)
|
||||
{
|
||||
final double newValue = getDouble(key) + increaseWith;
|
||||
set(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
public double increaseDouble(String key, double defaultValue, double increaseWith)
|
||||
{
|
||||
final double newValue = getDouble(key, defaultValue) + increaseWith;
|
||||
set(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String key)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -498,6 +614,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@Override
|
||||
public String getString(String key, String defaultValue)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -506,10 +623,36 @@ public class StatsSet implements IParserAdvUtils
|
||||
return String.valueOf(val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Duration getDuration(String key)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
throw new IllegalArgumentException("String value required, but not specified");
|
||||
}
|
||||
return TimeUtil.parseDuration(String.valueOf(val));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Duration getDuration(String key, Duration defaultValue)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
return TimeUtil.parseDuration(String.valueOf(val));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Enum<T>> T getEnum(String key, Class<T> enumClass)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
Objects.requireNonNull(enumClass);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -533,6 +676,8 @@ public class StatsSet implements IParserAdvUtils
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Enum<T>> T getEnum(String key, Class<T> enumClass, T defaultValue)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
Objects.requireNonNull(enumClass);
|
||||
final Object val = _set.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
@@ -555,6 +700,8 @@ public class StatsSet implements IParserAdvUtils
|
||||
@SuppressWarnings("unchecked")
|
||||
public <A> A getObject(String name, Class<A> type)
|
||||
{
|
||||
Objects.requireNonNull(name);
|
||||
Objects.requireNonNull(type);
|
||||
final Object obj = _set.get(name);
|
||||
if ((obj == null) || !type.isAssignableFrom(obj.getClass()))
|
||||
{
|
||||
@@ -567,6 +714,8 @@ public class StatsSet implements IParserAdvUtils
|
||||
@SuppressWarnings("unchecked")
|
||||
public <A> A getObject(String name, Class<A> type, A defaultValue)
|
||||
{
|
||||
Objects.requireNonNull(name);
|
||||
Objects.requireNonNull(type);
|
||||
final Object obj = _set.get(name);
|
||||
if ((obj == null) || !type.isAssignableFrom(obj.getClass()))
|
||||
{
|
||||
@@ -578,6 +727,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
|
||||
public SkillHolder getSkillHolder(String key)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object obj = _set.get(key);
|
||||
if ((obj == null) || !(obj instanceof SkillHolder))
|
||||
{
|
||||
@@ -589,6 +739,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
|
||||
public Location getLocation(String key)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object obj = _set.get(key);
|
||||
if ((obj == null) || !(obj instanceof Location))
|
||||
{
|
||||
@@ -600,6 +751,7 @@ public class StatsSet implements IParserAdvUtils
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<MinionHolder> getMinionList(String key)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
final Object obj = _set.get(key);
|
||||
if ((obj == null) || !(obj instanceof List<?>))
|
||||
{
|
||||
@@ -612,6 +764,8 @@ public class StatsSet implements IParserAdvUtils
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> List<T> getList(String key, Class<T> clazz)
|
||||
{
|
||||
Objects.requireNonNull(key);
|
||||
Objects.requireNonNull(clazz);
|
||||
final Object obj = _set.get(key);
|
||||
if ((obj == null) || !(obj instanceof List<?>))
|
||||
{
|
||||
@@ -720,54 +874,83 @@ public class StatsSet implements IParserAdvUtils
|
||||
return (Map<K, V>) obj;
|
||||
}
|
||||
|
||||
public void set(String name, Object value)
|
||||
public StatsSet set(String name, Object value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
_set.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void set(String key, boolean value)
|
||||
public StatsSet set(String key, boolean value)
|
||||
{
|
||||
_set.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void set(String key, byte value)
|
||||
public StatsSet set(String key, byte value)
|
||||
{
|
||||
_set.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void set(String key, short value)
|
||||
public StatsSet set(String key, short value)
|
||||
{
|
||||
_set.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void set(String key, int value)
|
||||
public StatsSet set(String key, int value)
|
||||
{
|
||||
_set.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void set(String key, long value)
|
||||
public StatsSet set(String key, long value)
|
||||
{
|
||||
_set.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void set(String key, float value)
|
||||
public StatsSet set(String key, float value)
|
||||
{
|
||||
_set.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void set(String key, double value)
|
||||
public StatsSet set(String key, double value)
|
||||
{
|
||||
_set.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void set(String key, String value)
|
||||
public StatsSet set(String key, String value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
_set.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void set(String key, Enum<?> value)
|
||||
public StatsSet set(String key, Enum<?> value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
_set.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public static StatsSet valueOf(String key, Object value)
|
||||
{
|
||||
final StatsSet set = new StatsSet();
|
||||
set.set(key, value);
|
||||
return set;
|
||||
}
|
||||
|
||||
public void remove(String key)
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
*/
|
||||
package org.l2jmobius.gameserver.model.interfaces;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* More advanced interface for parsers.<br>
|
||||
* Allows usage of get methods without fall back value.<br>
|
||||
@@ -40,5 +42,7 @@ public interface IParserAdvUtils extends IParserUtils
|
||||
|
||||
String getString(String key);
|
||||
|
||||
Duration getDuration(String key);
|
||||
|
||||
<T extends Enum<T>> T getEnum(String key, Class<T> clazz);
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
*/
|
||||
package org.l2jmobius.gameserver.model.interfaces;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* Simple interface for parser, enforces of a fall back value.<br>
|
||||
* More suitable for developers not sure about their data.<br>
|
||||
@@ -39,5 +41,7 @@ public interface IParserUtils
|
||||
|
||||
String getString(String key, String defaultValue);
|
||||
|
||||
Duration getDuration(String key, Duration defaultValue);
|
||||
|
||||
<T extends Enum<T>> T getEnum(String key, Class<T> clazz, T defaultValue);
|
||||
}
|
||||
|
||||
@@ -16,63 +16,86 @@
|
||||
*/
|
||||
package org.l2jmobius.gameserver.model.variables;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.l2jmobius.gameserver.model.StatsSet;
|
||||
import org.l2jmobius.gameserver.model.interfaces.IDeletable;
|
||||
import org.l2jmobius.gameserver.model.interfaces.IRestorable;
|
||||
import org.l2jmobius.gameserver.model.interfaces.IStorable;
|
||||
|
||||
/**
|
||||
* @author UnAfraid
|
||||
*/
|
||||
public abstract class AbstractVariables extends StatsSet implements IRestorable, IStorable
|
||||
public abstract class AbstractVariables extends StatsSet implements IRestorable, IStorable, IDeletable
|
||||
{
|
||||
private final AtomicBoolean _hasChanges = new AtomicBoolean(false);
|
||||
|
||||
public AbstractVariables()
|
||||
{
|
||||
super(new ConcurrentHashMap<>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Overriding following methods to prevent from doing useless database operations if there is no changes since player's login.
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void set(String name, boolean value)
|
||||
public StatsSet set(String name, boolean value)
|
||||
{
|
||||
_hasChanges.compareAndSet(false, true);
|
||||
super.set(name, value);
|
||||
return super.set(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String name, double value)
|
||||
public StatsSet set(String name, double value)
|
||||
{
|
||||
_hasChanges.compareAndSet(false, true);
|
||||
super.set(name, value);
|
||||
return super.set(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String name, Enum<?> value)
|
||||
public StatsSet set(String name, Enum<?> value)
|
||||
{
|
||||
_hasChanges.compareAndSet(false, true);
|
||||
super.set(name, value);
|
||||
return super.set(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String name, int value)
|
||||
public StatsSet set(String name, int value)
|
||||
{
|
||||
_hasChanges.compareAndSet(false, true);
|
||||
super.set(name, value);
|
||||
return super.set(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String name, long value)
|
||||
public StatsSet set(String name, long value)
|
||||
{
|
||||
_hasChanges.compareAndSet(false, true);
|
||||
super.set(name, value);
|
||||
return super.set(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String name, String value)
|
||||
public StatsSet set(String name, String value)
|
||||
{
|
||||
_hasChanges.compareAndSet(false, true);
|
||||
super.set(name, value);
|
||||
return super.set(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put's entry to the variables and marks as changed if required (<i>Useful when restoring to do not save them again</i>).
|
||||
* @param name
|
||||
* @param value
|
||||
* @param markAsChanged
|
||||
* @return
|
||||
*/
|
||||
public StatsSet set(String name, String value, boolean markAsChanged)
|
||||
{
|
||||
if (markAsChanged)
|
||||
{
|
||||
_hasChanges.compareAndSet(false, true);
|
||||
}
|
||||
return super.set(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -116,4 +116,27 @@ public class AccountVariables extends AbstractVariables
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteMe()
|
||||
{
|
||||
try (Connection con = DatabaseFactory.getConnection())
|
||||
{
|
||||
// Clear previous entries.
|
||||
try (PreparedStatement st = con.prepareStatement(DELETE_QUERY))
|
||||
{
|
||||
st.setString(1, _accountName);
|
||||
st.execute();
|
||||
}
|
||||
|
||||
// Clear all entries
|
||||
getSet().clear();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't delete variables for: " + _accountName, e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,12 @@ public class NpcVariables extends AbstractVariables
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteMe()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the stored player.
|
||||
* @param name the name of the variable
|
||||
|
||||
@@ -119,6 +119,29 @@ public class PlayerVariables extends AbstractVariables
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteMe()
|
||||
{
|
||||
try (Connection con = DatabaseFactory.getConnection())
|
||||
{
|
||||
// Clear previous entries.
|
||||
try (PreparedStatement st = con.prepareStatement(DELETE_QUERY))
|
||||
{
|
||||
st.setInt(1, _objectId);
|
||||
st.execute();
|
||||
}
|
||||
|
||||
// Clear all entries
|
||||
getSet().clear();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't delete variables for: " + getPlayer(), e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public PlayerInstance getPlayer()
|
||||
{
|
||||
return World.getInstance().getPlayer(_objectId);
|
||||
|
||||
Reference in New Issue
Block a user