mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-31 20:51:47 +01:00
Configuration methods .getX (int/double/etc) now try to cast existing values where possible. This fixes BUKKIT-290
By: Nathan Adams <dinnerbone@dinnerbone.com>
This commit is contained in:
parent
f6bfce4fa1
commit
efa01d0a28
@ -12,6 +12,7 @@ import java.util.regex.Pattern;
|
|||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.util.Vector;
|
import org.bukkit.util.Vector;
|
||||||
|
import static org.bukkit.util.NumberConversions.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A type of {@link ConfigurationSection} that is stored in memory.
|
* A type of {@link ConfigurationSection} that is stored in memory.
|
||||||
@ -315,7 +316,7 @@ public class MemorySection implements ConfigurationSection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Object def = getDefault(path);
|
Object def = getDefault(path);
|
||||||
return getInt(path, (def instanceof Integer) ? (Integer)def : 0);
|
return getInt(path, (def instanceof Number) ? toInt(def) : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getInt(String path, int def) {
|
public int getInt(String path, int def) {
|
||||||
@ -324,7 +325,7 @@ public class MemorySection implements ConfigurationSection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Object val = get(path, def);
|
Object val = get(path, def);
|
||||||
return (val instanceof Integer) ? (Integer)val : def;
|
return (val instanceof Number) ? toInt(val) : def;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isInt(String path) {
|
public boolean isInt(String path) {
|
||||||
@ -369,7 +370,7 @@ public class MemorySection implements ConfigurationSection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Object def = getDefault(path);
|
Object def = getDefault(path);
|
||||||
return getDouble(path, (def instanceof Double) ? (Double)def : 0);
|
return getDouble(path, (def instanceof Number) ? toDouble(def) : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getDouble(String path, double def) {
|
public double getDouble(String path, double def) {
|
||||||
@ -378,7 +379,7 @@ public class MemorySection implements ConfigurationSection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Object val = get(path, def);
|
Object val = get(path, def);
|
||||||
return (val instanceof Double) ? (Double)val : def;
|
return (val instanceof Number) ? toDouble(val) : def;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDouble(String path) {
|
public boolean isDouble(String path) {
|
||||||
@ -396,7 +397,7 @@ public class MemorySection implements ConfigurationSection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Object def = getDefault(path);
|
Object def = getDefault(path);
|
||||||
return getLong(path, (def instanceof Long) ? (Long)def : 0);
|
return getLong(path, (def instanceof Number) ? toLong(def) : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getLong(String path, long def) {
|
public long getLong(String path, long def) {
|
||||||
@ -405,7 +406,7 @@ public class MemorySection implements ConfigurationSection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Object val = get(path, def);
|
Object val = get(path, def);
|
||||||
return (val instanceof Long) ? (Long)val : def;
|
return (val instanceof Number) ? toLong(val) : def;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLong(String path) {
|
public boolean isLong(String path) {
|
||||||
|
@ -0,0 +1,92 @@
|
|||||||
|
package org.bukkit.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utils for casting number types to other number types
|
||||||
|
*/
|
||||||
|
public final class NumberConversions {
|
||||||
|
private NumberConversions() {}
|
||||||
|
|
||||||
|
public static int toInt(Object object) {
|
||||||
|
if (object instanceof Number) {
|
||||||
|
return ((Number)object).intValue();
|
||||||
|
} else {
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = Integer.valueOf((String)object);
|
||||||
|
} catch (Throwable ex) {}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float toFloat(Object object) {
|
||||||
|
if (object instanceof Number) {
|
||||||
|
return ((Number)object).floatValue();
|
||||||
|
} else {
|
||||||
|
float result = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = Float.valueOf((String)object);
|
||||||
|
} catch (Throwable ex) {}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double toDouble(Object object) {
|
||||||
|
if (object instanceof Number) {
|
||||||
|
return ((Number)object).doubleValue();
|
||||||
|
} else {
|
||||||
|
double result = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = Double.valueOf((String)object);
|
||||||
|
} catch (Throwable ex) {}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long toLong(Object object) {
|
||||||
|
if (object instanceof Number) {
|
||||||
|
return ((Number)object).longValue();
|
||||||
|
} else {
|
||||||
|
long result = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = Long.valueOf((String)object);
|
||||||
|
} catch (Throwable ex) {}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static short toShort(Object object) {
|
||||||
|
if (object instanceof Number) {
|
||||||
|
return ((Number)object).shortValue();
|
||||||
|
} else {
|
||||||
|
short result = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = Short.valueOf((String)object);
|
||||||
|
} catch (Throwable ex) {}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte toByte(Object object) {
|
||||||
|
if (object instanceof Number) {
|
||||||
|
return ((Number)object).byteValue();
|
||||||
|
} else {
|
||||||
|
byte result = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = Byte.valueOf((String)object);
|
||||||
|
} catch (Throwable ex) {}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -322,6 +322,18 @@ public abstract class ConfigurationSectionTest {
|
|||||||
assertNull(section.getString("doesntExist"));
|
assertNull(section.getString("doesntExist"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetDoubleFromInt() {
|
||||||
|
ConfigurationSection section = getConfigurationSection();
|
||||||
|
String key = "exists";
|
||||||
|
double value = 123;
|
||||||
|
|
||||||
|
section.set(key, (int)value);
|
||||||
|
|
||||||
|
assertEquals(value, section.getDouble(key), 1);
|
||||||
|
assertNull(section.getString("doesntExist"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetDouble_String_Double() {
|
public void testGetDouble_String_Double() {
|
||||||
ConfigurationSection section = getConfigurationSection();
|
ConfigurationSection section = getConfigurationSection();
|
||||||
|
Loading…
Reference in New Issue
Block a user