Example #1
0
        public bool OnBlockInteract(IPlayer byPlayer)
        {
            JsonObject obj = byPlayer.InventoryManager.ActiveHotbarSlot.Itemstack?.Collectible?.Attributes?["fertilizerProps"];

            if (obj == null || !obj.Exists)
            {
                return(false);
            }
            FertilizerProps props = obj.AsObject <FertilizerProps>();

            if (props == null)
            {
                return(false);
            }

            slowReleaseNutrients[0] = Math.Min(150, slowReleaseNutrients[0] + props.N);
            slowReleaseNutrients[1] = Math.Min(150, slowReleaseNutrients[1] + props.P);
            slowReleaseNutrients[2] = Math.Min(150, slowReleaseNutrients[2] + props.K);

            byPlayer.InventoryManager.ActiveHotbarSlot.TakeOut(1);
            byPlayer.InventoryManager.ActiveHotbarSlot.MarkDirty();

            (byPlayer as IClientPlayer)?.TriggerFpAnimation(EnumHandInteract.HeldItemInteract);
            Api.World.PlaySoundAt(Api.World.BlockAccessor.GetBlock(base.Pos).Sounds.Hit, base.Pos.X + 0.5, base.Pos.Y + 0.75, base.Pos.Z + 0.5, byPlayer, true, 12);

            MarkDirty(false);
            return(true);
        }
 public void AsObject()
 {
     IJsonValue value = null;
     Assert.IsNull(value.AsObject());
     Assert.IsNull(JsonExtensions.Null().AsObject());
     Assert.IsNull(JsonValue.CreateStringValue("asdfafd").AsObject());
     Assert.IsNull(JsonValue.CreateNumberValue(2.0).AsObject());
     Assert.IsNull(JsonValue.CreateBooleanValue(true).AsObject());
     Assert.IsNull(new JsonArray().AsObject());
     JsonObject obj = new JsonObject();
     value = obj;
     Assert.AreEqual(obj, obj.AsObject());
     Assert.AreEqual(obj, value);
 }
        public void ShouldParseEmptyObjectSucceed(
            string jsonFragment
            )
        {
            IJsonElement parser = new JsonObject();
            string       jsonRemainder;

            Assert.IsTrue(
                parser.Parse(
                    jsonFragment,
                    out jsonRemainder
                    )
                );
            Assert.IsNotNull(parser.AsObject());
        }
        public void ShouldParseObjectWithCorrectSyntaxWithNestedElementsSucceed(
            string jsonFragment
            )
        {
            IJsonElement parser = new JsonObject();
            string       jsonRemainder;

            Assert.IsTrue(
                parser.Parse(
                    jsonFragment,
                    out jsonRemainder
                    )
                );
            Assert.IsNotNull(parser.AsObject());
        }
Example #5
0
        public static ITexPositionSource getContentTexture(ICoreClientAPI capi, ItemStack stack, out float fillHeight)
        {
            ITexPositionSource contentSource = null;

            fillHeight = 0;

            JsonObject obj = stack?.ItemAttributes?["inContainerTexture"];

            if (obj != null && obj.Exists)
            {
                contentSource = new ContainerTextureSource(capi, stack, obj.AsObject <CompositeTexture>());
                fillHeight    = GameMath.Min(10 / 16f, 0.7f * stack.StackSize / stack.Collectible.MaxStackSize);
            }
            else
            {
                if (stack?.Block != null && (stack.Block.DrawType == EnumDrawType.Cube || stack.Block.Shape.Base.Path.Contains("basic/cube")) && capi.BlockTextureAtlas.GetPosition(stack.Block, "up", true) != null)
                {
                    contentSource = new BlockTopTextureSource(capi, stack.Block);
                    fillHeight    = GameMath.Min(10 / 16f, 0.7f * stack.StackSize / stack.Collectible.MaxStackSize);
                }
                else if (stack != null)
                {
                    if (stack.Class == EnumItemClass.Block)
                    {
                        if (stack.Block.Textures.Count > 1)
                        {
                            return(null);
                        }

                        contentSource = new ContainerTextureSource(capi, stack, stack.Block.Textures.FirstOrDefault().Value);
                    }
                    else
                    {
                        if (stack.Item.Textures.Count > 1)
                        {
                            return(null);
                        }

                        contentSource = new ContainerTextureSource(capi, stack, stack.Item.FirstTexture);
                    }


                    fillHeight = GameMath.Min(10 / 16f, 0.7f * stack.StackSize / stack.Collectible.MaxStackSize);
                }
            }

            return(contentSource);
        }
        public void AsObject()
        {
            IJsonValue value = null;

            Assert.IsNull(value.AsObject());
            Assert.IsNull(JsonExtensions.Null().AsObject());
            Assert.IsNull(JsonValue.CreateStringValue("asdfafd").AsObject());
            Assert.IsNull(JsonValue.CreateNumberValue(2.0).AsObject());
            Assert.IsNull(JsonValue.CreateBooleanValue(true).AsObject());
            Assert.IsNull(new JsonArray().AsObject());
            JsonObject obj = new JsonObject();

            value = obj;
            Assert.AreEqual(obj, obj.AsObject());
            Assert.AreEqual(obj, value);
        }
 public static WaterTightContainableProps GetInContainerProps(ItemStack stack)
 {
     try
     {
         JsonObject obj = stack?.ItemAttributes?["waterTightContainerProps"];
         if (obj != null && obj.Exists)
         {
             return(obj.AsObject <WaterTightContainableProps>(null, stack.Collectible.Code.Domain));
         }
         return(null);
     }
     catch (Exception)
     {
         return(null);
     }
 }
        public override void OnLoaded(ICoreAPI api)
        {
            base.OnLoaded(api);

            string strdress           = Attributes["clothescategory"].AsString();
            EnumCharacterDressType dt = EnumCharacterDressType.Unknown;

            Enum.TryParse(strdress, true, out dt);
            DressType = dt;


            JsonObject jsonObj = Attributes?["footStepSound"];

            if (jsonObj?.Exists == true)
            {
                string soundloc = jsonObj.AsString(null);
                if (soundloc != null)
                {
                    AssetLocation loc = AssetLocation.Create(soundloc, Code.Domain).WithPathPrefixOnce("sounds/");

                    if (soundloc.EndsWith("*"))
                    {
                        loc.Path       = loc.Path.TrimEnd('*');
                        FootStepSounds = api.Assets.GetLocations(loc.Path, loc.Domain).ToArray();
                    }
                    else
                    {
                        FootStepSounds = new AssetLocation[] { loc };
                    }
                }
            }

            jsonObj = Attributes?["statModifiers"];
            if (jsonObj?.Exists == true)
            {
                try
                {
                    StatModifers = jsonObj.AsObject <StatModifiers>();
                }
                catch (Exception e)
                {
                    api.World.Logger.Error("Failed loading statModifiers for item/block {0}. Will ignore. Exception: {1}", Code, e);
                    StatModifers = null;
                }
            }

            ProtectionModifiers defMods = null;

            jsonObj = Attributes?["defaultProtLoss"];
            if (jsonObj?.Exists == true)
            {
                try
                {
                    defMods = jsonObj.AsObject <ProtectionModifiers>();
                }
                catch (Exception e)
                {
                    api.World.Logger.Error("Failed loading defaultProtLoss for item/block {0}. Will ignore. Exception: {1}", Code, e);
                }
            }

            jsonObj = Attributes?["protectionModifiers"];
            if (jsonObj?.Exists == true)
            {
                try
                {
                    ProtectionModifiers = jsonObj.AsObject <ProtectionModifiers>();
                }
                catch (Exception e)
                {
                    api.World.Logger.Error("Failed loading protectionModifiers for item/block {0}. Will ignore. Exception: {1}", Code, e);
                    ProtectionModifiers = null;
                }
            }


            if (ProtectionModifiers != null && ProtectionModifiers.PerTierFlatDamageReductionLoss == null)
            {
                ProtectionModifiers.PerTierFlatDamageReductionLoss = defMods?.PerTierFlatDamageReductionLoss;
            }
            if (ProtectionModifiers != null && ProtectionModifiers.PerTierRelativeProtectionLoss == null)
            {
                ProtectionModifiers.PerTierRelativeProtectionLoss = defMods?.PerTierRelativeProtectionLoss;
            }
        }
