I made a mod that prevents hostile entities from despawning if they've been damaged. The purpose of this was to create a 'zoo' for my Minecraft survival world where I gather all the mobs legitimately able to be gathered and place them in my zoo for viewing. It works pretty well, but after a while, mobs will stop spawning, presumably because the mob limit has been met by mobs hiding in the shade of trees, or enderman who zipped into water, or critters who fell off of cliffs, or whatever, and never died. In that case I have to temporarily disable the mod, while standing inside the zoo (to prevent despawning), then re-enable it. This has only happened once after my friend and I went on an epic journey through the nether, building portals along the way, but I'd rather it didn't happen at all. My preferred method would be to instead prevent despawning if mobs were within some predetermined distance of some predetermined block (say, within 5 blocks of a glass block), but I can't figure out how to get the distance to the nearest block.
Is there a method for getting distance to a nearest block, or looking within a certain radius of a mob to see if a block exists within that radius? I could use that in my "if" statement to turn off despawning, rather than looking at damage.
Ideally, I'd like it to point to a config file, so I can post the mod and people would be allowed to change the config file to use whatever block they would prefer for despawn prevention, but if that is too complicated, I'd be fine with just selecting some block ID in advance (it could always be buried out of site if someone didn't like the look of it).
I'd also like to use this method to set the spawn behavior of mobs in a separate, as yet unworked-on mod.
TL;DR: Is there a method for getting distance to a nearest block, or looking within a certain radius of a mob to see if a block exists within that radius?
Unless my understanding of Java is completely wrong, Minecraft's code just doesn't work that way. I can't really find the edges of a box in some class file. Presently, there is a method for getDistancetoEntity() and getEntitiesInAABB() which return the distance to a certain entity from a given object and return the id's of entities within a certain radius from a given object. I am just trying to find out if there is a similar method like getDistancetoBlock() or getBlocksInAABB(), but there doesn't seem to be.
I suppose I may be able to use the above methods to find out if entities are near a certain block, but it seems like it would eat up a lot of memory if every block of a certain type was always checking for entities. I'm also not sure how I would change the behavior of the entity itself based on the code in the block. If I could figure it out that way, maybe I could make a block that uses a custom ID (call it... blockAnchor) and see if I could put an upper limit on the number that could be placed at any given time...
If you don't care to recode several parts of minecraft you could reprogram every mob and creature to detect a set block in range. If that block is detect set candespawn to false. or like rainingnoob said just make a mobspawner for each mob that way your zoo will always have stuff in it. If you use bukkit for your server there is a plugin that allows you to change the mobspawner to any mob including mobs from mods.
Rollback Post to RevisionRollBack
Author of Fluid Mechanics, Assembly Line, ICBM, Come Closer, and Artillects. Including Java Freelancer for hire specializing in creating and fixing minecraft mods.
If you don't care to recode several parts of minecraft you could reprogram every mob and creature to detect a set block in range. If that block is detect set candespawn to false. or like rainingnoob said just make a mobspawner for each mob that way your zoo will always have stuff in it. If you use bukkit for your server there is a plugin that allows you to change the mobspawner to any mob including mobs from mods.
I'd just make changes to entityliving, which I don't mind doing. I just can't figure out the method for actually looking for a block in range. I can get entities within a certain range using getentitieswithinaabb, but I don't know how to do the same for blocks.
EDIT: I'm pretty new to Java, so maybe I'm using the word "method" wrong. I'm looking for a command, function, module, whatever the appropriate term is, which either
a. tells me the distance from entity A to the nearest block B or
b. returns the ids of each block within a radius r (or x, y, z) of entity A.
I know this topic is quite old now, but im only starting out with java, and am looking for the same thing velcerik is.
I'm trying to create an ai where if a water mob takes drown damage (from being out of water) it would check for the nearest water block on a set radius and make its way to that block.
If anyone can help please do, help is always appreciated
Since this thread has been necroposted, in case anyone is interested you can accomplish the original poster's idea (preventing despawning in a zoo) fairly easily. There is an event called AllowDespawn. If you handle that you can prevent despawning.
In terms of finding a special block in area near the entity, you can just iterate through all the blocks in the area to check. Since you'd only have to run this code at the occasional time the entity might try to despawn there shouldn't be any real performance impact to worry about.
I know this topic is quite old now, but im only starting out with java, and am looking for the same thing velcerik is.
I'm trying to create an ai where if a water mob takes drown damage (from being out of water) it would check for the nearest water block on a set radius and make its way to that block.
If anyone can help please do, help is always appreciated
Girintina101
You should really just start a new thread. Anyway, here is what you would have to do.
First of all, the AI for most mobs is in a public list called tasks. Since it is public, that means in Java that you can edit it from other classes. So you can totally replace the AI for any vanilla mob. In this case you'll need to create another AI class for the case where it is out of the water. The AI class would check if it should execute based on it being out of water, and then set a path/move if it finds water nearby.
To find water nearby is very simple. Just do a for loop through all the positions around the entity and check for the block type.
Is there a method for getting distance to a nearest block, or looking within a certain radius of a mob to see if a block exists within that radius? I could use that in my "if" statement to turn off despawning, rather than looking at damage.
Ideally, I'd like it to point to a config file, so I can post the mod and people would be allowed to change the config file to use whatever block they would prefer for despawn prevention, but if that is too complicated, I'd be fine with just selecting some block ID in advance (it could always be buried out of site if someone didn't like the look of it).
I'd also like to use this method to set the spawn behavior of mobs in a separate, as yet unworked-on mod.
TL;DR: Is there a method for getting distance to a nearest block, or looking within a certain radius of a mob to see if a block exists within that radius?
I suppose I may be able to use the above methods to find out if entities are near a certain block, but it seems like it would eat up a lot of memory if every block of a certain type was always checking for entities. I'm also not sure how I would change the behavior of the entity itself based on the code in the block. If I could figure it out that way, maybe I could make a block that uses a custom ID (call it... blockAnchor) and see if I could put an upper limit on the number that could be placed at any given time...
I'd just make changes to entityliving, which I don't mind doing. I just can't figure out the method for actually looking for a block in range. I can get entities within a certain range using getentitieswithinaabb, but I don't know how to do the same for blocks.
EDIT: I'm pretty new to Java, so maybe I'm using the word "method" wrong. I'm looking for a command, function, module, whatever the appropriate term is, which either
a. tells me the distance from entity A to the nearest block B or
b. returns the ids of each block within a radius r (or x, y, z) of entity A.
I know this topic is quite old now, but im only starting out with java, and am looking for the same thing velcerik is.
I'm trying to create an ai where if a water mob takes drown damage (from being out of water) it would check for the nearest water block on a set radius and make its way to that block.
If anyone can help please do, help is always appreciated
Girintina101
Since this thread has been necroposted, in case anyone is interested you can accomplish the original poster's idea (preventing despawning in a zoo) fairly easily. There is an event called AllowDespawn. If you handle that you can prevent despawning.
In terms of finding a special block in area near the entity, you can just iterate through all the blocks in the area to check. Since you'd only have to run this code at the occasional time the entity might try to despawn there shouldn't be any real performance impact to worry about.
You should really just start a new thread. Anyway, here is what you would have to do.
First of all, the AI for most mobs is in a public list called tasks. Since it is public, that means in Java that you can edit it from other classes. So you can totally replace the AI for any vanilla mob. In this case you'll need to create another AI class for the case where it is out of the water. The AI class would check if it should execute based on it being out of water, and then set a path/move if it finds water nearby.
To find water nearby is very simple. Just do a for loop through all the positions around the entity and check for the block type.