2014-10-17 57 views
-1

在这个叫做minecraft的游戏中,你可能都听说过这个游戏,它对学习java和更多关于图书馆很有帮助。然而,我和我的朋友试图编写一个插件的乐趣,我们落在这个巨大的ArrayIndexOutOfBounds错误。我需要知道这有什么问题。从我收集,问题是在第53行未知ArrayIndexOutOfBounds:1例外

package com.dillyg10.pvpprotect; 

import com.earth2me.essentials.Essentials; 
import com.earth2me.essentials.User; 
import com.earth2me.essentials.UserMap; 
import java.io.PrintStream; 
import java.util.HashMap; 
import java.util.Map; 
import org.bukkit.Bukkit; 
import org.bukkit.ChatColor; 
import org.bukkit.command.Command; 
import org.bukkit.command.CommandSender; 
import org.bukkit.entity.Player; 
import org.bukkit.event.EventHandler; 
import org.bukkit.event.EventPriority; 
import org.bukkit.event.Listener; 
import org.bukkit.event.entity.EntityDamageByEntityEvent; 
import org.bukkit.event.player.PlayerCommandPreprocessEvent; 
import org.bukkit.event.player.PlayerQuitEvent; 
import org.bukkit.plugin.Plugin; 
import org.bukkit.plugin.PluginLoader; 
import org.bukkit.plugin.PluginManager; 
import org.bukkit.plugin.java.JavaPlugin; 
import org.bukkit.scheduler.BukkitScheduler; 

public class PvpProtect extends JavaPlugin implements Listener { 
    public Map<Player, Integer> tagged = new HashMap(); 

