Ramona
Terrarian
Right now, there is no standard way to do this.Is it possible to make an item Channel on right click, but not channel on left click? And/or vice versa?
Player.channel will always be set to false in Player.ItemCheck_Inner() if Player.controlUseItem is false. Though, ModItem.HoldItem() is called after this is done, and ModItem.CanUseItem() is called before an item is used.Though, if you are willing, you can easily add the ability to modify the condition of which the player is allowed to channel under.
In
Player.ItemCheck_Inner(), this is what decides whether or not the player is allowed to continue channeling:
C#:
bool allowChannel = this.controlUseItem;
if(this.mount.Active && this.mount.Type == MountID.Drill)
{
allowChannel = this.controlUseItem || this.controlUseTile;
}
if(allowChanneling)
this.channel = false;
allowChannel to whatever we want!
C#:
bool allowChannel = this.controlUseItem;
MyLoadingClass.ModifyAllowChannel(this, currentHeldItem, ref allowChannel); // We "insert" in this statement!
if(this.mount.Active && this.mount.Type == MountID.Drill)
{
allowChannel = this.controlUseItem || this.controlUseTile;
}
if(allowChanneling)
this.channel = false;
The way we would make this modification would be like this:
C#:
public override void Load()
{
IL_Player.ItemCheck_Inner += IL_Player_ItemCheck_Inner;
}
public static void IL_Player_ItemCheck_Inner(ILContext il)
{
ILCursor cur = new ILCursor(il);
// moves to after the "bool allowChannel = this.controlUseItem" statement
cur.GotoNext(MoveType.After,
// Matches to "this"
i => i.MatchLdarg0(),
// Matches to ".controlUseItem"
i => i.MatchLdfld<Player>("controlUseItem")
// Matches to "allowChannel = "
i => i.MatchStloc(6)
);
// Pushes "this"
cur.EmitLdarg0();
// Pushes "currentHeldItem" (an already existing local variable in
// Player.ItemCheck_Inner() that stores the palyer's held item)
cur.EmitLdloc1();
// Pushes "ref allowChannel"
cur.EmitLdloca(6);
// Calls MyLoadingClass.ModifyAllowChannel() with the last 3 things pushed/on_the_stack as the arguments
cur.EmitCall(typeof(MyLoadingClass).GetMethod("ModifyAllowChannel"));
}
public static void ModifyAllowChannel(PLayer player, Item heldItem, ref bool allowChannel)
{
// add your own conditions for channeling here!
}
In your case where you want enable or disable channeling for primary and secondary fire, you may have a
ModifyAllowChannel() that looks like this:
C#:
public static void ModifyAllowChannel(PLayer player, Item heldItem, ref bool allowChannel)
{
if(heldItem.type == ModContent.ItemType<MyChannelItem>())
{
// Disable channeling for left click:
if(player.altFunctionUse == 0)
allowChannel = false;
// Enable channeling for right click:
if(player.altFunctionUse != 0)
allowChannel = player.controlUseTile;
}
}
Something to note is that
player.controlUseItem keeps track if the player is left clicking, and player.controlUseTile keep track of right clicking. This means that whatever the MountID.Drill is, it's an example of channeling with right click. Also note that, even without this modification, it is still to possible to channel with right click if the player begins holding left click at the perfect moment!
Last edited: