Pig Stacking Plugin

About this lesson

Use loops to place a pile of entities atop one another.

Concepts Reviewed:

While Loops
Step 1
In this lesson, we will use loops to create a tower of pigs
Step 2
First we will use loops to make a simple function

This function will be used to display many blank lines in chat.

Step 3
Create our function

exports.name turns that name into a function you can call in chat

Step 4
Create an empty line in chat

Add this code into your clearChat function

Temp1517538211 Temp1517538321
Step 5
Create a variable to control how many times we want to do this.

linesToClear is a variable that will keep track of how many times we want to echo

Step 6
Create a While loop

This while loop should be around your echo, but beneath the linesToClear variable

Step 7
Decrease linesToClear after you clear a line
Temp1566259394 Temp1517961284
Check Your Work
Step 8
This is the code you should now have
Step 9
More lines!

Make your function clear all visible lines from chat

Step 10
It's pig stacking time

The rest of the lesson will walk you through creating a stack of pigs.

Step 11
Spawning a single pig

This is the code to spawn a pig.
In order to use it, we need a world and a location


Type the following two lines into chat to set up these variables.

Temp1517961749 Temp1517961835

Now we can use the above command to spawn a pig at that location in that world

Temp1517962127 Temp1517962221

Note that every time we reset the world we will need to recreate these variables

Step 12
Set up what we just did in game as its own function

Create pigStack as a new function at the bottom of our code
This will let us call /js pigStack() to spawn a pig at our location

Check Your Work
Step 13
Step 14

Make a loop like we did above to create more pigs!

Step 15
Return types

when you call the world.spawnEntity(location, entityType.pig) function in chat, it returns to you CraftPig
CraftPig is the pig we just spawned


we can store this return in a variable to further affect our pig

Temp1518130063 Temp1519257549
Step 16
Store Pigs being spawned in variables

Add this to our pigStack function.
This will allow us to reference the pigs we are creating.


Lets give them names to better keep track of them

Temp1566260076 Temp1519257603
Step 17
Now to stack them!

add this after you give them custom names

Temp1566260159 Temp1519257618
Step 18
More Pig Stacking!

update your while loop so we make a new upper pig each time.

Temp1566260290 Temp1519257643

This stacks all of the pigs on top of one pig

Step 19
Taller Stacks!

make the old upperPig the new lowerPig

Temp1566260364 Temp1519257722
Step 20
Different Creatures

change the entityType to your favorite creature

Temp1519259265 Temp1519259812 Temp1519259895 Temp1519260306
Pig Stacking Plugin Info


MVCode Clubs

Created By

Missing tiny jake wheaton


Fundamentals in Minecraft

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach