[JavaScript] Minecraft Minigames 3: CTF Arena - Combat

Planning
Step 1
In this lesson we are going to start building a capture the flag minigame for Minecraft using JavaScript!
Temp1465589872
Step 2
The first thing we need to do is find a large open area to place our arena
Temp1465416262
Step 3
Enter the command //wand to get a WorldEdit wand
Temp1465416298 Temp1465416334
Step 4
Select a large region and set the blocks to dirt

Left-click to select the first corner of the region
Right-click to select the second corner
Type the command //set dirt to change all selected blocks to dirt

You should then have a large flat area like this:

Step 5
Load the arena schematic

Enter the command //schem load mcedit arena-ctf.schematic to load the arena schematic using WorldEdit

Temp1465416880
Step 6
Position yourself on the edge of your flattened platform and paste the schematic

Use the command //paste to paste the arena schematic into your world.


NOTE: If it is positioned poorly, you can use the command //undo to remove it and try pasting it from somewhere else or sculpt your world a bit more before pasting.

Step 7
Add an appropriately colored banner to each base that we can use as a flag

NOTE: You will have some time to customize your arena during free play, for now just add the banners.

Step 8
Begin by defining the variables we are going to need for this plugin

Define these variables at the top of your plugin.js file

Temp1465507904
Step 9
Create variables to store some of the important locations

We will be starting the players in their base, sending them to jail when hit, and then returning them to their base from jail after 10 seconds. To do this we will need to know the coordinates of those locations in the arena.


X, Y, Z, PITCH, and YAW are placeholders, we will replace them with actual values in the next step.

Temp1465508697
Step 10
Position yourself in the spot you want the red team to spawn inside their base and press F3 to view the debugging info

We will use these values as the teleport location for the red base.
NOTE Always round to the nearest half or whole number (half will place you on the center of a block) don't bother with the decimal places.

In this example, we will say var redBase = location(world, -217.5, 84, 311, 180, -2.5);

Temp1465509146

Replace the placeholders with the actual values in your code:

Temp1465509107
Step 11
Do the same for the blue base

In this example we will say:


var blueBase = location(world, -92.5, 84, 289, 0, -2.5);

Temp1465509284
Step 12
And do the same again for the jail

For the jail, leave out the PITCH and YAW values. We don't really care which direction they are facing when we send them to jail, as long as they are trapped in the glass box!


In this example we will say:


var jail = location(world, -154.5, 81, 300);

Temp1465509409
Check Your Work
Step 13
At this point you should have values set for these three locations.

REMEMBER: Your values will be different!

Temp1465509565
Information
Step 14
Remember to SAVE YOUR WORK!

Click this save button at least once per minute if not more often. Do not lose code to forgetting to save and then leaving the page.

Temp1465517759
Step 15
The startCTF() function will start the game!

This function sets up the scoreboard and calls the addPlayer() function for each player on the server. We will define this function in the next step.

Temp1465510745
Step 16
Define the addPlayer() function

This function divides the online players into two even teams, initializes the scoreboard values for each player, and sets the players' starting attributes (survival mode, full food, holding snowball, etc.)

Temp1465510964
Step 17
Define the stopCTF() function

This function stops the game by setting gameStarted to false, clearing all scoreboards, player inventories, and existing timeouts, and displaying the "Game Stopped" message.

Temp1465511273
Step 18
Give the players infinite snowballs

Every time a player throws a snowball, give them a new one after 500 milliseconds. This will prevent the players from holding down the fire button to spam snowballs, they will have to aim with a fire rate of only 2/sec. Feel free to adjust the fire rate if you want, the slower the rate the more important aiming will become.

Temp1465511483
Step 19
We will use the EntityDamageByEntity event to handle players being hit by snowballs

This event will send the player hit by the snowball to jail for 10 seconds before returning them to their base and increase the score of the player who landed the hit in the "player_list" scoreboard (shows next to username when you press TAB)

Temp1465511953 Temp1465512013
Information
Step 20
Let's break this function down

Take your time, read the explanations, and try to understand what each piece of the code is doing for us.

Temp1465514187
Step 21
Save, deploy, and reload!

Remember to save your code many times along the way, not just at the end. You will need to deploy and reload your server each time you make changes to your plugin that you want to test in game.

Temp1465517626
Check Your Work
Step 22
Have an instructor help you test your plugin

With two players on the server, type /js startCTF() to start the game. You should be given a snowball and placed in your base. Test your ability to hit the opposing player with a snowball and send them to jail. They should return after 10 seconds, and the player who landed the hit should have their score increase by one. When you are finished testing, type /js stopCTF() to test ending the game.

Step 23
But what if a player joins while the game is in progress?

If a player connects to the server while the game is in progress we want to assign them a team and place them right into the action. This function will be called whenever a PlayerJoinEvent occurs and will call the addPlayer() to add them to a team!

Temp1465517251
Check Your Work
Step 24
Make sure players are added to the game if they connect to the server while the game is in progress

Type the command /js startCTF() to start the game and have an instructor connect to your server.

Temp1465517475
Planning
Step 25
In the next lesson we will add a key element to our capture the flag game, the flags!
Temp1465603067
[JavaScript] Minecraft Minigames 3: CTF Arena - Combat Info

Account

MVCode Clubs

Created By

Mvcc logo new aaron

Course:

Minecraft Minigames in JavaScript

Access Level

premium

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