Jump to content
  • Home
  • Files
  • Docs
  • Merch
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • Arraylist.clear clearing all array lists?
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 0
gummby8

Arraylist.clear clearing all array lists?

By gummby8, February 29, 2016 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

gummby8    26

gummby8

gummby8    26

  • Diamond Finder
  • gummby8
  • Members
  • 26
  • 479 posts
Posted February 29, 2016

I am making a tile entity that will spawn up to a preconfigured number of animals. It uses 2 lists a spawnList and a foundList.

it first clears the foundList

Searches for entities in the area. using AABB

compares the entities found in the AABB list with the spawnList

And entity IDs that match the spawnList are put into the foundList

Then the spawnList = foundList

Based on the new spawnList size it will spawn more or do nothing.

 



@Override
public void update() {
	ticksExisted++;
	//called each tick just like onUpdate
	// TODO Auto-generated method stub
	int time = 10 * 20; // 20 ticks per second times 10 seconds
	if (this.ticksExisted % time == (time - 1) && !this.worldObj.isRemote) { // if you check against 0, it will drop an item immediately every time the world loads
		foundList.clear();
		List list = this.worldObj.getEntitiesWithinAABB(EntityLiving.class, this.getRenderBoundingBox().expand(20, 20, 20));
		Iterator iterator = list.iterator();
        while (iterator.hasNext())
        {
            Entity entity = (Entity)iterator.next();
            	if (spawnList.contains(entity.getEntityId())){
            		foundList.add(entity.getEntityId());
            		System.out.println("found");
            		
            	}
            
        }
        

        System.out.println("spawnList = " + spawnList.size());
        System.out.println("foundList = " + foundList.size());

        


		spawnList = foundList;

		if (spawnList.size() < 5){
			spawnCreature(this.worldObj, message, this.pos.getX(),this.pos.getY() + 2, this.pos.getZ() );

		}


	}
}

 

 

The issue that I am seeing is if I have the foundList.clear(); in the code, the two lists are always 0 when it prints out. THe tile entity spawns a creature, lists the spawnList size as 1, but then the next cycle, the spawnList is zeroed out before it even gets to the spawnList = foundList.

 

Even if I comment the spawnList = foundList out. the spawnList is still cleared when the foundList is cleared. If I comment out the foundList.Clear() section, the lists count up normally. But I need the foundList to be cleared out at the beginning of each cycle.

 

So what the heck is going on?

  • Quote

Share this post


Link to post
Share on other sites

gummby8    26

gummby8

gummby8    26

  • Diamond Finder
  • gummby8
  • Members
  • 26
  • 479 posts
Posted February 29, 2016

further testing

 

		System.out.println("spawnList = " + this.spawnList.size());
		this.foundList.clear();
		System.out.println("spawnList = " + this.spawnList.size());

 

this prints out

 

1

0

 

What the hell?

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2098

Draco18s

Draco18s    2098

  • Reality Controller
  • Draco18s
  • Members
  • 2098
  • 14038 posts
Posted February 29, 2016

Ah.

 

Pointers.

 

You have created a reference to a single list.

  • Quote

Share this post


Link to post
Share on other sites

jabelar    591

jabelar

jabelar    591

  • Reality Controller
  • jabelar
  • Members
  • 591
  • 3266 posts
Posted February 29, 2016

you set spawnList = foundList.  So anything you do to one will happen to the other.

 

Java doesn't copy when you use an assignment to an object, rather it makes them both point to the same thing. This is powerful, but obviously also dangerous.

 

You need to use a copy method rather than an = assignment.

 

Note that this applies to objects, but for literals and primitives = operator creates a copy. So for example, if you wanted to copy an Array of int values, you can't use = between the arrays, but you could loop through the array and use = between the int elements. I hope you understand what I mean...

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2098

Draco18s

Draco18s    2098

  • Reality Controller
  • Draco18s
  • Members
  • 2098
  • 14038 posts
Posted February 29, 2016

(Note that strings are complex objects as far as Java is concerned, which can lead to some really naughty things).

  • Quote

Share this post


Link to post
Share on other sites

Enginecrafter    4

Enginecrafter

Enginecrafter    4

  • Creeper Killer
  • Enginecrafter
  • Members
  • 4
  • 100 posts
Posted February 29, 2016

As many, many more people said, this is not java school. Assigning and copying data types is Basic Java.

Just wanted to say to be carefull when posting or your thread will get locked and you may not know why. Just a warning.

  • Quote

Share this post


Link to post
Share on other sites

gummby8    26

gummby8

gummby8    26

  • Diamond Finder
  • gummby8
  • Members
  • 26
  • 479 posts
Posted February 29, 2016

you set spawnList = foundList.  So anything you do to one will happen to the other.

 

Java doesn't copy when you use an assignment to an object, rather it makes them both point to the same thing. This is powerful, but obviously also dangerous.

 

You need to use a copy method rather than an = assignment.

 

Note that this applies to objects, but for literals and primitives = operator creates a copy. So for example, if you wanted to copy an Array of int values, you can't use = between the arrays, but you could loop through the array and use = between the int elements. I hope you understand what I mean...

 

Got it

 

Collections.copy(spawnList, foundList);

 

That fixed it

Thanks

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6693

diesieben07

diesieben07    6693

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6693
  • 45741 posts
Posted February 29, 2016

(Note that strings are complex objects as far as Java is concerned, which can lead to some really naughty things).

This is why setting final fields is evil and should never be done.
  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2098

Draco18s

Draco18s    2098

  • Reality Controller
  • Draco18s
  • Members
  • 2098
  • 14038 posts
Posted February 29, 2016

(Note that strings are complex objects as far as Java is concerned, which can lead to some really naughty things).

This is why setting final fields is evil and should never be done.

Oh, absolutely.  I would never recommend hacking objects like that.  Just that the result is fucking hilarious.

This one fucks about with the IntegerCache for much the same result.

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6693

diesieben07

diesieben07    6693

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6693
  • 45741 posts
Posted February 29, 2016

The one which changes Boolean.TRUE and Boolean.FALSE is better imho :P

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2098

Draco18s

Draco18s    2098

  • Reality Controller
  • Draco18s
  • Members
  • 2098
  • 14038 posts
Posted March 1, 2016

The one which changes Boolean.TRUE and Boolean.FALSE is better imho :P

 

Hehe.

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 0
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • darkgreenminer
      [1.12.2] Multiple Structure Generation

      By darkgreenminer · Posted 1 minute ago

      Someone else had that same problem of their mod only spawning the last structure added to WorldGenCustomStructures, and I remembered that the solution I found was what a commentor named Redstone Tim mentioned, that in WorldGenStructure you have to remove 'static'.  I'm happy to email my version of these two classes to you if you want to have a look.  It took me hours and hours to get them working.  I have no idea what might cause the cascading gen lag, but fixing the multiple structure problem might help.    
    • troyvs
      problems starting with modding

      By troyvs · Posted 45 minutes ago

      what command did you run to set up?  
    • MightyAhmed
      Immediate Crash On Any Version Of Forge

      By MightyAhmed · Posted 1 hour ago

      ok so its been a while but it was workling fine before somehow but now minecraft still works it just freezes and lagspikes every 5 seconds please help me on this issue i cant find anything on the internet also ihave 4GB ram total in my computer and i have dedicated 2gb ram to minecraft in the JVM arguments section also i have 125 mods installed.
    • deanvangreunen
      Custom Armor Item - Help - MC/Forge 1.14.4/1.14.3

      By deanvangreunen · Posted 2 hours ago

      Hello, I'm in progress of making a Minecraft 1.14.4 Mod using Forge. Needing some help, Could you please look at the following Class File and Explain what I'm doing wrong or what I should be doing?.   The "OnArmorTick" and other ".....Tick" functions don't work.   My intent: - if water is below and near player by 1 block while the boots are on then turn the water into ice. I'm trying to implement "Frost Walking Boots"   Code: - FrostBootsItem.java <- File I need help with - My Project Repo  <- Repo, So if you want to see how my mod is setup. (includes my world saves, etc)   Dev Details: - Minecraft Version: 1.14.4 - Minecraft Snapshot: 20191020-1.14.3 - Forge Version: 1.14.4-28.1.61   Notes: - I've followed a tutorial for 1.14.4 modding by MCJty on youtube (The author of RFTools) - I'm new to minecraft modding. I have expeirenced as a software developer/engineer.   ❤️❤️❤️❤️❤️❤️❤️❤️❤️  ❤️  .Thanks in advance. ❤️  ❤️❤️❤️❤️❤️❤️❤️❤️❤️ 
    • DragonITA
      [1.14.4] How to get Minecraft Horse model/texture to make a custom unicorn?

      By DragonITA · Posted 2 hours ago

      please see the screenshoot above.
  • Topics

    • Merthew
      8
      [1.12.2] Multiple Structure Generation

      By Merthew
      Started November 7, 2018

    • coolian
      1
      problems starting with modding

      By coolian
      Started October 9

    • MightyAhmed
      83
      Immediate Crash On Any Version Of Forge

      By MightyAhmed
      Started November 10

    • deanvangreunen
      0
      Custom Armor Item - Help - MC/Forge 1.14.4/1.14.3

      By deanvangreunen
      Started 2 hours ago

    • DragonITA
      48
      [1.14.4] How to get Minecraft Horse model/texture to make a custom unicorn?

      By DragonITA
      Started December 9

  • Who's Online (See full list)

    • ricoc90
    • GttiqwT
    • darkgreenminer
    • Choonster
    • bitman
    • Ommina
    • ericgolde555
    • AkosM
    • jinenze
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • Arraylist.clear clearing all array lists?
  • Theme
  • Contact Us
  • Discord

Copyright © 2019 ForgeDevelopment LLC · Ads by Curse Powered by Invision Community