Jump to content

Recommended Posts

Posted

Hello,

 

I have implemented a new ore type that requires me to use a TileEntity due to my need for NBT data. When I started the project, my initial approach was to use this TileEntity class as in my oregen. However I discovered that when I flew around in creative I got a lot of stuttering. This led me to assume that my stuttering was the serializing in and out of my blocks as I moved around - despite the fact that my NBT was a single integer.

 

So after some work I have since redesigned the system so I use basic blocks that instead know how to convert themselves into the proper TileEntity when someone or something attempts to harvest them. So far this all works great and there is no measurable impact on performance.

 

As I move to the second phase of design I see a need to make a few changes to my Block class to handle more edge cases. Before I go down that path I thought I would check to see if my assumptions are correct.

 

If I had a TileEntity that had a single integer as NBT and populated it as prolifically as vanilla ore am I correct in assuming this would have a demonstrable impact on performance? I knew a lot less about the mod API's than I do now and there is still much I don't know so I want to be sure my assumption is correct. If my performance issue was likely related to something else then I would prefer to track that down and use the TileEntity as my ore directly.

 

Thanks.

 

 

Posted
3 hours ago, mmcmain said:

TileEntity due to my need for NBT data

TileEntities and NBT do not belong together. NBT is only for saving and loading from disk.

 

If you have a TileEntity that has stored data, then that data is just kept as a raw field on the TE.

 

Also:

Show your code.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

I was following Shadow's tutorial here:  https://shadowfacts.net/tutorials/forge-modding-1102/tile-entities/

 

So there is a problem using NBT inside of a TileEntity? If so that would explain it.

 

My class at the time of my stuttering test was quite literally his TileEntityCounter without the increment/decrement behavior on it when you clicked. The only other differences I had were variable names.

 

When I used that TileEntity as the product of my OreGen I got the bad stuttering. When I switched it to a block I got none. Hence my assumption at the time that the NBT data of 10's of thousands of blocks might be the culprit.

 

 

This is my current TileEntityOre class. I have not tested populating the world with these though.

 

package com.mmcmain.hardcoremining.block;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;

import java.util.Random;

public class TileEntityOre extends TileEntity
{
   private static final int DEFAULT_ADDL_DROPS = 160;
   private static final int DEFAULT_MIN_DROPS = 40;

   private static final String NBT_TAG = "oreCounter";

   Random random;
   
   
   private int oreCounter = -1;


   public TileEntityOre()
   {
      random = new Random();
      oreCounter = getDefaultOreCounter(random);
      markDirty();
   }

   @Override
   public NBTTagCompound writeToNBT(NBTTagCompound compound)
   {
      compound.setInteger(NBT_TAG, oreCounter);
      return super.writeToNBT(compound);
   }
   
   @Override
   public void readFromNBT(NBTTagCompound compound)
   {
      oreCounter = compound.getInteger(NBT_TAG);
      super.readFromNBT(compound);
   }

   public void setOreCounterForDepth(int depth)
    {
       oreCounter += additionalOreCounterForDepth(random, depth);
    }

    public static int estimateOreCounterForDepth(Random random, int depth)
    {
        return getDefaultOreCounter(random) + additionalOreCounterForDepth(random, depth);
    }

    public static int getDefaultOreCounter(Random random)
    {
        return random.nextInt(DEFAULT_ADDL_DROPS) + DEFAULT_MIN_DROPS;
    }

    public static int additionalOreCounterForDepth(Random random, int depth)
   {
      int oreCounter = 0;

      if (depth >= 128 && depth < 255)
         oreCounter += (random.nextInt(DEFAULT_ADDL_DROPS/4));
      else if (depth > 24 && depth <= 50)
         oreCounter += (random.nextInt(DEFAULT_ADDL_DROPS/2));
      else if ( depth > 0 && depth <= 24)
         oreCounter += (random.nextInt(DEFAULT_ADDL_DROPS));

      return oreCounter;
   }

   public void setOreCounter(int amount)
   {
      oreCounter = amount;
      markDirty();
   }

   public boolean hasOre()
   {
      return oreCounter > 0;
   }
   
   public int getOreCounter()
   {
      return oreCounter;
   }

   public int reduceOre()
   {
      return reduceOre(1);
   }

   public int reduceOre(int amount)
   {
      oreCounter -= amount;
      markDirty();
      return oreCounter;
   }
   
}

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.