Search code examples
javabooleanlistenerminecraftbukkit

Global boolean not being accesable by anoter class


I am trying to code a Minecraft plugin, that with one command makes all creepers that spawn charge, and with another makes them all uncharged. I have written the code that sets listeners(and by that, I mean I copied Dream's code from https://youtu.be/oHKcQ184aa8?t=22) and I just want to make it so there is a boolean that goes in the place of true in the following code(last line) '''

    @EventHandler
    public void creeperSpawn(CreatureSpawnEvent event) {
        if (event.getEntityType() == EntityType.CREEPER) {
            Creeper creeper = (Creeper) event.getEntity();
            creeper.setPowered(true);
        }
    } 

''' I want to change the true to a booolean named PoweredOrNot, and then when the command is run, it sets it Powered or not to true. The only problem being that it either says that PoweredOrNot is not defined, or when I add extends onCommand, it gives me the error "Syntax error on token "extends", throws expected" and two other errors , one on a bracket above saying "Syntax error, insert "}" to complete ClassBody",which adding another bracket doesn't work. and another error on a bracket below saying "Syntax error on token "}", delete this token, which deleting gets rid of the error.

I did research and made another class named Globals and it has the boolean name powered or not too, but when I did Globals.PoweredOrNot it would say it wasnt defined

here is the jar file for you to decompile as there are many classes and packages to work with. note, I think you might have to have the reference libraries for spiggot downloaded but idk if that is included in the jar. https://drive.google.com/file/d/18XWOHV13I2cB---xlG0socfRMOnnKfPv/view?usp=sharing

FULL code (for the startCommand class)

package me.Gwehyr.chargeall.commands;

import org.bukkit.entity.Creeper;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;

import me.Gwehyr.chargeall.*;


public class StartCommand implements CommandExecutor, Listener {

    private Main plugin;

    public StartCommand(Main plugin) {
        this.plugin = plugin;
        plugin.getCommand("charge").setExecutor(this);
    }

    @Override
    public boolean onCommand( CommandSender sender,  Command cmd,  String label, String[] args) {
        if (sender instanceof Player) {
            sender.sendMessage("Only Console may execute this command");
            return true;
        }else {
            boolean PoweredOrNot = true;
            sender.sendMessage("Creepers Charged!");
        }
        return true;
    }

@EventHandler
public void creeperSpawn(CreatureSpawnEvent event) extends onCommand {
    if (event.getEntityType() == EntityType.CREEPER) {
        Creeper creeper = (Creeper) event.getEntity();
        creeper.setPowered(PoweredOrNot);
    }
}

Solution

  • The reason it says it's not initialized is because it is only initialized inside your onCommand function. So instead of putting your boolean here (where the arrow is)

    public boolean onCommand( CommandSender sender,  Command cmd,  String label, String[] args) {
            if (sender instanceof Player) {
                sender.sendMessage("Only Console may execute this command");
                return true;
            }else {
                boolean PoweredOrNot = true;   <---------
                sender.sendMessage("Creepers Charged!");
            }
            return true;
        }
    

    put up the top of your class.

    public boolean PoweredOrNot;
    

    This makes sure it is defined and if you want to be able to access this boolean from another class (I'm not sure why you would, it depends on what you are making), instead you can put

    public static boolean PoweredOrNot;
    

    This doesn't have a value by default so you might want to set it to false or something.

    Also to the people saying for people new to Bukkit/coding in general, don't tell them to "learn before they start", it's good to get help and it prevents them from doing it next time. Hope this helped!