在这个名为“我的世界”的游戏中,你们所有人都听说过这个游戏非常适合学习java以及更多关于图书馆的知识。但是,我和我的朋友试图编写插件以获得乐趣,我们遇到了这个巨大的ArrayIndexOutOfBounds错误。我需要知道这有什么问题。从我收集的内容来看,问题出在第53行。
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;
}
}
答案 0 :(得分: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
行条件来保护循环,以防止尝试在数组末尾添加项目。
答案 1 :(得分:0)
你的错误最有可能发生在这一行:
removeUser[rx] = p;
创建像这样的数组时
Player[] removeUser = new Player[] {null};
它的大小不是动态的,所以如果您希望它的大小增加并且不想担心管理数组大小和复制,请使用Java ArrayList