2019-04-01 10:30:21 +00:00
|
|
|
package com.sk89q.jnbt;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2019-07-06 00:46:48 +00:00
|
|
|
import net.jpountz.lz4.LZ4BlockInputStream;
|
2019-04-01 10:30:21 +00:00
|
|
|
|
|
|
|
public abstract class CompressedCompoundTag<T> extends CompoundTag {
|
|
|
|
private T in;
|
|
|
|
|
|
|
|
public CompressedCompoundTag(T in) {
|
|
|
|
super(new HashMap<>());
|
|
|
|
this.in = in;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Map<String, Tag> getValue() {
|
|
|
|
if (in != null) decompress();
|
|
|
|
return super.getValue();
|
|
|
|
}
|
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
public abstract LZ4BlockInputStream adapt(T src) throws IOException;
|
2019-04-01 10:30:21 +00:00
|
|
|
|
|
|
|
public T getSource() {
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void decompress() {
|
|
|
|
try (NBTInputStream nbtIn = new NBTInputStream(adapt(in))) {
|
|
|
|
in = null;
|
|
|
|
CompoundTag tag = (CompoundTag) nbtIn.readTag();
|
|
|
|
Map<String, Tag> value = tag.getValue();
|
|
|
|
Map<String, Tag> raw = super.getValue();
|
|
|
|
for (Map.Entry<String, Tag> entry : value.entrySet()) {
|
|
|
|
raw.put(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|