Warung Bebas

Kamis, 26 Desember 2013

Forge - World generation, loading, saving

Registries for World generation, loading, saving

The registries involved in world generation are:
  • Dimension: DimensionManager.registerProviderType (in conjunction with WorldProvider) can be used to add new dimensions in addition to the Overworld, Ender, and Nether.
  • Terrain generation: GameRegistry.registerWorldGenerator(IWorldGenerator) � allows you to register an IWorldGenerator to generate new chunks as they are required.
  • Villages: VillageRegistry contains several methods to alter the random generation of village components (huts, farms, etc).
  • Biomes: There are a bunch of registries relating to Biomes that to be honest I don�t properly understand.  If you are interested, these are
    * GameRegistry.addBiome(BiomeGenBase).
    * BiomeManager.addSpawnBiome allows the new Biome to be used during random world generation,  .addStrongholdBiome and .addVillageBiome are used to control whether Strongholds and Villages will randomly spawn in this biome.
    * BiomeDictionary � types of Biomes (HILLS, PLAINS, etc)
  • Loot Chests: ChestGenHooks is used to create new types of random loot chests (DUNGEON_CHEST, VILLAGE_BLACKSMITH, etc).
  • Mob spawners: DungeonHooks addDungeonMob will let you add new mobs to the vanilla spawner.

New methods and classes

  • WorldProvider- has been modified to  allow player to override many methods related to a custom world they have made
  • IRenderHandlerhas been added to WorldProvider � it can be used by mods to perform a custom render of the sky or the clouds � setSkyRenderer() and setCloudRenderer(). 
  • Although Forge has added a rather tempting setAdditionalProperties() method to WorldInfo, the way it has been coded means it�s not actually useful for mods to use when storing permanent data about a world.    Use world.perWorldStorage instead (used by vanilla for villages, but can be hijacked for your own purposes relatively easily).

Events         

  • TickRegistry.registerTickHandler for WORLD ticks (every world update)
  • TickRegistry.registerTickHandler for WORLDLOAD event (when world is loaded)
  • WorldEvent.Load, .Unload, .Save

There�s an impressive pile of events related to world generation, biomes, loading and saving of chunks.  I don�t understand most of them so I�ll leave that for a later time.  Perhaps.



~Overview of Forge (and what it can do for you)
    Forge concepts
           Some general notes
           How Forge starts up your code (@Mod, @SidedProxy, etc) 
           Registering your classes
           Extra methods for vanilla base classes
           Events
     Forge summary - grouped by task
           Blocks
           Items
           TileEntities
           Entities
           World generation, loading, saving
           Miscellaneous - player input, rendering, GUI, chat, sounds

Forge - TileEntities

Registries

  • GameRegistry.registerTileEntity to add your TileEntity�s name
  • ClientRegistry.bindTileEntitySpecialRenderer (in conjuction with a TileEntitySpecialRenderer class).  The renderer is bound directly to the object�s class definition, unlike Block or Item there is no �shared ID� required.

New methods and classes

  • Several new methods added around rendering and controlling object lifetime

Events

None



~Overview of Forge (and what it can do for you)
    Forge concepts
           Some general notes
           How Forge starts up your code (@Mod, @SidedProxy, etc) 
           Registering your classes
           Extra methods for vanilla base classes
           Events
     Forge summary - grouped by task
           Blocks
           Items
           TileEntities
           Entities
           World generation, loading, saving
           Miscellaneous - player input, rendering, GUI, chat, sounds

Forge - Miscellaneous stuff � player input, rendering, GUI, chat, sounds

Player Input

  • KeyBindingRegistry � allows you to add your own keybindings to the vanilla ones � (for example some vanilla keybindings are A for left and D for right).  Your custom keybindings will appear on the options page so that the player can change them.
  • MouseEvent � allows you to intercept mouse clicks and mouse movement before the vanilla code sees it.

Rendering

