首先,我有
if (playerCommand.startsWith("yell")) {
Yell();
}
在标题为“commands.java”的类中此类处理输入命令。我希望在位于其他地方的类中使用此特定命令的方法,代码为
public class Yell {
/*
* The minimum rights required to use the yell channel
*/
private final static int MINIMUM_RIGHTS_REQUIREMENT = 0;
/*
* Time in seconds between the client must wait between submitting yells
*/
private final static int SECONDS_BETWEEN_SUBMITION = 3;
/*
* Minimum player rights at which restrictions are ignored
*/
private final static int BYPASS_LIMITATION_RIGHTS = 6;
/*
* An enumeration containing format data
*/
static enum yellData {
/* Rights, Color, Title */
PLAYER(0,"255","Player"),
SPECIAL(1,"16730502","Special"),
VETERAN(2,"13132800","Veteran"),
DONATOR(3,"42575","Donator"),
SUPER_DONATOR(4,"3327","Super Donator"),
EXTREME_DONATOR(5,"1800639","Extreme Donator"),
TRIAL_MODERATOR(6, "11809016", "Trial Mod"),
MODERATOR(7, "562688", "Mod"),
ADMIN(8, "15604003", "Admin"),
HEAD_ADMIN(9, "16274188", "Head Admin"),
CO_OWNER(10, "13369344", "Co-Owner"),
OWNER(11, "13369344", "Owner");
private int rights;
private String color, title;
private yellData(int rights, String color, String title) {
this.rights = rights;
this.color = color;
this.title = title;
}
private int getRights() {
return rights;
}
private String getColor() {
return color;
}
private String getTitle() {
return title;
}
}
public static void sendYellMessage(final Client c, String text) {
/* Restrictions - Do not apply to rights under Bypass Limitation Rights */
if (c.playerRights <= BYPASS_LIMITATION_RIGHTS) {
if (Connection.isMuted(c)) {
c.sendMessage("You are muted.");
return;
}
if (c.playerRights < MINIMUM_RIGHTS_REQUIREMENT) {
c.sendMessage("You do not have permission to use the yell channel.");
return;
}
if (c.playerCanYell == false) {
c.sendMessage("Please wait a few seconds between yells.");
return;
}
/* Formatting string prevention */
String[] forbidden = {"<col", "<shad", "<img", "<str", "<tran", "tradereq", "duelreq", "clanreq", "chalreq", "@str@", "@"};
for (int i = 0; i < forbidden.length; i++) {
if (text.substring(1).contains(forbidden[i])) {
c.sendMessage("Your submission contains illegal phrases or characters.");
System.out.println(formatPlayerName(c.playerName)+" is attempting to use illegal phrases in yell chat.");
return;
}
}
}
/* Generates the yell message */
String toSend = null;
for (final yellData rank : yellData.values()) {
if (c.playerRights == rank.getRights()) {
String prefix = "[<col="+rank.getColor()+">"+rank.getTitle()+"</col>"+"]";
String name = "<col=255>"+formatPlayerName(c.playerName)+"</col>";
String message = formatChat(text.replaceAll("/", ""));
toSend = prefix+" "+name+": "+message;
}
}
/* Send the message to all online players */
for (int j = 0; j < PlayerHandler.players.length; j++) {
if (PlayerHandler.players[j] != null) {
Client op = (Client) PlayerHandler.players[j];
op.sendMessage(toSend);
}
}
/* Prevent the client from flooding */
c.playerCanYell = false;
EventManager.getSingleton().addEvent(new Event() {
@Override
public void execute(EventContainer e) {
c.playerCanYell = true;
e.stop();
}
}, SECONDS_BETWEEN_SUBMITION * 1000 );
/* Truncate data after use to save memory */
toSend = null;
}
/*
* Capitalizes the first letter between spaces
* Used to properly format character names
*/
private static String formatPlayerName (String in) {
final StringBuilder output = new StringBuilder(in.length());
String[] words = in.split("\\s");
for(byte b=0,l=(byte) words.length;b<l;++b) {
if(b>0) output.append(" ");
output.append(Character.toUpperCase(words[b].charAt(0)))
.append(words[b].substring(1));
}
return output.toString();
}
/*
* Capitalizes the first letter and does not disturb
* the rest of the sentence capitalizations
*/
private static String formatChat (String in) {
return in.substring(0,1).toUpperCase() + in.substring(1);
}
如何在Commands.java文件中调用类“Yell.java”?我在使用
声明为对象的命令中大喊大叫Yell = new Yell();
答案 0 :(得分:1)
使用创建对象的引用
Yell yellObject = new Yell();
yellObject.methodName(); // Use appropriate methodName
new Yell()
创建通过引用变量访问的对象,同样将用于在创建的对象上调用方法。这里yellObject
是对创建对象的引用。
答案 1 :(得分:1)
使用
Yell yell = new Yell();
//它将创建您的Yell类的对象。
和
yell.method();
//这将调用Yell类的方法。
答案 2 :(得分:0)
Yell y = new Yell();
y.methodCommand();
如果Commands.java类中的方法是静态的,您可以通过类名直接调用它:Like
Yell.methodName():