Jump to content

Mod Not Loading After Adding CS File To Solution


neogoo123

Recommended Posts

Hello! I'm fairly new to the Vintage Story scene, and its modding in general, but have had some good successes. After getting used to everything and starting to add stuff that I actually wanted modded in, I started work on another part which has to do with modifying the scythe code. This would be the first coding work I'd done in it, so I figured I'd start out by making my own version of the scythe and then replace the vanilla one when I was done. I went about this by essentially just copying the ItemScythe code from the survival github to a new CS file called ItemScytheModded. This is where I intended to play with the tool. After adding the file and adding the code from the ItemScythe file, trying to run the game through Visual studio leads to it not loading my mod. It had been working before adding the code, and seems to load the mod when I remove most of the ItemScythe code. Note that I've tried both having the item registered and not, and both ways the mod still won't load. I've gone about trying to remove all json that included the file (as at first I figured I'd try just patching the scythe to use my class rather than the vanilla), as well as making an entirely different item in my assets folder, but regardless the game won't even load the mod to see those json files. Am I misunderstanding something here or is this weird?

Here's the ItemScytheModded code, so you can see it's the same as in the original file:

Spoiler




using System;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
using Vintagestory.GameContent;

namespace VSMods.src.items
{
    public class ItemScytheModded : ItemShears
    {
        string[] allowedPrefixes;
        string[] disallowedSuffixes;

        public override int MultiBreakQuantity { get { return 5; } }

        public override void OnLoaded(ICoreAPI api)
        {
            base.OnLoaded(api);
            allowedPrefixes = Attributes["codePrefixes"].AsArray<string>();
            disallowedSuffixes = Attributes["disallowedSuffixes"].AsArray<string>();
        }

        public override bool CanMultiBreak(Block block)
        {
            for (int i = 0; i < allowedPrefixes.Length; i++)
            {
                if (block.Code.Path.StartsWith(allowedPrefixes[i]))
                {
                    // Disable scything on thick snow variants (-snow2, -snow3 etc.)
                    if (disallowedSuffixes != null)
                    {
                        for (int j = 0; j < disallowedSuffixes.Length; j++)
                        {
                            if (block.Code.Path.EndsWith(disallowedSuffixes[j])) return false;
                        }
                    }

                    return true;
                }
            }

            return false;
        }

        public override void OnHeldAttackStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, ref EnumHandHandling handling)
        {
            base.OnHeldAttackStart(slot, byEntity, blockSel, entitySel, ref handling);
            if (blockSel == null) return;

            IPlayer byPlayer = (byEntity as EntityPlayer)?.Player;
            if (!byEntity.World.Claims.TryAccess(byPlayer, blockSel.Position, EnumBlockAccessFlags.BuildOrBreak))
            {
                return;
            }

            if (CanMultiBreak(api.World.BlockAccessor.GetBlock(blockSel.Position)))
            {
                handling = EnumHandHandling.PreventDefault;
                byEntity.Attributes.SetBool("didBreakBlocks", false);
                byEntity.Attributes.SetBool("didPlaySound", false);
            }
            else
            {
                handling = EnumHandHandling.PreventDefault;
                byEntity.Attributes.SetBool("didBreakBlocks", false);
            }
        }

        public override bool OnHeldAttackStep(float secondsPassed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSelection, EntitySelection entitySel)
        {
            if (blockSelection == null) return false;
            if (!CanMultiBreak(api.World.BlockAccessor.GetBlock(blockSelection.Position)) && byEntity.Attributes.GetBool("didBreakBlocks") == false) return false;

            if (byEntity.World.Side == EnumAppSide.Client)
            {
                ModelTransform tf = new ModelTransform();
                tf.EnsureDefaultValues();

                float t = secondsPassed / 1.35f;

                float f = (float)Easings.EaseOutBack(Math.Min(t * 2f, 1));
                float f2 = (float)Math.Sin(GameMath.Clamp(Math.PI * 1.4f * (t - 0.5f), 0, 3));

                tf.Translation.X += Math.Min(0.2f, t * 3);
                tf.Translation.Y -= Math.Min(0.75f, t * 3);
                tf.Translation.Z -= Math.Min(1, t * 3);
                tf.ScaleXYZ += Math.Min(1, t * 3);
                tf.Origin.X -= Math.Min(0.75f, t * 3);
                tf.Rotation.X = -Math.Min(30, t * 30) + f * 30 + (float)f2 * 120f;
                tf.Rotation.Z = -f * 110;

                if (secondsPassed > 1.75f)
                {
                    float b = 2 * (secondsPassed - 1.75f);
                    tf.Rotation.Z += b * 140;
                    tf.Rotation.X /= (1 + b * 10);
                    tf.Translation.X -= b * 0.4f;
                    tf.Translation.Y += b * 2 / 0.75f;
                    tf.Translation.Z += b * 2;
                    //tf.Origin.X += b * 2 / 0.75f;
                    //tf.ScaleXYZ -= b * 2;
                }

                byEntity.Controls.UsingHeldItemTransformBefore = tf;
            }

            if (secondsPassed > 0.75f && byEntity.Attributes.GetBool("didPlaySound") == false)
            {
                api.World.PlaySoundAt(new AssetLocation("sounds/tool/scythe1"), byEntity, (byEntity as EntityPlayer)?.Player, true, 16);
                byEntity.Attributes.SetBool("didPlaySound", true);
            }

            if (secondsPassed > 1.45f && byEntity.Attributes.GetBool("didBreakBlocks") == false)
            {
                if (byEntity.World.Side == EnumAppSide.Server)
                {
                    IPlayer byPlayer = (byEntity as EntityPlayer)?.Player;
                    if (!byEntity.World.Claims.TryAccess(byPlayer, blockSelection.Position, EnumBlockAccessFlags.BuildOrBreak))
                    {
                        return false;
                    }

                    OnBlockBrokenWith(byEntity.World, byEntity, slot, blockSelection);
                }
                byEntity.Attributes.SetBool("didBreakBlocks", true);
            }

            // Crappy fix to make harvesting not buggy T_T
            if (api.Side == EnumAppSide.Server) return true;

            return secondsPassed < 2f;
        }

        public override void OnHeldAttackStop(float secondsPassed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSelection, EntitySelection entitySel)
        {

        }
    }
}

 

