package org.cyclops.cyclopscore.persist.world;

import com.google.common.collect.Maps;
import org.cyclops.cyclopscore.init.ModBase;
import org.cyclops.cyclopscore.persist.nbt.NBTPersist;

import java.util.Map;

/**
 * Global counter that is shared over all dimensions, persisted, and consistent over server and clients.
 * @author rubensworks
 */
public class GlobalCounters extends WorldStorage {

    @NBTPersist
    private Map<String, Integer> counters = Maps.newHashMap();

    public GlobalCounters(ModBase mod) {
        super(mod);
    }

    /**
     * Get the next counter value for the given key.
     * @param key the key for the counter.
     * @return The next counter value.
     */
    public synchronized int getNext(String key) {
        int next = 0;
        if(counters.containsKey(key)) {
            next = counters.get(key);
        }
        counters.put(key, next + 1);
        return next;
    }

    @Override
    public void reset() {
        counters.clear();
    }

    @Override
    protected String getDataId() {
        return "GlobalCounterData";
    }

}

@@ -753,109 +753,334 @@
     
-int next = 0;%0A        if(counters.containsKey(key)) %7B%0A            next = counters.get(key);%0A        %7D
+// Get value from counter map%0A        Integer nextObject = counters.get(key);%0A        int next = 0;%0A        if (nextObject != null) %7B%0A            next = nextObject;%0A        %7D%0A%0A        // Handle overflows%0A        int incr = next + 1;%0A        if (incr %3C 0) %7B%0A            incr = 0;%0A        %7D%0A%0A        // Store value for next call
 %0A   
@@ -1106,19 +1106,16 @@
 ey, 
-next + 1
+incr
 );%0A
+%0A
     

Handle overflows in GlobalCounters