我正在制作Bukkit插件,我需要能够从其他类中获取变量。在我的Main
课程中,我尝试从其他课程中获取变量,但我没有收到任何控制台错误,但我无法获取变量而我无法获得要使用CommandHandler
类(启用命令)。
这是我的Main
班级代码:
package me.AngryCupcake274.EntityManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
// variables for testing if user wants these entities to be removed
Boolean arrow;
Boolean boat;
Boolean droppeditem;
Boolean egg;
Boolean enderpearl;
Boolean endersignal;
Boolean experienceorb;
Boolean fireball;
Boolean firework;
Boolean fishinghook;
Boolean itemframe;
Boolean leashhitch;
Boolean lightning;
Boolean minecart;
Boolean minecartchest;
Boolean minecartcommand;
Boolean minecartfurnace;
Boolean minecarthopper;
Boolean minecartmobspawner;
Boolean minecarttnt;
Boolean painting;
Boolean primedtnt;
Boolean smallfireball;
Boolean snowball;
Boolean splashpotion;
Boolean thrownexpbottle;
Boolean witherskull;
// variables for telling how long the user wants for delays
int loopdelay;
String oneminutemessage;
String oneminutecolor;
String threesecondsmessage;
String threesecondscolor;
String twosecondsmessage;
String twosecondscolor;
String onesecondmessage;
String onesecondcolor;
String finalmessage;
String finalcolor;
String oneMinWarn;
// variables for the warnings
String threeSecWarn;
String twoSecWarn;
String oneSecWarn;
String removeInfo;
String[] worldNames;
Server server = Bukkit.getServer();
// variables for players
Player cleaner;
// variables for technical messages
ConsoleCommandSender console = server.getConsoleSender();
PluginDescriptionFile pdf = this.getDescription();
String pdfEnable = ChatColor.RED + pdf.getName() + " " + ChatColor.GREEN
+ pdf.getVersion() + ChatColor.AQUA + " Enabled!";
String pdfDisable = ChatColor.RED + pdf.getName() + " " + ChatColor.GREEN
+ pdf.getVersion() + ChatColor.DARK_PURPLE + " Enabled!";
// variables for in-program
int counter = 0;
int totalcounter = 0;
// variables for getting plugins
ConfigHandler ch;
CommandHandler comh;
RemoveEntities re;
CleanEntities cent;
public void onEnable(ConfigHandler ch, CommandHandler comh, RemoveEntities re, CleanEntities cent) {
this.ch = ch;
this.comh = comh;
this.re = re;
this.cent = cent;
this.getCommand("entitymanager").setExecutor(comh);
// test to make sure all pluigins are reachable
console.sendMessage(ChatColor.DARK_RED + "EntityManager - main class enabled!");
if (ch.test == 1) {
console.sendMessage(ChatColor.RED + "ConfigHandler - class enabled!");
}
if (comh.test == 1) {
console.sendMessage(ChatColor.GOLD + "CommandHandler - class enabled!");
}
if (re.test == 1) {
console.sendMessage(ChatColor.YELLOW + "RemoveEntities - class enabled!");
}
if (cent.test == 1) {
console.sendMessage(ChatColor.GREEN + "CleanEntities - class enabled!");
}
}
public void onDisable() {
}
}
这是我的CommandHandler
班级代码:
package me.AngryCupcake274.EntityManager;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class CommandHandler extends JavaPlugin implements CommandExecutor {
private Main plugin;
private CleanEntities cent;
String entityname;
int test = 1;
public boolean onCommand(CommandSender sender, Command cmd,
String commandLabel, String[] args) {
Player p = (Player) sender;
plugin.cleaner = (Player) sender;
if (cmd.getName().equalsIgnoreCase("entitymanager")) {
// player typed '/entitymanager'
if (p.hasPermission("entitymanager.main")
|| p.hasPermission("entitymanager.*")
|| p.hasPermission("*.*")) {
// player has permission
if (args[0].equalsIgnoreCase("help")) {
// player typed '/entitymanager help'
if (p.hasPermission("entitymanager.help")
|| p.hasPermission("entitymanager.*")
|| p.hasPermission("*.*")) {
// player has permission
p.sendMessage(ChatColor.DARK_PURPLE + "Help:");
p.sendMessage("");
p.sendMessage(ChatColor.GOLD + "'/entitymanager help' "
+ ChatColor.RED + "for help");
p.sendMessage(ChatColor.GOLD
+ "'/entitymanager entitylist [page number]' "
+ ChatColor.RED + "for entity list");
p.sendMessage(ChatColor.GOLD
+ "'/entitymanager clearentity [entity name]' "
+ ChatColor.RED + "to remove a specific entity");
p.sendMessage(ChatColor.GOLD
+ "'/entitymanager clearentity all' "
+ ChatColor.RED + "to remove all entities");
// p.sendMessage(ChatColor.GOLD +
// "'/entitymanager gui' "
// + ChatColor.RED + "to open an EntityManager GUI");
p.sendMessage(ChatColor.RED + "Incorrect arguments!");
return true;
}
p.sendMessage(ChatColor.RED + "You don't have permissions!");
return true;
}
if (args[0].equalsIgnoreCase("entitylist")) {
// player typed '/entitymanager entitylist'
if (p.hasPermission("entitymanager.entitylist")
|| p.hasPermission("entitymanager.*")
|| p.hasPermission("*.*")) {
// player has permission
if (args[1].equalsIgnoreCase(null)
|| args[1].equalsIgnoreCase("1")) {
// player typed '/entitymanager entitylist'
// or
// player typed '/entitymanager entitylist 1'
p.sendMessage(ChatColor.DARK_PURPLE
+ "Entity List: Page 1/8");
p.sendMessage("");
p.sendMessage(ChatColor.GOLD + "arrow"); // 1
p.sendMessage(ChatColor.GOLD + "bat"); // 2
p.sendMessage(ChatColor.GOLD + "blaze"); // 3
p.sendMessage(ChatColor.GOLD + "boat"); // 4
p.sendMessage(ChatColor.GOLD + "cavespider"); // 5
p.sendMessage(ChatColor.GOLD + "chicken"); // 6
p.sendMessage(ChatColor.GOLD + "cow"); // 7
p.sendMessage(ChatColor.GOLD + "creeper"); // 8
// end of page 1
}
if (args[1].equalsIgnoreCase("2")) {
// player typed '/entitymanager entitylist 2'
p.sendMessage(ChatColor.DARK_PURPLE
+ "Entity List: Page 2/8");
p.sendMessage("");
p.sendMessage(ChatColor.GOLD + "item"); // 1
p.sendMessage(ChatColor.GOLD + "egg"); // 2
p.sendMessage(ChatColor.GOLD + "endercrystal"); // 3
p.sendMessage(ChatColor.GOLD + "enderdragon"); // 4
p.sendMessage(ChatColor.GOLD + "enderpearl"); // 5
p.sendMessage(ChatColor.GOLD + "endersignal"); // 6
p.sendMessage(ChatColor.GOLD + "enderman"); // 7
p.sendMessage(ChatColor.GOLD + "experienceorb"); // 8
// end of page 2
}
if (args[1].equalsIgnoreCase("3")) {
// player typed '/entitymanager entitylist 3'
p.sendMessage(ChatColor.DARK_PURPLE
+ "Entity List: Page 3/8");
p.sendMessage("");
p.sendMessage(ChatColor.GOLD + "fireball"); // 1
p.sendMessage(ChatColor.GOLD + "firework"); // 2
p.sendMessage(ChatColor.GOLD + "fishinghook"); // 3
p.sendMessage(ChatColor.GOLD + "ghast"); // 4
p.sendMessage(ChatColor.GOLD + "giant"); // 5
p.sendMessage(ChatColor.GOLD + "horse"); // 6
p.sendMessage(ChatColor.GOLD + "irongolem"); // 7
p.sendMessage(ChatColor.GOLD + "itemframe"); // 8
// end of page 3
}
if (args[1].equalsIgnoreCase("4")) {
// player typed '/entitymanager entitylist 4'
p.sendMessage(ChatColor.DARK_PURPLE
+ "Entity List: Page 4/8");
p.sendMessage("");
p.sendMessage(ChatColor.GOLD + "leashhitch"); // 1
p.sendMessage(ChatColor.GOLD + "lightning"); // 2
p.sendMessage(ChatColor.GOLD + "magmacube"); // 3
p.sendMessage(ChatColor.GOLD + "minecart"); // 4
p.sendMessage(ChatColor.GOLD + "minecartchest"); // 5
p.sendMessage(ChatColor.GOLD + "minecartcommand"); // 6
p.sendMessage(ChatColor.GOLD + "minecartfurnace"); // 7
p.sendMessage(ChatColor.GOLD + "minecarthopper"); // 8
// end of page 4
}
if (args[1].equalsIgnoreCase("5")) {
p.sendMessage(ChatColor.DARK_PURPLE
+ "Entity List: Page 5/8");
p.sendMessage("");
p.sendMessage(ChatColor.GOLD + "minecartmobspawner"); // 1
p.sendMessage(ChatColor.GOLD + "minecarttnt"); // 2
p.sendMessage(ChatColor.GOLD + "mushroomcow"); // 3
p.sendMessage(ChatColor.GOLD + "ocelot"); // 4
p.sendMessage(ChatColor.GOLD + "painting"); // 5
p.sendMessage(ChatColor.GOLD + "pig"); // 6
p.sendMessage(ChatColor.GOLD + "pigzombie"); // 7
p.sendMessage(ChatColor.GOLD + "primedtnt"); // 8
// end of page 5
}
if (args[1].equalsIgnoreCase("6")) {
p.sendMessage(ChatColor.DARK_PURPLE
+ "Entity List: Page 6/8");
p.sendMessage("");
p.sendMessage(ChatColor.GOLD + "sheep"); // 1
p.sendMessage(ChatColor.GOLD + "silverfish"); // 2
p.sendMessage(ChatColor.GOLD + "skeleton"); // 3
p.sendMessage(ChatColor.GOLD + "slime"); // 4
p.sendMessage(ChatColor.GOLD + "smallfireball"); // 5
p.sendMessage(ChatColor.GOLD + "snowball"); // 6
p.sendMessage(ChatColor.GOLD + "snowman"); // 7
p.sendMessage(ChatColor.GOLD + "spider"); // 8
// end of page 6
}
if (args[1].equalsIgnoreCase("7")) {
// player typed '/entitymanager entitylist 7'
p.sendMessage(ChatColor.DARK_PURPLE
+ "Entity List: Page 7/8");
p.sendMessage("");
p.sendMessage(ChatColor.GOLD + "splashpotion"); // 1
p.sendMessage(ChatColor.GOLD + "squid"); // 2
p.sendMessage(ChatColor.GOLD + "thrownexpbottle"); // 3
p.sendMessage(ChatColor.GOLD + "villager"); // 4
p.sendMessage(ChatColor.GOLD + "witch"); // 5
p.sendMessage(ChatColor.GOLD + "wither"); // 6
p.sendMessage(ChatColor.GOLD + "witherskull"); // 7
p.sendMessage(ChatColor.GOLD + "wolf"); // 8
// end of page 7
}
if (args[1].equalsIgnoreCase("8")) {
// player typed '/entitymanager entitylist 8'
p.sendMessage(ChatColor.DARK_PURPLE
+ "Entity List: Page 8/8");
p.sendMessage("");
p.sendMessage(ChatColor.GOLD + "zombie"); // 1
// end of page 8
}
return true;
}
p.sendMessage(ChatColor.RED + "You don't have permissions!");
return true;
}
if (args[0].equalsIgnoreCase("clearentity")) {
// player typed '/entitymanager clearentity'
entityname = args[1].toLowerCase();
switch (entityname) {
case "arrow":
cent.clearArrow(); // player cleared arrows
break;
case "bat":
cent.clearBat(); // player cleared bats
break;
case "blaze":
cent.clearBlaze(); // player cleared blazes
break;
case "boat":
cent.clearBoat(); // player cleared boats
break;
case "cavespider":
cent.clearCaveSpider(); // player cleared save spiders
break;
case "chicken":
cent.clearChicken(); // player cleared chickens
break;
case "cow":
cent.clearCow(); // player cleared cows
break;
case "creeper":
cent.clearCreeper(); // player cleared creepers
break;
case "item":
cent.clearItem(); // player cleared items
break;
case "egg":
cent.clearEgg(); // player cleared eggs'
break;
}
p.sendMessage(ChatColor.RED + "Incorrect arguments!");
return true;
}
p.sendMessage(ChatColor.RED + "Incorrect arguments!");
return true;
}
}
return false;
}
}
答案 0 :(得分:1)
而不是
Boolean arrow;
你应该试试
public static Boolean arrow;
这是一种将一个变量传递给任何其他类的方法,使其变为静态!
您应该对要通过其他类访问的所有变量执行此操作。
答案 1 :(得分:1)
从不同类中获取变量的方法是使声明static
或使constructor
调用这些变量。
这种方式的好处是你可以通过输入classname加一个点来轻松地从其他类调用这个变量,从那里可以得到任何静态的变量/方法。
这样做的缺点是 ANYTHING 可以更改此变量,包括单独的插件。任何插件或其他jar文件都可以更改此变量,如果另一个插件可以导致问题。
如何执行此操作只需在声明变量之前放置static
。例如:
static boolean example = false;
这样做的好处是只有你的插件可以用类里面的构造函数来改变它。该类中的任何变量都可以轻松访问。
这样做的缺点是它比仅仅static
更复杂。它可能导致设置CommandExecutors和侦听器的问题。设置它们时,必须匹配构造函数。
要执行此操作,您需要将其放在类文件中:
[class to import] plugin;
public [current class]([class to import] instance)
{
plugin = instance;
}
plugin
变量将通过plugin.
包含其中的所有变量/方法。在这里,您可以轻松调用这些变量。要在注册事件/命令时匹配构造函数,您将匹配构造函数,如下所示:
this.getServer().getPluginManager().registerEvents(new [classWithEventsToBeRegistered](this), this)
该类与构造函数匹配。这对命令来说也是一样。
答案 2 :(得分:0)
您可以使用static
修饰符。通常在java中你创建类的实例,每个实例可能有不同的变量值。使用static
修饰符的变量或方法与类直接关联,而不与类的实例关联。该类的每个实例都共享方法变量的相同值。
无修饰符(仅对包可见)
private
(仅在课程中可见)
public
(包括外部包在内的整个世界都可见)
protected
(对包和所有子类可见)
您可能想要做的是因为两个类都在同一个包中,要么使用无访问修饰符或protected
访问修饰符与static
修饰符结合使用,所以您的变量将如下所示:
static Boolean arrow;
static Boolean boat;
static Boolean droppeditem;
static Boolean egg;
static Boolean enderpearl;
static Boolean endersignal;
//or
protected static Boolean arrow;
protected static Boolean boat;
protected static Boolean droppeditem;
protected static Boolean egg;
protected static Boolean enderpearl;
protected static Boolean endersignal;
//and so on