[JavaScript] Minecraft Minigames 5: CTF Arena - Improvements

Planning
Step 1
In this lesson will add some improvements to our capture the flag game like colored armor for each team!

We will also prevent cheating by stopping players from breaking blocks, using the inventory, and more!

Temp1465603740
Step 2
It's very hard to tell who is on each team when they are far away. Let's add some colored armor to make it easier.

This function giveArmorSet(player, color) will give a full set of leather armor of the given color to the player

We will call this function in addPlayer() to give players armor at the start of the game.

Temp1466094630
Step 3
Call the giveArmorSet() function in addPlayer() to give the player armor when they are assigned a team

Add these lines of code to the addPlayer(player) function.

Temp1466094769
Check Your Work
Step 4
Save, deploy, reload, and test your colored armor

Make sure you have a full set of leather armor matching your team's color.

Step 5
Each time you are hit by a snowball your armor will lose some durability, so let's repair the armor when you get hit.

Add this function repairArmor(player) that will repair all armor for the given player

Temp1466096216
Step 6
While a player is in jail, repair their armor.

Add this code to the onEntityDamageByEntity function.

Temp1466096412
Step 7
While the game is running we need to prevent players from cheating and/or destroying the arena

Let's add some code to prevent all block breaking events from occurring while the game is running.

Temp1466096858

Add this code to your plugin to prevent players from breaking blocks while gameStarted is true

Temp1466097002
Check Your Work
Step 8
Start the game and then try to break a block, it won't work!
Step 9
We also need to prevent several other events from occurring to stop players from cheating or breaking the game

Events we are going to cancel:
1. Block Breaking
2. Block Place
3. Item Switch
4. Inventory Click
5. Drop Item
6. Food Level Change
7. Entity Damage


Add the other 6 event cancelling functions to your plugin:

Temp1466101046 Temp1466101195 Temp1466101275 Temp1466101327 Temp1466101368 Temp1466101413
Step 10
Save, deploy, and reload!
Temp1466101859
Check Your Work
Step 11
Start the game and test your canceled events

Things you should check for:
1. You cannot break blocks
2. You cannot place blocks
3. You can't switch the item in your hand
4. You cannot move items around your inventory
5. You cannot drop items
6. Your food level does not change after running
7. You don't take damage from fire or falling

Step 12
Next let's make the game more interesting by adding powerups that grant the player random effects

Choose where you want the powerups to spawn and place a block of your choice in those positions to mark them. In this example I will use emerald blocks to mark the powerup spawn points because the powerups will be emeralds. Feel free to choose a different item and/or block for your powerups.

Temp1466180530



Here is a video of me marking my powerup locations. You don't need to place them in these exact locations, this is just a suggestion.

Step 13
Make a new array to hold all the powerup locations

Name this array powerUpLocs and leave it empty for now.

Temp1466181527
Step 14
Find the powerup spawning locations for each of your powerup spawn points

Position yourself facing straight down on top of a block marking a powerup spawn point, looking at the center of the block as shown here:

Then find the location you are standing and round to the nearest half (0.5) unit

Temp1466182107



Then add this location to your code like this:

Temp1466182449
Step 15
For powerup locations marked with two blocks find the location as shown here to center the powerup

If you used two blocks to mark a powerup location so that you could keep it centered in the walkway, find the center of the line on which the two blocks touch as shown here:

Temp1466182806



Then add this location to your code like this:

Temp1466186467
Step 16
Continue with this process until you have set all of the spawn locations for your arena

In this example I have 17 powerup locations. You can choose to have as many as you want, but make sure the locations are balanced between the two teams!

Do not copy this code exactly, your numbers will be different!

Temp1466188523
Step 17
Define a function that places a dropped emerald at a given location

We will use this function to spawn a dropped emerald (or other item, feel free to change the item used as the pickup) at a given location. Since items move a bit when they are dropped we will teleport the item back to the desired location 1500ms after spawning it.

Temp1466453539
Step 18
When the game is started spawn a powerup in each location in the powerUpLocs array

This for loop will call the function spawnPowerUp(location) once per location stored in the powerUpLocs array, spawning a dropped emerald at each location.

Add this code to the startCTF() function.

Temp1466453844
Step 19
Save, deploy, and reload
Temp1466454200
Check Your Work
Step 20
Start the game and check each powerup location for a dropped emerald

Make sure ALL of the locations in the powerUpLocs array have a dropped emerald!

Step 21
When a player picks up an item give them a randomized potion effect

Define a function onItemPickup that we will register to be called when a PlayerPickupItemEvent occurs. This function will grant one of six random potion effects to the player for a short period of time.

Temp1466456168



Let's break this function down so it is easier to customize:

Temp1466456574
Information
Step 22
The newPotionEffect() function

Let's talk a bit about the newPotionEffect(effect, duration, strength) function so it is easier for you to customize the powerup effects in your game.


The newPotionEffect() function takes three parameters:
effect - the type of potion effect to create
duration - the number of seconds the effect will last
strength - the power level of the effect

Temp1466457963

This will result in a Speed IV effect that lasts for 30 seconds

Here is a list of all possible effect types: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/potion/PotionEffectType.html


NOTE: Not all of these will be useful or detrimental during a capture the flag minigame so try to pick effects that will help or harm the players in the context of the game!

Step 23
Customize the powerup effects

This step is optional.


Customize the powerup effects that the players can be granted by changing the effect type, duration, and/or strength of the possible powerup effects.

Temp1466460166
Check Your Work
Step 24
Test out your powerup effects

Make sure you are granted a random effect when you pick up an emerald. The emerald should also be removed from your inventory immediately upon picking it up.


You may notice that when you are granted invisibility your armor is still visible so it doesn't help very much! In the next step we will temporarily remove the players armor when they get an invisibility effect.

Step 25
Define a function removeArmor(player) that will remove all armor from a given player

The removeArmor(player) function will remove all armor from the player
We will use this to make the player completely invisible during the invisibility effect.

Temp1466460775
Step 26
Remove the player's armor if they are granted an invisibility effect

This code will remove all armor from a player when they are granted the invisibility effect. It also gives them a new set of armor of the correct color after 30 seconds (when the invisibility effect ends). If you changed the duration of your invisibility effect make sure this delay matches the amount of time the invisibility effect lasts.

Temp1466461016
Check Your Work
Step 27
Test your improved invisibility powerup

Make sure your armor is removed when you get an invisibility effect and the right color armor is returned to you when the effect ends.

Challenge
Step 28
CHALLENGE: Customize your game!

Here are so ideas to get you started:

Things to consider modifying
- Fire rate of snowballs
- Effect types, duration, strength (i.e. short duration flight effect)
- Number of powerups and their locations
- Number of flag captures required to win

Things to add
- Take multiple hits to be sent to jail
- Elimination mode: players never returned to base from jail
- Your own ideas!

[JavaScript] Minecraft Minigames 5: CTF Arena - Improvements 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