Make entity teleport to a random nearby location but NOT inside blocks.

Started by lanto on Sun, 05/15/2022 - 01:22

Topic category: Help with modding (Java Edition)

Last seen on 22:06, 27. May 2022
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Make entity teleport to a random nearby location but NOT inside blocks.
Sun, 05/15/2022 - 01:22

Heyyy, i'm trying to make a mob teleport when hit, Enderman-style teleport.

I have two issues:

1.- I'm not sure how to limit how far it could teleport.

2.- I don't know how to make it not teleport inside blocks. (I don't want it to teleport inside the walls and suffocate if i hit him inside a cave or something.)

How could i do this? Is it too hard?

Last seen on 00:03, 27. May 2022
Joined Jul 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
If none of this makes since…
Sun, 05/15/2022 - 04:13

If none of this makes since to you then I would do more research like watch videos on MCREATOR basic coding.

There are ways to make this a little better but this is the basic idea of what you would need to do.

# The basic concept here is you have a max Distance that you want the entity to teleport to.

# So what you do is keep that number persistent with the other values you already updated/changed.

# So for example:

# Lets say you have a max distance of 3. Lets also say your coordinates are: X: 0 Y: 0 Z: 0.

# Okay so an example of something we could do is: [ X: 1 Y: -1 Z: 1 or X: -1 Y: 1 Z: -1 ]

# notice how many changes were made in the examples above, We made 3 changes and we either added 1 or subtracted 1 from each.

# so the idea in this example is you could either:

change 3 numbers 1 time,

change 1 number 3 times,

change 1 number 2 times and another number 1 time,

or also not change any.

# This code is a little sloppy but would work

x = x position of target

y = y position of target

z = z position of target

maxDistance = 50  # set this to be your max distance

maxDistanceSelected = 0  # don't change this value

random number generated from 0 to maxDistance every time you define the x, y, z coordinates

 

maxDistanceSelected = x + ((random [0,1]) * maxDistance)

x = maxDistanceSelected

maxDistance =  maxDistance - maxDistanceSelected

   

maxDistanceSelected = y + ((random [0,1]) * maxDistance)

y = maxDistanceSelected

maxDistance =  maxDistance - maxDistanceSelected 

 

maxDistanceSelected = z + ((random [0,1]) * maxDistance)

z = maxDistanceSelected

maxDistance =  maxDistance - maxDistanceSelected

then you need a loop.

H = get the entities height

counter= 0

isGoodTp = False

if get block at [x] ([y] - 1) [z] is air then call the code again. if not then

# We will now check if the block meets are standards of the parameters we have.

repeat H times   # this is a loop

counter = counter + 1 # this will help us keep track of how many blocks we have already check to see if it's air.

if get block at [x] [y] [z] is air block then continue. 

if get block at [x] ([y] + counter ) [z] is air block then continue if else break loop.

if counter == H then (isGoodTp = True)

 

# We have now made sure the that the location has something other than air at the bottom

#and also made sure the entity would not tp inside of a block.

if isGoodTp == True then set entities location to [X] [Y] [Z]