mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-26 13:53:07 +00:00
Multithread Market Saving + Record History for Markets
This commit is contained in:
1 parent
9e7c2d7e6a
commit
3507cb85c5
13 files changed
+172
-74
No files matched your search
@@ -6,11 +6,17 @@ import java.util.Map;
|
||||
|
||||
public class CacheHashMap<K, V extends CacheHashMap.CachedObject<?>> extends HashMap<K, V> {
|
||||
private final long maxCacheTime;
|
||||
private long saveInterval;
|
||||
|
||||
public CacheHashMap(long maxCacheTime) {
|
||||
this.maxCacheTime = maxCacheTime;
|
||||
}
|
||||
|
||||
public CacheHashMap(long maxCacheTime, long saveInterval) {
|
||||
this.maxCacheTime = maxCacheTime;
|
||||
this.saveInterval = saveInterval;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(Object key) {
|
||||
V object = super.get(key);
|
||||
@@ -19,9 +25,16 @@ public class CacheHashMap<K, V extends CacheHashMap.CachedObject<?>> extends Has
|
||||
Iterator<Map.Entry<K,V>> iterator = this.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<K,V> entry = iterator.next();
|
||||
long timeDiff = entry.getValue().timeSinceAccessed();
|
||||
if (timeDiff > this.maxCacheTime) {
|
||||
long lifeTimeDiff = entry.getValue().timeSinceAccessed();
|
||||
if (lifeTimeDiff > this.maxCacheTime) {
|
||||
entry.getValue().save();
|
||||
iterator.remove();
|
||||
} else if (this.saveInterval > 0) {
|
||||
long saveTimeDiff = entry.getValue().timeSinceSaved();
|
||||
if (saveTimeDiff > this.saveInterval) {
|
||||
entry.getValue().save();
|
||||
entry.getValue().updateSaveTime();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,22 +42,34 @@ public class CacheHashMap<K, V extends CacheHashMap.CachedObject<?>> extends Has
|
||||
}
|
||||
|
||||
public static class CachedObject<T> {
|
||||
private long timeSaved;
|
||||
private long lastTimeAccessed;
|
||||
private final T object;
|
||||
|
||||
public CachedObject(T object) {
|
||||
this.object = object;
|
||||
this.lastTimeAccessed = System.currentTimeMillis();
|
||||
this.timeSaved = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public long timeSinceAccessed() {
|
||||
return System.currentTimeMillis() - this.lastTimeAccessed;
|
||||
}
|
||||
|
||||
public long timeSinceSaved() {
|
||||
return System.currentTimeMillis() - this.timeSaved;
|
||||
}
|
||||
|
||||
public void updateTimeAccessed() {
|
||||
this.lastTimeAccessed = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public void updateSaveTime() {
|
||||
this.timeSaved = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public void save(){}
|
||||
|
||||
public T getObject() {
|
||||
return this.object;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@ package noppes.npcs.util;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
public class CustomNPCsThreader {
|
||||
public static final Executor playerDataThread = Executors.newSingleThreadExecutor();
|
||||
public static final Executor customNPCThread = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package noppes.npcs.util;
|
||||
|
||||
import foxz.utils.Market;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
public class MarketCachedObject extends CacheHashMap.CachedObject<NBTTagCompound> {
|
||||
public MarketCachedObject(NBTTagCompound object) {
|
||||
super(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
Market.saveFile(getObject());
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,10 @@ package noppes.npcs.util;
|
||||
import com.google.common.io.Files;
|
||||
import cpw.mods.fml.common.ObfuscationReflectionHelper;
|
||||
import net.minecraft.nbt.*;
|
||||
import noppes.npcs.LogWriter;
|
||||
import org.apache.commons.io.Charsets;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -211,7 +209,17 @@ public class NBTJsonUtil {
|
||||
}
|
||||
return json;
|
||||
}
|
||||
static class JsonLine{
|
||||
|
||||
public static NBTTagCompound loadNBTData(File file){
|
||||
try {
|
||||
return CompressedStreamTools.readCompressed(new FileInputStream(file));
|
||||
} catch (Exception e) {
|
||||
LogWriter.error("Error loading: " + file.getName(), e);
|
||||
}
|
||||
return new NBTTagCompound();
|
||||
}
|
||||
|
||||
static class JsonLine{
|
||||
private String line;
|
||||
public JsonLine(String line){
|
||||
this.line = line;
|
||||
|
||||
Reference in new issue
Block a user