A lot of the custom stuff related to rendering has been covered in the other sections, however there are also quite a few other events related to rendering, listed below:

  • Various events used to customise Biome-dependent rendering, such as grass colour � see BiomeEvent.
  • FOVUpdateEvent (FOV = Field of View, which means how �wide� your view is)
  • RenderGameOverlayEvent gets called for a whole stack of different parts of the screen overlay, such as hotbar, crosshairs, health bar, etc.  See enum ElementType in the event for more information.
  • DrawBlockHighlightEvent� draw the highlight box around the block when your cursor is pointing at it
  • Another bundle of Events under RenderLivingEvent and RenderPlayerEvent can be used to control the rendering of the player and other entities in quite some detail.

Graphical User Interface

I�m a bit rubbish on this section because I�ve never actually customised a GUI.  From what I can tell, there are a few forge-related additions:
  • AchievementPage.registerAchievementPage lets you add your own achievement pages.
  • NetworkRegistry.registerGuiHandler (using IGuiHandler) � not sure how to use this.
  • GuiIngameForge � replace vanilla in-game overlays with many extra events, and also adds a large number of  flags you can change to turn rendering of HUD elements on/off � for example hotbar, crosshairs, helmet, experience bar, etc.
  • GuiControlsScrollPanel� accommodates any extra key bindings you have added to the KeyBindingRegistry
  • GuiOpenEvent � used to replace a GUI with your own customised version
  • PlayerOpenContainerEvent� when opening a GUI container.

Chat

There are a few interesting events related to chat:
  • NetworkRegistry.registerChatListener(using  IChatListener) for intercepting chat messages including commands
  • NetworkRegistry.registerConnectionHandler(IConnectionHandler) which gives control over clients and players connecting to the server.
  • CommandEvent
  • ServerChatEvent
  • ClientChatReceivedEvent

Sounds

Sounds are registered using SoundLoadEvent  instead of a registry call.  Don�t ask me why.  They are played using using vanilla methods such as SoundManager.playSound.  Some related events are
  • SoundSetupEvent
  • PlayBackgroundMusicEvent
  • PlaySoundEffectEvent
  • PlaySoundEffectSourceEvent
  • PlaySoundEvent
  • PlaySoundSourceEvent
  • PlayStreamingEvent
  • PlayStreamingSourceEvent
  • PlaySoundAtEntityEvent


~Overview of Forge (and what it can do for you)
    Forge concepts
           Some general notes
           How Forge starts up your code (@Mod, @SidedProxy, etc) 
           Registering your classes
           Extra methods for vanilla base classes
           Events
     Forge summary - grouped by task
           Blocks
           Items
           TileEntities
           Entities
           World generation, loading, saving
           Miscellaneous - player input, rendering, GUI, chat, sounds

Forge - Entities

Forge has a lot of code related to entities.  Broadly speaking it can be grouped roughly into
  • inanimate entities (minecarts, dropped items)
  • animals and mobs
  • the player

Registries

  • RenderingRegistry.registerEntityRenderingHandler (in conjunction with a Render class).   The renderer is bound directly to the object�s class definition, unlike Blocks no shared ID is required.
  • EntityRegistry.registerModEntity to add your entity to the game
  • EntityRegistry.addSpawn to control how your entity spawns randomly into the world, for example if you create a new Entity called Deer and you want them to spawn randomly in the woods/forest but not in the desert. 
  • DungeonHooks.addDungeonMob can be used to add your mob to mob spawners found in dungeons.
  • VillagerRegistry: contains a number of methods to add trading recipes and control the spawning of different villager types.

New methods and classes

  • Models: These contain a few classes related to rendering, I�m not actually sure if they�re ever used since it seems people nearly always use Techne to make new Entity rendering models and that appears to use vanilla Models only.
  • IEntityAdditionalSpawnData is used to add extra info to the mod�s spawn packet � the server provides the information, and the client can read it when the entity is spawned.
  • IExtendedEntityProperties - for storing extra permanent information in an Entity, i.e. information that will be saved to disk and reloaded.

Events

  • EntityMinecart.registerCollisionHandler(IMinecartCollisionHandler) � superceded
  • GameRegistry.registerPlayerTracker - for tracking players � login, logout, respawn, dimension change.
  • WorldEvent.PotentialSpawns � control which entities are spawned in which location.
  • TickRegistry.registerTickHandlerfor PLAYER event � whenever an EntityPlayer�s onUpdate is called.
There is a whole stack of other events related to entities that I�m not going to list in detail  - look in the events package if you want to browse through them.  They can be broadly split into several types:
  • Rendering events
  • Change of state � eg spawning and despawning, death, picking up, discarding, throwing, use by player
  • Interactions with the player, other entities, items, especially attack, damage, targeting
  • Player actions � eg flying, falling, sleeping, left or right clicking
Personally I find it sometimes takes me a few tries to find the right event.  There is often some overlap between the different events but subtle differences too.  Usually boils down to guesswork from looking at the vanilla code, plus a bit of trial and error.



~Overview of Forge (and what it can do for you)
    Forge concepts
           Some general notes
           How Forge starts up your code (@Mod, @SidedProxy, etc) 
           Registering your classes
           Extra methods for vanilla base classes
           Events
     Forge summary - grouped by task
           Blocks
           Items
           TileEntities
           Entities
           World generation, loading, saving
           Miscellaneous - player input, rendering, GUI, chat, sounds

Forge - Blocks

An overview of the Forge code related to Blocks.

Registries

  • GameRegistry.registerBlock to register your block and (optionally) associate it with an ItemBlock.  If you don�t provide an ItemBlock, it will make a default one itself and register it for you.  If you provide an ItemBlock, you don�t need to register the ItemBlock yourself separately.  GameRegistry.findBlock can be used to retrieve the block corresponding to a given name.
  • RenderingRegistry.registerBlockHandler (in conjunction with an ISimpleBlockRenderingHandler class).  RenderingRegistry uses a unique renderID, returned by the class�s getRenderType, to find the renderer with the matching .getRenderID() method.   RenderingRegistry.getNextAvailableRenderId() is useful for getting a unique ID for the linked classes to share.  See here for more information.
  • MinecraftForge.addGrassPlant  (and .addGrassSeedfor the corresponding Item) are used to add new types of grass.
  • MinecraftForge has a several methods used to add new tools and control their effectiveness against different blocks, in particular setToolClass and setBlockHarvestLevel.
  • LanguageRegistry.addNameForObject � allows you to add a displayed name for your Blocks.  Apparently it�s better to use a Language Pack instead of this registry.  I haven�t tried those yet so I don�t know.
  • ChestGenHooks is used to control the spawning of loot chests during world generation.  See the World generation section.

Utility functions

  • RotationHelper: a couple of functions to help rotate vanilla blocks to face the desired direction (eg piston, doors, etc), convert from block�s metadata to a standard �direction value�.

New methods and classes

  • Block has many methods added to let mods change behaviour more easily � ladder, fire, tileentities, beds, harvesting, leaves interaction, explosion interaction, plant interactions, redstone, and a few more.   They are defined after the comment in the Block code
    /*===========Forge Start ========*/.
  • IPlantableinterface for plants
  • IShearableinterface for blocks which can be �shorn� by shears � eg like tallgrass or vines
  • Fluids:  A variety of classes to let you define your own fluids (like lava and water) and control their behaviour; comprised of BlockFluidBase, BlockFluidClassic, Fluid and FluidStack.

Events

  • BreakEvent � when a block is broken
  • DrawBlockHighlightEvent� draw the highlight box around the block when your cursor is pointing at it
  • PlayerInteractEvent� among other things - when a player left-clicks a block
  • HarvestDropsEvent� when a block is harvested
  • HarvestCheck � can the player break this block with this tool?
  • BreakSpeed � speed of breaking this block with this tool
  • SaplingGrowTreeEvent
  • BonemealEvent � when bonemeal is used on a plant
  • Various FluidEvents related to the Forge Fluids classes


