Started by Mopru on Sat, 01/28/2023 - 22:32
Topic category: Help with modding (Java Edition)
I'm just gonna put it straight: I'm making a dimension full of spiders and I want one to walk on water. I've tried a lot but no other tutorial has given me a straight answer. Either it isn't a code block or it just doesn't make sense to have as actual code (I don't know coding but it just doesn't look right). Please help I feel like I'm running in endless circles.
Hey, I came across your post and it seemed similar enough to some things I've already done, so I decided to give it a shot.
I understand this thread is a little old now and hopefully you've found a solution by now, but if not- and for anyone else who may come across this- I'll go through the process in stages:
First off, when you go to create a mob, make sure to tick the box to make them a water entity. Regarding their AI, you don't want to set the entity base to a spider, just leave it as "(none)". I tried testing this out multiple ways, but using the base spider AI appears to conflict with water-walking.
I arranged my AI task/goals list as follows:
(I tested the mob's aggro and path finding with chickens)
Now, regarding triggers/procedures, you do want one for "On each tick update".
In order to get the entity to walk on top of water, you basically just want it to swim as close to the surface as it can - while minimizing how much it bobs up and down in the water.
Here I started by checking for water just barely above the entity's position. If you check for water at the same level, or below the entity, the entity will be too high out of the water and won't be able to move properly. It'd just get stuck in the air above the water.
Next, if the velocity of the entity is below zero - in other words, if they're trying to move downward - force them to move back up instead. This prevents them from diving down and also helps keep them from bobbing up and down too much.
If the entity isn't moving downward, then add some upward velocity to them instead. This keeps them at a certain height at the surface of the water.
In my testing, I encountered a problem where constantly forcing the entity upwards resulted in it sometimes dying of fall damage once it touched ground, so I added a block in there to constantly reset its fall distance as long as it's just above water.
Next, I added dolphin's grace to the entity to increase its speed on water, as well as to give its movement a little 'slipperiness' - kind of like if it were walking on ice. It makes it look like it's walking on water a bit more realistically by giving it some momentum.
The second part of the procedure I'll get back to in a bit...
Now, onto code modifications. I'll preface this by saying I don't actually know how to write code, myself.
I can still read it decently well and I can make some adjustments here and there, but I don't know how to write something from scratch. So don't worry about how involving the following code is. It's pretty easy to copy over onto your custom entity.
Now then, you'll want to open up your custom entity's code, Drowned code, and Spider code. For those who don't know how to open up vanilla code, go ahead and drag the left sidebar in MCreator over (on the far left side of the MCreator window there's a bar you can drag to the right). Search for "drowned" and open "Drowned.java", and then do the same for the spider.
You also want to change your entity's "SWIM_SPEED" while you're editing its code. For whatever reason, a movement speed value of 0.3 results in decently quick movement on land, but a swim speed of 0.3 is pretty slow in the water. If you decided to give your entity dolphin's grace, I recommend setting its swim speed to 2.5. If you did not give it dolphin's grace, I recommend setting its swim speed to 3.5 instead.
Starting with the drowned code, look for what I've labeled in the images above and copy/paste that code into your custom entity's code. There is also some code you want to disable or delete from your entity. I personally prefer using /* and */ to turn chunks of code into a "comment" - allowing you to keep it there without it bothering anything. (Even if it's unnecessary to keep, you may want to toggle some code on or off like this as you're testing different things. You won't have to look for- or rewrite- something you deleted if you simply turned it into a comment instead.)
After you copied the drowned code over, I suggest saving and locking your entity's code, and then attempting to run the client to make sure what you just did works. (If you do mess up somewhere, it's easier to debug if you don't make so many changes at once.) Afterwards, do the same thing you just did with the spider code. You'll also have to write one line to have your entity's code import the spider code- since one thing you copied requires it.
Now that that's out of the way, in theory your custom entity should be done. However, I couldn't seem to figure out how to get the entity to correctly path find and climb up to a target that's out of the water. I tried adding additional bits of code and tweaking things, but to no avail. So, open up that procedure you made and add the second half.
What this does, is it checks to see if your custom entity sees something it wants to attack. If it does, then it compares the location of itself and its target, and then gives your entity a constant, slight push towards its target. Since it can climb like a spider now, this push causes your entity to climb out of the water and onto land- including up walls- to go after the target it's trying to attack.
Lastly, I do also want to go over a few (potential) problems with this entity. Overall I'd say it functions pretty well; however, there were some kinks I couldn't seem to work out...
Firstly, when standing on water and simply idling, they sometimes just... spin... I presume it's due to them trying to path to a location below them that they cannot reach.
Secondly, if they are in water and their target is on land - they don't path to their target too well. They prefer to stick to the water - which could be a good thing or a bad thing depending on how you want them to behave. They won't go straight for a target on land; instead, they'll try and swim around to whatever water is closest to their target. The second part of the procedure helps but doesn't completely fix this issue. If it doesn't work sufficiently to your liking, I recommend slightly upping the velocity values. Try 0.005 for a start. As you increase these values, you may want to decrease the speed value associated with their MeleeAttackGoal so they don't become too fast.
And finally, there have been one or two cases in my testing where one of these entities will just start walking away from their target without stopping. I haven't been able to reproduce this bug, but thought it worth mentioning.
I'm not really familiar with how embedding images works here, but since they haven't loaded properly in my first post, as the time of writing, here's a link to the imgur album for them: https://imgur.com/a/gBPq3Xp