3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-27 08:30:09 +01:00

Merge pull request #1036 from KennyTV/master

Remove some more unncessary/doubled map check
Dieser Commit ist enthalten in:
Myles 2018-10-03 17:12:47 +01:00 committet von GitHub
Commit 8bfcc550f7
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
22 geänderte Dateien mit 124 neuen und 117 gelöschten Zeilen

Datei anzeigen

@ -27,16 +27,19 @@ public class ProtocolDetectorService implements Runnable {
public static Integer getProtocolId(String serverName) { public static Integer getProtocolId(String serverName) {
// Step 1. Check Config // Step 1. Check Config
Map<String, Integer> servers = ((BungeeConfigAPI) Via.getConfig()).getBungeeServerProtocols(); Map<String, Integer> servers = ((BungeeConfigAPI) Via.getConfig()).getBungeeServerProtocols();
if (servers.containsKey(serverName)) { Integer protocol = servers.get(serverName);
return servers.get(serverName); if (protocol != null) {
return protocol;
} }
// Step 2. Check Detected // Step 2. Check Detected
if (detectedProtocolIds.containsKey(serverName)) { Integer detectedProtocol = detectedProtocolIds.get(serverName);
return detectedProtocolIds.get(serverName); if (detectedProtocol != null) {
return detectedProtocol;
} }
// Step 3. Use Default // Step 3. Use Default
if (servers.containsKey("default")) { Integer defaultProtocol = servers.get("default");
return servers.get("default"); if (defaultProtocol != null) {
return defaultProtocol;
} }
// Step 4: Use bungee lowest supported... *cries* // Step 4: Use bungee lowest supported... *cries*
return BungeeVersionProvider.getLowestSupportedVersion(); return BungeeVersionProvider.getLowestSupportedVersion();
@ -58,10 +61,9 @@ public class ProtocolDetectorService implements Runnable {
detectedProtocolIds.put(key, serverPing.getVersion().getProtocol()); detectedProtocolIds.put(key, serverPing.getVersion().getProtocol());
if (((BungeeConfigAPI) Via.getConfig()).isBungeePingSave()) { if (((BungeeConfigAPI) Via.getConfig()).isBungeePingSave()) {
Map<String, Integer> servers = ((BungeeConfigAPI) Via.getConfig()).getBungeeServerProtocols(); Map<String, Integer> servers = ((BungeeConfigAPI) Via.getConfig()).getBungeeServerProtocols();
if (servers.containsKey(key)) { Integer protocol = servers.get(key);
if (servers.get(key) == serverPing.getVersion().getProtocol()) { if (protocol != null && protocol == serverPing.getVersion().getProtocol()) {
return; return;
}
} }
// Ensure we're the only ones writing to the config // Ensure we're the only ones writing to the config
synchronized (Via.getPlatform().getConfigurationProvider()) { synchronized (Via.getPlatform().getConfigurationProvider()) {

Datei anzeigen

@ -18,15 +18,14 @@ public class ViaProviders {
} }
public <T extends Provider> void use(Class<T> provider, T value) { public <T extends Provider> void use(Class<T> provider, T value) {
if (lonelyProviders.contains(provider)) { lonelyProviders.remove(provider);
lonelyProviders.remove(provider);
}
providers.put(provider, value); providers.put(provider, value);
} }
public <T extends Provider> T get(Class<T> provider) { public <T extends Provider> T get(Class<T> provider) {
if (providers.containsKey(provider)) { Provider rawProvider = providers.get(provider);
return (T) providers.get(provider); if (rawProvider != null) {
return (T) rawProvider;
} else { } else {
if (lonelyProviders.contains(provider)) { if (lonelyProviders.contains(provider)) {
throw new IllegalStateException("There was no provider for " + provider + ", one is required!"); throw new IllegalStateException("There was no provider for " + provider + ", one is required!");

Datei anzeigen

@ -137,10 +137,8 @@ public abstract class Protocol {
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception { public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
Pair<State, Integer> statePacket = new Pair<>(state, packetWrapper.getId()); Pair<State, Integer> statePacket = new Pair<>(state, packetWrapper.getId());
Map<Pair<State, Integer>, ProtocolPacket> packetMap = (direction == Direction.OUTGOING ? outgoing : incoming); Map<Pair<State, Integer>, ProtocolPacket> packetMap = (direction == Direction.OUTGOING ? outgoing : incoming);
ProtocolPacket protocolPacket; ProtocolPacket protocolPacket = packetMap.get(statePacket);
if (packetMap.containsKey(statePacket)) { if (protocolPacket == null) {
protocolPacket = packetMap.get(statePacket);
} else {
return; return;
} }
// write packet id // write packet id

Datei anzeigen

@ -165,13 +165,14 @@ public class ProtocolRegistry {
if (current.size() > 50) return null; // Fail safe, protocol too complicated. if (current.size() > 50) return null; // Fail safe, protocol too complicated.
// First check if there is any protocols for this // First check if there is any protocols for this
if (!registryMap.containsKey(clientVersion)) { Map<Integer, Protocol> inputMap = registryMap.get(clientVersion);
if (inputMap == null) {
return null; // Not supported return null; // Not supported
} }
// Next check there isn't an obvious path // Next check there isn't an obvious path
Map<Integer, Protocol> inputMap = registryMap.get(clientVersion); Protocol protocol = inputMap.get(serverVersion);
if (inputMap.containsKey(serverVersion)) { if (protocol != null) {
current.add(new Pair<>(serverVersion, inputMap.get(serverVersion))); current.add(new Pair<>(serverVersion, protocol));
return current; // Easy solution return current; // Easy solution
} }
// There might be a more advanced solution... So we'll see if any of the others can get us there // There might be a more advanced solution... So we'll see if any of the others can get us there
@ -211,8 +212,9 @@ public class ProtocolRegistry {
public static List<Pair<Integer, Protocol>> getProtocolPath(int clientVersion, int serverVersion) { public static List<Pair<Integer, Protocol>> getProtocolPath(int clientVersion, int serverVersion) {
Pair<Integer, Integer> protocolKey = new Pair<>(clientVersion, serverVersion); Pair<Integer, Integer> protocolKey = new Pair<>(clientVersion, serverVersion);
// Check cache // Check cache
if (pathCache.containsKey(protocolKey)) { List<Pair<Integer, Protocol>> protocolList = pathCache.get(protocolKey);
return pathCache.get(protocolKey); if (protocolList != null) {
return protocolList;
} }
// Generate path // Generate path
List<Pair<Integer, Protocol>> outputPath = getProtocolPath(new ArrayList<Pair<Integer, Protocol>>(), clientVersion, serverVersion); List<Pair<Integer, Protocol>> outputPath = getProtocolPath(new ArrayList<Pair<Integer, Protocol>>(), clientVersion, serverVersion);

Datei anzeigen

@ -77,8 +77,9 @@ public class ProtocolVersion {
} }
public static ProtocolVersion getProtocol(int id) { public static ProtocolVersion getProtocol(int id) {
if (versions.containsKey(id)) { ProtocolVersion protocolVersion = versions.get(id);
return versions.get(id); if (protocolVersion != null) {
return protocolVersion;
} else { } else {
return new ProtocolVersion(id, "Unknown (" + id + ")"); return new ProtocolVersion(id, "Unknown (" + id + ")");
} }

Datei anzeigen

@ -42,8 +42,10 @@ public class BlockEntityRewriter {
} }
public static String toNewIdentifier(String oldId) { public static String toNewIdentifier(String oldId) {
if (oldToNewNames.containsKey(oldId)) String newName = oldToNewNames.get(oldId);
return oldToNewNames.get(oldId); if (newName != null) {
return newName;
}
return oldId; return oldId;
} }
} }

Datei anzeigen

@ -90,8 +90,9 @@ public class EntityIdRewriter {
public static void toClient(CompoundTag tag) { public static void toClient(CompoundTag tag) {
if (tag.get("id") instanceof StringTag) { if (tag.get("id") instanceof StringTag) {
StringTag id = tag.get("id"); StringTag id = tag.get("id");
if (oldToNewNames.containsKey(id.getValue())) { String newName = oldToNewNames.get(id.getValue());
id.setValue(oldToNewNames.get(id.getValue())); if (newName != null) {
id.setValue(newName);
} }
} }
} }
@ -117,8 +118,9 @@ public class EntityIdRewriter {
CompoundTag entityTag = item.getTag().get("EntityTag"); CompoundTag entityTag = item.getTag().get("EntityTag");
if (entityTag.get("id") instanceof StringTag) { if (entityTag.get("id") instanceof StringTag) {
StringTag id = entityTag.get("id"); StringTag id = entityTag.get("id");
if (oldToNewNames.inverse().containsKey(id.getValue())) { String newName = oldToNewNames.inverse().get(id.getValue());
id.setValue(oldToNewNames.inverse().get(id.getValue())); if (newName != null) {
id.setValue(newName);
} }
} }
} }

Datei anzeigen

@ -33,9 +33,7 @@ public class EntityTracker extends StoredObject {
} }
public Optional<Entity1_11Types.EntityType> get(int id) { public Optional<Entity1_11Types.EntityType> get(int id) {
if (!has(id)) return Optional.fromNullable(clientEntityTypes.get(id));
return Optional.absent();
return Optional.of(clientEntityTypes.get(id));
} }
public void addHologram(int entId) { public void addHologram(int entId) {

Datei anzeigen

@ -28,9 +28,7 @@ public class EntityTracker extends StoredObject {
} }
public Optional<Entity1_12Types.EntityType> get(int id) { public Optional<Entity1_12Types.EntityType> get(int id) {
if (!has(id)) return Optional.fromNullable(clientEntityTypes.get(id));
return Optional.absent();
return Optional.of(clientEntityTypes.get(id));
} }
} }

Datei anzeigen

@ -46,11 +46,14 @@ public class EntityNameRewriter {
} }
public static String rewrite(String entName) { public static String rewrite(String entName) {
if (entityNames.containsKey(entName)) String entityName = entityNames.get(entName);
return entityNames.get(entName); if (entityName != null) {
if (entityNames.containsKey("minecraft:" + entName)) return entityName;
return entityNames.get("minecraft:" + entName); }
else entityName = entityNames.get("minecraft:" + entName);
if (entityName != null) {
return entityName;
} else
return entName; return entName;
} }
} }

Datei anzeigen

@ -39,15 +39,15 @@ public class BlockEntityProvider implements Provider {
return -1; return -1;
String id = (String) tag.get("id").getValue(); String id = (String) tag.get("id").getValue();
BlockEntityHandler handler = handlers.get(id);
if (!handlers.containsKey(id)) { if (handler == null) {
if (Via.getManager().isDebug()) { if (Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Unhandled BlockEntity " + id + " full tag: " + tag); Via.getPlatform().getLogger().warning("Unhandled BlockEntity " + id + " full tag: " + tag);
} }
return -1; return -1;
} }
int newBlock = handlers.get(id).transform(user, tag); int newBlock = handler.transform(user, tag);
if (sendUpdate && newBlock != -1) if (sendUpdate && newBlock != -1)
sendBlockChange(user, position, newBlock); sendBlockChange(user, position, newBlock);

Datei anzeigen

@ -46,10 +46,6 @@ public class PaintingProvider implements Provider {
// Handle older versions // Handle older versions
if (!motive.startsWith("minecraft:")) if (!motive.startsWith("minecraft:"))
motive = "minecraft:" + motive.toLowerCase(); motive = "minecraft:" + motive.toLowerCase();
return Optional.fromNullable(paintings.get(motive));
if (paintings.containsKey(motive))
return Optional.of(paintings.get(motive));
return Optional.absent();
} }
} }

Datei anzeigen

@ -28,9 +28,7 @@ public class EntityTracker extends StoredObject {
} }
public Optional<Entity1_13Types.EntityType> get(int id) { public Optional<Entity1_13Types.EntityType> get(int id) {
if (!has(id)) return Optional.fromNullable(clientEntityTypes.get(id));
return Optional.absent();
return Optional.of(clientEntityTypes.get(id));
} }
} }

Datei anzeigen

@ -57,8 +57,9 @@ public class FakeTileEntity {
} }
public static CompoundTag getFromBlock(int x, int y, int z, int block) { public static CompoundTag getFromBlock(int x, int y, int z, int block) {
if (tileEntities.containsKey(block)) { CompoundTag originalTag = tileEntities.get(block);
CompoundTag tag = tileEntities.get(block).clone(); if (originalTag != null) {
CompoundTag tag = originalTag.clone();
tag.put(new IntTag("x", x)); tag.put(new IntTag("x", x));
tag.put(new IntTag("y", y)); tag.put(new IntTag("y", y));
tag.put(new IntTag("z", z)); tag.put(new IntTag("z", z));

Datei anzeigen

@ -230,8 +230,9 @@ public class ItemRewriter {
tag = new CompoundTag("tag"); tag = new CompoundTag("tag");
} }
CompoundTag entityTag = new CompoundTag("EntityTag"); CompoundTag entityTag = new CompoundTag("EntityTag");
if (ENTTIY_ID_TO_NAME.containsKey((int) item.getData())) { String entityName = ENTTIY_ID_TO_NAME.get((int) item.getData());
StringTag id = new StringTag("id", ENTTIY_ID_TO_NAME.get((int) item.getData())); if (entityName != null) {
StringTag id = new StringTag("id", entityName);
entityTag.put(id); entityTag.put(id);
tag.put(entityTag); tag.put(entityTag);
} }
@ -379,12 +380,14 @@ public class ItemRewriter {
if (oldID >= 16384) { if (oldID >= 16384) {
oldID -= 8192; oldID -= 8192;
} }
if (POTION_INDEX.containsKey(oldID)) {
return POTION_INDEX.get(oldID); Integer index = POTION_INDEX.get(oldID);
if (index != null) {
return index;
} }
oldID = POTION_NAME_TO_ID.get(potionNameFromDamage((short) oldID)); oldID = POTION_NAME_TO_ID.get(potionNameFromDamage((short) oldID));
return POTION_INDEX.containsKey(oldID) ? POTION_INDEX.get(oldID) : 0; return (index = POTION_INDEX.get(oldID)) != null ? index : 0;
} }
private static void registerEntity(Integer id, String name) { private static void registerEntity(Integer id, String name) {

Datei anzeigen

@ -173,11 +173,7 @@ public enum MetaIndex {
private static Optional<MetaIndex> getIndex(Entity1_10Types.EntityType type, int index) { private static Optional<MetaIndex> getIndex(Entity1_10Types.EntityType type, int index) {
Pair pair = new Pair<>(type, index); Pair pair = new Pair<>(type, index);
if (metadataRewrites.containsKey(pair)) { return Optional.fromNullable(metadataRewrites.get(pair));
return Optional.of(metadataRewrites.get(pair));
}
return Optional.absent();
} }
public static MetaIndex searchIndex(Entity1_10Types.EntityType type, int index) { public static MetaIndex searchIndex(Entity1_10Types.EntityType type, int index) {

Datei anzeigen

@ -5,6 +5,7 @@ import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Pair;
import us.myles.ViaVersion.api.Triple; import us.myles.ViaVersion.api.Triple;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.entities.Entity1_10Types;
import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.Protocol;
@ -184,8 +185,9 @@ public class EntityPackets {
List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
int entityID = wrapper.get(Type.VAR_INT, 0); int entityID = wrapper.get(Type.VAR_INT, 0);
EntityTracker tracker = wrapper.user().get(EntityTracker.class); EntityTracker tracker = wrapper.user().get(EntityTracker.class);
if (tracker.getClientEntityTypes().containsKey(entityID)) { Entity1_10Types.EntityType type = tracker.getClientEntityTypes().get(entityID);
MetadataRewriter.transform(tracker.getClientEntityTypes().get(entityID), metadataList); if (type != null) {
MetadataRewriter.transform(type, metadataList);
} else { } else {
// Buffer // Buffer
tracker.addMetadataToBuffer(entityID, metadataList); tracker.addMetadataToBuffer(entityID, metadataList);

Datei anzeigen

@ -214,8 +214,9 @@ public class SpawnPackets {
List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
int entityID = wrapper.get(Type.VAR_INT, 0); int entityID = wrapper.get(Type.VAR_INT, 0);
EntityTracker tracker = wrapper.user().get(EntityTracker.class); EntityTracker tracker = wrapper.user().get(EntityTracker.class);
if (tracker.getClientEntityTypes().containsKey(entityID)) { Entity1_10Types.EntityType type = tracker.getClientEntityTypes().get(entityID);
MetadataRewriter.transform(tracker.getClientEntityTypes().get(entityID), metadataList); if (type != null) {
MetadataRewriter.transform(type, metadataList);
} else { } else {
Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityID); Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityID);
metadataList.clear(); metadataList.clear();
@ -319,8 +320,9 @@ public class SpawnPackets {
List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
int entityID = wrapper.get(Type.VAR_INT, 0); int entityID = wrapper.get(Type.VAR_INT, 0);
EntityTracker tracker = wrapper.user().get(EntityTracker.class); EntityTracker tracker = wrapper.user().get(EntityTracker.class);
if (tracker.getClientEntityTypes().containsKey(entityID)) { Entity1_10Types.EntityType type = tracker.getClientEntityTypes().get(entityID);
MetadataRewriter.transform(tracker.getClientEntityTypes().get(entityID), metadataList); if (type != null) {
MetadataRewriter.transform(type, metadataList);
} else { } else {
Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityID); Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityID);
metadataList.clear(); metadataList.clear();

Datei anzeigen

@ -284,9 +284,7 @@ public enum SoundEffect {
public static SoundEffect getByName(String name) { public static SoundEffect getByName(String name) {
name = name.toLowerCase(); name = name.toLowerCase();
if (effects.containsKey(name)) return effects.get(name);
return effects.get(name);
return null;
} }
} }

Datei anzeigen

@ -25,8 +25,7 @@ public class CommandBlockStorage extends StoredObject {
public void unloadChunk(int x, int z) { public void unloadChunk(int x, int z) {
Pair<Integer, Integer> chunkPos = new Pair<>(x, z); Pair<Integer, Integer> chunkPos = new Pair<>(x, z);
if (storedCommandBlocks.containsKey(chunkPos)) storedCommandBlocks.remove(chunkPos);
storedCommandBlocks.remove(chunkPos);
} }
public void addOrUpdateBlock(Position position, CompoundTag tag) { public void addOrUpdateBlock(Position position, CompoundTag tag) {
@ -54,19 +53,18 @@ public class CommandBlockStorage extends StoredObject {
public Optional<CompoundTag> getCommandBlock(Position position) { public Optional<CompoundTag> getCommandBlock(Position position) {
Pair<Integer, Integer> chunkCoords = getChunkCoords(position); Pair<Integer, Integer> chunkCoords = getChunkCoords(position);
if (!storedCommandBlocks.containsKey(chunkCoords))
return Optional.absent();
Map<Position, CompoundTag> blocks = storedCommandBlocks.get(chunkCoords); Map<Position, CompoundTag> blocks = storedCommandBlocks.get(chunkCoords);
if (blocks == null)
if (!blocks.containsKey(position))
return Optional.absent(); return Optional.absent();
CompoundTag tag = blocks.get(position).clone(); CompoundTag tag = blocks.get(position);
if (tag == null)
return Optional.absent();
tag = tag.clone();
tag.put(new ByteTag("powered", (byte) 0)); tag.put(new ByteTag("powered", (byte) 0));
tag.put(new ByteTag("auto", (byte) 0)); tag.put(new ByteTag("auto", (byte) 0));
tag.put(new ByteTag("conditionMet", (byte) 0)); tag.put(new ByteTag("conditionMet", (byte) 0));
return Optional.of(tag); return Optional.of(tag);
} }

Datei anzeigen

@ -59,13 +59,13 @@ public class EntityTracker extends StoredObject {
} }
public UUID getEntityUUID(int id) { public UUID getEntityUUID(int id) {
if (uuidMap.containsKey(id)) { UUID uuid = uuidMap.get(id);
return uuidMap.get(id); if (uuid == null) {
} else { uuid = UUID.randomUUID();
UUID uuid = UUID.randomUUID();
uuidMap.put(id, uuid); uuidMap.put(id, uuid);
return uuid;
} }
return uuid;
} }
public void setSecondHand(Item item) { public void setSecondHand(Item item) {
@ -117,11 +117,11 @@ public class EntityTracker extends StoredObject {
} }
public void handleMetadata(int entityID, List<Metadata> metadataList) { public void handleMetadata(int entityID, List<Metadata> metadataList) {
if (!clientEntityTypes.containsKey(entityID)) { Entity1_10Types.EntityType type = clientEntityTypes.get(entityID);
if (type == null) {
return; return;
} }
Entity1_10Types.EntityType type = clientEntityTypes.get(entityID);
for (Metadata metadata : new ArrayList<>(metadataList)) { for (Metadata metadata : new ArrayList<>(metadataList)) {
// Fix: wither (crash fix) // Fix: wither (crash fix)
if (type == Entity1_10Types.EntityType.WITHER) { if (type == Entity1_10Types.EntityType.WITHER) {
@ -272,21 +272,23 @@ public class EntityTracker extends StoredObject {
} }
public void addMetadataToBuffer(int entityID, List<Metadata> metadataList) { public void addMetadataToBuffer(int entityID, List<Metadata> metadataList) {
if (metadataBuffer.containsKey(entityID)) { final List<Metadata> metadata = metadataBuffer.get(entityID);
metadataBuffer.get(entityID).addAll(metadataList); if (metadata != null) {
metadata.addAll(metadataList);
} else { } else {
metadataBuffer.put(entityID, metadataList); metadataBuffer.put(entityID, metadataList);
} }
} }
public void sendMetadataBuffer(int entityID) { public void sendMetadataBuffer(int entityID) {
if (metadataBuffer.containsKey(entityID)) { List<Metadata> metadataList = metadataBuffer.get(entityID);
if (metadataList != null) {
PacketWrapper wrapper = new PacketWrapper(0x39, null, getUser()); PacketWrapper wrapper = new PacketWrapper(0x39, null, getUser());
wrapper.write(Type.VAR_INT, entityID); wrapper.write(Type.VAR_INT, entityID);
wrapper.write(Types1_9.METADATA_LIST, metadataBuffer.get(entityID)); wrapper.write(Types1_9.METADATA_LIST, metadataList);
MetadataRewriter.transform(getClientEntityTypes().get(entityID), metadataBuffer.get(entityID)); MetadataRewriter.transform(getClientEntityTypes().get(entityID), metadataList);
handleMetadata(entityID, metadataBuffer.get(entityID)); handleMetadata(entityID, metadataList);
if (metadataBuffer.get(entityID).size() > 0) { if (metadataList.size() > 0) {
try { try {
wrapper.send(Protocol1_9TO1_8.class); wrapper.send(Protocol1_9TO1_8.class);
} catch (Exception e) { } catch (Exception e) {

Datei anzeigen

@ -14,7 +14,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ConcurrentSkipListMap;
public abstract class Config implements ConfigurationProvider { public abstract class Config implements ConfigurationProvider {
private static ThreadLocal<Yaml> yaml = new ThreadLocal<Yaml>() { private static final ThreadLocal<Yaml> YAML = new ThreadLocal<Yaml>() {
@Override @Override
protected Yaml initialValue() { protected Yaml initialValue() {
DumperOptions options = new DumperOptions(); DumperOptions options = new DumperOptions();
@ -58,7 +58,7 @@ public abstract class Config implements ConfigurationProvider {
Map<String, Object> config = null; Map<String, Object> config = null;
if (location.exists()) { if (location.exists()) {
try (FileInputStream input = new FileInputStream(location)) { try (FileInputStream input = new FileInputStream(location)) {
config = (Map<String, Object>) yaml.get().load(input); config = (Map<String, Object>) YAML.get().load(input);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
@ -71,7 +71,7 @@ public abstract class Config implements ConfigurationProvider {
Map<String, Object> defaults = config; Map<String, Object> defaults = config;
try (InputStream stream = jarConfigFile.openStream()) { try (InputStream stream = jarConfigFile.openStream()) {
defaults = (Map<String, Object>) yaml.get().load(stream); defaults = (Map<String, Object>) YAML.get().load(stream);
for (String option : unsupported) { for (String option : unsupported) {
defaults.remove(option); defaults.remove(option);
} }
@ -97,7 +97,7 @@ public abstract class Config implements ConfigurationProvider {
public synchronized void saveConfig(File location, Map<String, Object> config) { public synchronized void saveConfig(File location, Map<String, Object> config) {
try { try {
commentStore.writeComments(yaml.get().dump(config), location); commentStore.writeComments(YAML.get().dump(config), location);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -128,33 +128,37 @@ public abstract class Config implements ConfigurationProvider {
} }
public <T> T get(String key, Class<T> clazz, T def) { public <T> T get(String key, Class<T> clazz, T def) {
if (this.config.containsKey(key)) { Object o = this.config.get(key);
return (T) this.config.get(key); if (o != null) {
return (T) o;
} else { } else {
return def; return def;
} }
} }
public boolean getBoolean(String key, boolean def) { public boolean getBoolean(String key, boolean def) {
if (this.config.containsKey(key)) { Object o = this.config.get(key);
return (boolean) this.config.get(key); if (o != null) {
return (boolean) o;
} else { } else {
return def; return def;
} }
} }
public String getString(String key, String def) { public String getString(String key, String def) {
if (this.config.containsKey(key)) { final Object o = this.config.get(key);
return (String) this.config.get(key); if (o != null) {
return (String) o;
} else { } else {
return def; return def;
} }
} }
public int getInt(String key, int def) { public int getInt(String key, int def) {
if (this.config.containsKey(key)) { Object o = this.config.get(key);
if (this.config.get(key) instanceof Number) { if (o != null) {
return ((Number) this.config.get(key)).intValue(); if (o instanceof Number) {
return ((Number) o).intValue();
} else { } else {
return def; return def;
} }
@ -164,9 +168,10 @@ public abstract class Config implements ConfigurationProvider {
} }
public double getDouble(String key, double def) { public double getDouble(String key, double def) {
if (this.config.containsKey(key)) { Object o = this.config.get(key);
if (this.config.get(key) instanceof Number) { if (o != null) {
return ((Number) this.config.get(key)).doubleValue(); if (o instanceof Number) {
return ((Number) o).doubleValue();
} else { } else {
return def; return def;
} }
@ -176,8 +181,9 @@ public abstract class Config implements ConfigurationProvider {
} }
public List<Integer> getIntegerList(String key) { public List<Integer> getIntegerList(String key) {
if (this.config.containsKey(key)) { Object o = this.config.get(key);
return (List<Integer>) this.config.get(key); if (o != null) {
return (List<Integer>) o;
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }