Search in sources :

Example 1 with ISoliniaNPCEventHandlerTypeAdapterFactory

use of com.solinia.solinia.Factories.ISoliniaNPCEventHandlerTypeAdapterFactory in project solinia3-core by mixxit.

the class JsonNPCRepository method reload.

@Override
public void reload() {
    List<ISoliniaNPC> file = new ArrayList<ISoliniaNPC>();
    try {
        GsonBuilder gsonbuilder = new GsonBuilder();
        gsonbuilder.registerTypeAdapterFactory(new ISoliniaNPCEventHandlerTypeAdapterFactory(SoliniaNPCEventHandler.class));
        Gson gson = gsonbuilder.create();
        BufferedReader br = new BufferedReader(new FileReader(filePath));
        file = gson.fromJson(br, new TypeToken<List<SoliniaNPC>>() {
        }.getType());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    NPCs.clear();
    for (ISoliniaNPC i : file) {
        if (i.getEventHandlers().size() > 0) {
            for (ISoliniaNPCEventHandler handler : i.getEventHandlers()) {
                if (handler.getNpcId() == 0) {
                    handler.setNpcId(i.getId());
                    System.out.println("Corrected invalid NPC event handler " + handler.getTriggerdata() + " on npc: " + i.getId());
                }
            }
        }
        NPCs.put(i.getId(), i);
    }
    System.out.println("Reloaded " + NPCs.size() + " NPCs");
}
Also used : ISoliniaNPCEventHandler(com.solinia.solinia.Interfaces.ISoliniaNPCEventHandler) SoliniaNPCEventHandler(com.solinia.solinia.Models.SoliniaNPCEventHandler) ISoliniaNPCEventHandler(com.solinia.solinia.Interfaces.ISoliniaNPCEventHandler) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Gson(com.google.gson.Gson) ISoliniaNPCEventHandlerTypeAdapterFactory(com.solinia.solinia.Factories.ISoliniaNPCEventHandlerTypeAdapterFactory) ISoliniaNPC(com.solinia.solinia.Interfaces.ISoliniaNPC) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ISoliniaNPCEventHandlerTypeAdapterFactory

use of com.solinia.solinia.Factories.ISoliniaNPCEventHandlerTypeAdapterFactory in project solinia3-core by mixxit.

the class JsonNPCRepository method commit.

@Override
public void commit() {
    // TODO Auto-generated method stub
    GsonBuilder gsonbuilder = new GsonBuilder();
    // gsonbuilder.setPrettyPrinting();
    gsonbuilder.registerTypeAdapterFactory(new ISoliniaNPCEventHandlerTypeAdapterFactory(SoliniaNPCEventHandler.class));
    Gson gson = gsonbuilder.create();
    String jsonOutput = gson.toJson(NPCs.values(), new TypeToken<List<SoliniaNPC>>() {
    }.getType());
    try {
        File file = new File(filePath);
        if (!file.exists())
            file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        OutputStreamWriter outWriter = new OutputStreamWriter(fileOut);
        outWriter.append(jsonOutput);
        outWriter.close();
        fileOut.close();
        System.out.println("Commited " + NPCs.size() + " NPCs");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : ISoliniaNPCEventHandler(com.solinia.solinia.Interfaces.ISoliniaNPCEventHandler) SoliniaNPCEventHandler(com.solinia.solinia.Models.SoliniaNPCEventHandler) GsonBuilder(com.google.gson.GsonBuilder) TypeToken(com.google.gson.reflect.TypeToken) ISoliniaNPC(com.solinia.solinia.Interfaces.ISoliniaNPC) SoliniaNPC(com.solinia.solinia.Models.SoliniaNPC) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) Gson(com.google.gson.Gson) ISoliniaNPCEventHandlerTypeAdapterFactory(com.solinia.solinia.Factories.ISoliniaNPCEventHandlerTypeAdapterFactory) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File)

Aggregations

Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 ISoliniaNPCEventHandlerTypeAdapterFactory (com.solinia.solinia.Factories.ISoliniaNPCEventHandlerTypeAdapterFactory)2 ISoliniaNPC (com.solinia.solinia.Interfaces.ISoliniaNPC)2 ISoliniaNPCEventHandler (com.solinia.solinia.Interfaces.ISoliniaNPCEventHandler)2 SoliniaNPCEventHandler (com.solinia.solinia.Models.SoliniaNPCEventHandler)2 FileNotFoundException (java.io.FileNotFoundException)2 TypeToken (com.google.gson.reflect.TypeToken)1 SoliniaNPC (com.solinia.solinia.Models.SoliniaNPC)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1