Objects in Programming

Link to objects-in-programming

This page aims to orient the scripter that is thinking of working with features and concepts that require a bit more than just chaining crafting table calls. It will help you understand a bit of how Java and ZenCode programming actually works, which is crucial to understand and manage concepts where logic is necessary, such as Conditions in Loot Modifiers, Events, or JEI Categories. Now, onto the explanation!

What are Objects?

Link to what-are-objects

Most tangible things you manipulate and use as parameters are Objects. Objects have a type, properties and methods that you can use or access.

Kinds of objects you should have seen at this point:

  • String (Stores a sequence of letters), example: "hello"
  • Int (Stores a numerical, whole value), example : 42
  • Double and Float (Store a numerical decimal value), examples: 0.2 and 0.2f
  • IItemStack (Stores an ItemStack, which is an Item plus it's data), example: <item:minecraft:pumpkin>

Doubles and floats are mostly interchangeable and the difference is that you need the "f" character in case it is a float. In most cases ZC will cast them for you.

Minecraft is written in Java, a programming language. A proper Java program is basically the combination of existing libraries and new files written in order to create Objects. These contain properties and methods. Properties store a value, which can be anything, like an entity's registryName, it's position, it's health, while Methods execute an action with relation to the Object itself. Going back to an Entity, a method might kill the entity, change it's position, or manipulate it's attributes. Methods can therefore, take parameters which affect the desired result.

Recipes, Items, Entities, NBT data are all examples of Objects created through code.

CraftTweaker exposes most of the properties and methods that exist in code to you, the user, so you can access them. The collection of what is available for you to use is found in the autogenerated pages in the site.

You are going to need to understand getting, manipulating, and using objects for the moment you choose to dive into the more complex aspects of CraftTweaker. You've used them in the past, whenever you tweaked a recipe, modified some loot or hid an Item from JEI. However, if you want to check for conditions, or build a complex loop, you'll need to start building complex structures where using properties and methods will be a huge percentage of what you're doing. In other words, you'll start to program and write code!

Understanding Objects goes further than knowing <item:minecraft:pumpkin> is called IItemStack. It's understanding what an IItemStack represents in code, which methods are available to it. The beauty of designing an object is making the code as abstract as possible and putting everything behind a layer of abstraction, to later worry as little as possible about specific cases.

It takes a while to start abstracting concepts and parameters, but the key is getting used to checking the documentation page for each class, and understanding which method and which property does what, whether it is a complex type or one as simple as an integer. However, it is something that will come naturally by spending time with the mod and tinkering with things.

Just know that, in the context of an event and in the game, types are not isolated. Entities have a Level they exist in, a position, IItemStacks have data, Players have Inventories, Players are also Entities too, along with other concepts you'll find by mostly playing with CraftTweaker and looking at the docs.