-1

I'm new to java and i'm trying to make a minecraft plugin. I want a debug mode so I decided to create a boolean that I can switch with a command, but whatever I do it returns false except the p.sendMessage at the end that always says the debug message.Here's my code:

package fr.davidp027.itemlogger;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
    public class Main extends JavaPlugin implements Listener {

 public static Boolean debug = false;

 public void onEnable() {
  PluginManager pm = getServer().getPluginManager();
  pm.registerEvents(this, this);
 }

 public void onDiasble() {

 }

 public boolean onCommand(CommandSender sender, 
   Command cmd, String label, String[] args) {
  //Player player = (Player) sender;
  if (cmd.getLabel().equalsIgnoreCase("lping")) {
   sender.sendMessage("Command Working :D");
  } else if (cmd.getLabel().equalsIgnoreCase("ildebug")) {
   if (Main.debug = false) {
    sender.sendMessage("Debug mode turned ON. Type command again to disable.");
    Main.debug = true;
   } else if (Main.debug = true) {
    sender.sendMessage("Debug mode turned OFF. Type command again to enable.");
    Main.debug = false;
   }
  }
  return false;
 }

 @EventHandler
 public void Blockplace(BlockPlaceEvent e) {
  Player p = e.getPlayer();
  Material b = e.getBlock().getType();
  Location l = e.getBlock().getLocation();
  if (Main.debug = true) {
   p.sendMessage(ChatColor.RED + "You placed the following block: " + ChatColor.GOLD + b + 
     ChatColor.RED + " at the following location" + ChatColor.GOLD + " X: " + l.getBlockX() + " Y: " + 
     l.getBlockY() + " Z: " + l.getBlockZ() + ChatColor.RED + " and your name is: " + ChatColor.GOLD + p.getDisplayName());
  }
 }
}
2

3 Answers 3

2

This

if(Main.debug = false){

Performs an assignment, not a test. You need

if(Main.debug == false){

or just

if (!Main.debug){

Also,

else if(Main.debug = true) 

should be

else if(Main.debug == true) 

or

else if(Main.debug) 

or (in your case)

else
1
  • Works, thanks for the quick anwser. I'll accept it once I can I need to wait 7 mins before.
    – ax752
    Oct 26, 2014 at 5:22
2

if(Main.debug = false){ is basically assigning false to Main.debug and then evaluating the value, which is false, so it goes to the next condition....

It should be something more like...

if(!Main.debug){
    sender.sendMessage("Debug mode turned ON. Type command again to disable.");
    Main.debug = true;
} else { // Can't be anything else...
    sender.sendMessage("Debug mode turned OFF. Type command again to enable.");
    Main.debug = false;
}

And, last but not least, the method only ever returns false...

return false;

So, I don't know what you're actually expecting...

1

Reference Equality, Relational, and Conditional Operators

The Equality and Relational Operators

The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand. The majority of these operators will probably look familiar to you as well. Keep in mind that you must use "==", not "=", when testing if two primitive values are equal.

== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to

In your code you are using the assignment operator =:

if(Main.debug = false){

You should be using the equality operator ==:

if(Main.debug == false){

and

} else if(Main.debug == true) {

In addition, as debug is a Boolean (as Code-Apprentice pointed out), you can simplify your code as well:

if(!Main.debug){ // debug is false

and

} else { // debug is true
1
  • 1
    Better yet, remove the comparison all together since debug is already a Boolean value. Oct 26, 2014 at 5:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.