gpt4 book ai didi

java - 从另一个类导入命令参数

转载 作者:太空宇宙 更新时间:2023-11-04 10:03:15 25 4
gpt4 key购买 nike

我现在必须在插件上工作几天左右,但我不确定如何做到这一点。我有一个命令/punish,它会打开一个 list ,管理员可以在其中对 arg[0] 中的玩家静音、踢、禁止或警告玩家。一旦命令打开第一个库存,我就创建了一个名为 guievents 的不同类来控制库存。我的问题是,如何将参数从一个类中的命令获取到另一个类。我在谷歌上搜索了一段时间,大多数时候我发现人们使用 HashMap ,但是,我从来不太明白他们是如何做到这一点的,或者我是否应该使用 HashMap 。命令类:

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;
}
}

guievents 类之一:

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;
}
}
}

在第二个屏幕截图中,我放置了一个临时“名称”作为占位符,因为我不知道要放置什么。如果有人要回复,您能否解释一下您在做什么,这样我就不会再次达不到这一点。谢谢

最佳答案

您可以使用 HashMap,

你应该做的是在你的 CommandClass 中创建一个 HashMap。

将 Player 对象设为 Key,并将 arg(s) 设为值,如下所示:HashMap myHashMap - 如果是单个参数。

HashMap myHashMap - 如果有多个参数。

然后,每次打开 GUI 之前,您都应该将 Player 和 Arg 放入 HashMap 中。您应该在命令类中实现用于获取值的方法,例如 public String getArg(Player player),并返回值。还有一个名为 public void removeArgs(Player player) 的删除方法(这将防止错误),应该在 GUI 关闭时或您喜欢的任何时候调用它。

希望您能从中找到任何用处。

关于java - 从另一个类导入命令参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53244498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com