[ SOLVED ] Octo's Ghost Lib - Would like revising

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");
}
 

Thanks in advance!

Why comment out player.sendMessage?

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.

Oh lol forgot it was a library :stuck_out_tongue:

1 Like
public boolean checkGhost(Player player) {
    if(player == null || !(ghostTeam.hasPlayer(player)))
        return false;
    else
        return true;
}

I believe this (or other similar checks) can be

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");
}
1 Like
public boolean checkGhost(Player player) {
	if(player == null || !(ghostTeam.hasPlayer(player))) {
		return false;
	} else {
		return true;
	}
}

I’d replace this by:

public boolean isGhost(Player player) {
	return (player != null) && ghostTeam.hasPlayer(player);
}

The parentheses around player != null are not required, but it adds readability.
Also .checkGhost() is absolutely fine but I’d call it .isGhost()

is checkGhost (isGhost) the only one you changed?

Also, this kind of return logic is new to me. Thank you! :)
return (player != null) && ghostTeam.hasPlayer(player)

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.

Yup, the rest should be fine.

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.:

true && true && true && true && true && false && true && true = false

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.

1 Like

That gives me a reason to finally learn git. I’ll do that, thank you.

I’ve seen the single ampersand in use before as well. is it like = vs == ?

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.

2 Likes

Agreed. Thank you, and all, for the info, help, and knowledge! :D Thread now solved.

Hehe same here, never understood binary operators either…

| is a bitwise inclusive OR
& is a bitwise AND

A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits.

A bitwise OR same thing for OR

More info on those from Oracle’s page if you’re interested, but I don’t think they really clarify much here XD

/me makes hand-over-head gesture

so, 4 & 4 is then 0100 + 0100?