Class InteractionContext

java.lang.Object
com.hypixel.hytale.server.core.entity.InteractionContext

public class InteractionContext extends Object
Contains state passed between interactions in a chain
  • Method Details

    • fork

      @Nonnull public InteractionChain fork(@Nonnull InteractionContext context, @Nonnull RootInteraction rootInteraction, boolean predicted)
      Forks from the current interaction into the passed interaction ids.

      Calls fork(InteractionType, InteractionContext, RootInteraction, boolean) with the type set to match the type of the currently running chain.

      See InteractionManager for more information about how forks work.

      Parameters:
      context - The context to use for the forked interaction chain.
      rootInteraction - The interactions to run.
      Returns:
      The forked chain.
      Throws:
      IllegalArgumentException - If the context matches the current context
    • fork

      @Nonnull public InteractionChain fork(@Nonnull InteractionType type, @Nonnull InteractionContext context, @Nonnull RootInteraction rootInteraction, boolean predicted)
      Forks from the current interaction into the passed interaction ids.

      The chain will be run as if it was run by the given type. This means that input required for certain interactions (e.g. charging) will be the same as if that interaction type was run originally.

      The context used cannot be the same as the current one, a new one must be created for the fork. If the type of the chain matches the current one then simply duplicating the current context is enough.

      var chain = context.fork(context.duplicate(), forkInteractions, true);
      // Store the chain if needed
      
      // Next tick
      if (chain.getServerState() != InteractionState.NotFinished) {
          // Chain completed
      }
      

      If the type of interaction is different then creating new interactions via forInteraction(InteractionManager, Ref, InteractionType, ComponentAccessor) is recommended so that the system has the correct inventory information.

      var contexts = Interaction.forInteraction(manager, ref, InteractionType.Secondary, componentAccessor);
      var chain = context.fork(InteractionType.Secondary, contexts[0], forkInteractions, true);
      // Store the chain if needed
      
      // Next tick
      if (chain.getServerState() != InteractionState.NotFinished) {
          // Chain completed
      }
      

      See InteractionManager for more information about how forks work.

      Parameters:
      type - The interaction type that the new chain should be run as
      context - The context to use for the forked interaction chain.
      rootInteraction - The interactions to run.
      Returns:
      The forked chain.
      Throws:
      IllegalArgumentException - If the context matches the current context
    • fork

      @Nonnull public InteractionChain fork(@Nonnull InteractionChainData data, @Nonnull InteractionType type, @Nonnull InteractionContext context, @Nonnull RootInteraction rootInteraction, boolean predicted)
    • duplicate

      @Nonnull public InteractionContext duplicate()
      Creates a duplicate of this interaction context.

      This duplicate isn't a full clone and will only copy information that is useful for a forked interaction.

      Returns:
      The duplicate context.
    • getEntity

      @Nonnull public Ref<EntityStore> getEntity()
      Returns:
      the entity reference of the running entity
    • getOwningEntity

      @Nullable public Ref<EntityStore> getOwningEntity()
      Returns:
      the entity reference of the owner entity
    • execute

      public void execute(@Nonnull RootInteraction nextInteraction)
      Pushes the passed root interaction on to the stack and as the current root.

      This will cause the chain execute the passed root interaction from this point onwards. Once the root interaction completes it will return back to the current root.

      Parameters:
      nextInteraction - The root interaction to run.
    • getChain

      @Nullable public InteractionChain getChain()
      Returns:
      the interaction chain associated with this context
    • getEntry

      @Nullable public InteractionEntry getEntry()
      Returns:
      the interaction entry associated with this context
    • getOperationCounter

      public int getOperationCounter()
      Returns the current index into the operations list of the current root interaction.
      Returns:
      The index into the operations list of the current root interaction.
    • setOperationCounter

      public void setOperationCounter(int operationCounter)
      Sets the operation counter, changing the next operation that will be run.

      Generally you should be either using jump(Label) or letting the automatic incrementing set the operation counter.

      Parameters:
      operationCounter - The index to run next.
    • jump

      public void jump(@Nonnull Label label)
      Changes the operation counter to point to the given label so that the operation at that index will be run next.
      Parameters:
      label - The label to jump to.
    • getOriginalItemType

      @Nullable public Item getOriginalItemType()
      Returns the original item type that triggered the chain
    • getHeldItemSectionId

      public int getHeldItemSectionId()
    • getHeldItemContainer

      @Nullable public ItemContainer getHeldItemContainer()
    • getHeldItemSlot

      public byte getHeldItemSlot()
    • getHeldItem

      @Nullable public ItemStack getHeldItem()
    • setHeldItem

      public void setHeldItem(@Nullable ItemStack heldItem)
    • createHeldItemContext

      @Nullable public ItemContext createHeldItemContext()
      Returns:
      an item context based on the held item of this context, if any
    • getInteractionVarsGetter

      public Function<InteractionContext, Map<String,String>> getInteractionVarsGetter()
    • getInteractionVars

      public Map<String,String> getInteractionVars()
    • setInteractionVarsGetter

      public void setInteractionVarsGetter(Function<InteractionContext, Map<String,String>> interactionVarsGetter)
    • getInteractionManager

      public InteractionManager getInteractionManager()
    • getTargetEntity

      @Nullable public Ref<EntityStore> getTargetEntity()
    • getTargetBlock

      @Nullable public BlockPosition getTargetBlock()
    • getTargetBlockRaw

      @Nullable public BlockPosition getTargetBlockRaw()
    • getMetaStore

      @Nonnull public DynamicMetaStore<InteractionContext> getMetaStore()
    • getState

      @Nonnull public InteractionSyncData getState()
    • getClientState

      @Nullable public InteractionSyncData getClientState()
    • getServerState

      @Nonnull public InteractionSyncData getServerState()
    • getInstanceStore

      @Nonnull public DynamicMetaStore<Interaction> getInstanceStore()
    • allowSkipChainOnClick

      public boolean allowSkipChainOnClick()
    • setLabels

      public void setLabels(Label[] labels)
      Sets the labels that will be returned from getLabel(int) calls.
      Parameters:
      labels - The labels to use.
    • hasLabels

      public boolean hasLabels()
      Returns whether this context currently has any labels set.
      Returns:
      Whether there are any labels set.
    • getLabel

      public Label getLabel(int index)
      Returns the label at the given index.
      Parameters:
      index - The index to lookup
      Returns:
      The returned label.
    • getSnapshot

      public EntitySnapshot getSnapshot(@Nonnull Ref<EntityStore> ref, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
    • setSnapshotProvider

      public void setSnapshotProvider(@Nullable InteractionContext.SnapshotProvider snapshotProvider)
    • setTimeShift

      public void setTimeShift(float shift)
    • getCommandBuffer

      @Nullable public CommandBuffer<EntityStore> getCommandBuffer()
      Returns the current command buffer for this tick.

      Only valid for the duration of the current interaction tick.

    • getRootInteractionId

      @Nullable public String getRootInteractionId(@Nonnull InteractionType type)
      Returns the root interaction that would be run for the given interaction type.

      This will look up the interaction on the currently held item or default to unarmed interactions if no item is held.

      Parameters:
      type - The interaction type to lookup
      Returns:
      The root interaction id or null.
    • toString

      @Nonnull public String toString()
      Overrides:
      toString in class Object
    • forProxyEntity

      @Nonnull public static InteractionContext forProxyEntity(@Nonnull InteractionManager manager, @Nonnull Ref<EntityStore> entityRef, @Nonnull Ref<EntityStore> runningForEntity, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
    • forInteraction

      @Nonnull public static InteractionContext forInteraction(@Nonnull InteractionManager manager, @Nonnull Ref<EntityStore> ref, @Nonnull InteractionType type, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Returns an interaction context initialized with the correct held item for the given interaction type.
      Parameters:
      ref - The entity this context is for.
      type - The type of interaction that this context is for.
      componentAccessor - The component accessor for component access
      Returns:
      The created context.
    • forInteraction

      @Nonnull public static InteractionContext forInteraction(@Nonnull InteractionManager manager, @Nonnull Ref<EntityStore> ref, @Nonnull InteractionType type, int equipSlot, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
    • withoutEntity

      @Nonnull public static InteractionContext withoutEntity()