Skip to content

Commit

Permalink
[Incomplete] Make schematics track NBT
Browse files Browse the repository at this point in the history
Doesn't seem to want to work.
  • Loading branch information
mcmonkey4eva committed Apr 3, 2016
1 parent e7586ff commit 135b698
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 11 deletions.
@@ -1,9 +1,18 @@
package net.aufdemrand.denizen.utilities.blocks;

import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.jnbt.CompoundTag;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.minecraft.server.v1_9_R1.BlockPosition;
import net.minecraft.server.v1_9_R1.NBTTagCompound;
import net.minecraft.server.v1_9_R1.TileEntity;
import net.minecraft.server.v1_9_R1.TileEntityChest;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_9_R1.CraftServer;
import org.bukkit.craftbukkit.v1_9_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_9_R1.block.CraftBlock;

import java.util.List;

Expand All @@ -23,10 +32,28 @@ public BlockData(short mat, byte dat) {
public BlockData(Block block) {
material = block.getType();
data = block.getData();
TileEntity te = ((CraftWorld) block.getWorld()).getHandle().getTileEntity(
new BlockPosition(block.getX(), block.getY(), block.getZ()));
if (te != null) {
ctag = new NBTTagCompound();
te.save(ctag);
}
}

public void setBlock(Block block) {
block.setTypeIdAndData(material.getId(), (byte) data, false);
if (ctag != null) {
ctag.setInt("x", block.getX());
ctag.setInt("y", block.getY());
ctag.setInt("z", block.getZ());
// TODO: make this work!
TileEntity te = TileEntity.a(((CraftServer) Bukkit.getServer()).getServer(), ctag);
BlockPosition blockPos = new BlockPosition(block.getX(), block.getY(), block.getZ());
((CraftWorld) block.getWorld()).getHandle().setTileEntity(blockPos, te);
((CraftWorld) block.getWorld()).getHandle().a(te);
((CraftWorld) block.getWorld()).getHandle().getChunkAtWorldCoords(blockPos).a(blockPos, te);
// dB.log("Built ctag: " + ctag.toString());
}
}

public String toCompressedFormat() {
Expand All @@ -45,11 +72,13 @@ public static BlockData fromCompressedString(String str) {
return data;
}

public CompoundTag getNBTTag() {
return null;
NBTTagCompound ctag = null;

public NBTTagCompound getNBTTag() {
return ctag;
}

public void setNBTTag(CompoundTag tag) {
return;
public void setNBTTag(NBTTagCompound tag) {
ctag = tag;
}
}
Expand Up @@ -5,6 +5,7 @@
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.jnbt.*;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.minecraft.server.v1_9_R1.NBTTagCompound;
import org.bukkit.Location;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.BlockVector;
Expand Down Expand Up @@ -259,7 +260,8 @@ else if (entry.getKey().equals("z")) {
BlockVector pt = new BlockVector(x, y, z);
BlockData block = new BlockData(blocks[index], blockData[index]);
if (tileEntitiesMap.containsKey(pt)) {
block.setNBTTag(new CompoundTag(tileEntitiesMap.get(pt)));
CompoundTag otag = new CompoundTag(tileEntitiesMap.get(pt));
block.setNBTTag(otag.toNMSTag());
}
cbs.blocks.add(block);
}
Expand Down Expand Up @@ -320,11 +322,11 @@ public void saveMCEditFormatToStream(OutputStream os) {
blocks[index] = (byte) bd.material.getId();
blockData[index] = (byte) bd.data;

CompoundTag rawTag = bd.getNBTTag();
CompoundTag rawTag = bd.getNBTTag() == null ? null : CompoundTag.fromNMSTag(bd.getNBTTag());
if (rawTag != null) {
HashMap<String, Tag> values = new HashMap<String, Tag>();
for (Map.Entry<String, Tag> entry : rawTag.getValue().entrySet()) {
values.put(entry.getKey(), entry.getValue()); // TODO: Why manual copy?
values.put(entry.getKey(), entry.getValue());
}
values.put("id", new StringTag(null)); // block.getNbtId()
values.put("x", new IntTag(x));
Expand Down
112 changes: 108 additions & 4 deletions src/main/java/net/aufdemrand/denizen/utilities/jnbt/CompoundTag.java
Expand Up @@ -19,10 +19,9 @@

package net.aufdemrand.denizen.utilities.jnbt;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.server.v1_9_R1.*;

import java.util.*;

/**
* The {@code TAG_Compound} tag.
Expand All @@ -31,6 +30,111 @@ public final class CompoundTag extends Tag {

private final Map<String, Tag> value;

public NBTTagCompound toNMSTag() {
NBTTagCompound tag = new NBTTagCompound();
for (Map.Entry<String, Tag> entry: value.entrySet()) {
if (entry.getValue() instanceof IntTag) {
tag.setInt(entry.getKey(), ((IntTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof ByteTag) {
tag.setByte(entry.getKey(), ((ByteTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof ByteArrayTag) {
tag.setByteArray(entry.getKey(), ((ByteArrayTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof CompoundTag) {
tag.set(entry.getKey(), ((CompoundTag) entry.getValue()).toNMSTag());
}
else if (entry.getValue() instanceof DoubleTag) {
tag.setDouble(entry.getKey(), ((DoubleTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof FloatTag) {
tag.setFloat(entry.getKey(), ((FloatTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof IntArrayTag) {
tag.setIntArray(entry.getKey(), ((IntArrayTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof ListTag) {
NBTTagList list = new NBTTagList();
List<Tag> tags = ((ListTag) entry.getValue()).getValue();
for (Tag btag : tags) {
HashMap<String, Tag> btags = new HashMap<String, Tag>();
btags.put("test", btag);
CompoundTag comp = new CompoundTag(btags);
list.add(comp.toNMSTag().get("test"));
}
tag.set(entry.getKey(), list);
}
else if (entry.getValue() instanceof LongTag) {
tag.setLong(entry.getKey(), ((LongTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof ShortTag) {
tag.setShort(entry.getKey(), ((ShortTag) entry.getValue()).getValue());
}
else if (entry.getValue() instanceof StringTag) {
tag.setString(entry.getKey(), ((StringTag) entry.getValue()).getValue());
}
}
return tag;
}

public static CompoundTag fromNMSTag(NBTTagCompound tag) {
HashMap<String, Tag> tags = new HashMap<String, Tag>();
for (String key: tag.c()) {
NBTBase base = tag.get(key);
if (base instanceof NBTTagInt) {
tags.put(key, new IntTag(((NBTTagInt) base).d()));
}
else if (base instanceof NBTTagByte) {
tags.put(key, new ByteTag(((NBTTagByte) base).f()));
}
else if (base instanceof NBTTagFloat) {
tags.put(key, new FloatTag(((NBTTagFloat) base).h()));
}
else if (base instanceof NBTTagDouble) {
tags.put(key, new DoubleTag(((NBTTagDouble) base).g()));
}
else if (base instanceof NBTTagByteArray) {
tags.put(key, new ByteArrayTag(((NBTTagByteArray) base).c()));
}
else if (base instanceof NBTTagIntArray) {
tags.put(key, new IntArrayTag(((NBTTagIntArray) base).c()));
}
else if (base instanceof NBTTagCompound) {
tags.put(key, fromNMSTag(((NBTTagCompound) base)));
}
else if (base instanceof NBTTagEnd) {
tags.put(key, new EndTag());
}
else if (base instanceof NBTTagLong) {
tags.put(key, new LongTag(((NBTTagLong) base).c()));
}
else if (base instanceof NBTTagShort) {
tags.put(key, new ShortTag(((NBTTagShort) base).e()));
}
else if (base instanceof NBTTagString) {
tags.put(key, new StringTag(((NBTTagString) base).a_()));
}
else if (base instanceof NBTTagList) {
NBTTagList list = (NBTTagList) base;
if (list.size() > 0) {
NBTBase nbase = list.h(0);
NBTTagCompound comp = new NBTTagCompound();
comp.set("test", nbase);
ListTagBuilder ltb = new ListTagBuilder(fromNMSTag(comp).getValue().get("test").getClass());
for (int i = 0 ; i < list.size(); i++) {
NBTBase nbase2 = list.h(i);
NBTTagCompound comp2 = new NBTTagCompound();
comp2.set("test", nbase2);
ltb.add(fromNMSTag(comp2).getValue().get("test"));
}
tags.put(key, ltb.build());
}
}
}
return new CompoundTag(tags);
}

/**
* Creates the tag with an empty name.
*
Expand Down

0 comments on commit 135b698

Please sign in to comment.