mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-21 20:52:34 +00:00
Port capability patches to 1.13 and refactor to new system (#5134)
* resource package and misc leftover rendering stuff * Port Item and ItemStack patches * Extract most item patches to interface * Refactor Item#setNoRepair to be part of Item.Builder * Adapt AnimationItemOverrideList to new cap system * Improve null contract of OptionalCapabilityInstance * Remove Capability.cast * Update CapabilityAnimation to new cap system * Remove OptionalCapabilityInstance#flatMap and add NonNullConsumer * Add capability patch to Entity, not everything yet * Fix Entity patch and add missing methods to IForgeEntity * Fix null contract of CapabilityDispatcher * Add TileEntity caps * Adapt FluidUtil to new cap system * Add world caps * Add chunk caps * Clean up cap patches * Replace all tabs with spaces * Move builder methods on Item to Item.Builder Whitepsace fix * Sided stuff take 2 * Prevent OptionalCapabilityInstance creating additional empty instances * Cache getCapability result in AnimationTESR * Fix imports in TileEntity patch * Pull out cap code to base class, add village caps
This commit is contained in:
1 parent
f851b37aa0
commit
334d925972
45 files changed
+1967
-266
No files matched your search
@@ -135,17 +135,6 @@ public class Capability<T>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this inside ICapabilityProvider.getCapability to avoid unchecked cast warnings.
|
||||
* Example: return SOME_CAPABILITY.cast(instance);
|
||||
* Use with caution;
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <R> R cast(T instance)
|
||||
{
|
||||
return (R)instance;
|
||||
}
|
||||
|
||||
// INTERNAL
|
||||
private final String name;
|
||||
private final IStorage<T> storage;
|
||||
|
||||
@@ -20,11 +20,14 @@
|
||||
package net.minecraftforge.common.capabilities;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
import net.minecraft.nbt.INBTBase;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
@@ -41,6 +44,8 @@ import net.minecraftforge.common.util.INBTSerializable;
|
||||
* Internally the handlers are baked into arrays for fast iteration.
|
||||
* The ResourceLocations will be used for the NBT Key when serializing.
|
||||
*/
|
||||
@ParametersAreNonnullByDefault
|
||||
@MethodsReturnNonnullByDefault
|
||||
public final class CapabilityDispatcher implements INBTSerializable<NBTTagCompound>, ICapabilityProvider
|
||||
{
|
||||
private ICapabilityProvider[] caps;
|
||||
@@ -87,7 +92,7 @@ public final class CapabilityDispatcher implements INBTSerializable<NBTTagCompou
|
||||
|
||||
|
||||
@Override
|
||||
public <T> OptionalCapabilityInstance<T> getCapability(Capability<T> cap, EnumFacing side)
|
||||
public <T> OptionalCapabilityInstance<T> getCapability(Capability<T> cap, @Nullable EnumFacing side)
|
||||
{
|
||||
for (ICapabilityProvider c : caps)
|
||||
{
|
||||
@@ -122,7 +127,7 @@ public final class CapabilityDispatcher implements INBTSerializable<NBTTagCompou
|
||||
}
|
||||
}
|
||||
|
||||
public boolean areCompatible(CapabilityDispatcher other) //Called from ItemStack to compare equality.
|
||||
public boolean areCompatible(@Nullable CapabilityDispatcher other) //Called from ItemStack to compare equality.
|
||||
{ // Only compares serializeable caps.
|
||||
if (other == null) return this.writers.length == 0; // Done this way so we can do some pre-checks before doing the costly NBT serialization and compare
|
||||
if (this.writers.length == 0) return other.writers.length == 0;
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.capabilities;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.event.ForgeEventFactory;
|
||||
|
||||
@MethodsReturnNonnullByDefault
|
||||
@ParametersAreNonnullByDefault
|
||||
public abstract class CapabilityProvider implements ICapabilityProvider
|
||||
{
|
||||
private @Nullable CapabilityDispatcher capabilities;
|
||||
|
||||
protected final void gatherCapabilities() { gatherCapabilities(null); }
|
||||
|
||||
protected final void gatherCapabilities(@Nullable ICapabilityProvider parent)
|
||||
{
|
||||
this.capabilities = ForgeEventFactory.gatherCapabilities(getClass(), this, parent);
|
||||
}
|
||||
|
||||
protected final @Nullable CapabilityDispatcher getCapabilities()
|
||||
{
|
||||
return this.capabilities;
|
||||
}
|
||||
|
||||
public final boolean areCapsCompatible(CapabilityProvider other)
|
||||
{
|
||||
return areCapsCompatible(other.getCapabilities());
|
||||
}
|
||||
|
||||
public final boolean areCapsCompatible(@Nullable CapabilityDispatcher other)
|
||||
{
|
||||
final CapabilityDispatcher disp = getCapabilities();
|
||||
if (disp == null)
|
||||
{
|
||||
if (other == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return other.areCompatible(null);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return disp.areCompatible(other);
|
||||
}
|
||||
}
|
||||
|
||||
protected final @Nullable NBTTagCompound serializeCaps()
|
||||
{
|
||||
final CapabilityDispatcher disp = getCapabilities();
|
||||
if (disp != null)
|
||||
{
|
||||
return disp.serializeNBT();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected final void deserializeCaps(NBTTagCompound tag)
|
||||
{
|
||||
final CapabilityDispatcher disp = getCapabilities();
|
||||
if (disp != null)
|
||||
{
|
||||
disp.deserializeNBT(tag);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public <T> OptionalCapabilityInstance<T> getCapability(@Nonnull Capability<T> cap, @Nullable EnumFacing side)
|
||||
{
|
||||
final CapabilityDispatcher disp = getCapabilities();
|
||||
return disp == null ? OptionalCapabilityInstance.empty() : disp.getCapability(cap, side);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.capabilities;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
//Exactly like Consumer, except there IS a contract that the parameter must not be null.
|
||||
@FunctionalInterface
|
||||
public interface NonNullConsumer<T>
|
||||
{
|
||||
void accept(@Nonnull T t);
|
||||
}
|
||||
+25
-37
@@ -28,6 +28,14 @@ import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
|
||||
@ParametersAreNonnullByDefault
|
||||
@MethodsReturnNonnullByDefault
|
||||
public class OptionalCapabilityInstance<T>
|
||||
{
|
||||
private final NonNullSupplier<T> supplier;
|
||||
@@ -35,7 +43,7 @@ public class OptionalCapabilityInstance<T>
|
||||
private Set<Consumer<OptionalCapabilityInstance<T>>> listeners = new HashSet<>();
|
||||
private boolean isValid = true;
|
||||
|
||||
private static final OptionalCapabilityInstance<Void> EMPTY = new OptionalCapabilityInstance<>(null);
|
||||
private static final @Nonnull OptionalCapabilityInstance<Void> EMPTY = new OptionalCapabilityInstance<>(null);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> OptionalCapabilityInstance<T> empty()
|
||||
@@ -49,17 +57,17 @@ public class OptionalCapabilityInstance<T>
|
||||
return (OptionalCapabilityInstance<X>)this;
|
||||
}
|
||||
|
||||
private OptionalCapabilityInstance(NonNullSupplier<T> instanceSupplier)
|
||||
private OptionalCapabilityInstance(@Nullable NonNullSupplier<T> instanceSupplier)
|
||||
{
|
||||
this.supplier = instanceSupplier;
|
||||
}
|
||||
|
||||
public static <T> OptionalCapabilityInstance<T> of(final NonNullSupplier<T> instanceSupplier)
|
||||
public static <T> OptionalCapabilityInstance<T> of(final @Nullable NonNullSupplier<T> instanceSupplier)
|
||||
{
|
||||
return new OptionalCapabilityInstance<>(instanceSupplier);
|
||||
return instanceSupplier == null ? EMPTY.cast() : new OptionalCapabilityInstance<>(instanceSupplier);
|
||||
}
|
||||
|
||||
private T getValue()
|
||||
private @Nullable T getValue()
|
||||
{
|
||||
if (!isValid)
|
||||
return null;
|
||||
@@ -103,10 +111,11 @@ public class OptionalCapabilityInstance<T>
|
||||
* @throws NullPointerException if mod object is present and {@code consumer} is
|
||||
* null
|
||||
*/
|
||||
public void ifPresent(Consumer<? super T> consumer)
|
||||
public void ifPresent(NonNullConsumer<? super T> consumer)
|
||||
{
|
||||
if (isValid && getValue() != null)
|
||||
consumer.accept(getValue());
|
||||
T val = getValue();
|
||||
if (isValid && val != null)
|
||||
consumer.accept(val);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -148,30 +157,6 @@ public class OptionalCapabilityInstance<T>
|
||||
return isPresent() ? OptionalCapabilityInstance.of(()->mapper.apply(getValue())) : empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* If a value is present, apply the provided {@code Optional}-bearing
|
||||
* mapping function to it, return that result, otherwise return an empty
|
||||
* {@code Optional}. This method is similar to {@link #map(Function)},
|
||||
* but the provided mapper is one whose result is already an {@code Optional},
|
||||
* and if invoked, {@code flatMap} does not wrap it with an additional
|
||||
* {@code Optional}.
|
||||
*
|
||||
* @param <U> The type parameter to the {@code Optional} returned by
|
||||
* @param mapper a mapping function to apply to the mod object, if present
|
||||
* the mapping function
|
||||
* @return the result of applying an {@code Optional}-bearing mapping
|
||||
* function to the value of this {@code Optional}, if a value is present,
|
||||
* otherwise an empty {@code Optional}
|
||||
* @throws NullPointerException if the mapping function is null or returns
|
||||
* a null result
|
||||
*/
|
||||
public<U> OptionalCapabilityInstance<U> flatMap(Function<? super T, Optional<U>> mapper)
|
||||
{//I am not sure this is valid, or how to handle this, it's just a copy pasta from Optional. I dont think its needed. Returning a null supplier is bad
|
||||
Objects.requireNonNull(mapper);
|
||||
final U value = map(mapper).orElse(Optional.empty()).orElse(null); // To keep the non-null contract we have to evaluate right now. Should we allow this function at all?
|
||||
return value != null ? OptionalCapabilityInstance.of(() -> value) : OptionalCapabilityInstance.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the mod object if present, otherwise return {@code other}.
|
||||
*
|
||||
@@ -181,7 +166,8 @@ public class OptionalCapabilityInstance<T>
|
||||
*/
|
||||
public T orElse(T other)
|
||||
{
|
||||
return getValue() != null ? getValue() : other;
|
||||
T val = getValue();
|
||||
return val != null ? val : other;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,9 +180,10 @@ public class OptionalCapabilityInstance<T>
|
||||
* @throws NullPointerException if mod object is not present and {@code other} is
|
||||
* null
|
||||
*/
|
||||
public T orElseGet(Supplier<? extends T> other)
|
||||
public T orElseGet(NonNullSupplier<? extends T> other)
|
||||
{
|
||||
return getValue() != null ? getValue() : other.get();
|
||||
T val = getValue();
|
||||
return val != null ? val : other.get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -217,8 +204,9 @@ public class OptionalCapabilityInstance<T>
|
||||
*/
|
||||
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X
|
||||
{
|
||||
if (getValue() != null)
|
||||
return getValue();
|
||||
T val = getValue();
|
||||
if (val != null)
|
||||
return val;
|
||||
throw exceptionSupplier.get();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user