    public void onEnable() { 
     if (getEss() == null) { 
      System.out.println("You must have Essentials installed"); 
      getPluginLoader().disablePlugin(this); 
      return; 
     } 
     Bukkit.getPluginManager().registerEvents(this, this); 
     Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { 
      public void run() { 
       Player[] removeUser = new Player[] {null}; 
       Player[] addUser = new Player[] {null}; 
       int[] x = new int[] {0}; 
       int rx = 0; 
       int ax = 0; 
       for (Player p : PvpProtect.this.tagged.keySet()) { 
        int i = ((Integer)PvpProtect.this.tagged.get(p)).intValue(); 
        i--; 
        if (i == 0) { 
         p.sendMessage("§aYou are no longer pvp-tagged! You are free to logout without penalty."); 
         removeUser[rx] = p; 
         rx = rx + 1; 
        } else { 
         if(Bukkit.getServer().getOnlinePlayers().length > 0) { 
          if(ax > 0) { 
           addUser[(ax - 1)] = p; 
           x[(ax - 1)] = i; 
          } else { 
           addUser[ax] = p; 
           x[ax] = i; 
          } 
          ax = ax + 1; 
         } 
        } 
       } 

       int rx1 = 0; 
       int ax1 = 0; 
       while(rx1 <= (rx - 1)) { 
        PvpProtect.this.tagged.remove(removeUser[(rx1)]); 
        rx1 = rx1 + 1; 
       } 

       while(ax1 <= (ax - 1)) { 
        if(ax1 > 0) { 
         PvpProtect.this.tagged.put(addUser[(ax1 - 1)], Integer.valueOf(x[(ax1 - 1)])); 
        } else { 
         PvpProtect.this.tagged.put(addUser[ax1], Integer.valueOf(x[ax1])); 
        } 
        ax1 = ax1 + 1; 
       } 
      } 
     }, 20L, 20L); 
    } 

    @EventHandler 
    public void onCommandProcessed(PlayerCommandPreprocessEvent e) { 
     String message = e.getMessage(); 
     String[] subpars = message.split(" "); 
     Player p = e.getPlayer(); 
     if ((subpars[0].replace("/", "").equalsIgnoreCase("fly")) && (this.tagged.containsKey(p)) && (!p.hasPermission("pvpprotect.untagable"))) { 
      p.sendMessage("§cYou cannot do /fly while tagged!"); 
      e.setCancelled(true); 
      return; 
     } 
     if ((subpars[0].replace("/", "").equalsIgnoreCase("god")) && (this.tagged.containsKey(p)) && (!p.hasPermission("pvpprotect.untagable"))) { 
      p.sendMessage("§cYou cannot do /god while tagged!"); 
      e.setCancelled(true); 
      return; 
     } 
    } 

    @EventHandler(priority=EventPriority.NORMAL) 
    public void onEntityDamageByEntity(EntityDamageByEntityEvent e) { 
     if (e.isCancelled()) { 
      return; 
     } 
     if ((e.getDamager() instanceof Player)) { 
      Player damager = (Player)e.getDamager(); 
      User du = getEss().getUserMap().getUser(damager.getName()); 
      if ((e.getEntity() instanceof Player)) { 
       if ((!((Player)e.getEntity()).hasPermission("")) && (!this.tagged.containsKey((Player)e.getEntity()))) { 
        ((Player)e.getEntity()).sendMessage("§4You are combat tagged! You cannot use modes s/a fly or god AND cannot logout without death!"); 
       } 
       this.tagged.put((Player)e.getEntity(), Integer.valueOf(7)); 
      } 
      if (du.isFlying()) { 
       if (!(e.getEntity() instanceof Player)) { 
        if (!damager.hasPermission("pvpprotect.flypvp.mob")) { 
         damager.sendMessage(ChatColor.DARK_RED + "You must turn off flying to hurt mobs"); 
         e.setCancelled(true); 
        } 
       } else if (!damager.hasPermission("pvpprotect.flypvp.player")) { 
        damager.sendMessage(ChatColor.DARK_RED + "You must turn off flying to hurt players"); 
        e.setCancelled(true); 
        return; 
       } 
      } 
      if (du.isGodModeEnabled()) { 
       if (!(e.getEntity() instanceof Player)) { 
        if (!damager.hasPermission("pvpprotect.godpvp.mob")) { 
         damager.sendMessage(ChatColor.DARK_RED + "You must turn off god to hurt mobs."); 
         e.setCancelled(true); 
        } 
       } else if (!damager.hasPermission("pvpprotect.godpvp.player")) { 
        damager.sendMessage(ChatColor.DARK_RED + "You must turn off god to hurt players."); 
        e.setCancelled(true); 
        return; 
       } 
      } 
     } 
    } 

    @EventHandler 
    public void onPlayerLogout(PlayerQuitEvent e) { 
     if (this.tagged.containsKey(e.getPlayer())) { 
      e.getPlayer().setHealth(0); 
      Bukkit.broadcastMessage("§c" + e.getPlayer().getName() + " died from §6Pvp-logging! §cDon't be an idiot like him, fight your battles"); 
     } 
    } 

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { 
     if (label.equalsIgnoreCase("pvprot")) { 
      sender.sendMessage("§aPvpProtection v 1.1"); 
      return true; 
     } 
     return false; 
    } 

    public Essentials getEss() { 
     Plugin plugin = Bukkit.getPluginManager().getPlugin("Essentials"); 
     if ((plugin instanceof Essentials)) { 
      return (Essentials)plugin; 
     } 
     return null; 
    } 
} 
+1

你可以发布你正在得到的确切的错误? – Kylar 2014-10-17 22:47:12

+1

哪一行是第53行?不要让我们尝试和计数你的线。 – khelwood 2014-10-17 22:49:51

+0

IndexOutOfBounds意味着你正在尝试索引数组,但这超出了数组的范围 – 2014-10-17 22:49:56

回答

1

ArrayIndexOutOfBoundsException表明你想在一个数组,它是无效的值分配给一个位置。例如,如果你有一个长度为5的数组,试图把东西放在位置10将会抛出ArrayIndexOutOfBoundsException。 (记住,那就是,阵列位置从0开始,所以我们的长度为5的阵列在位置0-4的元素。)你的代码的

线53是这样的:

addUser[(ax - 1)] = p; 

如果该行投掷ArrayIndexOutOfBounds,那么它有理由说,你正试图将一些东西放入addUser阵列中,不适合。

现在你的异常指示,你想放东西在位置1(这是说,ax = 2),但你addUser数组大小的1

你需要警惕你的循环沿着(ax - 1) < addUser.length行的条件,以防止尝试添加超过数组末尾的项目。

0

你的错误是在这一行最有可能发生的事情:

removeUser[rx] = p;

当你创建喜欢这里

Player[] removeUser = new Player[] {null}; 

数组它的大小是不是动态的,所以如果你期待它的大小增加和不想担心管理阵列大小和复制,使用Java ArrayList