importing command arguments from another class











up vote
1
down vote

favorite
1












I have to been working on a plugin for a couple of days or so now and I'm not sure on how to do this. I have a command /punish which opens an inventory where moderators can mute, kick, ban or warn the player in arg[0]. once the command has opened the first inventory, I made a different class called guievents which controls the inventory. My question is, how do I get the args from the command in one class to another class. I've been searching on google for a while and most of the time I found people using hashmaps, however, I never quite understood how they were doing it, or if I should use hashmaps at all.
Command Class:



package io.github.bxnie.gui.punish;

import java.util.ArrayList;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import net.minecraft.server.v1_12_R1.CommandExecute;

public class punish extends CommandExecute implements Listener, CommandExecutor{

public String punish = "punish";

//open main GUI for moderation /punish
public boolean onCommand(CommandSender sender, Command cmd, String label, String args) {
Player p = (Player) sender;
if (!(sender instanceof Player)) {
sender.sendMessage("Only players may execute this command!");
return true;
}
if (cmd.getName().equalsIgnoreCase(punish) && sender instanceof Player) {
if (p.hasPermission("fp.punish")) {
if (args.length == 0){
p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Fiore" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + "Correct usage: " + ChatColor.GRAY + ChatColor.ITALIC + "/punish <player>");
return false;
}
if (args.length >= 1){
String message = "";
Player target = Bukkit.getPlayer(args[0]);
for (int i = 0; i < args.length; i++) {
message = message + args[i] + " ";
}
if (message.length() == 0){
p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Fiore" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + "Correct usage: " + ChatColor.GRAY + ChatColor.ITALIC + "/punish <player>");
return false;
}
if (target == null){
p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Fiore" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + "Correct usage: " + ChatColor.GRAY + ChatColor.ITALIC + "player not online");
return false;
}
//Creates the Inventory
Inventory punishgui = Bukkit.createInventory(null, 9, ChatColor.RED + "Punish Menu");

//Where the Items and Meta are made
ItemStack warn = new ItemStack(Material.CONCRETE, 1, (short) 14);
ItemMeta warnmeta = warn.getItemMeta();
warnmeta.setDisplayName(ChatColor.RED + "Warn Player");
warnmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> warnlore = new ArrayList<String>();
warnlore.add(ChatColor.GRAY + "Click this to Warn the Selected Player!");
warnmeta.setLore(warnlore);
warn.setItemMeta(warnmeta);

ItemStack kick = new ItemStack(Material.CONCRETE, 1);
ItemMeta kickmeta = kick.getItemMeta();
kickmeta.setDisplayName(ChatColor.WHITE + "Kick Player");
kickmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> kicklore = new ArrayList<String>();
kicklore.add(ChatColor.GRAY + "Click this to Kick the Selected Player!");
kickmeta.setLore(kicklore);
kick.setItemMeta(kickmeta);

ItemStack mute = new ItemStack(Material.CONCRETE, 1, (short) 1);
ItemMeta mutemeta = mute.getItemMeta();
mutemeta.setDisplayName(ChatColor.GOLD + "Mute Player");
mutemeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> mutelore = new ArrayList<String>();
mutelore.add(ChatColor.GRAY + "Click this to Mute the Selected Player!");
mutemeta.setLore(mutelore);
mute.setItemMeta(mutemeta);

ItemStack tempban = new ItemStack(Material.CONCRETE, 1, (short) 7);
ItemMeta tempbanmeta = tempban.getItemMeta();
tempbanmeta.setDisplayName(ChatColor.GRAY + "TempBan Player");
tempbanmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> tempbanlore = new ArrayList<String>();
tempbanlore.add(ChatColor.GRAY + "Click this to TempBan the Selected Player!");
tempbanmeta.setLore(tempbanlore);
tempban.setItemMeta(tempbanmeta);

ItemStack ban = new ItemStack(Material.CONCRETE, 1, (short) 15);
ItemMeta banmeta = ban.getItemMeta();
banmeta.setDisplayName(ChatColor.DARK_GRAY + "Ban Player");
banmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> banlore = new ArrayList<String>();
banlore.add(ChatColor.GRAY + "Click this to Ban the Selected Player!");
banmeta.setLore(banlore);
ban.setItemMeta(banmeta);

//Positioning
punishgui.setItem(2, warn);
punishgui.setItem(3, kick);
punishgui.setItem(4, mute);
punishgui.setItem(5, tempban);
punishgui.setItem(6, ban);

p.openInventory(punishgui);
}
} else {
p.sendMessage(ChatColor.RED + "Insufficient Permission!");
return false;
}
}
return true;
}
}


