Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.osemu.cache;
- import com.typesafe.config.Config;
- import com.typesafe.config.ConfigFactory;
- import io.osemu.cache.definitions.CacheTypeDefinition;
- import io.osemu.cache.definitions.animation.AnimationTypeDefinition;
- import io.osemu.cache.definitions.item.ItemTypeDefinition;
- import io.osemu.cache.definitions.npc.NpcTypeDefinition;
- import lombok.Getter;
- import nl.bartpelle.dawnguard.DataStore;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- public class CacheRepository {
- public static void main(String[] args) throws IOException {
- CacheRepository repository = new CacheRepository();
- repository.initializeCache();
- System.out.println(repository.getItem(4151).toString());
- }
- /**
- * Configures the local cache for use
- */
- public CacheRepository() {
- this.configuration = ConfigFactory.systemProperties().withFallback(ConfigFactory.parseFileAnySyntax(new File("cache_configurations.conf")));
- this.dataStore = new DataStore(new File(getConfiguration().getString("filestore.location")));
- }
- /**
- * Initializes the cache for loading of definitions.
- * @return definition loading.
- */
- public CacheRepository initializeCache() {
- getLogger().info("Attempting to instantiate cache...");
- return storeDefinitions();
- }
- /**
- * Loads and stores the definitions for use
- */
- public CacheRepository storeDefinitions() {//434
- getDefinitions().put(ItemTypeDefinition.class, new ItemTypeDefinition[getDataStore().getIndex(2).getDescriptor().getLastFileId(10)]);
- getDefinitions().put(NpcTypeDefinition.class, new NpcTypeDefinition[getDataStore().getIndex(2).getDescriptor().getLastFileId(9)]);
- getDefinitions().put(AnimationTypeDefinition.class, new AnimationTypeDefinition[getDataStore().getIndex(2).getDescriptor().getLastFileId(12)]);
- getLogger().info("Successfully loaded {} item definitions.", total(ItemTypeDefinition.class));
- getLogger().info("Successfully loaded {} npc definitions.", total(NpcTypeDefinition.class));
- getLogger().info("Successfully loaded {} animation definitions.", total(AnimationTypeDefinition.class));
- return this;
- }
- /**
- * Loads the definition based on the type and id of the type argument.
- * @param type The {@link CacheTypeDefinition}.
- * @param id The id to link to the {@link CacheTypeDefinition}.
- * @return If no definition was found for reference.
- */
- private <T extends CacheTypeDefinition> T loadDefinition(Class<T> type, int id) {
- if (type == ItemTypeDefinition.class) return (T) new ItemTypeDefinition(id, getDataStore().getIndex(2).getContainer(10).getFileData(id, true, true));
- if (type == NpcTypeDefinition.class) return (T) new NpcTypeDefinition(id, getDataStore().getIndex(2).getContainer(9).getFileData(id, true, true));
- if (type == AnimationTypeDefinition.class) return (T) new AnimationTypeDefinition(id, getDataStore().getIndex(2).getContainer(12).getFileData(id, true, true));
- return null;
- }
- /**
- * Gathers the {@link CacheTypeDefinition} referenced
- * @param type The {@link CacheTypeDefinition}.
- * @param id The id to reference from the {@link CacheTypeDefinition}.
- * @return The definitions.
- */
- public <T extends CacheTypeDefinition> T get(Class<T> type, int id) {
- T[] arr = (T[]) getDefinitions().get(type);
- if (id < 0 || id >= arr.length) return null;
- if (arr[id] == null) return arr[id] = loadDefinition(type, id);
- return ((T[]) getDefinitions().get(type))[id];
- }
- /**
- * Returns the {@link ItemTypeDefinition} of the requested item
- * @param id The id of the requested item.
- * @return The {@link ItemTypeDefinition} of the item requested.
- */
- public <T extends CacheTypeDefinition> T getItem(int id) {
- Class<T> type = (Class<T>) ItemTypeDefinition.class;
- T[] arr = (T[]) getDefinitions().get(type);
- if (id < 0 || id >= arr.length) return null;
- if (arr[id] == null) return arr[id] = loadDefinition(type, id);
- return ((T[]) getDefinitions().get(type))[id];
- }
- public void unset(Class<? extends CacheTypeDefinition> type, int id) {
- Object[] arr = getDefinitions().get(type);
- arr[id] = null;
- }
- public int total(Class<? extends CacheTypeDefinition> type) {
- return getDefinitions().get(type).length;
- }
- @Getter
- private final Config configuration;
- @Getter
- private final DataStore dataStore;
- @Getter
- private Map<Class<? extends CacheTypeDefinition>, CacheTypeDefinition[]> definitions = new HashMap<>();
- @Getter
- private final Logger logger = LogManager.getLogger(this.getClass());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement