Solved Changing the value of boolean/array in class

Discussion in 'Spigot Plugin Development' started by noisersup, Apr 26, 2019.

  1. So, in the ExampleClass I have 3 functions and 3 arrays.

    I wanted to check which function was used recently by a player when he place a block and give him different output, so I made, of course an event and 3 arrays to store the player data.

    The problem is that when I use for example the exampleFunction and after it I place the block I have an output like without using any function.


    ExampleClass:
    Code (Java):
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;

    import java.util.ArrayList;
    public class ExampleClass implements Listener {
        private ArrayList<Player> funUsed = new ArrayList<>();
        private ArrayList<Player> fun2Used = new ArrayList<>();
        private ArrayList<Player> fun3Used = new ArrayList<>();
       
        public void exampleFunction(Player p){
            fun2Used.remove(p);
            fun3Used.remove(p);
           
            funUsed.add(p);
            p.sendMessage("You used the function!");
        }
        public void exampleFunction2(Player p){
            funUsed.remove(p);
            fun3Used.remove(p);

            fun2Used.add(p);
            p.sendMessage("You used the function 2!");
        }
        public void exampleFunction3(Player p){
            funUsed.remove(p);
            fun2Used.remove(p);

            fun3Used.add(p);
            p.sendMessage("You used the function 3!");
        }
       
        @EventHandler
        public void onPlace(BlockPlaceEvent e){

            if(funUsed.contains(e.getPlayer())){e.getPlayer().sendMessage("block placed... Wow, you used the function before!");}
            else if(fun2Used.contains(e.getPlayer())){e.getPlayer().sendMessage("block placed... Wow, you used the second function before!");}
            else if(fun3Used.contains(e.getPlayer())){e.getPlayer().sendMessage("block placed... Wow, you used the third function before!");}
            else{e.getPlayer().sendMessage("Block placed.");}
        }
    }
     
    Main:
    Code (Java):
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
        @Override
        public void onEnable() {
            this.getCommand("example").setExecutor(new Cmds());
            getServer().getPluginManager().registerEvents(new ExampleClass(),this);
        }
    }
     
    Cmds:
    Code (Java):
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class Cmds implements CommandExecutor{
        ExampleClass ex = new ExampleClass();

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
            if(sender instanceof Player){
                Player p = (Player)sender;

                if(cmd.getName().equalsIgnoreCase("example")){
                    ex.exampleFunction(p);
                }
            }

            return false;
        }
    }
     
  2. Listener is called from main not cmds
     
  3. So I must make commands in main?
     
  4. Hello,

    You should use a HashMap<UUID, Integer> instead.

    When executing the command, just add the player in the HashMap: myMap.put(player.getUniqueId(), 1);
    (replace 1 by the function you want)
    ------------------------
    Create an unique function void:
    Code (Java):
    public void exampleFunction(Player p, Integer function){
         myMap.put(p.getUniqueId(), function); // This will replace any existant value of p.getUniqueId() if exist
         p.sendMessage("You used the function " + function);
    }
    ------------------------
    And in your BlockPlaceEvent, check if the player executed a function
    Code (Java):

    Integer functionNumber = myMap.get(player.getUniqueId());
    if(functionNumber != null){
        e.getPlayer().sendMessage("block placed... Wow, you used the function " + functionNumber + "!");
    }else{
        // No function used
    }
     
    ------------------------
    Also, ExampleClass ex = new ExampleClass(); is useless.
    You should create the HashMap as static somewhere, so you can access it from the command class
     
    • Like Like x 1
    • Optimistic Optimistic x 1
  5. Thank You! I forgot about static methods!
     
  6. Or actually have getServer().getPluginManager().registerEvents(new ExampleClass(),this); the instance of the class in a private final field in the main class along with a public getter for it. Then from the Cmds class, through dependency injection, get that variable and access the methods that way. Why even encourage static abuse?

    Useless? Uh? It's not useless at all, it's just misused. Instead of that, it should be private and should be relocated to the main class along with a public getter as mentioned earlier. And when registering the event, you'd simply use the variable 'ex' instead of using new ExampleClass again.

    When creating a new instance of something, a new object will also be made. Thus why the player is never added to the collection, because they're being added to a different collection than the one that is being checked on the block place event. Static of course exists so as to make these instances belong to the class so creating new instances won't create a different collection. However, static can be avoided by following my suggestion of using dependency injection.
     
    #6 Shin1gamiX, Apr 26, 2019
    Last edited: Apr 26, 2019

Share This Page