Class ReplaceFieldComparisonWithInstanceOf

java.lang.Object
net.neoforged.neoforge.coremods.ReplaceFieldComparisonWithInstanceOf
All Implemented Interfaces:
ITransformer<org.objectweb.asm.tree.MethodNode>

public class ReplaceFieldComparisonWithInstanceOf extends Object implements ITransformer<org.objectweb.asm.tree.MethodNode>
Replaces code such as itemstack.getItem() == Items.CROSSBOW with instanceof checks such as itemstack.getItem() instanceof CrossbowItem. This transformer targets a set of methods to replace the occurrence of a single field-comparison.
  • Field Details

    • LOG

      private static final org.slf4j.Logger LOG
    • targets

      private final Set<ITransformer.Target<org.objectweb.asm.tree.MethodNode>> targets
    • fieldOwner

      private final String fieldOwner
    • fieldName

      private final String fieldName
    • replacementClassName

      private final String replacementClassName
  • Constructor Details

    • ReplaceFieldComparisonWithInstanceOf

      public ReplaceFieldComparisonWithInstanceOf(String fieldOwner, String fieldName, String replacementClassName, List<ITransformer.Target<org.objectweb.asm.tree.MethodNode>> methodsToScan)
      Parameters:
      fieldOwner - The class that owns fieldName
      fieldName - The name of a field in fieldOwner
      replacementClassName - Reference comparisons against fieldName in fieldOwner are replaced by instanceof checks against this class.
      methodsToScan - The methods to scan
  • Method Details

    • getTargetType

      public TargetType<org.objectweb.asm.tree.MethodNode> getTargetType()
      Specified by:
      getTargetType in interface ITransformer<org.objectweb.asm.tree.MethodNode>
    • targets

      public Set<ITransformer.Target<org.objectweb.asm.tree.MethodNode>> targets()
      Description copied from interface: ITransformer
      Return a set of ITransformer.Target identifying which elements this transformer wishes to try and apply to. The ITransformer.getTargetType() must match the T variable for the transformer as documented in TargetType, other combinations will be rejected.
      Specified by:
      targets in interface ITransformer<org.objectweb.asm.tree.MethodNode>
      Returns:
      The set of targets this transformer wishes to apply to
    • transform

      public org.objectweb.asm.tree.MethodNode transform(org.objectweb.asm.tree.MethodNode methodNode, ITransformerVotingContext votingContext)
      Description copied from interface: ITransformer
      Transform the input to the ITransformer's desire. The context from the last vote is provided as well.
      Specified by:
      transform in interface ITransformer<org.objectweb.asm.tree.MethodNode>
      Parameters:
      methodNode - The ASM input node, which can be mutated directly
      votingContext - The voting context
      Returns:
      An ASM node of the same type as that supplied. It will be used for subsequent rounds of voting.
    • castVote

      Description copied from interface: ITransformer
      Return the TransformerVoteResult for this transformer. The transformer should evaluate whether or not is is a candidate to apply during the round of voting in progress, represented by the context parameter. How the vote works: After all votes from candidate transformers are collected, the NOs are removed from the current set of voters, one from the set of YES voters is selected and it's ITransformer.transform(Object, ITransformerVotingContext) method called. It is then removed from the set of transformers and another round is performed.
      Specified by:
      castVote in interface ITransformer<org.objectweb.asm.tree.MethodNode>
      Parameters:
      context - The context of the vote
      Returns:
      A TransformerVoteResult indicating the desire of this transformer