Jump to content

[1.7.10] readFromNBT loaded, but odd behavior?


seppitm

Recommended Posts

Hello, i'm trying to read strings from NBT. i have attached the code for read and write.

My problem is that he reads the both Strings groupName and ownerName and set the fields in the entity. but when i acces them in the onBlockActivated method, they are empty.

For the Inventory stuff, everything work fine O.o but i'm using the same method for the groupName and the ownerName. Can someone explain me the odd behavior or say me how to fix this ?

 

public void writeToNBT(NBTTagCompound par1)
   {
      super.writeToNBT(par1);
      
          NBTTagList itemList = new NBTTagList();
          for (int i = 0; i < inv.length; i++) {
                  ItemStack stack = inv[i];
                  if (stack != null) {
                          NBTTagCompound tag = new NBTTagCompound();
                          tag.setByte("Slot", (byte) i);
                          stack.writeToNBT(tag);
                          itemList.appendTag(tag);
                  }
          }
          par1.setTag("Inventory", itemList);
      
          NBTTagList theMatterSettings = new NBTTagList();
          NBTTagCompound baseMatter = new NBTTagCompound();
          baseMatter.setString("groupName", groupName);
          baseMatter.setString("ownerName", ownerName);
          
          theMatterSettings.appendTag(baseMatter);
          
          par1.setTag("thematter", theMatterSettings);
          
      NBTTagList groupMembers2 = new NBTTagList();

  
      for(String name:groupMembers)
      {
    	  NBTTagCompound tagName = new NBTTagCompound();
    	  tagName.setString("name", name);
    	  groupMembers2.appendTag(tagName);
      }

      
      par1.setTag("themattergroupmembers", groupMembers2);
      
      System.out.println("Write to NTB");
      System.out.println(groupName);
      
   }

   @Override
   public void readFromNBT(NBTTagCompound par1)
   {
      super.readFromNBT(par1);
          
          NBTTagList tagList = par1.getTagList("Inventory",par1.getId());
          for (int i = 0; i < tagList.tagCount(); i++) {
                  NBTTagCompound tag = (NBTTagCompound) tagList.getCompoundTagAt(i);
                  byte slot = tag.getByte("Slot");
                  if (slot >= 0 && slot < inv.length) {
                          inv[slot] = ItemStack.loadItemStackFromNBT(tag);
                  }
          }
          
          NBTTagList theMatterSettings = par1.getTagList("thematter",par1.getId());

          if(theMatterSettings.tagCount() > 0)
          {
        	  NBTTagCompound baseMatter =theMatterSettings.getCompoundTagAt(0);
        	  
        	  groupName = baseMatter.getString("groupName");
    	      ownerName = baseMatter.getString("ownerName");
    	      
    	      NBTTagList groupTagList = par1.getTagList("themattergroupmembers",par1.getId());
    	      
    	      groupMembers.clear();
    	      
    	      for (int i = 0; i < groupTagList.tagCount(); i++) {
    	    	  groupMembers.add(groupTagList.getCompoundTagAt(i).getString("name"));
    		}
          }
        	  
      System.out.println("Read from NTB");

      System.out.println(this.groupName);
   }

 

//EDIT:

Found the bug. The values are only set on the server (world.isRemote=false). is there a way to automatically sync the entity with the client?

Link to comment
Share on other sites

Ich gehe mal davon aus das du Deutsch bist (wegen dem Namen)

 

Nee ich setze in dem Entity den Namen des Spielers dem der Block gehört. Derjenige kann mit nem bestimmten Gegenstand den Block aktivieren und andere nicht. Darum muss ich den Owner zum Client syncen.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.