2014-10-17 54 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