The odd thing about this to me is that even with the file not being used in any json and not being called anywhere, it just stops the game from loading the mod, but does load the mod if I remove parts of the code, specifically all of the overrides.

Edited by William Mann
Solved my issue, for the most part.
Link to comment
Share on other sites

Update: So by removing only the OnHeldAttackStep() and OnHeldAttackStart() functions, the mod actually loads, so it has to do something with that. I'll continue debugging why this stops the mod loading process, but that this point I'm still quite confused.

Link to comment
Share on other sites

Thanks for the reply, Quentin!

Update: I seem to have narrowed down the issue to specifically lines 59, 118, and 126, which are the places where there are "(byEntity as EntityPlayer)?.Player;"s. I happened to look into the log files (I had been, but didn't really notice anything weird in the client and server debug logs) and found that in server-event file there was this:

Spoiler


15.3.2021 16:12:08 [Error] [mymod] Compiler errors during compilation:
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(59): Invalid expression term '.' [CS1525]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(59): Syntax error, ':' expected [CS1003]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(118): Invalid expression term '.' [CS1525]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(118): Syntax error, ':' expected [CS1003]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(118): ; expected [CS1002]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(118): Invalid expression term ',' [CS1525]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(118): ; expected [CS1002]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(118): ; expected [CS1002]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(118): Invalid expression term ',' [CS1525]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(118): ; expected [CS1002]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(118): ; expected [CS1002]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(118): Invalid expression term ')' [CS1525]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(126): Invalid expression term '.' [CS1525]
15.3.2021 16:12:08 [Error] [mymod]     src\items\ItemScytheModded.cs(126): Syntax error, ':' expected [CS1003]

 

 By removing the question marks (I'd never seen question marks used like this myself so I figured that might have been it) I successfully got it to load the mod albeit with other issues but hey at least I'm actually loading the mod.

 

Link to comment
Share on other sites

Alright, so, I think I've fixed my issue but I'm left with some questions still. I'm going to put this as solved for now incase others who are in my situation come across this. What I had to do was replace all of the sections in the code that used "(byPlayer as EntityPlayer)?.Player;" with 

Spoiler



EntityPlayer e = byEntity as EntityPlayer;

IPlayer byPlayer = null;

if(e != null)
{
	byPlayer = e.Player;
}

 

This has fixed my issue, and I'm now able to modify the class at my leisure. My main issue here is if that operator, the "?", in this specific context, is part of a different CS version and I'm just not up-to-date? I've only ever seen the question mark operator used as a ternary operator. Also I'm going to change the bubble to "Solved!".

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.