Coding Java for Noobs, or rather how to make your scripts optimized, understandable and useful for yourself and for others.
I've started this topic because sometimes when I look though scripts my eyes starting to bleed and my soul starts to scream.
Spoiler
Let's begin, from some basic stupid examples:
| This guy will create new array like this: array[1] = 0; array[2] = 0; array[3] = 0; array[4] = 0; array[5] = 0;
or even worst: a1 = 0; a2 = 0; a3 = 0; a4 = 0; a5 = 0;
| | | | And this guy will write it like this: int[] array = {0,0,0,0,0};
See the difference? |
Java Language is easy to learn, easy to start and easy to mess up. My point is try to pack as much code as you can in functions:
Good example is Paul's pardon system:
if (pvi < 0) {
boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "ipardon", 1);
if (haspardon) {
player.setRelationship("independent", 0);
Global.getSectorAPI().addMessage("The Gun Runners explained things to the Independents, they will see you as neutral now.");
cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "ipardon", 1);
}}
if (pvt < 0) {
boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "tpardon", 1);
if (haspardon) {
player.setRelationship("tritachyon", 0);
Global.getSectorAPI().addMessage("The Gun Runners hacked Tri-Tachyon, you've been erased from their kill list.");
cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "tpardon", 1);
}}And my way of using this pardon system:
PardonCheck(player, playercargo, cargo, "idf", "idf_pardon", "The Negotiators probed their connections with IDF, you now can try to contact them.");
PardonCheck(player, playercargo, cargo, "hierarchy", "hir_pardon", "The Negotiators bribed main command of Hierarchy in this sector, they will ignore you now.");
private void PardonCheck(FactionAPI player, CargoAPI playercargo, CargoAPI stationcargo, String side, String pardonitem, String message) {
if (player.getRelationship(side) < 0) {
boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, pardonitem, 1);
if (haspardon) {
player.setRelationship(side, 0);
Global.getSectorAPI().addMessage(message);
stationcargo.addItems(CargoAPI.CargoItemType.RESOURCES, pardonitem, 1);
}
}
}
If you won't follow this simple rule of coding, sooner or later you will got lost in your own code.
All questions regarding java are welcome