Example #9
0
        public override void Initialize(JsonObject properties)
        {
            base.Initialize(properties);

            StorageProps = properties.AsObject <GroundStorageProperties>(null, collObj.Code.Domain);
        }
Example #10
0
        protected MeshData getContentMesh(ItemStack stack, BlockPos forBlockPos, string shapefilename)
        {
            ICoreClientAPI capi = api as ICoreClientAPI;

            WaterTightContainableProps props         = GetStackProps(stack);
            ITexPositionSource         contentSource = null;


            float fillHeight = 0;

            if (props != null)
            {
                contentSource = new ContainerTextureSource(capi, stack, props.Texture);
                fillHeight    = GameMath.Min(10 / 16f, 0.2f * stack.StackSize / props.ItemsPerLitre / 16f);
            }
            else
            {
                JsonObject obj = stack?.ItemAttributes?["inContainerTexture"];
                if (obj != null && obj.Exists)
                {
                    contentSource = new ContainerTextureSource(capi, stack, obj.AsObject <CompositeTexture>());
                    fillHeight    = GameMath.Min(10 / 16f, 0.7f * stack.StackSize / stack.Collectible.MaxStackSize);
                }
                else
                {
                    if (stack?.Block != null && (stack.Block.DrawType == EnumDrawType.Cube || stack.Block.Shape.Base.Path.Contains("basic/cube")))
                    {
                        contentSource = new BlockTopTextureSource(capi, stack.Block);
                        fillHeight    = GameMath.Min(10 / 16f, 0.7f * 1);
                    }
                }
            }

            if (stack != null && contentSource != null)
            {
                Shape    shape = capi.Assets.TryGet("shapes/block/wood/barrel/" + shapefilename).ToObject <Shape>();
                MeshData contentMesh;
                capi.Tesselator.TesselateShape("barrel", shape, out contentMesh, contentSource, new Vec3f(Shape.rotateX, Shape.rotateY, Shape.rotateZ));

                contentMesh.Translate(0, fillHeight, 0);

                if (props != null && props.TintIndex > 0)
                {
                    int col = capi.ApplyColorTintOnRgba(props.TintIndex, ColorUtil.WhiteArgb, 196, 128, false);
                    if (forBlockPos != null)
                    {
                        col = capi.ApplyColorTintOnRgba(props.TintIndex, ColorUtil.WhiteArgb, forBlockPos.X, forBlockPos.Y, forBlockPos.Z, false);
                    }

                    byte[] rgba = ColorUtil.ToBGRABytes(col);

                    for (int i = 0; i < contentMesh.Rgba.Length; i++)
                    {
                        contentMesh.Rgba[i] = (byte)((contentMesh.Rgba[i] * rgba[i % 4]) / 255);
                    }
                }


                return(contentMesh);
            }

            return(null);
        }