Package net.neoforged.neoforge.coremods
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.-
Nested Class Summary
Nested classes/interfaces inherited from interface cpw.mods.modlauncher.api.ITransformer
ITransformer.Target<T>
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final String
private final String
private static final org.slf4j.Logger
private final String
private final Set
<ITransformer.Target<org.objectweb.asm.tree.MethodNode>> Fields inherited from interface cpw.mods.modlauncher.api.ITransformer
DEFAULT_LABEL
-
Constructor Summary
ConstructorsConstructorDescriptionReplaceFieldComparisonWithInstanceOf
(String fieldOwner, String fieldName, String replacementClassName, List<ITransformer.Target<org.objectweb.asm.tree.MethodNode>> methodsToScan) -
Method Summary
Modifier and TypeMethodDescriptioncastVote
(ITransformerVotingContext context) Return theTransformerVoteResult
for this transformer.TargetType
<org.objectweb.asm.tree.MethodNode> Set
<ITransformer.Target<org.objectweb.asm.tree.MethodNode>> targets()
Return a set ofITransformer.Target
identifying which elements this transformer wishes to try and apply to.org.objectweb.asm.tree.MethodNode
transform
(org.objectweb.asm.tree.MethodNode methodNode, ITransformerVotingContext votingContext) Transform the input to the ITransformer's desire.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface cpw.mods.modlauncher.api.ITransformer
labels
-
Field Details
-
LOG
private static final org.slf4j.Logger LOG -
targets
-
fieldOwner
-
fieldName
-
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 ownsfieldName
fieldName
- The name of a field infieldOwner
replacementClassName
- Reference comparisons againstfieldName
infieldOwner
are replaced by instanceof checks against this class.methodsToScan
- The methods to scan
-
-
Method Details
-
getTargetType
- Specified by:
getTargetType
in interfaceITransformer<org.objectweb.asm.tree.MethodNode>
-
targets
Description copied from interface:ITransformer
Return a set ofITransformer.Target
identifying which elements this transformer wishes to try and apply to. TheITransformer.getTargetType()
must match the T variable for the transformer as documented inTargetType
, other combinations will be rejected.- Specified by:
targets
in interfaceITransformer<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 interfaceITransformer<org.objectweb.asm.tree.MethodNode>
- Parameters:
methodNode
- The ASM input node, which can be mutated directlyvotingContext
- 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 theTransformerVoteResult
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:- If the transformer wishes to be a candidate, it should return
TransformerVoteResult.YES
. - If the transformer wishes to exit the voting (the transformer has already
has its intended change applied, for example), it should return
TransformerVoteResult.NO
- If the transformer wishes to wait for future rounds of voting it should return
TransformerVoteResult.DEFER
. Note that if there is no YES candidate, but DEFER candidates remain, this is a DEFERRAL stalemate and the game will crash. - If the transformer wishes to crash the game, it should return
TransformerVoteResult.REJECT
. This is extremely frowned upon, and should not be used except in extreme circumstances. If an incompatibility is present, it should detect and handle it in theITransformationService.onLoad(cpw.mods.modlauncher.api.IEnvironment, java.util.Set<java.lang.String>)
ITransformer.transform(Object, ITransformerVotingContext)
method called. It is then removed from the set of transformers and another round is performed.- Specified by:
castVote
in interfaceITransformer<org.objectweb.asm.tree.MethodNode>
- Parameters:
context
- The context of the vote- Returns:
- A TransformerVoteResult indicating the desire of this transformer
- If the transformer wishes to be a candidate, it should return
-