Yes, I understand this is bukkit code. Yes, I understand bukkit is dead. :) With that said, can any of you smarter individuals take a look through this and tell me if there is anything I could improve or optimize?
//stick this in your main
private Team ghostTeam;
//stick this in onEnable();
makeTeam();
//stick this at the bottom of your code
public void makeTeam(){
Scoreboard board = Bukkit.getServer().getScoreboardManager().getMainScoreboard();
ghostTeam = board.getTeam("ghosts");
if(ghostTeam == null){
ghostTeam = board.registerNewTeam("ghosts");
}
ghostTeam.setCanSeeFriendlyInvisibles(true);
}
public void makeGhost(Player player){
if(!ghostTeam.hasPlayer(player)){
ghostTeam.addPlayer(player);
}
player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 15));
//player.sendMessage("now a ghost");
}
public void makeSolid(Player player){
if(!ghostTeam.hasPlayer(player)){
ghostTeam.addPlayer(player);
}
player.removePotionEffect(PotionEffectType.INVISIBILITY);
//player.sendMessage("now solid");
}
public boolean checkGhost(Player player){
if(player == null || !(ghostTeam.hasPlayer(player))){
return false;
}else{
return true;
}
}
public void addTeam(Player player){
ghostTeam.addPlayer(player);
//player.sendMessage("you were added to the ghost team");
}
public void removeTeam(Player player){
ghostTeam.removePlayer(player);
//player.sendMessage("you were removed from the ghost team");
}
I left them in there as both an easy explanation of what it is doing, and to test the system by uncommenting and running. This is a Lib, and people might not want the text popping up every time something is called.
public boolean checkGhost(Player player) {
return player != null && ghostTeam.hasPlayer(player);
}
Copy/pasta’d it into N++ and reformatted it a bit for readability. I’m not sure why indentation didn’t persist on the OP
//stick this in your main
private Team ghostTeam;
//stick this in onEnable();
makeTeam();
//stick this at the bottom of your code
public void makeTeam() {
Scoreboard board = Bukkit.getServer().getScoreboardManager().getMainScoreboard();
ghostTeam = board.getTeam("ghosts");
if(ghostTeam == null)
ghostTeam = board.registerNewTeam("ghosts");
ghostTeam.setCanSeeFriendlyInvisibles(true);
}
public void makeGhost(Player player) {
if(!ghostTeam.hasPlayer(player))
ghostTeam.addPlayer(player);
player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 15));
//player.sendMessage("now a ghost");
}
public void makeSolid(Player player) {
if(!ghostTeam.hasPlayer(player))
ghostTeam.addPlayer(player);
player.removePotionEffect(PotionEffectType.INVISIBILITY);
//player.sendMessage("now solid");
}
public boolean checkGhost(Player player) {
return player != null && ghostTeam.hasPlayer(player);
}
public void addTeam(Player player) {
ghostTeam.addPlayer(player);
//player.sendMessage("you were added to the ghost team");
}
public void removeTeam(Player player) {
ghostTeam.removePlayer(player);
//player.sendMessage("you were removed from the ghost team");
}
That’s about all I’d change syntactically other than the indentation and removing the first blank line after opening statements. I was relatively amused by that return logic as well when IntelliJ started suggesting them XD Bit more clever than Eclipse like that (generally). I haven’t actually looked at the code close enough to know what it’s actually doing to know if there’s anything else I’d change.
It’s sticking players in a team, and giving them invisibility after turning seeFriendly on. it causes the players to become translucent. :) And yeah, looking back I had a double-negative, while the one you wrote is a straight line. Although, it’s returning true && true. but if one of those is false, it would return true && false, making it just false? Is that how it works?
The double ampersand (&&) instructs java to look at the booleans in order, and if one operation returns false, to not even pay any mind to those after it. So in effect, if at any point any statement in the chain of && appended statements equal false, it’ll return false for the entire statement. i.e.:
It’ll stop checking after the first false and not care about anything after that. In a similar fashion, double pipes (||) for the OR operator will return true for the entire statement if any given statement in it returns true, and stops checking any proceeding statements.
If you’d like more code revised by the way, you can open up a GitHub repository and I’ll make sure to follow it and contribute (make a pull request) if I find something that could be added or changed.
Not exactly. In terms of the ‘=’ signs, a single is an assign operator (setting a variable to a value), whereas the double ‘==’ is a comparison, which returns true if something equals something else. But it’s sort of an exact comparison operator. When comparing non-primitive data types with ‘==’, it’ll only return true if they are the same object (share the same space in memory), so it’s generally a better option to use obj.equals(anotherObj) when comparing non-primitives.
As for single & and | operators, I think those are binary operators, and I haven’t really understood the use for those yet. Someone else could probably better explain those better than me. I wouldn’t think they’re necessarily difficult to understand, but I’ve just never found a need to use them.