1
0
mirror of https://github.com/Anuken/Mindustry.git synced 2024-11-11 03:31:19 +03:00
Mindustry/CONTRIBUTING.md

70 lines
4.5 KiB
Markdown
Raw Normal View History

2019-11-18 08:55:33 +03:00
# Contributing
This is for code contributions. For translations, see [TRANSLATING](TRANSLATING.md).
## Basic Guidelines
#### Use an IDE.
Specifically, IntelliJ IDEA. Download the (free) Community Edition of it [here](https://www.jetbrains.com/idea/download/). Some people use other tools, like VS Code, but I would personally not recommend them for Java development.
#### Always test your changes.
Do not submit something without at least running the game to see if it compiles.
If you are submitting a new block, make sure it has a name and description, and that it works correctly in-game. If you are changing existing block mechanics, test them out first.
2019-11-18 09:07:26 +03:00
#### Do not make large changes before discussing them first.
2019-11-18 08:55:33 +03:00
If you are interested in adding a large mechanic/feature or changing large amounts of code, first contact me (Anuken) via [Discord](https://discord.gg/mindustry) (preferred method) or via e-mail (*anukendev@gmail.com*).
For most changes, this should not be necessary. I just want to know if you're doing something big so I can offer advice and/or make sure you're not wasting your time on it.
## Style Guidelines
2019-11-18 16:29:52 +03:00
#### Follow the formatting guidelines.
This means:
- No spaces around parentheses: `if(condition){`, `SomeType s = (SomeType)object`
- Same-line braces.
- 4 spaces indentation
- `camelCase`, **even for constants or enums**. Why? Because `SCREAMING_CASE` is ugly, annoying to type and does not achieve anything useful. Constants are *less* dangerous than variables, not more.
- No underscores for anything. (Yes, I know `Bindings` violates this principle, but that's for legacy reasons and really should be cleaned up some day)
Import [this style file](.github/Mindustry-CodeStyle-IJ.xml) into IntelliJ to get correct formatting when developing Mindustry.
2019-11-18 08:55:33 +03:00
2019-11-18 09:00:16 +03:00
#### Do not use incompatible Java features (java.util.function, java.awt).
Android [does not support](https://developer.android.com/studio/write/java8-support#supported_features) many of Java 8's features, such as the packages `java.util.function`, `java.util.stream` or `forEach` in collections. Do not use these in your code.
2019-12-25 09:39:38 +03:00
If you need to use functional interfaces, use the ones in `arc.func`, which are more or less the same with different naming schemes.
2019-11-18 08:55:33 +03:00
2019-11-18 09:00:16 +03:00
The same applies to any class *outside* of the standard `java.[n]io` / `java.net` / `java.util` packages: Most of them are not supported.
2019-11-18 08:55:33 +03:00
`java.awt` is one of these packages: do not use it, ever. It is not supported on any platform, even desktop - the entire package is removed during JRE minimization.
In general, if you are using IntelliJ, you should be warned about platform incompatiblities.
#### Use `arc` collections and classes when possible.
2019-12-25 09:39:38 +03:00
Instead of using `java.util.List`, `java.util.HashMap`, and other standard Java collections, use `Array`, `ObjectMap` and other equivalents from `arc.struct`.
2019-11-18 09:00:16 +03:00
Why? Because that's what the rest of the codebase uses, and the standard collections have a lot of cruft and usability issues associated with them.
2019-11-21 00:58:34 +03:00
In the rare case that concurrency is required, you may use the standard Java classes for that purpose (e.g. `CopyOnWriteArrayList`).
What you'll usually need to change:
- `HashSet` -> `ObjectSet`
- `HashMap` -> `ObjectMap`
- `List` / `ArrayList` / `Stack` -> `Array`
2019-12-25 09:39:38 +03:00
- `java.util.Queue` -> `arc.struct.Queue`
2019-11-21 00:58:34 +03:00
- *Many others*
2019-11-18 08:55:33 +03:00
2019-11-18 09:04:37 +03:00
#### Avoid boxed types (Integer, Boolean)
Never create variables or collections with boxed types `Array<Integer>` or `ObjectMap<Integer, ...>`. Use the collections specialized for this task, e.g. `IntArray` and `IntMap`.
#### Do not allocate anything if possible.
Never allocate `new` objects in the main loop. If you absolutely require new objects, use `Pools` to obtain and free object instances.
Otherwise, use the `Tmp` variables for things like vector/shape operations, or create `static` variables for re-use.
If using a list, make it a static variable and clear it every time it is used. Re-use as much as possible.
2019-11-18 08:55:33 +03:00
#### Avoid bloated code and unnecessary getters/setters.
2019-11-18 09:00:16 +03:00
This is situational, but in essence what it means is to avoid using any sort of getters and setters unless absolutely necessary. Public or protected fields should suffice for most things.
If something needs to be encapsulated in the future, IntelliJ can handle it with a few clicks.
2019-11-18 08:55:33 +03:00
#### Do not create methods unless necessary.
2019-11-21 00:58:34 +03:00
Unless a block of code is very large or used in more than 1-2 places, don't split it up into a separate method. Making unnecessary methods only creates confusion, and may slightly decrease performance.