Skip to content

Commit

Permalink
Bounds checking for inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed May 13, 2020
1 parent 339e5db commit 25f6227
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/main/java/net/citizensnpcs/api/trait/trait/Inventory.java
Expand Up @@ -6,6 +6,7 @@
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -182,8 +183,22 @@ public void setContents(ItemStack[] contents) {
maxCopySize = 36;
} else if (npc.getEntity() instanceof StorageMinecart) {
dest = ((StorageMinecart) npc.getEntity()).getInventory();
} else if (npc.getEntity() instanceof Horse) {
dest = ((Horse) npc.getEntity()).getInventory();
}
if (SUPPORT_ABSTRACT_HORSE) {
try {
if (npc.getEntity() instanceof AbstractHorse) {
dest = ((AbstractHorse) npc.getEntity()).getInventory();
}
} catch (Throwable t) {
SUPPORT_ABSTRACT_HORSE = false;
if (npc.getEntity() instanceof Horse) {
dest = ((Horse) npc.getEntity()).getInventory();
}
}
} else {
if (npc.getEntity() instanceof Horse) {
dest = ((Horse) npc.getEntity()).getInventory();
}
}

if (dest == null)
Expand All @@ -193,10 +208,14 @@ public void setContents(ItemStack[] contents) {
}

for (int i = 0; i < maxCopySize; i++) {
dest.setItem(i, contents[i]);
if (i < contents.length) {
dest.setItem(i, contents[i]);
}
}
if (view != null) {
for (int i = 0; i < maxCopySize; i++) {
if (view == null)
return;
for (int i = 0; i < maxCopySize; i++) {
if (i < contents.length && i < view.getSize()) {
view.setItem(i, contents[i]);
}
}
Expand Down Expand Up @@ -227,4 +246,6 @@ void setItemInHand(ItemStack item) {
public String toString() {
return "Inventory{" + Arrays.toString(contents) + "}";
}

private static boolean SUPPORT_ABSTRACT_HORSE = true;
}

0 comments on commit 25f6227

Please sign in to comment.