首先,我有
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();