tModLoader Making a Tile drop items when interacted with. [SOLVED]

Sylvette

Steampunker
I am trying to create a tile that dispenses an item when interacted with (right click), however, I can't seen to get it to work. Here is the code used:


C#:
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ObjectData;

namespace SylvetteQOL.Tiles
{
    public class CandyBowl : ModTile
    {
        public override void SetStaticDefaults() {
            // Properties
            Main.tileTable[Type] = false;
            Main.tileSolidTop[Type] = false;
            Main.tileNoAttach[Type] = true;
            Main.tileLavaDeath[Type] = true;
            TileID.Sets.HasOutlines[Type] = false;
            Main.tileFrameImportant[Type] = true;
            TileID.Sets.DisableSmartCursor[Type] = true;

            AdjTiles = new int[] { TileID.PiggyBank };

            // Placement
            TileObjectData.newTile.CopyFrom(TileObjectData.Style2x1);
            TileObjectData.newTile.StyleHorizontal = true;
            TileObjectData.newTile.CoordinateHeights = new[] { 18 };
            TileObjectData.addTile(Type);

            // Etc
            ModTranslation name = CreateMapEntryName();
            name.SetDefault("Candy Bowl");
            AddMapEntry(new Color(120, 120, 120), name);
        }

        public override void KillMultiTile(int x, int y, int frameX, int frameY) {
            Item.NewItem(new EntitySource_TileBreak(x, y), x * 16, y * 16, 32, 16, ModContent.ItemType<Items.BowlofCandy>());
        }

        public override void MouseOver(int i, int j) {
            Player player = Main.LocalPlayer;
            player.noThrow = 2;
            player.cursorItemIconEnabled = true;
            player.cursorItemIconID = ModContent.ItemType<Items.BowlofCandy>();
        }

        public override bool RightClick(int i, int j) {
            Player player = Main.LocalPlayer;
            //var entitySource = NPC.GetSource_GiftOrReward();
            if (!Main.LocalPlayer.HasBuff(ModContent.BuffType<Buffs.NoMoreBuff>())){
                int CandyColor = Main.rand.Next(6);
                if (CandyColor == 0) {
                    player.QuickSpawnItem(ModContent.ItemType<Items.Candies.RedCandy>()); //The error occurs here
                }
                else if (CandyColor == 1) {
                    player.QuickSpawnItem(ModContent.ItemType<Items.Candies.BlueCandy>()); //And here
                }
                else if (CandyColor == 2) {
                    player.QuickSpawnItem(ModContent.ItemType<Items.Candies.PurpleCandy>()); //And here
                }
                else if (CandyColor == 3) {
                    player.QuickSpawnItem(ModContent.ItemType<Items.Candies.GreenCandy>()); //Here too
                }
                else if (CandyColor == 4) {
                    player.QuickSpawnItem(ModContent.ItemType<Items.Candies.YellowCandy>()); //Yeah
                }
                return true;
            }
            else
            return false;
        }
    }
}


I should also mention, I tried this other method before and it didn't work, it said "an object reference is required for the non-static field, method, or property 'Entity.GetSource_GiftOrReward(string?)'":


C#:
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ObjectData;

namespace SylvetteQOL.Tiles
{
    public class CandyBowl : ModTile
    {
        public override void SetStaticDefaults() {
            // Properties
            Main.tileTable[Type] = false;
            Main.tileSolidTop[Type] = false;
            Main.tileNoAttach[Type] = true;
            Main.tileLavaDeath[Type] = true;
            TileID.Sets.HasOutlines[Type] = false;
            Main.tileFrameImportant[Type] = true;
            TileID.Sets.DisableSmartCursor[Type] = true;

            AdjTiles = new int[] { TileID.PiggyBank };

            // Placement
            TileObjectData.newTile.CopyFrom(TileObjectData.Style2x1);
            TileObjectData.newTile.StyleHorizontal = true;
            TileObjectData.newTile.CoordinateHeights = new[] { 18 };
            TileObjectData.addTile(Type);

            // Etc
            ModTranslation name = CreateMapEntryName();
            name.SetDefault("Candy Bowl");
            AddMapEntry(new Color(120, 120, 120), name);
        }

        public override void KillMultiTile(int x, int y, int frameX, int frameY) {
            Item.NewItem(new EntitySource_TileBreak(x, y), x * 16, y * 16, 32, 16, ModContent.ItemType<Items.BowlofCandy>());
        }

        public override void MouseOver(int i, int j) {
            Player player = Main.LocalPlayer;
            player.noThrow = 2;
            player.cursorItemIconEnabled = true;
            player.cursorItemIconID = ModContent.ItemType<Items.BowlofCandy>();
        }

        public override bool RightClick(int i, int j) {
            Player player = Main.LocalPlayer;
            var entitySource = NPC.GetSource_GiftOrReward(); //Now the error is here
            if (!Main.LocalPlayer.HasBuff(ModContent.BuffType<Buffs.NoMoreBuff>())){
                int CandyColor = Main.rand.Next(6);
                if (CandyColor == 0) {
                    player.QuickSpawnItem(entitySource, ModContent.ItemType<Items.Candies.RedCandy>());
                }
                else if (CandyColor == 1) {
                    player.QuickSpawnItem(entitySource, ModContent.ItemType<Items.Candies.BlueCandy>());
                }
                else if (CandyColor == 2) {
                    player.QuickSpawnItem(entitySource, ModContent.ItemType<Items.Candies.PurpleCandy>());
                }
                else if (CandyColor == 3) {
                    player.QuickSpawnItem(entitySource, ModContent.ItemType<Items.Candies.GreenCandy>());
                }
                else if (CandyColor == 4) {
                    player.QuickSpawnItem(entitySource, ModContent.ItemType<Items.Candies.YellowCandy>());
                }
                return true;
            }
            else
            return false;
        }
    }
}


The code I based the erroneous section on was the OnChatButtonClicked part of ExamplePerson from the ExampleMod. I'm confused how it worked there but not here.
 
Ok nvm, I found out a solution:

Instead of using player.QuickSpawnItem, I'm using Item.NewItem. Here's the code with this implemented for anyone who has this hyperspecific issue in the future:

C#:
player.QuickSpawnItem(entitySource, ModContent.ItemType<Items.Candies.RedCandy>());
replaced with
C#:
Item.NewItem(new EntitySource_TileBreak(i, j), i * 16, j * 16, 32, 16, ModContent.ItemType<Items.Candies.RedCandy>());
 
Back
Top Bottom