one of the guievents classes:



package io.github.bxnie.events.punish;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import io.github.bxnie.gui.punish.punish;
import io.github.bxnie.gui.punish.punishmain;

public class punishmutemenu implements Listener {

@EventHandler
public void InventoryOnClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
Inventory open = event.getInventory();
ItemStack item = event.getCurrentItem();

if(open.getName().equals(ChatColor.RED + "Mute Time Menu")) {
event.setCancelled(true);
if(item == null || !item.hasItemMeta()) {
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "< Back")) {
player.openInventory(punishmain.punishmaingui());
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "5 Minutes")) {
Bukkit.dispatchCommand(player, "mute " + name + " 5mins");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "10 Minutes")) {
Bukkit.dispatchCommand(player, "mute " + name + " 10mins");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "30 Minutes")) {
Bukkit.dispatchCommand(player, "mute " + name + " 30mins");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "1 Hour")) {
Bukkit.dispatchCommand(player, "mute " + name + " 1hour");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "3 Hours")) {
Bukkit.dispatchCommand(player, "mute " + name + " 3hours");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "24 Hours")) {
Bukkit.dispatchCommand(player, "mute " + name + " 1day");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "7 Days")) {
Bukkit.dispatchCommand(player, "mute " + name + " 7days");
return;
}
return;
}
}
}


in the second screenshot, I put a temporary "name" as a placeholder as i dont know what to put. if anyone is to reply, could you like explain what you are doing just so i dont fall short of this again.
Thanks










share|improve this question






















  • So, as far as i can see, what you need your inventory class to know, is the player to punish, right? where do u instanciate the menu? simply pass the player to punish in its constructor.
    – Marcel
    Nov 11 at 0:02










  • how would I do that since the player to punish is different depending on what was typed in, /punish <username>, I can fetch the arg[0] from the onCommand, but I'm not sure on how to "export" that and use it in another class. would i use a HashMap? if so how would i go about setting that up
    – Ben Parkes
    Nov 11 at 0:11

















up vote
1
down vote

favorite
1












I have to been working on a plugin for a couple of days or so now and I'm not sure on how to do this. I have a command /punish which opens an inventory where moderators can mute, kick, ban or warn the player in arg[0]. once the command has opened the first inventory, I made a different class called guievents which controls the inventory. My question is, how do I get the args from the command in one class to another class. I've been searching on google for a while and most of the time I found people using hashmaps, however, I never quite understood how they were doing it, or if I should use hashmaps at all.
Command Class:



package io.github.bxnie.gui.punish;

import java.util.ArrayList;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import net.minecraft.server.v1_12_R1.CommandExecute;

public class punish extends CommandExecute implements Listener, CommandExecutor{

public String punish = "punish";

//open main GUI for moderation /punish
public boolean onCommand(CommandSender sender, Command cmd, String label, String args) {
Player p = (Player) sender;
if (!(sender instanceof Player)) {
sender.sendMessage("Only players may execute this command!");
return true;
}
if (cmd.getName().equalsIgnoreCase(punish) && sender instanceof Player) {
if (p.hasPermission("fp.punish")) {
if (args.length == 0){
p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Fiore" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + "Correct usage: " + ChatColor.GRAY + ChatColor.ITALIC + "/punish <player>");
return false;
}
if (args.length >= 1){
String message = "";
Player target = Bukkit.getPlayer(args[0]);
for (int i = 0; i < args.length; i++) {
message = message + args[i] + " ";
}
if (message.length() == 0){
p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Fiore" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + "Correct usage: " + ChatColor.GRAY + ChatColor.ITALIC + "/punish <player>");
return false;
}
if (target == null){
p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Fiore" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + "Correct usage: " + ChatColor.GRAY + ChatColor.ITALIC + "player not online");
return false;
}
//Creates the Inventory
Inventory punishgui = Bukkit.createInventory(null, 9, ChatColor.RED + "Punish Menu");

//Where the Items and Meta are made
ItemStack warn = new ItemStack(Material.CONCRETE, 1, (short) 14);
ItemMeta warnmeta = warn.getItemMeta();
warnmeta.setDisplayName(ChatColor.RED + "Warn Player");
warnmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> warnlore = new ArrayList<String>();
warnlore.add(ChatColor.GRAY + "Click this to Warn the Selected Player!");
warnmeta.setLore(warnlore);
warn.setItemMeta(warnmeta);

ItemStack kick = new ItemStack(Material.CONCRETE, 1);
ItemMeta kickmeta = kick.getItemMeta();
kickmeta.setDisplayName(ChatColor.WHITE + "Kick Player");
kickmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> kicklore = new ArrayList<String>();
kicklore.add(ChatColor.GRAY + "Click this to Kick the Selected Player!");
kickmeta.setLore(kicklore);
kick.setItemMeta(kickmeta);

ItemStack mute = new ItemStack(Material.CONCRETE, 1, (short) 1);
ItemMeta mutemeta = mute.getItemMeta();
mutemeta.setDisplayName(ChatColor.GOLD + "Mute Player");
mutemeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> mutelore = new ArrayList<String>();
mutelore.add(ChatColor.GRAY + "Click this to Mute the Selected Player!");
mutemeta.setLore(mutelore);
mute.setItemMeta(mutemeta);

ItemStack tempban = new ItemStack(Material.CONCRETE, 1, (short) 7);
ItemMeta tempbanmeta = tempban.getItemMeta();
tempbanmeta.setDisplayName(ChatColor.GRAY + "TempBan Player");
tempbanmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> tempbanlore = new ArrayList<String>();
tempbanlore.add(ChatColor.GRAY + "Click this to TempBan the Selected Player!");
tempbanmeta.setLore(tempbanlore);
tempban.setItemMeta(tempbanmeta);

ItemStack ban = new ItemStack(Material.CONCRETE, 1, (short) 15);
ItemMeta banmeta = ban.getItemMeta();
banmeta.setDisplayName(ChatColor.DARK_GRAY + "Ban Player");
banmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> banlore = new ArrayList<String>();
banlore.add(ChatColor.GRAY + "Click this to Ban the Selected Player!");
banmeta.setLore(banlore);
ban.setItemMeta(banmeta);

//Positioning
punishgui.setItem(2, warn);
punishgui.setItem(3, kick);
punishgui.setItem(4, mute);
punishgui.setItem(5, tempban);
punishgui.setItem(6, ban);

p.openInventory(punishgui);
}
} else {
p.sendMessage(ChatColor.RED + "Insufficient Permission!");
return false;
}
}
return true;
}
}


one of the guievents classes:



package io.github.bxnie.events.punish;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import io.github.bxnie.gui.punish.punish;
import io.github.bxnie.gui.punish.punishmain;

public class punishmutemenu implements Listener {

@EventHandler
public void InventoryOnClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
Inventory open = event.getInventory();
ItemStack item = event.getCurrentItem();

if(open.getName().equals(ChatColor.RED + "Mute Time Menu")) {
event.setCancelled(true);
if(item == null || !item.hasItemMeta()) {
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "< Back")) {
player.openInventory(punishmain.punishmaingui());
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "5 Minutes")) {
Bukkit.dispatchCommand(player, "mute " + name + " 5mins");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "10 Minutes")) {
Bukkit.dispatchCommand(player, "mute " + name + " 10mins");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "30 Minutes")) {
Bukkit.dispatchCommand(player, "mute " + name + " 30mins");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "1 Hour")) {
Bukkit.dispatchCommand(player, "mute " + name + " 1hour");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "3 Hours")) {
Bukkit.dispatchCommand(player, "mute " + name + " 3hours");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "24 Hours")) {
Bukkit.dispatchCommand(player, "mute " + name + " 1day");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "7 Days")) {
Bukkit.dispatchCommand(player, "mute " + name + " 7days");
return;
}
return;
}
}
}


in the second screenshot, I put a temporary "name" as a placeholder as i dont know what to put. if anyone is to reply, could you like explain what you are doing just so i dont fall short of this again.
Thanks










share|improve this question






















  • So, as far as i can see, what you need your inventory class to know, is the player to punish, right? where do u instanciate the menu? simply pass the player to punish in its constructor.
    – Marcel
    Nov 11 at 0:02










  • how would I do that since the player to punish is different depending on what was typed in, /punish <username>, I can fetch the arg[0] from the onCommand, but I'm not sure on how to "export" that and use it in another class. would i use a HashMap? if so how would i go about setting that up
    – Ben Parkes
    Nov 11 at 0:11















up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have to been working on a plugin for a couple of days or so now and I'm not sure on how to do this. I have a command /punish which opens an inventory where moderators can mute, kick, ban or warn the player in arg[0]. once the command has opened the first inventory, I made a different class called guievents which controls the inventory. My question is, how do I get the args from the command in one class to another class. I've been searching on google for a while and most of the time I found people using hashmaps, however, I never quite understood how they were doing it, or if I should use hashmaps at all.
Command Class:



package io.github.bxnie.gui.punish;

import java.util.ArrayList;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import net.minecraft.server.v1_12_R1.CommandExecute;

public class punish extends CommandExecute implements Listener, CommandExecutor{

public String punish = "punish";

//open main GUI for moderation /punish
public boolean onCommand(CommandSender sender, Command cmd, String label, String args) {
Player p = (Player) sender;
if (!(sender instanceof Player)) {
sender.sendMessage("Only players may execute this command!");
return true;
}
if (cmd.getName().equalsIgnoreCase(punish) && sender instanceof Player) {
if (p.hasPermission("fp.punish")) {
if (args.length == 0){
p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Fiore" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + "Correct usage: " + ChatColor.GRAY + ChatColor.ITALIC + "/punish <player>");
return false;
}
if (args.length >= 1){
String message = "";
Player target = Bukkit.getPlayer(args[0]);
for (int i = 0; i < args.length; i++) {
message = message + args[i] + " ";
}
if (message.length() == 0){
p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Fiore" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + "Correct usage: " + ChatColor.GRAY + ChatColor.ITALIC + "/punish <player>");
return false;
}
if (target == null){
p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Fiore" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + "Correct usage: " + ChatColor.GRAY + ChatColor.ITALIC + "player not online");
return false;
}
//Creates the Inventory
Inventory punishgui = Bukkit.createInventory(null, 9, ChatColor.RED + "Punish Menu");

//Where the Items and Meta are made
ItemStack warn = new ItemStack(Material.CONCRETE, 1, (short) 14);
ItemMeta warnmeta = warn.getItemMeta();
warnmeta.setDisplayName(ChatColor.RED + "Warn Player");
warnmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> warnlore = new ArrayList<String>();
warnlore.add(ChatColor.GRAY + "Click this to Warn the Selected Player!");
warnmeta.setLore(warnlore);
warn.setItemMeta(warnmeta);

ItemStack kick = new ItemStack(Material.CONCRETE, 1);
ItemMeta kickmeta = kick.getItemMeta();
kickmeta.setDisplayName(ChatColor.WHITE + "Kick Player");
kickmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> kicklore = new ArrayList<String>();
kicklore.add(ChatColor.GRAY + "Click this to Kick the Selected Player!");
kickmeta.setLore(kicklore);
kick.setItemMeta(kickmeta);

ItemStack mute = new ItemStack(Material.CONCRETE, 1, (short) 1);
ItemMeta mutemeta = mute.getItemMeta();
mutemeta.setDisplayName(ChatColor.GOLD + "Mute Player");
mutemeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> mutelore = new ArrayList<String>();
mutelore.add(ChatColor.GRAY + "Click this to Mute the Selected Player!");
mutemeta.setLore(mutelore);
mute.setItemMeta(mutemeta);

ItemStack tempban = new ItemStack(Material.CONCRETE, 1, (short) 7);
ItemMeta tempbanmeta = tempban.getItemMeta();
tempbanmeta.setDisplayName(ChatColor.GRAY + "TempBan Player");
tempbanmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> tempbanlore = new ArrayList<String>();
tempbanlore.add(ChatColor.GRAY + "Click this to TempBan the Selected Player!");
tempbanmeta.setLore(tempbanlore);
tempban.setItemMeta(tempbanmeta);

ItemStack ban = new ItemStack(Material.CONCRETE, 1, (short) 15);
ItemMeta banmeta = ban.getItemMeta();
banmeta.setDisplayName(ChatColor.DARK_GRAY + "Ban Player");
banmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> banlore = new ArrayList<String>();
banlore.add(ChatColor.GRAY + "Click this to Ban the Selected Player!");
banmeta.setLore(banlore);
ban.setItemMeta(banmeta);

//Positioning
punishgui.setItem(2, warn);
punishgui.setItem(3, kick);
punishgui.setItem(4, mute);
punishgui.setItem(5, tempban);
punishgui.setItem(6, ban);

p.openInventory(punishgui);
}
} else {
p.sendMessage(ChatColor.RED + "Insufficient Permission!");
return false;
}
}
return true;
}
}


one of the guievents classes:



package io.github.bxnie.events.punish;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import io.github.bxnie.gui.punish.punish;
import io.github.bxnie.gui.punish.punishmain;

