A catapult in Minecraft
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/main symplifying code Jul 31, 2017
.gitignore removed unnecessary cow behaviors from catapult Jun 14, 2017
README.adoc Clarified how to get the code working Nov 20, 2017

README.adoc

If you have any questions regarding this workshop, you can write to cesar.tronlozai@gmail.com

Download and Install

Download

Download the following software

Mac or Windows

Follow the instructions for Mac or Windows as explained below.

Mac

Make sure all the software lised in Download and Install is downloaded.

  1. In Finder, go to Applications, Utilities, double click on Terminal

  2. Change directory to Desktop as cd Desktop

  3. Make a new directory by giving the command mkdir forge

  4. Change to the directory cd forge

  5. Unzip the downloaded Forge 1.7.10 zip file as unzip ~/Downloads/forge-1.7.10-10.13.4.1558-1.7.10-src.zip

  6. Give the command ./gradlew setupDecompWorkspace eclipse. The output should show BUILD SUCCESSFUL.

Windows

Make sure all the software lised in Download and Install is downloaded.

  1. Open a Command Prompt as explained at http://windows.microsoft.com/en-us/windows-vista/open-a-command-prompt-window

  2. Change directory to Desktop as cd Desktop

  3. Make a new directory by giving the command mkdir forge

  4. Change to the directory cd forge

  5. In File Explorer, double click on the Forge 1.7.10 zip file downloaded earlier and extract all the contents in this newly created directory.

  6. In Command Prompt, give the command gradlew setupDecompWorkspace eclipse. The output should show BUILD SUCCESSFUL.

A detailed video with complete installation steps on Mac is given below:

Mac & Windows

  1. Delete the src folder found inside the forge folder which you have just created

  2. Click the download button to get a zip file with the starting source code

  3. Extract the src folder and copy into the forge folder

Verify

This is a very important step as this will confirm that you can actually start modding.

  1. Open up Eclipse

  2. In the “Eclipse Launcher” window, click on Browse…​, choose the location of eclipse directory in the unzipped forge directory

  3. Click on “OK”

  4. In the Eclipse window, you should see a “Minecraft” folder and be able to expand it. There should be no red exclamation marks next to it.

Using the Catapult

  1. Click on the green play button at the top of Eclipse to launch Minecraft

  2. In the game, make sure to create a world in Creative Mode

  3. In the game, open your inventory by pressing E

  4. Click on the compass at the top-right to enter search mode

  5. Search for Spawn Catapult

  6. Press T to open your chat window

  7. Right click on the ground while holding the Spawn Catapult to create a catapult

  8. Use the command /catapult <angle> <power> <color> to set the angle, power, and color of the projectile to be launched

  9. Right click on the catapult to launch a projectile

  10. To clear trajectories, equip a Sword and right-click on the catapult

Workshop content

Introduction

The aim of the workshop is to introduce Minecraft modding, basic Java principles and - it goes without saying - bring destructions with exploding cows.

The master branch contains all the functioning code, the workshop branch is the starting point for this workshop

Steps

  1. Introduction

  2. Spawning the catapult

  3. Trajectories

  4. First shot

  5. Explosions

  6. Better Explosions

  7. Huge Explosions

Introduction

We Introduce

  • Java

  • Minecraft modding

  • Eclipse IDE

  • Free fall

  • CERN (volunteers to give details explanation about the standard model and the Higgs boson )

Spawning the catapult

Let’s start Minecraft by clicking on the green arrow and create a new world in Creative Mode.

When we open the inventory (press E) and search for catapult we cannot find anything. We need to register the new catapult entity in the main modding file.

In Main.java

@EventHandler
public void init(FMLInitializationEvent event)
{
   registerColors();
   registerModEntity(EntityCatapult.class, new RenderCatapult(),
     "catapult", EntityRegistry.findGlobalUniqueEntityId(),
     0xC38751, 0xDCA556);
}

now that the catapult is registered, we can restart Minecraft and repeat the operation. We can now find the egg for the catapult. Let’s move it in the inventory, equip it, right click and we have a catapult!

Trajectories

Explain

  • Initial angle

  • Initial speed

  • Azimuth

Now we can do /catapult 40 10 red 0 and when we right click on the catapult we see a new trajectory

Let the kid play with different angles and velocity. Make them change the colour when changing settings so they can show different trajectory at the same time.

For a given power, what is the best angle (i.e. the one which gives the best range)?

First shot

Catapult are for trowing, so let’s throw something. A cow? Why not?!

In EntityCatapult.java we can prepare the cow to be thrown (just before return true):

EntityCow cow = new EntityCow(world);

The cow must ride on a block, so we need to prepare that block too:

EntityFallingBlock block = createBlock(false);
cow.mountEntity(block);

Then we need to spawn the entities into the Minecraft world:

world.spawnEntityInWorld(block);
world.spawnEntityInWorld(cow);

Let’s not forget to return true at the end of the method. The method should be

public boolean interact(EntityPlayer player) {
		World world = player.getEntityWorld();

		if (!world.isRemote) {
			return false;
		}

		double angle = Main.angle;
		double power = Main.power;

		if (!Main.parametersSet) {
			player.addChatComponentMessage(Main.createChatMessage(
					"Use " + new CommandCatapult().getCommandUsage(null) + " first!", EnumChatFormatting.RED));
			return false;
		}

		if (player.getHeldItem() != null && player.getHeldItem().getItem() instanceof ItemSword) {
			clearTrajectories();
			player.addChatComponentMessage(Main.createChatMessage("Cleared all trajectories", EnumChatFormatting.AQUA));
			return true;
		}

		if (!trajectories.contains(new Trajectory(angle, power, Main.getColorBlock().getColor(), Main.rotationAngle))) {
			trajectories.add(new Trajectory(angle, power, Main.getColorBlock().getColor(), Main.rotationAngle));
			player.addChatComponentMessage(Main.createChatMessage("Added a trajectory with Angle: " + angle
					+ " degrees, Power: " + Main.shownPower + ", Color: " + Main.color, EnumChatFormatting.AQUA));
		}

		EntityCow cow = new EntityCow(world);
		EntityFallingBlock block = createBlock(false);

		cow.mountEntity(block);

		world.spawnEntityInWorld(block);
		world.spawnEntityInWorld(cow);

		return true;
	}

Now let’s try it. We need to reset the catapult after each restart: /catapult 40 10 red 0

Explosions

Now what’s the point of a catapult if you can’t blow stuff up I ask you. Let’s get to work.

We need to trigger an explosion when the falling block touches the ground. Wait…​ We already have a piece of code that checks that. Rather than grounding the block we could start an explosion:

In FallingBlockEventHandler.java

world.createExplosion(ridingEntity, x, y, z, 2, false);
world.removeEntity(entity);
world.removeEntity(ridingEntity);

The first line starts an explosion on the block, using the X, Y, Z position of the block. We will explain the last parameters 2 and false later.

Let’s try it this way.

Better Explosions

We have some explosions but they are a bit weak, aren’t they? They actually don’t destroy anything. That’s a fail.

Let’s go back to the createExplosion method. The last parameter tells Minecraft if the explosion should destroy blocks around. Clearly it should so we need to change the false to true

world.createExplosion(ridingEntity, x, y, z, 2, true);

Huge Explosions

Ok that’s better, but could be better right? Let’s have a final look at the createExplosion method. The number parameter before true/false is the radius of the explosions. This is the number of blocks around the explosions that will be affected. Try to increase to 4, 6, 8 and see what happens :D

More

If time allowed we can explore the following ideas:

  • Start catapult from distance.

  • ???