importing command arguments from another class
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
java minecraft bukkit
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
add a comment |
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
add a comment |
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.
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 thispublic 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
|
show 7 more comments
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.
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 thispublic 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
|
show 7 more comments
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.
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 thispublic 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
|
show 7 more comments
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.
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.
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 thispublic 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
|
show 7 more comments
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 thispublic 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
|
show 7 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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