Advertisement
Guest User

nbt tag issues (line 189)

a guest
Oct 7th, 2015
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.90 KB | None | 0 0
  1. package net.dyeo.teleporter;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import com.sun.org.apache.bcel.internal.Constants;
  6.  
  7. import net.minecraft.block.Block;
  8. import net.minecraft.client.gui.GuiScreenBook;
  9. import net.minecraft.entity.Entity;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.init.Blocks;
  12. import net.minecraft.init.Items;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.nbt.NBTBase;
  16. import net.minecraft.nbt.NBTTagCompound;
  17. import net.minecraft.nbt.NBTTagList;
  18. import net.minecraft.util.BlockPos;
  19. import net.minecraft.util.ChatComponentTranslation;
  20. import net.minecraft.world.World;
  21. import net.minecraft.world.WorldSavedData;
  22.  
  23. /*
  24.  * TeleporterNetwork is the singleton responsible for saving the teleporter data onto the world file, and is
  25.  * responsible for retrieving destination and source nodes during teleportation.
  26.  */
  27. public class TeleporterNetwork extends WorldSavedData
  28. {
  29.    
  30.     //
  31.    
  32.     private ArrayList<TeleporterNode> network = new ArrayList<TeleporterNode>();
  33.    
  34.     //
  35.    
  36.     private static final String IDENTIFIER = Reference.MODID.toLowerCase();
  37.  
  38.     public TeleporterNetwork()
  39.     {
  40.         super(IDENTIFIER);
  41.     }
  42.    
  43.     public TeleporterNetwork(String identifier)
  44.     {
  45.         super(identifier);
  46.     }
  47.    
  48.     //
  49.  
  50.     @Override
  51.     public void readFromNBT(NBTTagCompound nbt)
  52.     {
  53.         final int NBT_TYPE = 10; // compound
  54.         NBTTagList netNBT = nbt.getTagList("Network", NBT_TYPE);
  55.        
  56.         if(network.size() != 0)
  57.         {
  58.             network.clear();
  59.         }
  60.        
  61.         for(int i = 0; i < netNBT.tagCount(); ++i)
  62.         {
  63.             TeleporterNode tempNode = new TeleporterNode();
  64.             NBTTagCompound nodeNBT = netNBT.getCompoundTagAt(i);
  65.             tempNode.readFromNBT(nodeNBT);
  66.             network.add(tempNode);
  67.            
  68.             System.out.println("[Teleporter][readFromNBT] Read worldData node " + i);
  69.         }
  70.        
  71.     }
  72.  
  73.     @Override
  74.     public void writeToNBT(NBTTagCompound nbt)
  75.     {
  76.         NBTTagList netNBT = new NBTTagList();
  77.        
  78.         for(int i = 0; i < network.size(); ++i)
  79.         {
  80.             TeleporterNode tempNode = network.get(i);
  81.             NBTTagCompound nodeNBT = new NBTTagCompound();
  82.             tempNode.writeToNBT(nodeNBT);
  83.             netNBT.appendTag(nodeNBT);
  84.            
  85.             System.out.println("[Teleporter][writeToNBT] Saved worldData node " + i);
  86.         }
  87.        
  88.         nbt.setTag("Network", netNBT);     
  89.     }
  90.    
  91.     private boolean isObstructed(World world, TeleporterNode node)
  92.     {
  93.         BlockPos bl1p = new BlockPos(node.pos.getX(),node.pos.getY()+1,node.pos.getZ());
  94.         BlockPos bl2p = new BlockPos(node.pos.getX(),node.pos.getY()+2,node.pos.getZ());
  95.         Block bl1 = world.getBlockState(bl1p).getBlock();
  96.         Block bl2 = world.getBlockState(bl2p).getBlock();
  97.        
  98.         if ((/*bl1 == Blocks.wall_sign || bl1 == Blocks.standing_sign || bl1 == Blocks.lever || bl1 == Blocks.vine || bl1 == Blocks.torch || bl1 == Blocks.air || bl1 == Blocks.redstone_torch || bl1 == Blocks.ladder || */bl1.isPassable(world, bl1p))
  99.                 &&
  100.             (/*bl2 == Blocks.wall_sign || bl2 == Blocks.standing_sign || bl2 == Blocks.lever || bl2 == Blocks.vine || bl2 == Blocks.torch || bl2 == Blocks.air || bl2 == Blocks.redstone_torch || bl2 == Blocks.ladder || */bl2.isPassable(world, bl2p)))
  101.         {
  102.             return false;
  103.         }
  104.        
  105.         return true;
  106.     }
  107.    
  108.     public TeleporterNode getNextNode(Entity entityIn, World world, ItemStack stack, TeleporterNode source)
  109.     {
  110.         TileEntityTeleporter sourceEnt = TileEntityTeleporter.getTileEntityAt(world, source.pos);
  111.        
  112.         boolean playerEnt = false;
  113.         if(entityIn instanceof EntityPlayer)
  114.         {
  115.             playerEnt = true;
  116.         }
  117.         int index = network.indexOf(source);
  118.         for(int i = index+1; i < network.size()+index; ++i)
  119.         {
  120.             TeleporterNode node =  network.get(i % network.size());
  121.             TileEntityTeleporter nodeEnt = TileEntityTeleporter.getTileEntityAt(world, node.pos);
  122.            
  123.             boolean isUnique = false;
  124.             boolean matches = false;
  125.             boolean diffDimension = false;
  126.                        
  127.             // check if dimensions are different
  128.             if(node == source)
  129.             {
  130.                 matches = false;
  131.             }
  132.             if(source.dimension != node.dimension)
  133.             {
  134.                 matches = false;
  135.                 diffDimension = true;
  136.             }
  137.             // check if keys are completely different
  138.             else if(stack == null && nodeEnt.itemStacks[0] != null)
  139.             {
  140.                 matches = false;
  141.             }
  142.             else if(stack != null && nodeEnt.itemStacks[0] == null)
  143.             {
  144.                 matches = false;
  145.             }
  146.             // check if keys are both empty
  147.             else if(stack == null && nodeEnt.itemStacks[0] == null)
  148.             {
  149.                 matches = true;        
  150.             }
  151.             // check if keys are the same
  152.             else if(stack.getItem().getUnlocalizedName().equals(nodeEnt.itemStacks[0].getItem().getUnlocalizedName())) 
  153.             {
  154.                 matches = true;
  155.             }
  156.            
  157.             if (matches == true)
  158.             {
  159.                 Item thisItem = stack.getItem(), nextItem = nodeEnt.itemStacks[0].getItem();
  160.                 if (thisItem == Items.written_book && nextItem == Items.written_book)
  161.                 {
  162.                     String author = stack.getTagCompound().getString("author");
  163.                     author += ":" + stack.getTagCompound().getString("title");
  164.                    
  165.                     String nodeAuthor = nodeEnt.itemStacks[0].getTagCompound().getString("author");
  166.                     nodeAuthor += ":" + nodeEnt.itemStacks[0].getTagCompound().getString("title");
  167.                     if (author.equals(nodeAuthor))
  168.                     {
  169.                         matches = true;
  170.                     }
  171.                     else
  172.                     {
  173.                         matches = false;
  174.                     }
  175.                 }
  176.                 else if (thisItem == Items.filled_map && nextItem == Items.filled_map)
  177.                 {
  178.                     if (stack.getItemDamage() == nodeEnt.itemStacks[0].getItemDamage())
  179.                     {
  180.                         matches = true;
  181.                     }
  182.                     else
  183.                     {
  184.                         matches = false;
  185.                     }
  186.                 }
  187.                 else
  188.                 {
  189.                     // this is where i'm having the issue
  190.                     NBTTagCompound tag = (NBTTagCompound)stack.getTagCompound().getTag("display");
  191.                     NBTTagCompound nextTag = (NBTTagCompound)nodeEnt.itemStacks[0].getTagCompound().getTag("display");
  192.                    
  193.                     String name = tag.getString("name");
  194.                     String nodeName = nextTag.getString("name");
  195.                     if (name.equals(nodeName))
  196.                     {
  197.                         matches = true;
  198.                     }
  199.                     else
  200.                     {
  201.                         matches = false;
  202.                     }
  203.                 }
  204.             }
  205.            
  206.             boolean obstructed = isObstructed(world,node);
  207.            
  208.             if(matches == true && obstructed == false && diffDimension == false && nodeEnt.isPowered == false)
  209.             {
  210.                 return node;
  211.             }
  212.             else if(matches == true && obstructed == true && diffDimension == false)
  213.             {
  214.                 EntityPlayer entp = (EntityPlayer)entityIn;
  215.                 if(playerEnt == true) entp.addChatMessage(new ChatComponentTranslation("Teleporter is blocked; skipping..."));
  216.             }
  217.             else if(matches == true && obstructed == false && diffDimension == false && nodeEnt.isPowered == true)
  218.             {
  219.                 EntityPlayer entp = (EntityPlayer)entityIn;
  220.                 if(playerEnt == true) entp.addChatMessage(new ChatComponentTranslation("Teleporter is disabled; skipping..."));
  221.             }
  222.         }
  223.  
  224.         if(playerEnt == true)
  225.         {
  226.             EntityPlayer entp = (EntityPlayer)entityIn;
  227.             entp.addChatMessage(new ChatComponentTranslation("No teleporters found that match your key."));
  228.         }
  229.        
  230.         System.out.println("[Teleporter] Destination not found");
  231.         return null;
  232.     }
  233.    
  234.     public TeleporterNode getNode(BlockPos pos, int dimension, boolean debug)
  235.     {
  236.         for(int i = 0; i < network.size(); ++i)
  237.         {
  238.             TeleporterNode node = network.get(i);
  239.             if(pos.getX() == node.pos.getX() && pos.getY() == node.pos.getY() && pos.getZ() == node.pos.getZ() && dimension == node.dimension)
  240.             {
  241.                 if(debug) System.out.println("[Teleporter][getNode] Getting node " + pos.getX() + "," + pos.getY() + "," + pos.getZ() + " from network");
  242.                 return node;
  243.             }
  244.         }
  245.        
  246.         if(debug) System.out.println("[Teleporter][getNode] No node " + pos.getX() + "," + pos.getY() + "," + pos.getZ() + " found in network");
  247.         return null;
  248.     }
  249.    
  250.     public void addNode(TeleporterNode node)
  251.     {
  252.         int index = network.size();
  253.         network.add(node);
  254.         markDirty();
  255.         System.out.println("[Teleporter][addNode] Appending node " + node.pos.getX() + "," + node.pos.getY() + "," + node.pos.getZ() + " to network " + "[" + index + "]");
  256.     }
  257.    
  258.     public boolean removeNode(BlockPos pos, int dimension)
  259.     {
  260.         for(int i = 0; i < network.size(); ++i)
  261.         {
  262.             TeleporterNode node = network.get(i);
  263.             if(pos.getX() == node.pos.getX() && pos.getY() == node.pos.getY() && pos.getZ() == node.pos.getZ() && dimension == node.dimension)
  264.             {
  265.                 network.remove(node);
  266.                 System.out.println("[Teleporter][removeNode] Removing node " + pos.getX() + "," + pos.getY() + "," + pos.getZ() + " from network");
  267.                 return true;
  268.             }
  269.         }
  270.        
  271.         System.out.println("[Teleporter][removeNode] No node " + pos.getX() + "," + pos.getY() + "," + pos.getZ() + " found in network");
  272.         return false;
  273.     }
  274.    
  275.     public static TeleporterNetwork get(World world, boolean debug)
  276.     {
  277.         TeleporterNetwork data = (TeleporterNetwork)world.loadItemData(TeleporterNetwork.class, IDENTIFIER);
  278.        
  279.         if(data == null)
  280.         {
  281.             if(debug) System.out.println("[Teleporter][get] New network created!");
  282.             data = new TeleporterNetwork();
  283.             world.setItemData(IDENTIFIER, data);
  284.         }
  285.         else
  286.         {
  287.             if(debug) System.out.println("[Teleporter][get] Network loaded!");
  288.         }      
  289.        
  290.         data.markDirty();      
  291.         return data;       
  292.     }
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement