You can spawn a random amount of chicken jockeys with the third Minecraft mod from “Absolute Beginner’s Guide To Minecraft Mods Programming, second edition” by Rogers Cadenhead.
In the onCommand method add an if-else statement for if (label.equalsIgnoreCase("zombiepig"))
then call a new method named executeZombiePigCommand(sender). You can copy/paste the executeCommand method and just change the title to executeZombiePigCommand. Next, update the line that says Chicken clucky = world.spawn(chickenSpot, Chicken.class);
so that it will spawn an instance of the Pig class and save it a variable type Pig (you can still use the variable name clucky if you want). Finally, update the line that says Zombie rob = world.spawn(chickenSpot, Zombie.class);
so that it spawns an instance of the PigZombie class and saves it in a variable type PigZombie.
In the onCommand method add an if-else statement for if (label.equalsIgnoreCase("villagersheep"))
then call a new method named executeVillagerSheepCommand(sender). You can copy/paste the executeCommand method and just change the title to executeVillagerSheepCommand. First, make it spawn an instance of the Sheep class and save it a variable type Sheep (you can still use the variable name clucky if you want). Next, make it spawn an instance of the Villager class and saves it in a variable type Villager. Look at the Spigot API for the Villager class and find the setBaby method. Notice that unlike the setBaby method for the Zombie class, this method does NOT require any arguments. Finally, look at the Spigot API for the Sheep class and the DyeColor class in order to create an array of type DyeColor that contains several colors, randomly select a DyeColor from the list, and set the sheep’s wool color.
A normal distribution (also known as a bell curve) will expect that median values are more likely and extreme values are less likely. Consider rolling two dice. You have about a 44% change of rolling a 6, 7, or 8, but you only have about a 3% chance of rolling a 12. We would simulate rolling two dice in Java like this (int) (Math.random() * 6 + 1) + (int) (Math.random() * 6 + 1)
and we would get a bell curve of values ranging from 2 to 12. How can you change that code to generate a bell curve of values ranging from 8 to 16?
Source: https://javaminecraft.com/source/ZombieChicken/ZombieChicken.java
package com.javaminecraft;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Chicken;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public class ZombieChicken extends JavaPlugin {
public static final Logger LOG = Logger.getLogger("Minecraft");
@Override
public boolean onCommand(CommandSender sender, Command command,
String label, String[] arguments) {
if (label.equalsIgnoreCase("zombiechicken")) {
if (sender instanceof Player) {
executeCommand(sender);
return true;
}
}
return false;
}
public void executeCommand(CommandSender sender) {
Player me = (Player) sender;
Location spot = me.getLocation();
World world = me.getWorld();
// spawn 1-10 chicken-riding zombies
int quantity = (int) (Math.random() * 10) + 1;
for (int i = 0; i < quantity; i++) {
// set chicken and zombie location
Location chickenSpot = new Location(world,
spot.getX() + (Math.random() * 15),
spot.getY() + 5,
spot.getZ() + (Math.random() * 15));
// create the mobs
Chicken clucky = world.spawn(chickenSpot, Chicken.class);
Zombie rob = world.spawn(chickenSpot, Zombie.class);
rob.setBaby(true);
clucky.setPassenger(rob);
// increase the chicken's speed
int speed = (int) (Math.random() * 10);
PotionEffect potion = new PotionEffect(
PotionEffectType.SPEED,
Integer.MAX_VALUE,
speed);
clucky.addPotionEffect(potion);
}
LOG.info("[ZombieChicken] Created " + quantity + " chicken-riding zombies");
}
}
The second Minecraft mod from “Absolute Beginner’s Guide To Minecraft Mods Programming, second edition” by Rogers Cadenhead will spawn a random number of chickens in the air above the player when they type the command “/chickenstorm”
This is a very popular mod that I’ve actually had students mention to me in the past. For example… “In our Intro To Programming class, we learned how to make Minecraft mods and we made it rain chickens.”
Edit line 39 of the starter code for (int i = 0; i < Math.random() * NUM_CHICKENS + 1; i++) {
to something like for (int i = 0; i < randomAmount; i++) {
then add a new line above in which you declare a variable named randomAmount
and set its value to a random number 20-30. Here is how to generate a random range in Java… (int) (Math.random() * (maximum - minimum) + minimum)
The key to understanding this is to know that Math.random() will give you a decimal value from 0.0 to 0.9999999999. If we multiply Math.random * 30 then we will get random decimal values from 0.0 to 29.9999999999, but that’s not what we want. We should instead multiply by the number of possible values we want and there are 11 possible values from 20 to 30. Finally, add 20 to the random number.
In the onCommand method add an if-else statement for if (label.equalsIgnoreCase("firestorm"))
then call a new method named executeFirestormCommand(sender). You can copy/paste the executeCommand method and just change the title to executeFirestorm. Next, remove the code that makes 40% of them babies. Instead we will set them on fire. Check the Spigot API for the Chicken class and look for one of the Entity methods that has “fire” in the method name. You should find 3 methods that have to do with fire, but only one of them is a setter method. Invoke the method on clucky
and provide a large integer number (such as 999) as an argument.
If you completed the last challenge, then you probably created chickens that were on fire for about 4 seconds and then died. This just leaves feathers and roasted chicken all over the place. How can we make a REAL firestorm? First, check the Spigot API for the Chicken class and find how to increase the max health and set the health of clucky
. You might also notice that the Chicken class implements the ProjectileSource interface. You can use Fireball flamey = clucky.launchProjectile(Fireball.class);
to generate a Fireball projectile. Finally, check the Spigot API for the Fireball class and find out how to set the direction of flamey
. You will need to provide a Vector object as an argument, such as new Vector(0, -1, 0)
Source: https://javaminecraft.com/source/ChickenStorm/ChickenStorm.java
package com.javaminecraft;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Chicken;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class ChickenStorm extends JavaPlugin {
public static final Logger LOG = Logger.getLogger("Minecraft");
// the maximum number of chickens
private static final int NUM_CHICKENS = 30;
@Override
public boolean onCommand(CommandSender sender, Command command,
String label, String[] arguments) {
if (label.equalsIgnoreCase("chickenstorm")) {
if (sender instanceof Player) {
executeCommand(sender);
}
return true;
}
return false;
}
// handle the chickenstorm command
public void executeCommand(CommandSender sender) {
Player me = (Player) sender;
Location spot = me.getLocation();
World world = me.getWorld();
int quantity = 0;
// loop from 1 to NUM_CHICKENS times
for (int i = 0; i < Math.random() * NUM_CHICKENS + 1; i++) {
// pick a spot for the chicken above the player
Location chickenSpot = new Location(world,
spot.getX() - 15 + Math.random() * 30,
spot.getY() + 10 + Math.random() * 100,
spot.getZ() - 15 + Math.random() * 30);
if (chickenSpot.getBlock().getType() != Material.AIR) {
// don't put the chicken in a solid block
continue;
}
// create the chicken
Chicken clucky = world.spawn(chickenSpot, Chicken.class);
if (Math.random() < .4) {
// make 40% of them babies
clucky.setBaby();
} else {
// make the rest adults
clucky.setAdult();
}
quantity++;
}
// tell the server log how many were created
LOG.info(quantity + " chickens summoned");
}
}
This mod from “Absolute Beginner’s Guide To Minecraft Mods Programming, second edition” by Rogers Cadenhead will spawn a wolf when the player types the command “/petwolf”
The first thing to notice is that the starter code is MISSING an important line of code. The starter code below will spawn a wolf at the player’s location and set the collar color to pink. The starter code does NOT set the player to be the owner of the wolf.
Check the Spigot API documentation for the Wolf class and look for one of the Tameable methods that lets you change the owner.
Check the Spigot API for the DyeColor class for a list of the class constants (for example DyeColor.PINK), create an array of type DyeColor that contains several colors, and randomly select a DyeColor from the list.
The Wolf class lists the methods that can change a wolf’s custom name (look at the Nameable methods). Create an array of dog names, randomly select a name and use substring to get only the first half, randomly select a name and use substring to get only the second half, then set the wolf’s custom name to be the first half concatenated with the second half. This should generate interesting names like Coodo (Cooper + Fido), Root (Rover + Spot), Fiper (Fido + Cooper), Coover (Cooper + Rover), Fiot (Fido + Spot), Fiver (Fido + Rover) and Rodo (Rover + Fido).
Source: https://javaminecraft.com/source/PetWolf/PetWolf.java
package com.javaminecraft;
import java.util.logging.*;
import org.bukkit.*;
import org.bukkit.command.*;
import org.bukkit.entity.*;
import org.bukkit.plugin.java.*;
public class PetWolf extends JavaPlugin {
public static final Logger LOG = Logger.getLogger("Minecraft");
public boolean onCommand(CommandSender sender, Command command,
String label, String[] arguments) {
if (label.equalsIgnoreCase("petwolf")) {
if (sender instanceof Player) {
// get the player
Player me = (Player) sender;
// get the player's current location
Location spot = me.getLocation();
// get the game world
World world = me.getWorld();
// spawn one wolf
Wolf wolf = world.spawn(spot, Wolf.class);
// set the color of its collar
wolf.setCollarColor(DyeColor.PINK);
LOG.info("[PetWolf] Howl!");
return true;
}
}
return false;
}
}
RECENT POSTS