问题描述
- 求高手帮忙看看这段Java代码有什么错误。
-
package com.nightworld.why;import java.io.File;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;public class NightWorld extends JavaPlugin implements Listener {
int MaxPeopleOfNightWorld=NightWorld.this.getConfig().getInt("MaxPeopleOfNightWorld");
int EveryPeopleZombieSpeed=NightWorld.this.getConfig().getInt("EveryPeopleZombieSpeed");
int cacheTime;
boolean IsZombieRun=false;
boolean IsPlayerInNightWorld=false;
boolean RunAnother=false;
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
boolean IsNightWorld=NightWorldFile();
if (args.length
{
sender.sendMessage("§5参数过少");
return false;
}
if (args.length>1) {
sender.sendMessage("§5参数过多");
return false;
}
Plugin IsMv=getServer().getPluginManager().getPlugin("Multiverse-Core");
if (IsMv==null)
{
sender.sendMessage("§5对不起§3,您的服务器不拥有该插件的前置插件§5Multiverse-Core§3所以NightWorld插件停止运行。");
return true;
}
if(sender instanceof Player)
{
if (IsNightWorld==true)
{
if (cmd.getName().equalsIgnoreCase("nightworld")){
teleportNightWorld((Player)sender,args[0]);return true;
}
}
else
{
if (cmd.getName().equalsIgnoreCase("nightworld")){
sender.sendMessage("§3由于§5"+this.getConfig().getString("NightWorld")+"§3世界不存在。所以即将生成§5"+this.getConfig().getString("NightWorld")+"§3世界。");
getServer().dispatchCommand(getServer().getConsoleSender(), "mv create "+this.getConfig().getString("NightWorld")+" normal");
sender.sendMessage("§3生成§5"+this.getConfig().getString("NightWorld")+"§3世界完成");
sender.sendMessage("§3即将加载§5"+this.getConfig().getString("NightWorld")+"§3世界");
getServer().dispatchCommand(getServer().getConsoleSender(), "mv load "+this.getConfig().getString("NightWorld"));
sender.sendMessage("§3加载§5"+this.getConfig().getString("NightWorld")+"§3世界完成");
sender.sendMessage("§3即将配置§5"+this.getConfig().getString("NightWorld")+"§3世界");
World NightWorld=getServer().getWorld(this.getConfig().getString("NightWorld"));
int x=0;
int y=1;
int z=0;
Location NightWorldLocation=new Location(NightWorld,x,y,z);
Location NightWorldOn=new Location(NightWorld,x,y+1,z);
Block LocationId=NightWorldLocation.getBlock();
Block OnId=NightWorldOn.getBlock();
LocationId.setType(Material.AIR);
OnId.setType(Material.AIR);
NightWorld.setGameRuleValue("doDaylightCycle", "false");
NightWorld.setTime(18000L);
sender.sendMessage("§3配置§5"+this.getConfig().getString("NightWorld")+"§3世界完成");
teleportNightWorld((Player)sender,args[0]);return true;
}
}
}
else
{
sender.sendMessage("§5该指令只能由玩家进行");
return true;
}
return false;
}public boolean NightWorldFile(){
boolean IsNightWorld;
File NightWorldFile = new File("../"+this.getConfig().getString("NightWorld"));
if(Bukkit.getWorld(this.getConfig().getString("NightWorld"))!=null & !NightWorldFile.exists())
{IsNightWorld=true;}
else
{IsNightWorld=false;}
return IsNightWorld;}
public void teleportNightWorld(Player sender,String teleportplayer){
Player target;
if (teleportplayer!=null)
{
target = (Bukkit.getServer().getPlayer(teleportplayer));
if (target == null) {
sender.sendMessage("§5" + teleportplayer + "§3不在线");
return;
}
}
else
{
target = sender;
}
World NightWorld=getServer().getWorld(this.getConfig().getString("NightWorld"));
int x=0;
int y=1;
int z=0;
Location NightWorldLocation=new Location(NightWorld,x,y,z);
target.sendMessage("§2正在将§5"+target.getName()+"§2传送至永夜服务器");
target.teleport(NightWorldLocation);
getServer().dispatchCommand(getServer().getConsoleSender(), "spreadplayers ~ ~ 0 2500 false "+target.getName());
target.sendMessage("§2已经将§5"+target.getName()+"§2传送至永夜服务器");
}
public void onEnable(){
if (!getDataFolder().exists())
{
getDataFolder().mkdir();
}
File NightWorldConfig=new File(getDataFolder()+"config.yml");
if (!(NightWorldConfig.exists()))
{saveDefaultConfig();}
reloadConfig();
getLogger().info("NightWorld插件载入成功");
runFor();
}
public void onDisable() {
getLogger().info("NightWorld插件载出成功");
}
public void runFor()
{
Timer timer=new Timer();
timer.scheduleAtFixedRate(new NightWorldTimer(), 0L,1000L);
}
public void runForTime()
{
Timer timer=new Timer();
timer.scheduleAtFixedRate(new NightWorldTimer2(), 0L,1000L);
}
public void runForTimeFirst()
{
Timer timer=new Timer();
timer.scheduleAtFixedRate(new NightWorldTimer3(), 0L,cacheTime);
}
public class NightWorldTimer extends TimerTask {
@SuppressWarnings("deprecation")
public void run(){
if (IsZombieRun==false)
{
Player[] Players=getServer().getOnlinePlayers();
for (int PlayersLength=Players.length; PlayersLength > 0; PlayersLength--) {
if (Players[PlayersLength-1].getWorld().getName().equals(NightWorld.this.getConfig().getString("NightWorld")))
{
IsPlayerInNightWorld=true;
}
}
if (IsPlayerInNightWorld==true)
{
getLogger().info("3");
runForTime();
runForTimeFirst();
IsZombieRun=true;
}
}
else
{
int NotIsPlayer=0;
Player[] Players=getServer().getOnlinePlayers();
for (int PlayersLength=Players.length; PlayersLength > 0; PlayersLength--) {
if (!Players[PlayersLength-1].getWorld().getName().equals(NightWorld.this.getConfig().getString("NightWorld")))
{
NotIsPlayer++;
}
}
if (NotIsPlayer==Players.length)
{
IsPlayerInNightWorld=false;
}
}}
}
public class NightWorldTimer2 extends TimerTask {
public void run(){
if (IsPlayerInNightWorld==false)
{
return;
}
List NightWorldPlayer=getServer().getWorld(NightWorld.this.getConfig().getString("NightWorld")).getPlayers();
int NightWorldPlayers=NightWorldPlayer.size();
cacheTime= (MaxPeopleOfNightWorld+1-NightWorldPlayers)*EveryPeopleZombieSpeed;}
}
public class NightWorldTimer3 extends TimerTask {
public void run(){
getLogger().info("5.5");
if (IsPlayerInNightWorld==false)
{
getLogger().info("no");
IsZombieRun=false;
return;
}
List NightWorldPlayer=getServer().getWorld(NightWorld.this.getConfig().getString("NightWorld")).getPlayers();
int NightWorldPlayers=NightWorldPlayer.size();
double RandomNum=Math.random()*NightWorldPlayers;
Player NightWorldPlayerOfPlayer=NightWorldPlayer.get((int)RandomNum);
Location PlayerOfPlayerLocation=NightWorldPlayerOfPlayer.getLocation();
getServer().getWorld(NightWorld.this.getConfig().getString("NightWorld")).spawnEntity(PlayerOfPlayerLocation,EntityType.ZOMBIE);
getLogger().info("6");
}}
}
解决方案
最好能说下那地方出错,你说153行和171行,这又没行号,怎么看?
解决方案二:
我已经知道了。是在获取cachetime的时候没有提前获取。而是在与需要这一变量的函数同时进行。我已经延后了。就成功了
解决方案三:
出错在153和171行
解决方案四:
没有高手么。求高手帮我。