From 2000c5212cd37ff7cb309300cf5a30b497e1b20c Mon Sep 17 00:00:00 2001 From: Scribblon Date: Fri, 1 Aug 2014 05:18:33 -0700 Subject: [PATCH] Adding missing information about conventions when coding for a patch on an existing Minecraft class. --- If-you-want-to-contribute-to-Forge.md | 98 ++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/If-you-want-to-contribute-to-Forge.md b/If-you-want-to-contribute-to-Forge.md index 7c65fbb..3400dc3 100644 --- a/If-you-want-to-contribute-to-Forge.md +++ b/If-you-want-to-contribute-to-Forge.md @@ -11,8 +11,102 @@ Prerequisites: Eclipse (3.7 or better is probably good), Git (any version, must ### Tips - Take your time. Make sure your code does what it's supposed to do and does it well. Your PR probably won't be accepted if it doesn't cover all the uses -- Indent your code properly, and follow the conventions (take a look at other classes and read the code before editing). Space instead of tabs, open braces on a new line, etc. ONLY IF IT IS A NEW CLASS NOT IF IT IS A PATCH TO A MINECRAFT CLASS. +- Indent your code properly, and follow the conventions (take a look at other classes and read the code before editing). Space instead of tabs, open braces on a new line, etc. [ONLY IF IT IS A NEW CLASS NOT IF IT IS A PATCH TO A MINECRAFT CLASS](https://github.com/MinecraftForge/MinecraftForge/wiki/If-you-want-to-contribute-to-Forge#). - To update your repo to latest code from the official repository: 1. If it's the first time, add a remote for the official repo (`git remote add upstream gitg@github.com:MinecraftForge/MinecraftForge.git`) 2. Get the updates (`git fetch upstream`), and make sure you're on master (`git checkout master`) - 3. If you don't want to rewrite your history (e.g. others have cloned your repository), do a merge (`git merge upstream/master`). **For keeping pull requests as clean as possible, do a rebase** (`git rebase upstream/master`). Git may reject your push the first time after each rebase and you'll have to do `git push -f origin master` (or `git push origin master --force`). \ No newline at end of file + 3. If you don't want to rewrite your history (e.g. others have cloned your repository), do a merge (`git merge upstream/master`). **For keeping pull requests as clean as possible, do a rebase** (`git rebase upstream/master`). Git may reject your push the first time after each rebase and you'll have to do `git push -f origin master` (or `git push origin master --force`). + +### Conventions for coding patches for a Minecraft-class (`.java.patch`) +This section applies to the `ClassName.java.patch` section of your contributions. +Whenever a new update of Minecraft is released, these patches are applied to the original code provided by Mojang. To increase compatibility please keep the following conventions: + +#### While coding: +- Prevent to add extra spaces/line breaks, even when there are already existing ones in the code between statements. Below is an example that comes from `net.minecraft.entity.item.EntityItem#attackEntityFrom(DamageSourc, float)`: +``` + +this.setBeenAttacked(); +this.health = (int)((float)this.health - p_70097_2_); + +if (this.health <= 0) +{ + this.setDead(); +} + +return false; + +``` +Lets say we add an `if`-statement between `this.setBeenAttacked();` and `this.health = (int)((float)this.health - p_70097_2_);`. It seems that `if`-statements get extra spaces/line breaks in the original code. However, when contributing to a `java.patch`-file you should always try to keep the as changes minimal possible. +So, don't do: +``` + +this.setBeenAttacked(); +++ ++ if(isCondition) return false; +++ +this.health = (int)((float)this.health - p_70097_2_); + +if (this.health <= 0) +{ + this.setDead(); +} + +return false; + +``` +But do: +``` +this.setBeenAttacked(); +++ if(condition) return false; +this.health = (int)((float)this.health - p_70097_2_); + +if (this.health <= 0) +{ + this.setDead(); +} + +return false; + +``` + +- In spirit of the previous point: prevent to add more lines than necessary. If you can reduce your statement to one single line, please do so. +Example here is an `if`-statement that could be written as follows to improve readability: +``` +if((float)this.field_70291_e - p_70097_2_ <= 0.0F) +{ + if(net.minecraftforge.common.ForgeHooks.onItemDeath(this, p_70097_1_)) + { + return false; + } +} +``` +However, to keep changes to `.java.patch`-files as minimal as possible. Try to write that statement to one single line: +``` +if((float)this.field_70291_e - p_70097_2_ <= 0.0F && net.minecraftforge.common.ForgeHooks.onItemDeath(this, p_70097_1_)) return false; +``` +- Prevent using imports and try using full qualifications in the code. Using imports will generate extra lines in patch-files which can be prevented. Imports that are already in the file are not your concern. Example below: +``` + +import net.minecraft.otherimports +import net.minecraftforge.event.GenericEvent +import net.minecraftforge.otherimports + + if (isCondition) MinecraftForge.EVENT_BUS.post(new GenericEvent()); + +``` +Instead try to code it as: +``` + +import net.minecraft.otherimports +import net.minecraftforge.otherimports + + if (isCondition) MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.GenericEvent()); + +``` +Sidenote: As FML/Forge is trying to move away from imports try to use `net.minecraftforge.common.MinecraftForge` even when an existing import is already there. +An even better way is to add a function `net.minecraftforge.event.EventFactory` or `net.minecraftforge.common.ForgeHooks` and use the full qualification function names. + +#### Before `gradle genPatches` & Committing +Before committing, check if your IDE hasn't put in extra spaces/line breaks in the import section of you java-file. +Eclipse tend to reorganize the imports to improve readability and will cause extra spaces/line breaks to appear in the `.java.patch`-files. Review your patches (`ClassName.java.patch`) and the diff-states of your uncommitted changes on these patches for extra `++`'s or `+ +`'s. This means an extra space/line break has been added +Keep in mind that the original `.java.patch`-file never add extra spaces/break lines in between imports. . \ No newline at end of file