Skip to content

Commit

Permalink
add slot modifier system, deprecate grow/shrink functions, closes #178,
Browse files Browse the repository at this point in the history
closes #179
  • Loading branch information
TheIllusiveC4 committed Nov 30, 2021
1 parent 97cfca2 commit 15ba69c
Show file tree
Hide file tree
Showing 16 changed files with 1,188 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@

package top.theillusivec4.curios.api.type.capability;

import com.google.common.collect.Multimap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.Tuple;
import top.theillusivec4.curios.api.SlotContext;
import top.theillusivec4.curios.api.type.ISlotType;
import top.theillusivec4.curios.api.type.inventory.ICurioStacksHandler;

Expand Down Expand Up @@ -69,7 +74,7 @@ default int getVisibleSlots() {
void reset();

/**
* Gets the an Optional {@link ICurioStacksHandler} associated with the given {@link ISlotType}
* Gets the Optional {@link ICurioStacksHandler} associated with the given {@link ISlotType}
* identifier or Optional.empty() if it doesn't exist.
*
* @param identifier The identifier for the {@link ISlotType}
Expand Down Expand Up @@ -98,24 +103,6 @@ default int getVisibleSlots() {
*/
void processSlots();

/**
* Adds an amount of slots to the {@link ICurioStacksHandler} of a {@link ISlotType} associated
* with the identifier.
*
* @param identifier The identifier for the {@link ISlotType}
* @param amount The number of slots to add, must be non-negative
*/
void growSlotType(String identifier, int amount);

/**
* Removes an amount of slots from the {@link ICurioStacksHandler} of a {@link ISlotType}
* associated with the identifier.
*
* @param identifier The identifier for the {@link ISlotType}
* @param amount The number of slots to remove, must be non-negative
*/
void shrinkSlotType(String identifier, int amount);

/**
* Gets the wearer/owner of this handler instance.
*
Expand All @@ -132,7 +119,7 @@ default int getVisibleSlots() {
void loseInvalidStack(ItemStack stack);

/**
* Drops all of the ItemStacks found in the invalid stacks list. Used for handling items found in
* Drops all the ItemStacks found in the invalid stacks list. Used for handling items found in
* disabling/removing slots.
*/
void handleInvalidStacks();
Expand Down Expand Up @@ -165,8 +152,101 @@ default int getVisibleSlots() {
void loadInventory(ListNBT data);

/**
* Sets the total Fotrune and Looting bonuses of this handler and therefore it's bearer.
* Sets the total Fortune and Looting bonuses associated with the handler.
*
* @param fortuneAndLooting Two integers, the first one representing fortune and the second looting
*/
void setEnchantmentBonuses(Tuple<Integer, Integer> fortuneAndLooting);

/**
* Retrieves a set containing the {@link ICurioStacksHandler} that require its slot modifiers be
* synced to tracking clients.
*
* @return A set of {@link ICurioStacksHandler} that need to be synced to tracking clients
*/
Set<ICurioStacksHandler> getUpdatingInventories();

/**
* Adds the specified slot modifiers to the handler as temporary slot modifiers.
* <br>
* These slot modifiers are not serialized and disappear upon deserialization.
*
* @param modifiers A {@link Multimap} with slot identifiers as keys and attribute modifiers as values
*/
void addTransientSlotModifiers(Multimap<String, AttributeModifier> modifiers);

/**
* Adds the specified slot modifiers to the handler as permanent slot modifiers.
*
* @param modifiers A {@link Multimap} with slot identifiers as keys and attribute modifiers as values
*/
void addPermanentSlotModifiers(Multimap<String, AttributeModifier> modifiers);

/**
* Removes the specified slot modifiers from the handler.
*
* @param modifiers A {@link Multimap} with slot identifiers as keys and attribute modifiers as values
*/
void removeSlotModifiers(Multimap<String, AttributeModifier> modifiers);

/**
* Removes all the slot modifiers from the handler.
*/
void clearSlotModifiers();

/**
* Retrieves all the slot modifiers from the handler.
*
* @return A {@link Multimap} with slot identifiers as keys and attribute modifiers as values
*/
Multimap<String, AttributeModifier> getModifiers();

/**
* Serializes the handler to NBT.
*
* @return Data for the handler represented as a {@link CompoundNBT}
*/
CompoundNBT writeNBT();

/**
* Deserializes the handler from NBT.
*
* @param compoundNBT Data for the handler represented as a {@link CompoundNBT}
*/
void readNBT(CompoundNBT compoundNBT);

/**
* Removes the cached modifiers that appear upon deserialization of the handler.
* <br>
* Primarily for internal use, used as a workaround to avoid calculating slot stacks before slot
* modifiers are initially applied.
*/
void clearCachedSlotModifiers();

// ============ DEPRECATED ================

/**
* @param identifier The identifier for the {@link ISlotType}
* @param amount The number of slots to add, must be non-negative
* @deprecated Add a slot modifier instead using {@link top.theillusivec4.curios.api.type.util.ICuriosHelper#addSlotModifier(Multimap, String, UUID, double, AttributeModifier.Operation)}
* when overriding {@link ICurio#getAttributeModifiers(SlotContext, UUID)}
* <br>
* Adds an amount of slots to the {@link ICurioStacksHandler} of a {@link ISlotType} associated
* with the identifier.
*/
@Deprecated
void growSlotType(String identifier, int amount);

/**
* @param identifier The identifier for the {@link ISlotType}
* @param amount The number of slots to remove, must be non-negative
* @deprecated Add a slot modifier instead using {@link top.theillusivec4.curios.api.type.util.ICuriosHelper#addSlotModifier(Multimap, String, UUID, double, AttributeModifier.Operation)}
* when overriding {@link ICurio#getAttributeModifiers(SlotContext, UUID)}
* <p>
* Removes an amount of slots from the {@link ICurioStacksHandler} of a {@link ISlotType}
* associated with the identifier.
*/
@Deprecated
void shrinkSlotType(String identifier, int amount);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@

package top.theillusivec4.curios.api.type.inventory;

import com.google.common.collect.Multimap;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.NonNullList;
import top.theillusivec4.curios.api.SlotContext;

public interface ICurioStacksHandler {

Expand Down Expand Up @@ -59,14 +66,6 @@ public interface ICurioStacksHandler {
*/
int getSlots();

/**
* Gets the size offset for this instance. This value is used to persist size changes for this
* handler even when the underlying size changes.
*
* @return The number of the size offset for this instance
*/
int getSizeShift();

/**
* Gets whether or not this stack handler should be visible. This does not lock the stack handler
* from being used regardless.
Expand All @@ -84,31 +83,146 @@ public interface ICurioStacksHandler {
boolean hasCosmetic();

/**
* Increases the number of slots by the given amount.
* Writes the data for this handler.
*
* @param amount The number of slots to add to the handler
* @return A {@link CompoundNBT} representing the serialized data
*/
void grow(int amount);
CompoundNBT serializeNBT();

/**
* Decreases the number of slots by the given amount. This should not decrease the final number of
* slots below 1.
* Reads the data into this handler.
*
* @param amount The number of slots to remove from the handler
* @param nbt A {@link CompoundNBT} representing the serialized data
*/
void shrink(int amount);
void deserializeNBT(CompoundNBT nbt);

/**
* Writes the data for this handler.
* Retrieves the slot identifier associated with the handler.
*
* @return A {@link CompoundNBT} representing the serialized data
* @return The slot identifier
*/
CompoundNBT serializeNBT();
String getIdentifier();

/**
* Reads the data into this handler.
* Retrieves all the slot modifiers on the handler.
*
* @param nbt A {@link CompoundNBT} representing the serialized data
* @return A map of modifiers with the UUID as keys and {@link AttributeModifier} as values
*/
void deserializeNBT(CompoundNBT nbt);
Map<UUID, AttributeModifier> getModifiers();

/**
* Retrieves all the permanent slot modifiers on the handler.
* <br>
* These slot modifiers are serialized on the handler.
*
* @return A set of {@link AttributeModifier}
*/
Set<AttributeModifier> getPermanentModifiers();

/**
* Retrieves all the slot modifiers for a given operation on the handler.
*
* @param operation The operation of the modifiers
* @return A collection of {@link AttributeModifier}
*/
Collection<AttributeModifier> getModifiersByOperation(AttributeModifier.Operation operation);

/**
* Adds a temporary slot modifier to the handler.
* <br>
* These slot modifiers are not serialized on the handler.
*
* @param modifier The {@link AttributeModifier} instance to add
*/
void addTransientModifier(AttributeModifier modifier);

/**
* Adds a permanent slot modifier to the handler.
* <br>
* These slot modifiers are serialized on the handler.
*
* @param modifier The {@link AttributeModifier} instance to add
*/
void addPermanentModifier(AttributeModifier modifier);

/**
* Removes a slot modifier from the handler.
*
* @param uuid The UUID of the modifier to remove
*/
void removeModifier(UUID uuid);

/**
* Removes all the slot modifiers on the handler.
*/
void clearModifiers();

/**
* Removes the cached modifiers that appear upon deserialization of the handler.
* <br>
* Primarily for internal use, used as a workaround to avoid calculating slot stacks before slot
* modifiers are initially applied.
*/
void clearCachedModifiers();

/**
* Copies all the slot modifiers from another instance to this one.
*
* @param other The other instance
*/
void copyModifiers(ICurioStacksHandler other);

/**
* Recalculates the slot modifiers and resizes the handler.
*/
void update();

/**
* Retrieves the NBT data to sync to clients.
*
* @return The data represented as a {@link CompoundNBT}
*/
CompoundNBT getSyncTag();

/**
* Applies the NBT data synced to clients.
* <br>
* Client-side only.
*
* @param tag The data represented as a {@link CompoundNBT}
*/
void applySyncTag(CompoundNBT tag);

// ============ DEPRECATED ================

/**
* @return The number of the size offset for this instance
* @deprecated Use the new attribute modifier system through {@link ICurioStacksHandler#getModifiers()}
* <br>
* Gets the size offset for this instance. This value is used to persist size changes for this
* handler even when the underlying size changes.
*/
@Deprecated
int getSizeShift();

/**
* @param amount The number of slots to add to the handler
* @deprecated Add a slot modifier instead using {@link top.theillusivec4.curios.api.type.util.ICuriosHelper#addSlotModifier(Multimap, String, UUID, double, AttributeModifier.Operation)}
* when overriding {@link top.theillusivec4.curios.api.type.capability.ICurio#getAttributeModifiers(SlotContext, UUID)}
* <p>
* Increases the number of slots by the given amount.
*/
@Deprecated
void grow(int amount);

/**
* @param amount The number of slots to remove from the handler
* @deprecated Add a slot modifier instead using {@link top.theillusivec4.curios.api.type.util.ICuriosHelper#addSlotModifier(Multimap, String, UUID, double, AttributeModifier.Operation)}
* when overriding {@link top.theillusivec4.curios.api.type.capability.ICurio#getAttributeModifiers(SlotContext, UUID)}
* <p>
* Decreases the number of slots by the given amount. This should not decrease the final number of
* slots below 1.
*/
@Deprecated
void shrink(int amount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ Optional<ImmutableTriple<String, Integer, ItemStack>> findEquippedCurio(
Multimap<Attribute, AttributeModifier> getAttributeModifiers(SlotContext slotContext, UUID uuid,
ItemStack stack);

/**
* Adds a slot modifier to a specified attribute map.
*
* @param map A {@link Multimap} of attributes to attribute modifiers
* @param identifier The identifier of the slot to add the modifier onto
* @param uuid A UUID associated wth the slot
* @param amount The amount of the modifier
* @param operation The operation of the modifier
*/
void addSlotModifier(Multimap<Attribute, AttributeModifier> map, String identifier, UUID uuid,
double amount, AttributeModifier.Operation operation);

/**
* Checks if the ItemStack is valid for a particular stack and slot context.
*
Expand Down
Loading

0 comments on commit 15ba69c

Please sign in to comment.