From 0d87f2c39d1b5c9dcecea45f5136e5b2db792061 Mon Sep 17 00:00:00 2001 From: Steveice10 Date: Fri, 16 Mar 2012 22:16:56 -0700 Subject: [PATCH] Added TagBuilder. --- .../me/steveice10/opennbt/TagBuilder.java | 140 ++++++++++++++++++ .../opennbt/utils/DoubleIntHashMap.java | 41 +++++ 2 files changed, 181 insertions(+) create mode 100644 src/main/java/me/steveice10/opennbt/TagBuilder.java create mode 100644 src/main/java/me/steveice10/opennbt/utils/DoubleIntHashMap.java diff --git a/src/main/java/me/steveice10/opennbt/TagBuilder.java b/src/main/java/me/steveice10/opennbt/TagBuilder.java new file mode 100644 index 0000000..75be5a7 --- /dev/null +++ b/src/main/java/me/steveice10/opennbt/TagBuilder.java @@ -0,0 +1,140 @@ +package me.steveice10.opennbt; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import me.steveice10.opennbt.stream.NBTOutputStream; +import me.steveice10.opennbt.tag.ByteArrayTag; +import me.steveice10.opennbt.tag.ByteTag; +import me.steveice10.opennbt.tag.CompoundTag; +import me.steveice10.opennbt.tag.DoubleTag; +import me.steveice10.opennbt.tag.EndTag; +import me.steveice10.opennbt.tag.FloatTag; +import me.steveice10.opennbt.tag.IntArrayTag; +import me.steveice10.opennbt.tag.IntTag; +import me.steveice10.opennbt.tag.ListTag; +import me.steveice10.opennbt.tag.LongTag; +import me.steveice10.opennbt.tag.ShortTag; +import me.steveice10.opennbt.tag.StringTag; +import me.steveice10.opennbt.tag.Tag; + +public class TagBuilder { + + private String name; + private List tags = new ArrayList(); + + public TagBuilder() { + this(""); + } + + public TagBuilder(String name) { + this.name = name; + } + + public TagBuilder append(String name, byte b[]) { + this.tags.add(new ByteArrayTag(name, b)); + return this; + } + + public TagBuilder append(String name, byte b) { + this.tags.add(new ByteTag(name, b)); + return this; + } + + public TagBuilder append(CompoundTag compound) { + this.tags.add(compound); + return this; + } + + public TagBuilder append(String name, double d) { + this.tags.add(new DoubleTag(name, d)); + return this; + } + + public TagBuilder append(EndTag tag) { + this.tags.add(tag); + return this; + } + + public TagBuilder append(String name, float f) { + this.tags.add(new FloatTag(name, f)); + return this; + } + + public TagBuilder append(String name, int i[]) { + this.tags.add(new IntArrayTag(name, i)); + return this; + } + + public TagBuilder append(String name, int i) { + this.tags.add(new IntTag(name, i)); + return this; + } + + public TagBuilder append(String name, Class clazz, List l) { + this.tags.add(new ListTag(name, clazz, l)); + return this; + } + + public TagBuilder append(String name, long l) { + this.tags.add(new LongTag(name, l)); + return this; + } + + public TagBuilder append(String name, short s) { + this.tags.add(new ShortTag(name, s)); + return this; + } + + public TagBuilder append(String name, String s) { + this.tags.add(new StringTag(name, s)); + return this; + } + + public TagBuilder append(TagBuilder builder) { + this.tags.add(builder.toCompoundTag()); + return this; + } + + public CompoundTag toCompoundTag() { + Map tagMap = new HashMap(); + for(Tag tag : this.tags) { + tagMap.put(tag.getName(), tag); + } + + return new CompoundTag(this.name, tagMap); + } + + public List toList() { + return new ArrayList(this.tags); + } + + public NBTOutputStream toOutputStream(String file) { + File nbt = new File(file); + + try { + if(!nbt.exists()) { + if(nbt.getParentFile() != null) nbt.getParentFile().mkdirs(); + nbt.createNewFile(); + } + + NBTOutputStream out = new NBTOutputStream(new FileOutputStream(nbt)); + for(Tag tag : this.tags) { + out.writeTag(tag); + } + + return out; + } catch(IOException e) { + System.out.println("Failed to create NBTOutputStream for file \"" + file + "\" from a TagBuilder."); + e.printStackTrace(); + } + + return null; + } + +} diff --git a/src/main/java/me/steveice10/opennbt/utils/DoubleIntHashMap.java b/src/main/java/me/steveice10/opennbt/utils/DoubleIntHashMap.java new file mode 100644 index 0000000..e7f2864 --- /dev/null +++ b/src/main/java/me/steveice10/opennbt/utils/DoubleIntHashMap.java @@ -0,0 +1,41 @@ +package me.steveice10.opennbt.utils; + +import java.util.HashMap; + +public class DoubleIntHashMap extends HashMap { + + private static final long serialVersionUID = 1L; + + public DoubleIntHashMap() { + super(100); + } + + public DoubleIntHashMap(int capacity) { + super(capacity); + } + + public T put(int key1, int key2, T value) { + int key = key(key1, key2); + return super.put(key, value); + } + + public T get(int key1, int key2) { + int key = key(key1, key2); + return super.get(key); + } + + public boolean containsKey(int key1, int key2) { + int key = key(key1, key2); + return super.containsKey(key); + } + + public T remove(int key1, int key2) { + int key = key(key1, key2); + return super.remove(key); + } + + private static final int key(int x, int z) { + return (x & 0xF) << 11 | (z & 0xF) << 7; + } + +}