~Overview of Forge (and what it can do for you)
    Forge concepts
           Some general notes
           How Forge starts up your code (@Mod, @SidedProxy, etc) 
           Registering your classes
           Extra methods for vanilla base classes
           Events
     Forge summary - grouped by task
           Blocks
           Items
           TileEntities
           Entities
           World generation, loading, saving
           Miscellaneous - player input, rendering, GUI, chat, sounds

Forge - Items

An overview of the Forge code related to Items.  Note � EntityItems (i.e. Items which have been thrown onto the ground) are not covered here, see Entities instead.

Registries

  • GameRegistry.registerItemis used to register your item.  In some cases, you may have a single Item which represents different things depending on the damage value � for example ItemCloth (wool), which uses the damage value to represent the colour of the wool.  In this case use GameRegistry .registerCustomItemStackto register the ItemStack instead of the Item.
    In general, for an ItemBlock (Item which corresponds to a Block, eg ItemLeaves and BlockLeaves) you shouldn�t register it separately, instead you should supply it to GameRegistry.registerBlock when you register the corresponding Block.
    GameRegistry.findItem can be used to find the Item corresponding to a given name.
  • MinecraftForgeClient.registerItemRenderer (in conjunction with an IItemRenderer class). MinecraftForgeClient binds the IItemRenderer to the item ID.
    There is a special registry for rendering armour. The vanilla code is restricted to a predetermined list of armour types (�prefixes�) - "leather", "chainmail", "iron", "diamond", "gold". You can add your own (eg �lycra�) using
    RenderingRegistry.addNewArmourRendererPrefix
    When an entity wearing your armour is rendered, it will then use a texture of the form lycra_boots.png, lycra_leggings.png, etc. (This can also be achieved using Item.getArmorTexture, which is probably a better way to be honest).
  • MinecraftForge. addGrassSeed(and . addGrassPlant  for the corresponding Block) are used to add new types of grass.
  • MinecraftForge has a several methods used to add new tools and control their effectiveness against different blocks, in particular setToolClass and setBlockHarvestLevel.
  • LanguageRegistry.addNameForObject� allows you to add a displayed name for your Items and ItemStacks.  Apparently it�s better to use a Language Pack instead of this registry.  I haven�t tried those yet so I don�t know.
  • GameRegistry.addRecipe (shaped, shapeless) lets you add custom recipes for crafting items.
  • GameRegistry.addSmelting, GameRegistry.registerFuelHandler(with IFuelHandler) � for controlling how furnaces make use of your Items- allows you to add custom smelting recipes (eg smelting iron ore to ingots) or add custom fuels (in addition to coal, wood, etc).

When registering crafting recipes or smelting recipes, use OreDictionary.WILDCARD_VALUE for ItemStacks where any damage value will match (eg for a recipe using wool (ItemCloth) where any colour can be used).

Utility functions

  • EnumHelper (and EnumHelperClient for use on client-side only)� can be used to add extra elements to the various vanilla enums, for example vanilla EnumToolMaterial which has WOOD, STONE, IRON, EMERALD (diamond), and GOLD, to which you could add for example OVERCOOKED_SPAGHETTI.

New methods and classes

  • Item has many methods added to let mods change behaviour more easily � when dropped, used (eaten), used on entities or blocks, rendering, armor, potions, enchantment, They are defined after the comment in the Item code
    /*===========Forge Start ========*/.
  • ISpecialArmor � ItemArmor classes call the functions in this interface when calculating damage reduction � if you define your Item to implement ISpecialArmor, the appropriate methods on your item will be called when damage reduction is being calculated.
  • IPlantableinterface for plants
  • Ores: OreDictionary, ShapedOreRecipes, ShapelessOreRecipes - As far as I can tell, this lets you make recipes using ores provided by other mods, in particular BuildCraft.  Some vanilla examples of ore are iron, stone, wood.