public class punishmutemenu implements Listener {

@EventHandler
public void InventoryOnClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
Inventory open = event.getInventory();
ItemStack item = event.getCurrentItem();

if(open.getName().equals(ChatColor.RED + "Mute Time Menu")) {
event.setCancelled(true);
if(item == null || !item.hasItemMeta()) {
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "< Back")) {
player.openInventory(punishmain.punishmaingui());
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "5 Minutes")) {
Bukkit.dispatchCommand(player, "mute " + name + " 5mins");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "10 Minutes")) {
Bukkit.dispatchCommand(player, "mute " + name + " 10mins");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "30 Minutes")) {
Bukkit.dispatchCommand(player, "mute " + name + " 30mins");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "1 Hour")) {
Bukkit.dispatchCommand(player, "mute " + name + " 1hour");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "3 Hours")) {
Bukkit.dispatchCommand(player, "mute " + name + " 3hours");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "24 Hours")) {
Bukkit.dispatchCommand(player, "mute " + name + " 1day");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "7 Days")) {
Bukkit.dispatchCommand(player, "mute " + name + " 7days");
return;
}
return;
}
}
}


in the second screenshot, I put a temporary "name" as a placeholder as i dont know what to put. if anyone is to reply, could you like explain what you are doing just so i dont fall short of this again.
Thanks










share|improve this question













I have to been working on a plugin for a couple of days or so now and I'm not sure on how to do this. I have a command /punish which opens an inventory where moderators can mute, kick, ban or warn the player in arg[0]. once the command has opened the first inventory, I made a different class called guievents which controls the inventory. My question is, how do I get the args from the command in one class to another class. I've been searching on google for a while and most of the time I found people using hashmaps, however, I never quite understood how they were doing it, or if I should use hashmaps at all.
Command Class:



package io.github.bxnie.gui.punish;

import java.util.ArrayList;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import net.minecraft.server.v1_12_R1.CommandExecute;

public class punish extends CommandExecute implements Listener, CommandExecutor{

public String punish = "punish";

//open main GUI for moderation /punish
public boolean onCommand(CommandSender sender, Command cmd, String label, String args) {
Player p = (Player) sender;
if (!(sender instanceof Player)) {
sender.sendMessage("Only players may execute this command!");
return true;
}
if (cmd.getName().equalsIgnoreCase(punish) && sender instanceof Player) {
if (p.hasPermission("fp.punish")) {
if (args.length == 0){
p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Fiore" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + "Correct usage: " + ChatColor.GRAY + ChatColor.ITALIC + "/punish <player>");
return false;
}
if (args.length >= 1){
String message = "";
Player target = Bukkit.getPlayer(args[0]);
for (int i = 0; i < args.length; i++) {
message = message + args[i] + " ";
}
if (message.length() == 0){
p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Fiore" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + "Correct usage: " + ChatColor.GRAY + ChatColor.ITALIC + "/punish <player>");
return false;
}
if (target == null){
p.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Fiore" + ChatColor.DARK_GRAY + "] " + ChatColor.RED + "Correct usage: " + ChatColor.GRAY + ChatColor.ITALIC + "player not online");
return false;
}
//Creates the Inventory
Inventory punishgui = Bukkit.createInventory(null, 9, ChatColor.RED + "Punish Menu");

//Where the Items and Meta are made
ItemStack warn = new ItemStack(Material.CONCRETE, 1, (short) 14);
ItemMeta warnmeta = warn.getItemMeta();
warnmeta.setDisplayName(ChatColor.RED + "Warn Player");
warnmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> warnlore = new ArrayList<String>();
warnlore.add(ChatColor.GRAY + "Click this to Warn the Selected Player!");
warnmeta.setLore(warnlore);
warn.setItemMeta(warnmeta);

ItemStack kick = new ItemStack(Material.CONCRETE, 1);
ItemMeta kickmeta = kick.getItemMeta();
kickmeta.setDisplayName(ChatColor.WHITE + "Kick Player");
kickmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> kicklore = new ArrayList<String>();
kicklore.add(ChatColor.GRAY + "Click this to Kick the Selected Player!");
kickmeta.setLore(kicklore);
kick.setItemMeta(kickmeta);

ItemStack mute = new ItemStack(Material.CONCRETE, 1, (short) 1);
ItemMeta mutemeta = mute.getItemMeta();
mutemeta.setDisplayName(ChatColor.GOLD + "Mute Player");
mutemeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> mutelore = new ArrayList<String>();
mutelore.add(ChatColor.GRAY + "Click this to Mute the Selected Player!");
mutemeta.setLore(mutelore);
mute.setItemMeta(mutemeta);

ItemStack tempban = new ItemStack(Material.CONCRETE, 1, (short) 7);
ItemMeta tempbanmeta = tempban.getItemMeta();
tempbanmeta.setDisplayName(ChatColor.GRAY + "TempBan Player");
tempbanmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> tempbanlore = new ArrayList<String>();
tempbanlore.add(ChatColor.GRAY + "Click this to TempBan the Selected Player!");
tempbanmeta.setLore(tempbanlore);
tempban.setItemMeta(tempbanmeta);

ItemStack ban = new ItemStack(Material.CONCRETE, 1, (short) 15);
ItemMeta banmeta = ban.getItemMeta();
banmeta.setDisplayName(ChatColor.DARK_GRAY + "Ban Player");
banmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ArrayList<String> banlore = new ArrayList<String>();
banlore.add(ChatColor.GRAY + "Click this to Ban the Selected Player!");
banmeta.setLore(banlore);
ban.setItemMeta(banmeta);

//Positioning
punishgui.setItem(2, warn);
punishgui.setItem(3, kick);
punishgui.setItem(4, mute);
punishgui.setItem(5, tempban);
punishgui.setItem(6, ban);

p.openInventory(punishgui);
}
} else {
p.sendMessage(ChatColor.RED + "Insufficient Permission!");
return false;
}
}
return true;
}
}


