Thread: Natural World Item Spawning

Results 1 to 7 of 7
  1. #1 Natural World Item Spawning 
    Registered Member
    BuryMeInKief's Avatar
    Join Date
    Jan 2018
    Posts
    17
    Thanks given
    6
    Thanks received
    6
    Rep Power
    76


    This code will spawn items in the world on a timed task. You can set the spawn location, the item it spawns, and the amount. Along with that, it supports randomized capabilities. Like spawning "random" items, along with randomized spawn tiles.
    There are methods that can be used to test/compare/get flooritem data.
    For example you can pass a worldtile and find what flooritems are on that tile. If it contains a random item, and a few others.


    It works like this.

    You take this a text file, write a key, the item id, the amount, and its location. If the itemid is 0, it will load itemid that is in the randomItems Array. You can decide what these items are.

    Pack using the floor packer, to create a .IB for the server to read and then store into memory as a hashmap later.



    GitHub - DevMichael0101/FloorItemsLoader



    Inside World.java, add a task to check for the floor items along with how it will add / remove them. This is the code for that.

    World.java
    Code:
    
    	private static final void addFloorItemsTask() {
    		CoresManager.slowExecutor.scheduleWithFixedDelay(new Runnable() {
    			@Override
    			public void run() {
    				try {
    					for(int floorItemKey = 1 ; floorItemKey < Settings.MAX_FLOORITEMS + 1; floorItemKey++){	 //load the a RespawnableFloorItem data, using the flooritemKey indexer. These are the keys / first number in the notepad
    							//Regular flooritems
    							WorldTile spawnTile = new WorldTile(FloorItemsLoader.get_FloorItem_X(floorItemKey) ,FloorItemsLoader.get_FloorItem_Y(floorItemKey) ,FloorItemsLoader.get_FloorItem_Z(floorItemKey));		//The tile for the Respawnable item.
    							Region region = getRegion(spawnTile.getRegionId());//The region of the tile				
    							FloorItem floorItem = new FloorItem( new Item(FloorItemsLoader.get_FloorItem_ItemID(floorItemKey) , FloorItemsLoader.get_FloorItem_Amount(floorItemKey)  ) , spawnTile ,null,false,false); // The floor item, belonging to NULL.
    							
    							//Randomized floorItems
    							FloorItem floorItemRandom = new FloorItem( FloorItemsLoader.create_RandomItem(), spawnTile ,null,false,false); // The floor item, belonging to NULL.
    							FloorItem retrievedFloorItem;// used for Random items.
    
    
    								if(FloorItemsLoader.get_FloorItem_ItemID(floorItemKey) == 0){//Loading a random item.
    
    									if(FloorItemsLoader.tileDoesContainRandomItem( floorItemKey,region, spawnTile )  ){ //the passed tile, does contain a flooritem that has an item that is within the randomitems Array
    										retrievedFloorItem = FloorItemsLoader.getRandomFlooritemOnTile( floorItemKey,region,spawnTile); //now that we know the tile does contain a random item, we need to get that flooritem on the tile
    										if(retrievedFloorItem != null)  { removeGroundItem(region,spawnTile,retrievedFloorItem); }
    										addGroundItem(  spawnTile,floorItemRandom); //spawn new floor items
    									}else{//The tile did not contain a random item, so add one.
    										addGroundItem(  spawnTile,floorItemRandom);
    									}
    
    								}else{
    
    
    									if(FloorItemsLoader.doesContainExactly(floorItemKey , region,	spawnTile,	floorItem) ) { // This is going to pass the above data, to be tested against flooritems that exist or don't.
    										//The amounts were found in the amount requested , do nothing. :)
    									}else{
    										//The amount of flooritems was not equal to the desired. This could mean, none was found, or some amount was found. This only looks for null ownership items, not player owned.
    										//Because of this, we will remove them and then add them back. Other wise, it will just stay 0 or the other amount for forever until reset or someone picks the single item.
    										//Let's remove the items, if they exist, then add them.
    										removeGroundItem(region,spawnTile,floorItem); //remove any found floor items
    										addGroundItem(  spawnTile,floorItem); //spawn new floor items
    									}
    						
    								}
    
    					}
    
    				} catch (Throwable e) {
    					//Logger.handle(e);
    				}
    			}
    		//}, 0,30, TimeUnit.MINUTES);   
    		}, 0,5, TimeUnit.SECONDS);
    
    	}
    
    	public static final void addGroundItem(WorldTile tile,FloorItem item) {
    		try{			
    			if(   item.getDefinitions().isStackable()   ){ //just add 1 stackable item
    				addGroundItem(item, tile);
    			}else{//add mutli items
    
    				for(int spawned_Amount = 0; spawned_Amount != item.getAmount() ; spawned_Amount++ ){
    					addGroundItem(new Item(item.getId() , 1  ), tile); //add 1 on the tile
    				}
    
    			}
    		}catch(Exception e){
    			//Logger.handle(e);
    		}
    	}
    
    	public static final void removeGroundItem(Region region,WorldTile floorItemTile,FloorItem item) {
    		try{
    			region.forceGetFloorItems();
    			FloorItem flooritem = region.getGroundItem(item.getId(), floorItemTile ,  null);
    			if(flooritem==null){return;}
    			if(   flooritem.getDefinitions().isStackable()   ){
    			    removeGroundItem(flooritem);
    			}else{
    				for(int spawned_Amount = 0; spawned_Amount < item.getAmount() ; spawned_Amount++ ){
    					region.forceGetFloorItems();
    					flooritem = region.getGroundItem(  item.getId(), floorItemTile ,  null  ); //new floor item
    					if(flooritem!=null){ removeGroundItem(flooritem); } //let's not remove what doesn't exist.
    				}
    			}
    		}catch(Exception e){
    			Logger.handle(e);
    		}
    	}
    Last edited by BuryMeInKief; 11-18-2023 at 06:24 AM. Reason: Improved code
    Onwards and upwards! -- Bongs, Weights, & Protein Shakes.
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    sυввч

    Sub's Avatar
    Join Date
    Aug 2007
    Age
    24
    Posts
    4,351
    Thanks given
    1,189
    Thanks received
    358
    Rep Power
    2845
    What exactly is the use case here? Is it to stop duplicate item spawns?
    Reply With Quote  
     

  4. #3  
    Registered Member
    BuryMeInKief's Avatar
    Join Date
    Jan 2018
    Posts
    17
    Thanks given
    6
    Thanks received
    6
    Rep Power
    76
    Quote Originally Posted by Sub View Post
    What exactly is the use case here? Is it to stop duplicate item spawns?
    Just for testing i have it spawning and deleting. In a live environment you would want to comment out the delete if already there.
    It won't duplicate.
    Onwards and upwards! -- Bongs, Weights, & Protein Shakes.
    Reply With Quote  
     

  5. #4  
    Registered Member
    Tyluur's Avatar
    Join Date
    Jun 2010
    Age
    26
    Posts
    5,067
    Thanks given
    1,787
    Thanks received
    1,728
    Rep Power
    2204
    Hope you implement jackson and get rid of Matrix's old file i/o system =].
    Quote Originally Posted by blakeman8192 View Post
    Keep trying. Quitting is the only true failure.
    Spoiler for skrrrrr:

    Attached image
    Reply With Quote  
     

  6. #5  
    Extreme Donator

    Genesis's Avatar
    Join Date
    Sep 2010
    Posts
    4,091
    Thanks given
    1,507
    Thanks received
    1,978
    Rep Power
    4928
    Good work
    Reply With Quote  
     

  7. Thankful user:


  8. #6  
    Community Veteran

    Dexter Morgan's Avatar
    Join Date
    Nov 2008
    Age
    18
    Posts
    4,405
    Thanks given
    1,162
    Thanks received
    742
    Rep Power
    3091
    Not really worth a show off and the code can be done better, maybe you meant to post it in another section?

    For example


    Code:
    	public static HashMap <Integer, Integer> floorItems; // FloorItemIndex, ItemID
    	public static HashMap <Integer, Integer> floorItemsAmount; // FloorItemIndex, ItemAmount
    	public static HashMap <Integer, Integer> floorItemsX; // FloorItemIndex, x
    	public static HashMap <Integer, Integer> floorItemsY; // FloorItemIndex, y
    	public static HashMap <Integer, Integer> floorItemsZ; // FloorItemIndex, z
    Should be handled in a single variable.

    Also some of your naming starts with camel case and ends with underscores in the middle, why? Not sure if it's your IDE's fault but your conventions are all over the place

    You should consider using Json for IO because it is simple and can be done in a few lines

    With a few simple changes you can have a good system so I would have to say not bad for a release.
    Reply With Quote  
     

  9. Thankful user:


  10. #7  
    Registered Member
    BuryMeInKief's Avatar
    Join Date
    Jan 2018
    Posts
    17
    Thanks given
    6
    Thanks received
    6
    Rep Power
    76
    Currently fixing all my awful naming now, along with improving the code cause i found some bugs that are not so good.

    I really wish i could have gotten all that hashmap into a single variable, i looked into how and it would involve a wrapper. It would only let me store a key and a value so i went with just doing 1 for everything.
    Hashmap is also O(1) time so it's not like it's a performance hit, which i'm thankful for.

    I'm looking to improve it, which i think involves exactly what you mention, a Json.
    Thank you i appreciate it

    [edit]
    Has been improved and put on git.



    ---

    Random item support has been added. Code updated.
    Last edited by BuryMeInKief; 11-18-2023 at 06:17 AM.
    Reply With Quote  
     

  11. Thankful user:



Thread Information
Users Browsing this Thread

There are currently 2 users browsing this thread. (1 members and 1 guests)

  1. BuryMeInKief

User Tag List

Similar Threads

  1. Replies: 16
    Last Post: 11-16-2013, 12:29 AM
  2. Need help in global world item spawning
    By josia90 in forum Help
    Replies: 0
    Last Post: 08-27-2009, 08:30 PM
  3. Securing Item Spawning
    By darkest_mage in forum Tutorials
    Replies: 0
    Last Post: 09-16-2007, 07:34 AM
  4. adding item spawns my way
    By luke1105 in forum Tutorials
    Replies: 1
    Last Post: 09-08-2007, 03:45 PM
  5. making items spawn on ground
    By messiaH in forum Tutorials
    Replies: 4
    Last Post: 05-18-2007, 04:12 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •