Skip to content

Commit

Permalink
Fix PotionEffectType.java
Browse files Browse the repository at this point in the history
Fixing an exception call when starting the server:
java.lang.ArrayIndexOutOfBoundsException: Index 34 out of bounds for length 34
        at org.bukkit.potion.PotionEffectType.registerPotionEffectType(PotionEffectType.java:337) ~[banner-1.19.4-274.jar:?]
        at com.mohistmc.banner.fabric.FabricInjectBukkit.addEnumEffectAndPotion(FabricInjectBukkit.java:131) ~[banner-1.19.4-274.jar:?]
        at com.mohistmc.banner.fabric.FabricInjectBukkit.init(FabricInjectBukkit.java:57) ~[banner-1.19.4-274.jar:?]
        at net.minecraft.class_3324.handler$zde000$banner$init(class_3324.java:1102) ~[server-intermediary.jar:?]
        at net.minecraft.class_3324.<init>(class_3324.java:149) ~[server-intermediary.jar:?]
        at net.minecraft.class_3174.<init>(class_3174.java:17) ~[server-intermediary.jar:?]
        at net.minecraft.class_3176.method_3823(class_3176.java:161) ~[server-intermediary.jar:?]
        at net.minecraft.server.MinecraftServer.method_29741(MinecraftServer.java:650) ~[server-intermediary.jar:?]
        at net.minecraft.server.MinecraftServer.method_29739(MinecraftServer.java:266) ~[server-intermediary.jar:?]
        at java.lang.Thread.run(Thread.java:833) ~[?:?]
  • Loading branch information
Quenteez committed May 26, 2023
1 parent fcfdcf3 commit d4ed020
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/main/java/org/bukkit/potion/PotionEffectType.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.bukkit.potion;

import com.google.common.base.Preconditions;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import java.util.*;

import org.bukkit.Color;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
Expand Down Expand Up @@ -265,10 +265,7 @@ public boolean equals(Object obj) {
return false;
}
final PotionEffectType other = (PotionEffectType) obj;
if (this.id != other.id) {
return false;
}
return true;
return this.id == other.id;
}

@Override
Expand All @@ -280,8 +277,7 @@ public int hashCode() {
public String toString() {
return "PotionEffectType[" + id + ", " + getName() + "]";
}

private static final PotionEffectType[] byId = new PotionEffectType[34];
private static final ArrayList<PotionEffectType> byId = new ArrayList<>();
private static final Map<String, PotionEffectType> byName = new HashMap<String, PotionEffectType>();
private static final Map<NamespacedKey, PotionEffectType> byKey = new HashMap<NamespacedKey, PotionEffectType>();
// will break on updates.
Expand Down Expand Up @@ -309,9 +305,10 @@ public static PotionEffectType getByKey(@Nullable NamespacedKey key) {
@Deprecated
@Nullable
public static PotionEffectType getById(int id) {
if (id >= byId.length || id < 0)
if (id >= byId.size() || id < 0) {
return null;
return byId[id];
}
return byId.get(id);
}

/**
Expand All @@ -334,15 +331,21 @@ public static PotionEffectType getByName(@NotNull String name) {
* @param type PotionType to register
*/
public static void registerPotionEffectType(@NotNull PotionEffectType type) {
if (byId[type.id] != null || byName.containsKey(type.getName().toLowerCase(java.util.Locale.ENGLISH)) || byKey.containsKey(type.key)) {
if (byId.size() <= type.id) {
byId.ensureCapacity(type.id + 1);
while (byId.size() <= type.id) {
byId.add(null);
}
}

if (byId.get(type.id) != null || byName.containsKey(type.getName().toLowerCase(Locale.ENGLISH)) || byKey.containsKey(type.key)) {
throw new IllegalArgumentException("Cannot set already-set type");
} else if (!acceptingNew) {
throw new IllegalStateException(
"No longer accepting new potion effect types (can only be done by the server implementation)");
throw new IllegalStateException("No longer accepting new potion effect types (can only be done by the server implementation)");
}

byId[type.id] = type;
byName.put(type.getName().toLowerCase(java.util.Locale.ENGLISH), type);
byId.set(type.id, type);
byName.put(type.getName().toLowerCase(Locale.ENGLISH), type);
byKey.put(type.key, type);
}

Expand All @@ -361,6 +364,6 @@ public static void stopAcceptingRegistrations() {
*/
@NotNull
public static PotionEffectType[] values() {
return Arrays.copyOfRange(byId, 1, byId.length);
return byId.toArray(new PotionEffectType[0]);
}
}

0 comments on commit d4ed020

Please sign in to comment.