Events

  • GameRegistry.registerPickupHandler� when player picks up an item, superceded by EntityItemPickupEvent.
  • ItemTossEvent  - when the item is about to enter the world (discarded by player)
  • FillBucketEvent � when a bucket is filled (click on water, lava etc) or emptied.
  • ItemTooltipEvent � when your cursor is hovering over an item, to let you customise the ToolTip text that appears.
  • PlayerDestroyItemEvent� whenever an item is destroyed / used up.
  • UseHoeEvent � player uses a hoe tool
  • OreRegisterEvent
  • GameRegistry.registerCraftingHandler(ICraftingHandler) for when an item is crafted or smelted.
  • PotionBrewedEvent


~Overview of Forge (and what it can do for you)
    Forge concepts
           Some general notes
           How Forge starts up your code (@Mod, @SidedProxy, etc) 
           Registering your classes
           Extra methods for vanilla base classes
           Events
     Forge summary - grouped by task
           Blocks
           Items
           TileEntities
           Entities
           World generation, loading, saving
           Miscellaneous - player input, rendering, GUI, chat, sounds

Overview of Forge (and what it can do for you)

The main purpose of Forge is to allow multiple mods to be installed without interfering with each other. It does this in a few ways;
  1. providing a standard mechanism to load and initialise mods.
  2. providing ways for your mod to interact with the vanilla minecraft code without you having to modify or overwrite any of the vanilla code.
  3. allowing your mods �assets� (custom sounds, textures, etc) to be stored in your mod�s zip file instead of in the vanilla assets folder.

Loading and Initialising your Mod

Forge achieves the first objective by searching for your mod in the mods folder. When it finds your mod, it uses a technique called reflection to discover which of your mod�s methods it should call to initialise it. During initialisation, your mod then calls a number of forge methods to instruct forge on how to interact with your mod�s code.  More details below.

The load and initialisation sequence is covered in some more detail here.
Forge also uses a file called Mcmod.info (if provided by your mod) to display a description and other information about your mod.

Avoiding the need to modify vanilla code

The three key ways that Forge achieves this second objective are:
  1. providing a number of Registries where you can register your new Blocks, Items, Recipes, etc, so that the vanilla code can interact with them.
  2. adding extra methods to the vanilla base classes (eg Block), so that your code can override them when you extend the base class � i.e. using inheritance to make vanilla code call your new code.
  3. providing a multitude of �events� that your mod can register its interest in, for example �call myModTickHandler every 1/20 of a second� or �call mySleepHandler� every time a player sleeps in a bed.

Loading your mod�s assets

Forge adds your mod to the list of places that minecraft searches when it�s looking for assets. The most confusing part of the assets folder is that you need to know where to put it! When your mod is packaged up as a zip and placed in the mods folder, this is easy.

Basic structure of the zip file for an example mod "speedytools".

When you�re debugging, it�s a bit harder. You need to find the base path that Forge looks in. To be honest I�m not sure what this is supposed to be. Here is how I figured it out for my setup:
http://www.minecraftforge.net/forum/index.php?topic=11963.0
If you're using Eclipse (which I'm not), apparently you should put your assets into 
\src\minecraft\
 in order to get them copied into the right place during debugging.

Based on painful experience I'd suggest you use lowercase for your modid, folder names, and filenames. This will avoid subtle problems where (eg) textures are loaded ok during testing but can�t be found once packaged up into a zip (zip files are case-sensitive, windows folders aren�t).

For those who are interested - FMLFileResourcePack and FMLFolderResourcePack are the two classes that Forge uses to represent your mod assets, i.e. textures, sounds, etc - either as a file (zip) or a folder respectively. When minecraft goes looking for (eg) a texture such as �mymod:myawesometexture.png�, it uses �mymod� to identify the correct FML Resource Pack and pull the correct data out.



~Overview of Forge (and what it can do for you)
    Forge concepts
           Some general notes
           How Forge starts up your code (@Mod, @SidedProxy, etc) 
           Registering your classes
           Extra methods for vanilla base classes
           Events
     Forge summary - grouped by task
           Blocks
           Items
           TileEntities
           Entities
           World generation, loading, saving
           Miscellaneous - player input, rendering, GUI, chat, sounds