one of the guievents classes:



package io.github.bxnie.events.punish;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import io.github.bxnie.gui.punish.punish;
import io.github.bxnie.gui.punish.punishmain;

public class punishmutemenu implements Listener {

@EventHandler
public void InventoryOnClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
Inventory open = event.getInventory();
ItemStack item = event.getCurrentItem();

if(open.getName().equals(ChatColor.RED + "Mute Time Menu")) {
event.setCancelled(true);
if(item == null || !item.hasItemMeta()) {
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "< Back")) {
player.openInventory(punishmain.punishmaingui());
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "5 Minutes")) {
Bukkit.dispatchCommand(player, "mute " + name + " 5mins");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "10 Minutes")) {
Bukkit.dispatchCommand(player, "mute " + name + " 10mins");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "30 Minutes")) {
Bukkit.dispatchCommand(player, "mute " + name + " 30mins");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "1 Hour")) {
Bukkit.dispatchCommand(player, "mute " + name + " 1hour");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "3 Hours")) {
Bukkit.dispatchCommand(player, "mute " + name + " 3hours");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "24 Hours")) {
Bukkit.dispatchCommand(player, "mute " + name + " 1day");
return;
}
if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "7 Days")) {
Bukkit.dispatchCommand(player, "mute " + name + " 7days");
return;
}
return;
}
}
}


in the second screenshot, I put a temporary "name" as a placeholder as i dont know what to put. if anyone is to reply, could you like explain what you are doing just so i dont fall short of this again.
Thanks







java minecraft bukkit






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 23:39









Ben Parkes

84




84












  • So, as far as i can see, what you need your inventory class to know, is the player to punish, right? where do u instanciate the menu? simply pass the player to punish in its constructor.
    – Marcel
    Nov 11 at 0:02










  • how would I do that since the player to punish is different depending on what was typed in, /punish <username>, I can fetch the arg[0] from the onCommand, but I'm not sure on how to "export" that and use it in another class. would i use a HashMap? if so how would i go about setting that up
    – Ben Parkes
    Nov 11 at 0:11




















  • So, as far as i can see, what you need your inventory class to know, is the player to punish, right? where do u instanciate the menu? simply pass the player to punish in its constructor.
    – Marcel
    Nov 11 at 0:02










  • how would I do that since the player to punish is different depending on what was typed in, /punish <username>, I can fetch the arg[0] from the onCommand, but I'm not sure on how to "export" that and use it in another class. would i use a HashMap? if so how would i go about setting that up
    – Ben Parkes
    Nov 11 at 0:11


















So, as far as i can see, what you need your inventory class to know, is the player to punish, right? where do u instanciate the menu? simply pass the player to punish in its constructor.
– Marcel
Nov 11 at 0:02




So, as far as i can see, what you need your inventory class to know, is the player to punish, right? where do u instanciate the menu? simply pass the player to punish in its constructor.
– Marcel
Nov 11 at 0:02












how would I do that since the player to punish is different depending on what was typed in, /punish <username>, I can fetch the arg[0] from the onCommand, but I'm not sure on how to "export" that and use it in another class. would i use a HashMap? if so how would i go about setting that up
– Ben Parkes
Nov 11 at 0:11






how would I do that since the player to punish is different depending on what was typed in, /punish <username>, I can fetch the arg[0] from the onCommand, but I'm not sure on how to "export" that and use it in another class. would i use a HashMap? if so how would i go about setting that up
– Ben Parkes
Nov 11 at 0:11














1 Answer
1






active

oldest

votes

















up vote
0
down vote













You could use HashMaps,



What you should do is make a HashMap in your CommandClass.



Make the Player object the Key and the arg(s) the value like this:
HashMap myHashMap - In case of a single argument.



HashMap myHashMap - In case of multiple arguments.



Then each time before you open the GUI you should put the Player and Arg(s) in the HashMap. You should implement methods in your command class for fetching the values, for example, public String getArg(Player player), and return the value. And also a remove method(This will prevent errors) called, public void removeArgs(Player player), this should be called when the GUI is closed or whenever you like it.



Hope you could find any use in this.






share|improve this answer





















  • yeah thank you, ill give it a shot, ill comment here what my results were
    – Ben Parkes
    Nov 11 at 0:22










  • just a quick question how do i put the Player and Arg(s) into the hash map, sorry this is my first time really using them
    – Ben Parkes
    Nov 11 at 0:29










  • myHashMap.put(Player, Args);
    – YourPalJake
    Nov 11 at 0:30










  • and im creating the Hashmap Outside the onCommand Constructor so for example like this public HashMap<Player, args> arghash = new HashMap<Player, args>(); this is where i get confused, do i put it inside or outside the constructor
    – Ben Parkes
    Nov 11 at 0:35












  • You totally can, as that happens directly when the class gets initiated. But mind if you wanna limit the access of your HashMap by making it private or any other access modifier and use methods. You should also store the commandClass instance in the Event class in a field, so you won't have to use static methods as that is a bad habit.
    – YourPalJake
    Nov 11 at 0:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244498%2fimporting-command-arguments-from-another-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













You could use HashMaps,



What you should do is make a HashMap in your CommandClass.



Make the Player object the Key and the arg(s) the value like this:
HashMap myHashMap - In case of a single argument.



HashMap myHashMap - In case of multiple arguments.



Then each time before you open the GUI you should put the Player and Arg(s) in the HashMap. You should implement methods in your command class for fetching the values, for example, public String getArg(Player player), and return the value. And also a remove method(This will prevent errors) called, public void removeArgs(Player player), this should be called when the GUI is closed or whenever you like it.



Hope you could find any use in this.






share|improve this answer





















  • yeah thank you, ill give it a shot, ill comment here what my results were
    – Ben Parkes
    Nov 11 at 0:22










  • just a quick question how do i put the Player and Arg(s) into the hash map, sorry this is my first time really using them
    – Ben Parkes
    Nov 11 at 0:29










  • myHashMap.put(Player, Args);
    – YourPalJake
    Nov 11 at 0:30










  • and im creating the Hashmap Outside the onCommand Constructor so for example like this public HashMap<Player, args> arghash = new HashMap<Player, args>(); this is where i get confused, do i put it inside or outside the constructor
    – Ben Parkes
    Nov 11 at 0:35












  • You totally can, as that happens directly when the class gets initiated. But mind if you wanna limit the access of your HashMap by making it private or any other access modifier and use methods. You should also store the commandClass instance in the Event class in a field, so you won't have to use static methods as that is a bad habit.
    – YourPalJake
    Nov 11 at 0:44















up vote
0
down vote













You could use HashMaps,



What you should do is make a HashMap in your CommandClass.



Make the Player object the Key and the arg(s) the value like this:
HashMap myHashMap - In case of a single argument.



HashMap myHashMap - In case of multiple arguments.



Then each time before you open the GUI you should put the Player and Arg(s) in the HashMap. You should implement methods in your command class for fetching the values, for example, public String getArg(Player player), and return the value. And also a remove method(This will prevent errors) called, public void removeArgs(Player player), this should be called when the GUI is closed or whenever you like it.



Hope you could find any use in this.






share|improve this answer





















  • yeah thank you, ill give it a shot, ill comment here what my results were
    – Ben Parkes
    Nov 11 at 0:22










  • just a quick question how do i put the Player and Arg(s) into the hash map, sorry this is my first time really using them
    – Ben Parkes
    Nov 11 at 0:29










  • myHashMap.put(Player, Args);
    – YourPalJake
    Nov 11 at 0:30










  • and im creating the Hashmap Outside the onCommand Constructor so for example like this public HashMap<Player, args> arghash = new HashMap<Player, args>(); this is where i get confused, do i put it inside or outside the constructor
    – Ben Parkes
    Nov 11 at 0:35












  • You totally can, as that happens directly when the class gets initiated. But mind if you wanna limit the access of your HashMap by making it private or any other access modifier and use methods. You should also store the commandClass instance in the Event class in a field, so you won't have to use static methods as that is a bad habit.
    – YourPalJake
    Nov 11 at 0:44













up vote
0
down vote










up vote
0
down vote









You could use HashMaps,



What you should do is make a HashMap in your CommandClass.



Make the Player object the Key and the arg(s) the value like this:
HashMap myHashMap - In case of a single argument.



HashMap myHashMap - In case of multiple arguments.



Then each time before you open the GUI you should put the Player and Arg(s) in the HashMap. You should implement methods in your command class for fetching the values, for example, public String getArg(Player player), and return the value. And also a remove method(This will prevent errors) called, public void removeArgs(Player player), this should be called when the GUI is closed or whenever you like it.



Hope you could find any use in this.






share|improve this answer












You could use HashMaps,



What you should do is make a HashMap in your CommandClass.



Make the Player object the Key and the arg(s) the value like this:
HashMap myHashMap - In case of a single argument.



HashMap myHashMap - In case of multiple arguments.



Then each time before you open the GUI you should put the Player and Arg(s) in the HashMap. You should implement methods in your command class for fetching the values, for example, public String getArg(Player player), and return the value. And also a remove method(This will prevent errors) called, public void removeArgs(Player player), this should be called when the GUI is closed or whenever you like it.



Hope you could find any use in this.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 0:18









YourPalJake

116




116












  • yeah thank you, ill give it a shot, ill comment here what my results were
    – Ben Parkes
    Nov 11 at 0:22










  • just a quick question how do i put the Player and Arg(s) into the hash map, sorry this is my first time really using them
    – Ben Parkes
    Nov 11 at 0:29










  • myHashMap.put(Player, Args);
    – YourPalJake
    Nov 11 at 0:30










  • and im creating the Hashmap Outside the onCommand Constructor so for example like this public HashMap<Player, args> arghash = new HashMap<Player, args>(); this is where i get confused, do i put it inside or outside the constructor
    – Ben Parkes
    Nov 11 at 0:35












  • You totally can, as that happens directly when the class gets initiated. But mind if you wanna limit the access of your HashMap by making it private or any other access modifier and use methods. You should also store the commandClass instance in the Event class in a field, so you won't have to use static methods as that is a bad habit.
    – YourPalJake
    Nov 11 at 0:44


















  • yeah thank you, ill give it a shot, ill comment here what my results were
    – Ben Parkes
    Nov 11 at 0:22










  • just a quick question how do i put the Player and Arg(s) into the hash map, sorry this is my first time really using them
    – Ben Parkes
    Nov 11 at 0:29










  • myHashMap.put(Player, Args);
    – YourPalJake
    Nov 11 at 0:30










  • and im creating the Hashmap Outside the onCommand Constructor so for example like this public HashMap<Player, args> arghash = new HashMap<Player, args>(); this is where i get confused, do i put it inside or outside the constructor
    – Ben Parkes
    Nov 11 at 0:35












  • You totally can, as that happens directly when the class gets initiated. But mind if you wanna limit the access of your HashMap by making it private or any other access modifier and use methods. You should also store the commandClass instance in the Event class in a field, so you won't have to use static methods as that is a bad habit.
    – YourPalJake
    Nov 11 at 0:44
















yeah thank you, ill give it a shot, ill comment here what my results were
– Ben Parkes
Nov 11 at 0:22




yeah thank you, ill give it a shot, ill comment here what my results were
– Ben Parkes
Nov 11 at 0:22












just a quick question how do i put the Player and Arg(s) into the hash map, sorry this is my first time really using them
– Ben Parkes
Nov 11 at 0:29




just a quick question how do i put the Player and Arg(s) into the hash map, sorry this is my first time really using them
– Ben Parkes
Nov 11 at 0:29












myHashMap.put(Player, Args);
– YourPalJake
Nov 11 at 0:30




myHashMap.put(Player, Args);
– YourPalJake
Nov 11 at 0:30












and im creating the Hashmap Outside the onCommand Constructor so for example like this public HashMap<Player, args> arghash = new HashMap<Player, args>(); this is where i get confused, do i put it inside or outside the constructor
– Ben Parkes
Nov 11 at 0:35






and im creating the Hashmap Outside the onCommand Constructor so for example like this public HashMap<Player, args> arghash = new HashMap<Player, args>(); this is where i get confused, do i put it inside or outside the constructor
– Ben Parkes
Nov 11 at 0:35














You totally can, as that happens directly when the class gets initiated. But mind if you wanna limit the access of your HashMap by making it private or any other access modifier and use methods. You should also store the commandClass instance in the Event class in a field, so you won't have to use static methods as that is a bad habit.
– YourPalJake
Nov 11 at 0:44




You totally can, as that happens directly when the class gets initiated. But mind if you wanna limit the access of your HashMap by making it private or any other access modifier and use methods. You should also store the commandClass instance in the Event class in a field, so you won't have to use static methods as that is a bad habit.
– YourPalJake
Nov 11 at 0:44


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244498%2fimporting-command-arguments-from-another-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python