コード例 #1
0
        public void Attach(ClothSystem sys, ClothPoint point)
        {
            if (!entity.WatchedAttributes.HasAttribute("clothIds"))
            {
                entity.WatchedAttributes["clothIds"] = new IntArrayAttribute(new int[] { sys.ClothId });
            }

            ClothIds.AddInt(sys.ClothId);

            point.PinTo(entity, new Vec3f(0, 0.5f, 0));
        }
コード例 #2
0
        private void attachToBlock(EntityAgent byEntity, BlockPos toPosition, ClothSystem sys, ItemSlot slot)
        {
            var pEnds = sys.Ends;

            // 2 possible cases
            // - We just created a new rope: use pEnds[1] because its unattached
            // - We already have both ends attached and want to re-attach the player held end

            ClothPoint cpoint = pEnds[0].PinnedToEntity?.EntityId == byEntity.EntityId && pEnds[1].Pinned ? pEnds[0] : pEnds[1];

            cpoint.PinTo(toPosition, new Vec3f(0.5f, 0.5f, 0.5f));
        }
コード例 #3
0
        public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
        {
            ClothSystem cs = cm.GetClothSystemAttachedToBlock(blockSel.Position);

            if (cs != null)
            {
                Entity   byEntity = byPlayer.Entity;
                ItemSlot slot     = byPlayer.InventoryManager.ActiveHotbarSlot;

                Vec3d lpos     = new Vec3d(0, byEntity.LocalEyePos.Y - 0.25f, 0);
                Vec3d aheadPos = lpos.AheadCopy(0.25f, byEntity.SidedPos.Pitch, byEntity.SidedPos.Yaw);

                // Already handled by ItemRope
                if (!slot.Empty && slot.Itemstack.Collectible.Code.Path == "rope" && (cs.FirstPoint.PinnedToEntity?.EntityId == byPlayer.Entity.EntityId || cs.LastPoint.PinnedToEntity?.EntityId == byPlayer.Entity.EntityId))
                {
                    return(base.OnBlockInteractStart(world, byPlayer, blockSel, ref handling));
                }

                ClothPoint targetPoint = cs.FirstPoint.PinnedToBlockPos == blockSel.Position ? cs.FirstPoint : cs.LastPoint;

                ItemStack stack = new ItemStack(world.GetItem(new AssetLocation("rope")));
                stack.Attributes.SetInt("clothId", cs.ClothId);
                stack.Attributes.SetLong("ropeHeldByEntityId", byEntity.EntityId);

                if (byPlayer.InventoryManager.TryGiveItemstack(stack, true))
                {
                    targetPoint.PinTo(byPlayer.Entity, aheadPos.ToVec3f());
                }
                else
                {
                    Entity ei = world.SpawnItemEntity(stack, blockSel.Position.ToVec3d().Add(0.5, 0.5, 0.5));
                    targetPoint.PinTo(ei, new Vec3f(0, 0.1f, 0));
                }

                handling = EnumHandling.PreventDefault;
                return(true);
            }

            return(base.OnBlockInteractStart(world, byPlayer, blockSel, ref handling));
        }
コード例 #4
0
        public override void OnCollected(ItemStack stack, Entity entity)
        {
            int clothId = stack.Attributes.GetInt("clothId");

            if (clothId != 0)
            {
                ClothSystem sys = cm.GetClothSystem(clothId);
                if (sys != null)
                {
                    ClothPoint p = null;
                    if (sys.FirstPoint.PinnedToEntity is EntityItem)
                    {
                        p = sys.FirstPoint;
                    }
                    if (sys.LastPoint.PinnedToEntity is EntityItem)
                    {
                        p = sys.LastPoint;
                    }

                    if (p != null)
                    {
                        Vec3d lpos     = new Vec3d(0, entity.LocalEyePos.Y - 0.3f, 0);
                        Vec3d aheadPos = lpos.AheadCopy(0.1f, entity.SidedPos.Pitch, entity.SidedPos.Yaw).AheadCopy(0.4f, entity.SidedPos.Pitch, entity.SidedPos.Yaw - GameMath.PIHALF);
                        p.PinTo(entity, aheadPos.ToVec3f());

                        ItemSlot collectedSlot = null;
                        (entity as EntityPlayer).WalkInventory((slot) => {
                            if (!slot.Empty && slot.Itemstack.Attributes.GetInt("clothId") == clothId)
                            {
                                collectedSlot = slot;
                                return(false);
                            }
                            return(true);
                        });
                        collectedSlot?.Itemstack?.Attributes.SetLong("ropeHeldByEntityId", entity.EntityId);
                        collectedSlot?.MarkDirty();
                    }
                }
            }
        }
コード例 #5
0
        public override void OnGroundIdle(EntityItem entityItem)
        {
            long ropeHeldByEntityId = entityItem.Itemstack.Attributes.GetLong("ropeHeldByEntityId");

            if (ropeHeldByEntityId != 0)
            {
                entityItem.Itemstack.Attributes.RemoveAttribute("ropeHeldByEntityId");

                int clothId = entityItem.Itemstack.Attributes.GetInt("clothId");
                if (clothId != 0)
                {
                    // Console.WriteLine(api.World.Side + ", clothid {0}, dropped on ground.", clothId);

                    ClothSystem sys = cm.GetClothSystem(clothId);
                    if (sys != null)
                    {
                        ClothPoint p = null;
                        if (sys.FirstPoint.PinnedToEntity?.EntityId == ropeHeldByEntityId)
                        {
                            p = sys.FirstPoint;
                        }
                        if (sys.LastPoint.PinnedToEntity?.EntityId == ropeHeldByEntityId)
                        {
                            p = sys.LastPoint;
                        }

                        if (p != null)
                        {
                            //  Console.WriteLine(api.World.Side + ", clothid {0}, dropped on ground, now pinned to dropped item.", clothId);

                            p.PinTo(entityItem, new Vec3f(entityItem.SelectionBox.X2 / 2, entityItem.SelectionBox.Y2 / 2, entityItem.SelectionBox.Z2 / 2));
                        }
                    }
                }
            }
        }