Forge concepts - registering your classes

Forge provides a number of registries to let you extend an existing base class (Block, Item, etc) and then tell the registry about your new Class to integrate it into the game. This is generally the best way to interface with the game, because it is modular, easy to understand and maintain, and relatively robust to changes in the vanilla code.
A broad overview of the different types of registries are listed below.  
  • Registries for adding new Blocks, Items, Entities - These registries are used to assign names to your Blocks, Items, Entities, and TileEntities. These are necessary for Minecraft to be able to save and load them properly.  (It is not automatically the same as the unlocalizedName member variable in each Item and Block.)
  • Registries for rendering objects - In order for your object to be drawn on the screen (Block, Item, Entity, TileEntity) you need to provide a renderer class for that object and register it.
  • Registries for crafting, smelting, spawning, villagers
  • Registries for World generation
  • Miscellaneous Registries - key binding, language, GUI screens, achievement pages


~Overview of Forge (and what it can do for you)
    Forge concepts
           Some general notes
           How Forge starts up your code (@Mod, @SidedProxy, etc) 
           Registering your classes
           Extra methods for vanilla base classes
           Events
     Forge summary - grouped by task
           Blocks
           Items
           TileEntities
           Entities
           World generation, loading, saving
           Miscellaneous - player input, rendering, GUI, chat, sounds

Forge concepts - Events

Forge provides a large number of events that you can subscribe to. When a particular part of the vanilla code is reached, it fires an �event� and calls any EventHandler code that you have subscribed to that particular event. An example is the PlayerSleepInBed event, which gets triggered whenever a player sleeps in a bed.
There are currently three main ways that you subscribe to events.
  1. The first is the Event Bus.  There are a lot of Event Bus events and it looks like this will be the preferred method for new events added in the future.
  2. The second is through the TickRegistry (for TickHandlers).
  3.  The third is through the @Mod and @NetworkMod annotations used at startup of your mod. 
In addition, there are a few miscellaneous others such as GameRegistry.registerPickupHandler and GameRegistry.registerPlayerTracker, EntityMinecart.registerCollisionHandler().

Event Bus

You subscribe your handler to the event bus using the following basic steps:
  1. Create a MyEventHandler class and register it using the .register method of the appropriate event bus � these are
    * MinecraftForge.EVENT_BUS (this is nearly always the one you need)
    * MinecraftForge.TERRAIN_GEN_BUS
    * MinecraftForge-ORE_GEN_BUS
  2.  Use @ForgeSubscribe to annotate each method in your MyEventHandler which handles an event, eg
@ForgeSubscribe
public void interceptMouseInput(MouseEvent event)

Whenever the event occurs, your method will be called, with relevant information passed in using the event variable.
Some events can be canceled, by setting event.setCanceled(true) before returning.  This is typically useful where you want to replace the vanilla behaviour with something different - for example if the LivingDropsEvent (called when a mob dies and drops treasure) is canceled, it skips the vanilla code to spawn dropped items.  Cancelable Events have the annotation @Cancelable immediately before the Event class declaration.
e.g.
@Cancelable
public class LivingDropsEvent extends LivingEvent


Some events have a "result" - "DEFAULT, ALLOW, DENY".  This is sometimes just used as an alternative to setCanceled, in other cases (eg LivingSpawnEvent) it is used to choose between "yes", "no", or "let vanilla decide".  @HasResult is used to annotate these events.

The events are all nicely organised into a package (minecraftforge/event) so it is worth a browse to see what flavours are available.

TickRegistry

The TickRegistry appears to be an older method of handler, it is used for frequent regular occurrences such as the game loop �tick� that is triggered 20 times per second.
The important steps are
  1. Create your class MyTickHandler implements ITickHandler
  2. Give it a ticks() method which returns a Set of the type of ticks that MyTickHandler is interested in.
  3. Override the tickStart() and/or tickEnd() method with the code that should be executed every time this event triggers.
  4. Register the MyTickHandler using TickRegistry.registerTickHandler.

