Skip to content

Commit

Permalink
Update inventory trait less frequently
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jan 1, 2023
1 parent f8f3d10 commit 28efd07
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
Expand Up @@ -14,6 +14,7 @@
import com.google.common.collect.Maps;

import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC.NPCUpdate;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.DataKey;
Expand Down Expand Up @@ -147,7 +148,7 @@ public void onSpawn() {

@Override
public void run() {
if (!npc.isSpawned() || !(npc.getEntity() instanceof LivingEntity))
if (!npc.isSpawned() || !(npc.getEntity() instanceof LivingEntity) || !npc.isUpdating(NPCUpdate.PACKET))
return;
if (npc.getEntity() instanceof Enderman) {
Enderman enderman = (Enderman) npc.getEntity();
Expand Down
76 changes: 46 additions & 30 deletions src/main/java/net/citizensnpcs/api/trait/trait/Inventory.java
Expand Up @@ -13,11 +13,13 @@
import org.bukkit.entity.Player;
import org.bukkit.entity.minecart.StorageMinecart;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
Expand All @@ -30,7 +32,7 @@
@TraitName("inventory")
public class Inventory extends Trait {
private ItemStack[] contents;
private int t;
private InventoryCloseListener listener;
private org.bukkit.inventory.Inventory view;
private final Set<InventoryView> views = new HashSet<InventoryView>();

Expand All @@ -48,38 +50,16 @@ public ItemStack[] getContents() {
if (view != null && !views.isEmpty()) {
return view.getContents();
}
if (npc.isSpawned()) {
saveContents(npc.getEntity());
}
return contents;
}

public org.bukkit.inventory.Inventory getInventoryView() {
return view;
}

@EventHandler(ignoreCancelled = true)
public void inventoryCloseEvent(InventoryCloseEvent event) {
if (!views.contains(event.getView()))
return;
ItemStack[] contents = event.getInventory().getContents();
for (int i = 0; i < contents.length; i++) {
this.contents[i] = contents[i];
if (i == 0) {
if (npc.getEntity() instanceof LivingEntity) {
npc.getOrAddTrait(Equipment.class).setItemInHand(contents[i]);
}
}
}
if (npc.getEntity() instanceof InventoryHolder) {
try {
int maxSize = ((InventoryHolder) npc.getEntity()).getInventory().getStorageContents().length;
((InventoryHolder) npc.getEntity()).getInventory().setStorageContents(Arrays.copyOf(contents, maxSize));
} catch (NoSuchMethodError e) {
int maxSize = ((InventoryHolder) npc.getEntity()).getInventory().getContents().length;
((InventoryHolder) npc.getEntity()).getInventory().setContents(Arrays.copyOf(contents, maxSize));
}
}
views.remove(event.getView());
}

@Override
public void load(DataKey key) throws NPCLoadException {
contents = parseContents(key);
Expand Down Expand Up @@ -119,6 +99,10 @@ public void onSpawn() {
}

public void openInventory(Player sender) {
if (listener != null) {
listener = new InventoryCloseListener();
Bukkit.getPluginManager().registerEvents(listener, CitizensAPI.getPlugin());
}
for (int i = 0; i < view.getSize(); i++) {
if (i >= contents.length)
break;
Expand All @@ -137,10 +121,6 @@ private ItemStack[] parseContents(DataKey key) throws NPCLoadException {

@Override
public void run() {
if (t++ > 10) {
saveContents(npc.getEntity());
t = 0;
}
if (views.isEmpty())
return;
Iterator<InventoryView> itr = views.iterator();
Expand All @@ -155,6 +135,9 @@ public void run() {

@Override
public void save(DataKey key) {
if (npc.isSpawned()) {
saveContents(npc.getEntity());
}
int slot = 0;
for (ItemStack item : contents) {
// Clear previous items to avoid conflicts
Expand Down Expand Up @@ -242,6 +225,11 @@ public void setItem(int slot, ItemStack item) {
} else {
throw new IndexOutOfBoundsException();
}

if (npc.getEntity() instanceof InventoryHolder) {
((InventoryHolder) npc.getEntity()).getInventory().setItem(slot, item);
}

if (slot == 0 && npc.getEntity() instanceof LivingEntity) {
npc.getOrAddTrait(Equipment.class).setItemInHand(item);
}
Expand All @@ -263,5 +251,33 @@ public String toString() {
return "Inventory{" + Arrays.toString(contents) + "}";
}

private class InventoryCloseListener implements Listener {
@EventHandler(ignoreCancelled = true)
public void inventoryCloseEvent(InventoryCloseEvent event) {
if (!views.contains(event.getView()))
return;
ItemStack[] contents = event.getInventory().getContents();
for (int i = 0; i < contents.length; i++) {
Inventory.this.contents[i] = contents[i];
if (i == 0) {
if (npc.getEntity() instanceof LivingEntity) {
npc.getOrAddTrait(Equipment.class).setItemInHand(contents[i]);
}
}
}
if (npc.getEntity() instanceof InventoryHolder) {
try {
int maxSize = ((InventoryHolder) npc.getEntity()).getInventory().getStorageContents().length;
((InventoryHolder) npc.getEntity()).getInventory()
.setStorageContents(Arrays.copyOf(contents, maxSize));
} catch (NoSuchMethodError e) {
int maxSize = ((InventoryHolder) npc.getEntity()).getInventory().getContents().length;
((InventoryHolder) npc.getEntity()).getInventory().setContents(Arrays.copyOf(contents, maxSize));
}
}
views.remove(event.getView());
}
}

private static boolean SUPPORT_ABSTRACT_HORSE = true;
}

0 comments on commit 28efd07

Please sign in to comment.