Solved Get a variable into another class

Discussion in 'Spigot Plugin Development' started by Wilsoon, Feb 8, 2020.

Thread Status:
Not open for further replies.
  1. So basically, I have my code which has commands and main class separated into different classes, however, I want to get the name of the sender of command A and use the name in Command B. However, I can't put a 'static' into the CommandSender nor can I put it in String or Player. How would I get that name to another class then?
     
  2. You can create a dataHolder class and get it from the main with a getter, I can't explain it, so I'll add my code.


    Code (Java):
    public class DataHolder {

        private Map<Player, ItemStack> itemStackMap;

        public DataHolder() {
            itemStackMap = new HashMap<>();
        }

        public Map<Player, ItemStack> getItemStackMap() {
            return itemStackMap;
        }

    }
     

    Code (Java):
    public class Main extends JavaPlugin {

        private DataHolder dataHolder;


        @Override
        public void onEnable() { }

        public DataHolder getDataHolder() {
            return dataHolder;
        }

    }
     
     
  3. I
    I don't really know what's a DataHolder and how I would put any values into it and use them.
    If you don't really know what I mean:
    I'm basically making a plugin and I separated all of my 3 commands into 3 classes. However, I need the "sender" value of one of my command classes and I want to use it in my Event class.
     
  4. It doesn't work that way, the onCommand will be triggered, as expected, on command and event listeners will be ttiggered when the event happens, they shouldn't share fields.
     
  5. You will need to create an Object that can save your information (player names or whatever) and then you need to share that Object with all of your command classes. That's the idea of the "DataHolder" class mentioned above.
     
  6. Strahan

    Benefactor

    What he's saying is make a class to hold whatever data you need then just pass it around to whatever other classes need it. DataHolder isn't a thing that you would need to know, it's just the name he made up for that class.

    As to specifically what you are doing, explain how the command part and the event part are supposed to interact.
     
    • Agree Agree x 1
  7. Well, I'll explain what I'm tryna do, as always, I try my best to make some plugins, and I ask my questions here.
    This time, I'm tryna make a mutechat plugin, the event I'm using is AsyncPlayerChatEvent, I'm checking if chat is currently muted
    using a boolean, and I'm making it so that when a player tries to talk when chat is muted, it will say "Chat is currently silenced by (Player)", however I can't get the sender variable to the (Player) part which is inside my Event class.

    I'll try this out myself, and ask any questions if I'm still blur.




    Another question would be:
    How would I check whether a player sent a /msg to another player? and cancel that event if a certain condition isn't met.
     
    #7 Wilsoon, Feb 8, 2020
    Last edited: Feb 8, 2020
  8. You should pass the same instance of some object with a boolean field to the CommandExecutor and the Listener. In the CommandExecutor you will change the value of the boolean and in the listener you will check for the value of the boolean.
    To cancel commands use PlayerCommandPreprocessEvent#setCancelled
     
  9. Yep, I've completed this using an Object, however, I've never done a PlayerCommandPreprocessEvent, I don't know how to get the event to check for a specific command.
     
  10. Code (Java):
    @EventHandler
    public void handleEvents(@NotNull PlayerCommandPreprocessEvent event) {
        if (event.getMessage().equalsIgnoreCase("command")) {
            // command 'command' got sent
        }
    }
     
  11. Is it possible to add aliases for the "command"? I'm gonna be scanning for /msg, which would come in /tell, /w, /whisper and everything else you could think of, is it possible to add aliases so anything that is like a /msg is stopped?
    ======================
    My other question:
    What do I need to do to get a toggle button for each player?
    Usually if I make a boolean, it would be for global, however, this time I want each player to have their own button that they can toggle. How would I do that?
     
  12. For the first one you can just check for each individual argument
    For example
    Code (Text):
    if (event.getmessage().equalsIgnoreCase("FirstAlias") || event.getMessage().equalsIgnoreCase("SecondAlias") {}
    as for the second you could go about that creating a HashMap that contains player and the boolean
    Code (Text):
    HashMap<Player, Boolean> map = new HashMap<>();
    map.put(player, true)
    etc
     
  13. Thanks, I'ma go burn my brain for a sec.
     
  14. Hmm, so I've worked out most things, and decided to use an ArrayList instead.
    However, I'm currently checking for the command "msg" in my Event class, which means I can't check for the player's name and see whether the player the message is going to has toggle messages off. How would I solve this?

    The problem is at onMessage()
    This is what my Event class is set up to look like:
    Code (Java):

    package com.gmail.calorious;

    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import commands.commandmutechat;
    import net.md_5.bungee.api.ChatColor;

    public class EventHandling implements Listener {
         public static List<String> togglepm = new ArrayList<>();
            @EventHandler
               public void onPlayerChat(AsyncPlayerChatEvent e) {
                   if((ChatControl.togglemutechat) && !(e.getPlayer().hasPermission("chatcontrol.bypass")))  {
                       Player p = e.getPlayer();
                       e.setCancelled(true);
                       p.sendMessage(ChatColor.RED + "Chat is currently muted by " + commandmutechat.getChatMuter() + ChatColor.RED + ".");
                   }
               }
         
            @EventHandler
            public void onMessage(PlayerCommandPreprocessEvent e) {
                if(e.getMessage().equalsIgnoreCase("msg") || e.getMessage().equalsIgnoreCase("tell") || e.getMessage().equalsIgnoreCase("w") || e.getMessage().equalsIgnoreCase("whisper") || e.getMessage().equalsIgnoreCase("w") || e.getMessage().equalsIgnoreCase("message")) {
                    if(e.getPlayer().hasPermission("chatcontrol.bypass")) {
                        e.setCancelled(false);
                        e.getPlayer().sendMessage(ChatColor.GOLD + "Note: The player you are attempting to message has private messages turned off.");
                        e.getPlayer().sendMessage(ChatColor.GOLD + "You are able to message as you are able to bypass.");
                    } else {
                        togglepm.contains(//args[1]??);
                    }
                }
            }

    }
     
    (Yes I get that I'm doing hard, coding, I may add configuration messages at a later time.)
     
  15. Well you need something to keep track of whether the player has messages turned on or off.
    In your command listener class (or which ever class it would be best to keep track of this) add in a HasMap and some verification code.
    Code (Java):
    HashMap<String, Boolean> msgOn = new HashMap<String, Boolean>();
    if (Bukkit.getServer().getPlayer("Player Name") != null) { // Make sure the name in the command is actually a player.
        if (msgOn.containsKey("Player Name")) { // See if the player is in the HashMap
            if (msgOn.get("Player Name")) { // This will return true if they allow messages, false if they turned it off.
                // Code you want to run goes here.
            }
        } else { // They are not in the map, add them and set them to allow messages.
            msgOn.put("Player Name", true);
            // Code you want to run goes here. (same as above. You might want to make it a function, so you don't have to type it twice)
        }
    }
    Then you need to come up with a way for the user to allow chat or not (I would guess another command), then you can just use msgOn.put("Player Name", true or false whatever they set); again.
     
  16. Yeah I get this, but my listener is currently set to only check for the /msg commands. This means it can't put anything like args[0] to get the player the msg is going to, without the args[0] to get the player, I can't do anything about the player's name to check for toggled messages.
     
  17. Oh so you don't know how to get the argument which is the player name?
    You can use:
    Code (Text):
    String[] args = event.getMessage().split(" ");
     
    args[0] would be the command
    args[1] would be the player name
    I think this is what you mean
     
  18. Thanks!
     
Thread Status:
Not open for further replies.