Some general notes

  • ITickHandler is used when you want the handler to be called every single tick.
  • IScheduledHandler is useful if you only want the handler to be called occasionally, for example every 400 ticks. It is registered using registerScheduledTickHandler.
  • The various types of Tick that you have available are listed in TickType; the most useful are
    CLIENT � the client tick loop
    SERVER � the server tick loop
    PLAYER � whenever an EntityPlayer�s onUpdate is called
    WORLD � each time a WorldServer is ticked
    WORLDLOAD � on the server, when a world is loaded

 @Mod and @NetworkMod for code startup 

The @Mod annotation is used to control startup of your code. See here.
The @NetworkMod annotation is used to register your custom packet handlers (IPacketHandler).

Others

The miscellaneous other handlers are all registered by calling a method specific for that handler, for example
  • GameRegistry.registerPickupHandler � when player picks up an item, superceded by EntityItemPickupEvent.
  • GameRegistry.registerPlayerTracker for tracking players � login, logout, respawn, dimension change.
  • EntityMinecart.registerCollisionHandler for minecart collisions
  • GameRegistry.registerCraftingHandler (ICraftingHandler) for when an item is crafted or smelted.
  • NetworkRegistry.registerChatListener(using IChatListener) for intercepting chat messages including commands
  • NetworkRegistry.registerConnectionHandler(IConnectionHandler) which gives control over clients and players connecting to the server.


~Overview of Forge (and what it can do for you)
    Forge concepts
           Some general notes
           How Forge starts up your code (@Mod, @SidedProxy, etc) 
           Registering your classes
           Extra methods for vanilla base classes
           Events
     Forge summary - grouped by task
           Blocks
           Items
           TileEntities
           Entities
           World generation, loading, saving
           Miscellaneous - player input, rendering, GUI, chat, sounds

Forge concepts - some general notes

Forge is comprised of a couple of main components
  • FML � forge mod loader � which is mostly concerned with loading mods and initialising them, but also provides most of the Registries.
  • Minecraftforge � adds a large number of extra events and hooks
They were developed independently and are stored in different packages (cpw.mods.fml vs net.minecraftforge), but apart from that you�ll use them in the same way. The biggest symptom you might notice is that the paradigms are sometimes a bit inconsistent � naming conventions, hook methods, event handlers, commenting style � but nothing too hard to get used to.

Often it is difficult to know exactly which forge methods or hooks are going to be useful to create the behaviour you want.  A general strategy I have found effective is:
  1. Think of a part of the vanilla minecraft which does something similar to what you want to do.
  2. Open up that item, block, entity (or whatever) and browse through the vanilla code to see how it works.
  3. Look for ways to divert the program flow to your own code:
    * Methods that you can override in your own classes (especially for Blocks, Items, Entities)
    * A forge event or hook inserted into the vanilla code, which you can register for.
Sometimes, if there is no appropriate event or class method to override, it is possible to check for a condition using a tickhandler (called 20 times per second) and act on that � for example, checking if the player is holding a particular item in their hand and making sparks fly from their head if it is.



~Overview of Forge (and what it can do for you)
    Forge concepts
           Some general notes
           How Forge starts up your code (@Mod, @SidedProxy, etc) 
           Registering your classes
           Extra methods for vanilla base classes
           Events
     Forge summary - grouped by task
           Blocks
           Items
           TileEntities
           Entities
           World generation, loading, saving
           Miscellaneous - player input, rendering, GUI, chat, sounds

Forge concepts - extra methods for vanilla base classes

In a number of cases, Forge has added extra methods to the base classes, or sometimes moved methods from a core class (eg World) to a different class which can be extended and registered (eg WorldProvider). These forge methods are generally very easy to spot because
  • They are usually moved to the end of the class, placed after a comment like /*===========Forge Start ========*/
  • The JavaDoc documentation (before each class declaration) is usually much better than the rest of the vanilla code.

