mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-12 16:27:47 +00:00
Support Lazy capabilities on itemstacks, for better ItemStack copy performance. (#7945)
This commit is contained in:
1 parent
b01a217f45
commit
30a33f5c3e
3 files changed
+248
-4
No files matched your search
@@ -22,35 +22,82 @@ package net.minecraftforge.common.capabilities;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import net.minecraft.MethodsReturnNonnullByDefault;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.event.ForgeEventFactory;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@MethodsReturnNonnullByDefault
|
||||
@ParametersAreNonnullByDefault
|
||||
public abstract class CapabilityProvider<B extends CapabilityProvider<B>> implements ICapabilityProvider
|
||||
{
|
||||
@VisibleForTesting
|
||||
static boolean SUPPORTS_LAZY_CAPABILITIES = true;
|
||||
|
||||
private final @Nonnull Class<B> baseClass;
|
||||
private @Nullable CapabilityDispatcher capabilities;
|
||||
private boolean valid = true;
|
||||
|
||||
private boolean isLazy = false;
|
||||
private Supplier<ICapabilityProvider> lazyParentSupplier = null;
|
||||
private CompoundTag lazyData = null;
|
||||
private boolean initialized = false;
|
||||
|
||||
protected CapabilityProvider(Class<B> baseClass)
|
||||
{
|
||||
this.baseClass = baseClass;
|
||||
this(baseClass, false);
|
||||
}
|
||||
|
||||
protected final void gatherCapabilities() { gatherCapabilities(null); }
|
||||
protected CapabilityProvider(final Class<B> baseClass, final boolean isLazy)
|
||||
{
|
||||
this.baseClass = baseClass;
|
||||
this.isLazy = SUPPORTS_LAZY_CAPABILITIES && isLazy;
|
||||
}
|
||||
|
||||
protected final void gatherCapabilities()
|
||||
{
|
||||
gatherCapabilities(() -> null);
|
||||
}
|
||||
|
||||
protected final void gatherCapabilities(@Nullable ICapabilityProvider parent)
|
||||
{
|
||||
gatherCapabilities(() -> parent);
|
||||
}
|
||||
|
||||
protected final void gatherCapabilities(@Nullable Supplier<ICapabilityProvider> parent)
|
||||
{
|
||||
if (isLazy && !initialized)
|
||||
{
|
||||
lazyParentSupplier = parent == null ? () -> null : parent;
|
||||
return;
|
||||
}
|
||||
|
||||
doGatherCapabilities(parent == null ? null : parent.get());
|
||||
}
|
||||
|
||||
private void doGatherCapabilities(@Nullable ICapabilityProvider parent)
|
||||
{
|
||||
this.capabilities = ForgeEventFactory.gatherCapabilities(baseClass, this, parent);
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
protected final @Nullable CapabilityDispatcher getCapabilities()
|
||||
{
|
||||
return this.capabilities;
|
||||
if (isLazy && !initialized)
|
||||
{
|
||||
doGatherCapabilities(lazyParentSupplier.get());
|
||||
if (lazyData != null)
|
||||
{
|
||||
deserializeCaps(lazyData);
|
||||
}
|
||||
}
|
||||
|
||||
return capabilities;
|
||||
}
|
||||
|
||||
public final boolean areCapsCompatible(CapabilityProvider<B> other)
|
||||
@@ -80,6 +127,11 @@ public abstract class CapabilityProvider<B extends CapabilityProvider<B>> implem
|
||||
|
||||
protected final @Nullable CompoundTag serializeCaps()
|
||||
{
|
||||
if (isLazy && !initialized)
|
||||
{
|
||||
return lazyData;
|
||||
}
|
||||
|
||||
final CapabilityDispatcher disp = getCapabilities();
|
||||
if (disp != null)
|
||||
{
|
||||
@@ -90,6 +142,12 @@ public abstract class CapabilityProvider<B extends CapabilityProvider<B>> implem
|
||||
|
||||
protected final void deserializeCaps(CompoundTag tag)
|
||||
{
|
||||
if (isLazy && !initialized)
|
||||
{
|
||||
lazyData = tag;
|
||||
return;
|
||||
}
|
||||
|
||||
final CapabilityDispatcher disp = getCapabilities();
|
||||
if (disp != null)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user