Class PartConfigHelpers

java.lang.Object
org.cyclops.integrateddynamics.core.helper.PartConfigHelpers

public final class PartConfigHelpers extends Object
Helpers for copying and pasting part configurations. All methods in here are meant to be called server-side only.
  • Method Details

    • getAspects

      public static Set<IAspect> getAspects(IPartType<?,?> partType)
      Parameters:
      partType - A part type.
      Returns:
      All aspects that the given part type can hold.
    • snapshot

      public static PartConfigSnapshot snapshot(ValueDeseralizationContext valueDeseralizationContext, IPartType partType, IPartState<?> state, Set<PartConfigSection> sections)
      Take a snapshot of the configuration of the given part.
      Parameters:
      valueDeseralizationContext - A value deserialization context.
      partType - The part type.
      state - The part state.
      sections - The sections to include.
      Returns:
      The snapshot.
    • getCopiedAspects

      protected static Predicate<IAspect> getCopiedAspects(IPartState<?> state)
      A writer only ever does what its active aspect says, so the aspects that it is not writing hold configuration that does nothing, and pasting it onto another part would only confuse. Parts without an active aspect, such as readers, use all of their aspects at once.
      Parameters:
      state - A part state.
      Returns:
      Which aspects of that part are worth copying.
    • snapshotPartSettings

      protected static PartConfigSnapshot.PartSettings snapshotPartSettings(IPartType partType, IPartState<?> state)
      Parameters:
      partType - A part type.
      state - A part state.
      Returns:
      The settings of the given part that differ from the defaults of a freshly placed part.
    • filterNonDefaultProperties

      protected static IAspectProperties filterNonDefaultProperties(IAspectProperties properties, IAspect<?,?> aspect)
      Parameters:
      properties - The properties of an aspect.
      aspect - The aspect that they belong to.
      Returns:
      Only the properties whose value differs from the aspect's default.
    • apply

      public static PartConfigApplyResult apply(ValueDeseralizationContext valueDeseralizationContext, @Nullable INetwork network, PartTarget target, IPartType partType, IPartState<?> state, PartConfigSnapshot snapshot, Set<PartConfigSection> sections, net.minecraft.world.entity.player.Player player)
      Paste the given snapshot onto the given part.
      Parameters:
      valueDeseralizationContext - A value deserialization context.
      network - The network of the part, or null if it is not in a network.
      target - The part target.
      partType - The part type.
      state - The part state.
      snapshot - The snapshot to paste.
      sections - The sections to paste.
      player - The player that is pasting, used to consume and eject variable cards.
      Returns:
      The outcome.
    • applyPartSettings

      protected static void applyPartSettings(@Nullable INetwork network, PartTarget target, IPartType partType, IPartState<?> state, PartConfigSnapshot snapshot, net.minecraft.world.entity.player.Player player, PartConfigApplyResult result)
      Only the settings that the snapshot actually holds are applied, so pasting never resets a setting that was left at its default on the copied part.
    • enabled

      protected static <T> Optional<T> enabled(PartConfigSnapshot snapshot, String setting, Optional<T> value)
      Parameters:
      snapshot - A snapshot.
      setting - The name of a general part setting.
      value - The value that the snapshot holds for it.
      Returns:
      That value, or empty if the player switched the setting off.
    • applyMaxOffset

      protected static void applyMaxOffset(IPartType partType, IPartState<?> state, int maxOffset, net.minecraft.world.entity.player.Player player, PartConfigApplyResult result)
      Raise the maximum offset of the given part to the given value, by consuming offset enhancements from the player. Enhancements can not be split, so the player can end up spending a bit more value than needed.
      Parameters:
      partType - The part type.
      state - The part state.
      maxOffset - The maximum offset to raise the part to.
      player - The player that is pasting.
      result - The outcome to report into.
    • applyAspectProperties

      protected static void applyAspectProperties(ValueDeseralizationContext valueDeseralizationContext, PartTarget target, IPartType partType, IPartState<?> state, PartConfigSnapshot snapshot, PartConfigApplyResult result)
    • readProperties

      public static IAspectProperties readProperties(ValueDeseralizationContext valueDeseralizationContext, net.minecraft.nbt.CompoundTag tag)
      Parameters:
      valueDeseralizationContext - A value deserialization context.
      tag - Serialized aspect properties.
      Returns:
      The deserialized aspect properties.
    • writeProperties

      public static net.minecraft.nbt.CompoundTag writeProperties(ValueDeseralizationContext valueDeseralizationContext, IAspectProperties properties)
      Parameters:
      valueDeseralizationContext - A value deserialization context.
      properties - Aspect properties.
      Returns:
      The serialized aspect properties.
    • applyPropertiesByType

      public static int applyPropertiesByType(IAspectProperties source, IAspectProperties properties, IAspect<?,?> aspect)
      Copy all property values from the source into the target properties, for all property types that the given aspect declares. Property types are matched by their value type and translation key, so properties can also be copied between different aspects.
      Parameters:
      source - The properties to copy from.
      properties - The properties to copy into.
      aspect - The aspect that the target properties belong to.
      Returns:
      The number of copied properties.
    • applyPropertiesByType

      public static int applyPropertiesByType(IAspectProperties source, IAspectProperties properties, IAspect<?,?> aspect, Predicate<IAspectPropertyTypeInstance> filter)
      Parameters:
      source - The properties to copy from.
      properties - The properties to copy into.
      aspect - The aspect that the target properties belong to.
      filter - Which of the source properties may be copied.
      Returns:
      The number of copied properties.
    • countPropertyTypes

      protected static int countPropertyTypes(IAspectProperties properties)
    • applyVariableCards

      public static void applyVariableCards(ValueDeseralizationContext valueDeseralizationContext, PartTarget target, IPartType partType, IPartState<?> state, List<PartConfigSnapshot.VariableCard> cards, net.minecraft.world.entity.player.Player player, PartConfigApplyResult result)
      Paste the given variable cards into the given part. Only the inventories that occur in the given cards are touched: their current contents are given back to the player, and replaced by copies of the given cards. Each pasted card consumes one blank Variable Card from the player's inventory, unless the player is in creative mode.
      Parameters:
      valueDeseralizationContext - A value deserialization context.
      target - The part target.
      partType - The part type.
      state - The part state.
      cards - The cards to paste.
      player - The player that is pasting.
      result - The outcome to report into.
    • isSameVariable

      public static boolean isSameVariable(net.minecraft.world.item.ItemStack itemStack, net.minecraft.world.item.ItemStack other)
      Two variable cards that only differ in the identifier of their variable produce the same value, so replacing one by the other would change nothing about the part that holds it.
      Parameters:
      itemStack - A variable card, which can be empty.
      other - Another variable card.
      Returns:
      If both cards hold the same variable.
    • resolveInventory

      @Nullable public static org.cyclops.cyclopscore.inventory.SimpleInventory resolveInventory(IPartType partType, IPartState<?> state, String inventoryName)
      Parameters:
      partType - A part type.
      state - A part state.
      inventoryName - An inventory name from a snapshot.
      Returns:
      The matching inventory inside the given part, or null if the part does not have it.
    • saveInventory

      protected static void saveInventory(IPartType partType, IPartState<?> state, PartTarget target, String inventoryName, org.cyclops.cyclopscore.inventory.SimpleInventory inventory, net.minecraft.world.entity.player.Player player)
    • isBlankVariable

      public static boolean isBlankVariable(net.minecraft.world.item.ItemStack itemStack)
      Parameters:
      itemStack - An item stack.
      Returns:
      If the given stack is a Variable Card without any variable in it.
    • countBlankVariables

      public static int countBlankVariables(net.minecraft.world.entity.player.Player player)
      Parameters:
      player - A player.
      Returns:
      The number of blank Variable Cards in the inventory of the given player.
    • consumeBlankVariables

      public static void consumeBlankVariables(net.minecraft.world.entity.player.Player player, int count)
      Remove the given number of blank Variable Cards from the inventory of the given player.
      Parameters:
      player - A player.
      count - The number of cards to remove.
    • copyVariable

      public static net.minecraft.world.item.ItemStack copyVariable(ValueDeseralizationContext valueDeseralizationContext, net.minecraft.world.item.ItemStack itemStack)
      Copy the given variable card, so that the copy refers to a new variable.
      Parameters:
      valueDeseralizationContext - A value deserialization context.
      itemStack - A variable card.
      Returns:
      The copy.
    • giveOrDrop

      public static void giveOrDrop(net.minecraft.world.entity.player.Player player, net.minecraft.world.item.ItemStack itemStack)
      Give the given item stack to the player, or drop it on the ground if their inventory is full.
      Parameters:
      player - A player.
      itemStack - An item stack.
    • countOffsetEnhancements

      public static int countOffsetEnhancements(net.minecraft.world.entity.player.Player player)
      Parameters:
      player - A player.
      Returns:
      The total offset enhancement value in the inventory of the given player.
    • consumeOffsetEnhancements

      public static int consumeOffsetEnhancements(net.minecraft.world.entity.player.Player player, int value)
      Remove offset enhancements from the inventory of the given player, starting at the smallest ones so that as little value as possible is wasted.
      Parameters:
      player - A player.
      value - The offset enhancement value to remove.
      Returns:
      The removed value, which can be higher than requested when enhancements did not add up exactly.
    • getOffsetEnhancementValue

      public static int getOffsetEnhancementValue(net.minecraft.world.item.ItemStack itemStack)
      Parameters:
      itemStack - An item stack.
      Returns:
      The offset that the given stack enhances a part by, or zero if it is not an offset enhancement.
    • getSnapshot

      public static Optional<PartConfigSnapshot> getSnapshot(net.minecraft.core.HolderLookup.Provider provider, net.minecraft.world.item.ItemStack wrench)
      Parameters:
      provider - A holder lookup provider.
      wrench - A Wrench.
      Returns:
      The configuration snapshot inside the given Wrench, if any.
    • setSnapshot

      public static void setSnapshot(net.minecraft.core.HolderLookup.Provider provider, net.minecraft.world.item.ItemStack wrench, PartConfigSnapshot snapshot)
      Store the given configuration snapshot inside the given Wrench.
      Parameters:
      provider - A holder lookup provider.
      wrench - A Wrench.
      snapshot - A configuration snapshot.