A number of the extra methods have been added as completely separate interfaces, for example IPlantable for plants and IShearable for entities which can be shorn.


Forge adds a few completely new classes and methods to make life easier-
  • Fluids:- A variety of classes to let you define your own fluids (like lava and water) and control their behaviour; comprised of BlockFluidBase, BlockFluidClassic, Fluid and FluidStack.
  • Models: A few classes related to rendering, not actually sure if they�re ever used since it seems people nearly always use Techne and that appears to use vanilla Models.
  • Ores: OreDictionary, ShapedOreRecipes, ShapelessOreRecipes - As far as I can tell, this lets you make recipes using ores provided by other mods, in particular BuildCraft.
  • RotationHelper: a couple of functions to help rotate vanilla blocks to face the desired direction (eg piston, doors, etc), convert from the block�s metadata to a standard �direction value�.
  • Configuration: class to help you create configuration files to store settings for your mod

Forge also adds a number of packets with base class ForgePackets or FMLPacket, they act �behind the scenes� and you�re unlikely to use them directly.



~Overview of Forge (and what it can do for you)
    Forge concepts
           Some general notes
           How Forge starts up your code (@Mod, @SidedProxy, etc) 
           Registering your classes
           Extra methods for vanilla base classes
           Events
     Forge summary - grouped by task
           Blocks
           Items
           TileEntities
           Entities
           World generation, loading, saving
           Miscellaneous - player input, rendering, GUI, chat, sounds

Kamis, 19 Desember 2013

Items

 Items are things that the player can carry, such as Pickaxe, Sword, Gemstone, Wood.  There are many types of Item, all derived from the Item base class. They are rendered in a number of different ways as described here

Like Blocks, there is generally only one instance of each Item class.  The Item class holds information that defines what that type of Item is (for example - what it looks like, how much damage it can take before breaking, how it interacts with blocks or entities, etc).

ItemStack is the class used when an Item is actually "created" - in addition to the Item type, it also stores the item count (eg a stack of 46 planks), "damage" (eg pickaxe "wear-and-tear" indicator), and several other pieces of information. ItemStacks are stored in "Slots" in the inventory, in the hotbar, in Containers (eg Chest, Furnace, etc).

Some general notes
  • In order to compare two different ItemStacks, you can't use the == operator (a common trap -see here for a discussion based around Strings).  Use areItemStacksEqual(itemStack1, itemStack2); instead.
  • Each Item defines a maximum stack size for the corresponding ItemStack, up to 64.  If this is >=2, the item is "Stackable", otherwise not.
  • If an Item can be damaged (maxDamage > 0) then it isDamageable, and the ItemStack.itemDamage is intepreted as the current damage (or "health") of the item.  Otherwise, ItemStack.itemDamage is interpreted as the type of item - for example ItemCloth (wool) which uses the itemDamage to specify the colour of the wool.  .hasSubTypes = true and .getMetadata are also required.
  • If an item is Stackable, it can't be Damageable.
  • An ItemStack can store custom information about the item, in the NBT stackTagCompound; see ItemStack.setTagInfo, .setTagCompound, .getTagCompound, etc.  Used by vanilla for enchantments.
  • An item which is in the world (eg sitting on the ground after the player has thrown it) is represented by the corresponding EntityItem, not an ItemStack. 
  • ItemBlocks are a special type of Item used to represent a Block when you have harvested it - for example Bedrock.  The vanilla code automatically generates an ItemBlock for each vanilla Block with an ID less than 256, giving it the same ID number, which unfortunately makes things complicated if you try to create your own ItemBlock.  For your own custom Blocks, when you register them with GameRegistry.registerBlock a matching ItemBlock will automatically be created - alternatively you can supply your own ItemBlock to GameRegistry.registerBlock to explicitly associate it with the target Block.


 

My Blog Copyright © 2012 Fast Loading -- Powered by Blogger