Fix capability resolvers being added to items on the client before capabilities got initialized
@@ -14,6 +14,7 @@ public class ItemCapabilityWrapper implements ICapabilityProvider {
protected final ItemStack itemStack;
private final List<ItemCapability> capabilities = new ArrayList<>();
+ private boolean capabilitiesInitialized;
public ItemCapabilityWrapper(ItemStack stack, ItemCapability... caps) {
itemStack = stack;
@@ -31,6 +32,14 @@ public class ItemCapabilityWrapper implements ICapabilityProvider {
// tries to check if we have it, so we just safety check to ensure that it is not so we don't have
// issues when caching our lazy optionals
if (capability != null && !itemStack.isEmpty()) {
+ if (!capabilitiesInitialized) {
+ //If we haven't initialized the capabilities yet (due to them being null), go through and initialize all our handlers
+ // once we have at least one capability requested that is not null
+ capabilitiesInitialized = true;
+ for (ItemCapability cap : capabilities) {
+ cap.addCapabilityResolvers(cap.capabilityCache);
+ }
+ }
//Only provide capabilities if we are not empty
for (ItemCapability cap : capabilities) {
if (cap.capabilityCache.isCapabilityDisabled(capability, null)) {
@@ -39,8 +48,9 @@ public class ItemCapabilityWrapper implements ICapabilityProvider {
} else if (cap.capabilityCache.canResolve(capability)) {
//Make sure that we load any data the cap needs from the stack, as it doesn't have any NBT set when it is initially initialized
// This also allows us to update to any direct changes on the NBT of the stack that someone may have made
+ //TODO: Potentially move the loading to the capability initializing spot, as NBT shouldn't be randomly changing anyways
+ // and then that may allow us to better cache the capabilities
cap.load();
- //TODO: Fix the fact that in the creative menu the items appear as if they have no contents until they are grabbed
return cap.capabilityCache.getCapabilityUnchecked(capability, null);
}
}
@@ -53,10 +63,6 @@ public class ItemCapabilityWrapper implements ICapabilityProvider {
private final CapabilityCache capabilityCache = new CapabilityCache();
private ItemCapabilityWrapper wrapper;
- protected ItemCapability() {
- addCapabilityResolvers(capabilityCache);
- }
-
protected abstract void addCapabilityResolvers(CapabilityCache capabilityCache);
protected void init() {
