Exemplo n.º 1
0
        //On contact with entity, if the entity is on the top and the entity is an item entity, pull the entity into the hopper's inventory.

        public override void OnEntityCollide(IWorldAccessor world, Entity entity, BlockPos pos, BlockFacing facing, Vec3d collideSpeed, bool isImpact)
        {
            base.OnEntityCollide(world, entity, pos, facing, collideSpeed, isImpact);

            // Don't suck up everything instantly
            if (world.Rand.NextDouble() < 0.9)
            {
                return;
            }

            if (facing == BlockFacing.UP && entity is EntityItem)
            {
                EntityItem  inWorldItem = (EntityItem)entity;
                BlockEntity blockEntity = world.BlockAccessor.GetBlockEntity(pos);
                if (blockEntity is BlockEntityItemFlow)
                {
                    BlockEntityItemFlow beItemFlow = (BlockEntityItemFlow)blockEntity;

                    WeightedSlot ws = beItemFlow.inventory.GetBestSuitedSlot(inWorldItem.Slot);

                    if (ws.slot != null) //we have determined there is room for this itemStack in this inventory.
                    {
                        inWorldItem.Slot.TryPutInto(api.World, ws.slot, 1);
                        if (inWorldItem.Slot.StackSize <= 0)
                        {
                            inWorldItem.Itemstack = null;
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        protected override void DoDeviceComplete()
        {
            deviceState = enDeviceState.IDLE;
            BlockPos    bp              = Pos.Copy().Offset(outputFace);
            BlockEntity checkblock      = Api.World.BlockAccessor.GetBlockEntity(bp);
            var         outputContainer = checkblock as BlockEntityContainer;

            if (Api.World is IServerWorldAccessor)
            {
                DummyInventory dummy = new DummyInventory(Api);

                List <ItemStack> outputitems = MacerationRecipe.GetMacerate(workingitem, Api, MachineName);
                foreach (ItemStack outitem in outputitems)
                {
                    if (outitem == null)
                    {
                        continue;
                    }
                    dummy[0].Itemstack = outitem;
                    //no output conatiner, spitout stuff
                    if (outputContainer != null)
                    {
                        bool stoptrying    = false;
                        int  safetycounter = 0;
                        while (!stoptrying)
                        {
                            WeightedSlot tryoutput = outputContainer.Inventory.GetBestSuitedSlot(dummy[0]);

                            if (tryoutput.slot != null)
                            {
                                ItemStackMoveOperation op = new ItemStackMoveOperation(Api.World, EnumMouseButton.Left, 0, EnumMergePriority.DirectMerge, dummy[0].StackSize);

                                dummy[0].TryPutInto(tryoutput.slot, ref op);
                                tryoutput.slot.MarkDirty();
                                if (dummy[0] == null)
                                {
                                    stoptrying = true;
                                }
                                else if (dummy[0].StackSize == 0)
                                {
                                    stoptrying = true;
                                }
                            }
                            else
                            {
                                stoptrying = true;
                            }
                            safetycounter++;
                            if (safetycounter > 24)
                            {
                                stoptrying = true;
                            }
                        }
                    }
                    Vec3d pos = bp.ToVec3d();

                    dummy.DropAll(pos);
                }
            }
        }
Exemplo n.º 3
0
        public override bool TryGiveItemStack(ItemStack itemstack)
        {
            if (itemstack == null || itemstack.StackSize == 0)
            {
                return(false);
            }

            ItemSlot dummySlot = new DummySlot(null);

            dummySlot.Itemstack = itemstack.Clone();

            ItemStackMoveOperation op = new ItemStackMoveOperation(World, EnumMouseButton.Left, 0, EnumMergePriority.AutoMerge, itemstack.StackSize);

            WeightedSlot wslot = inv.GetBestSuitedSlot(dummySlot, new List <ItemSlot>());

            if (wslot.weight > 0)
            {
                dummySlot.TryPutInto(wslot.slot, ref op);
                itemstack.StackSize -= op.MovedQuantity;
                WatchedAttributes.MarkAllDirty();
                return(op.MovedQuantity > 0);
            }

            return(false);
        }
Exemplo n.º 4
0
        public override WeightedSlot GetBestSuitedSlot(ItemSlot sourceSlot, List <ItemSlot> skipSlots = null)
        {
            WeightedSlot bestWSlot = new WeightedSlot();

            if (PutLocked || sourceSlot.Inventory == this)
            {
                return(bestWSlot);
            }

            // Don't allow any shift-clicking of currency
            if (this.CurrencyValuePerItem(sourceSlot) != 0)
            {
                return(bestWSlot);
            }

            // 1. Prefer already filled slots - only allowing shift-clicking into the 4 Selling Cart slots
            for (int i = 0; i < 4; i++)
            {
                ItemSlot slot = GetSellingCartSlot(i);
                if (skipSlots != null && skipSlots.Contains(slot))
                {
                    continue;
                }

                if (slot.Itemstack != null && slot.CanTakeFrom(sourceSlot))
                {
                    float curWeight = GetSuitability(sourceSlot, slot, true);

                    if (bestWSlot.slot == null || bestWSlot.weight < curWeight)
                    {
                        bestWSlot.slot   = slot;
                        bestWSlot.weight = curWeight;
                    }
                }
            }

            // 2. Otherwise use empty slots - again only allowing shift-clicking into the 4 Selling Cart slots
            for (int i = 0; i < 4; i++)
            {
                ItemSlot slot = GetSellingCartSlot(i);
                if (skipSlots != null && skipSlots.Contains(slot))
                {
                    continue;
                }

                if (slot.Itemstack == null && slot.CanTakeFrom(sourceSlot))
                {
                    float curWeight = GetSuitability(sourceSlot, slot, false);

                    if (bestWSlot.slot == null || bestWSlot.weight < curWeight)
                    {
                        bestWSlot.slot   = slot;
                        bestWSlot.weight = curWeight;
                    }
                }
            }

            return(bestWSlot);
        }
Exemplo n.º 5
0
        protected override void DoDeviceComplete()
        {
            deviceState = enDeviceState.IDLE;
            string userecipe = recipe;

            if (ingredient_subtype != "")
            {
                userecipe += "-" + ingredient_subtype;
            }
            Block outputBlock = Api.World.GetBlock(new AssetLocation(userecipe));
            Item  outputItem  = Api.World.GetItem(new AssetLocation(userecipe));

            if (outputBlock == null && outputItem == null)
            {
                deviceState = enDeviceState.ERROR; return;
            }
            ItemStack outputStack;

            if (outputBlock != null)
            {
                outputStack = new ItemStack(outputBlock, outputQuantity);
            }
            else
            {
                outputStack = new ItemStack(outputItem, outputQuantity);
            }

            dummy[0].Itemstack = outputStack;
            outputStack.Collectible.SetTemperature(Api.World, outputStack, lastheatreading);
            BlockPos    bp              = Pos.Copy().Offset(outputFace);
            BlockEntity checkblock      = Api.World.BlockAccessor.GetBlockEntity(bp);
            var         outputContainer = checkblock as BlockEntityContainer;

            if (outputContainer != null)
            {
                WeightedSlot tryoutput = outputContainer.Inventory.GetBestSuitedSlot(dummy[0]);

                if (tryoutput.slot != null)
                {
                    ItemStackMoveOperation op = new ItemStackMoveOperation(Api.World, EnumMouseButton.Left, 0, EnumMergePriority.DirectMerge, outputQuantity);

                    dummy[0].TryPutInto(tryoutput.slot, ref op);
                }
            }

            if (!dummy.Empty)
            {
                //If no storage then spill on the ground
                Vec3d pos = Pos.ToVec3d();

                dummy.DropAll(pos);
            }
            Api.World.PlaySoundAt(new AssetLocation("sounds/doorslide"), Pos.X, Pos.Y, Pos.Z, null, false, 8, 1);
            if (Api.World.Side == EnumAppSide.Client && animUtil != null)
            {
                animUtil.StopAnimation(Pos.ToString() + animationName);
            }
        }
Exemplo n.º 6
0
        public override WeightedSlot GetBestSuitedSlot(ItemSlot sourceSlot, List <IItemSlot> skipSlots = null)
        {
            if (!HaveCookingContainer)
            {
                skipSlots.Add(slots[2]);
                skipSlots.Add(slots[3]);
                skipSlots.Add(slots[4]);
                skipSlots.Add(slots[5]);
                skipSlots.Add(slots[6]);
            }

            WeightedSlot slot = base.GetBestSuitedSlot(sourceSlot, skipSlots);

            return(slot);
        }
Exemplo n.º 7
0
        protected override void DoDeviceComplete()
        {
            deviceState = enDeviceState.IDLE;
            Block outputItem = Api.World.GetBlock(new AssetLocation(recipe));

            if (outputItem == null)
            {
                deviceState = enDeviceState.ERROR; return;
            }

            ItemStack outputStack = new ItemStack(outputItem, outputQuantiy);

            dummy[0].Itemstack = outputStack;

            BlockPos    bp              = Pos.Copy().Offset(outputFace);
            BlockEntity checkblock      = Api.World.BlockAccessor.GetBlockEntity(bp);
            var         outputContainer = checkblock as BlockEntityContainer;

            if (outputContainer != null)
            {
                WeightedSlot tryoutput = outputContainer.Inventory.GetBestSuitedSlot(dummy[0]);
                if (tryoutput != null)
                {
                    ItemStackMoveOperation op = new ItemStackMoveOperation(Api.World, EnumMouseButton.Left, 0, EnumMergePriority.DirectMerge, outputQuantiy);
                    int qmoved = dummy[0].TryPutInto(tryoutput.slot, ref op);
                }
            }

            if (!dummy.Empty)
            {
                //If no storage then spill on the ground
                Vec3d pos = Pos.ToVec3d();

                dummy.DropAll(pos);
            }
            Api.World.PlaySoundAt(new AssetLocation("sounds/doorslide"), Pos.X, Pos.Y, Pos.Z, null, false, 8, 1);
            if (Api.World.Side == EnumAppSide.Client && animUtil != null)
            {
                animUtil.StopAnimation("process");
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Attempts to move the item from slot to slot.
        /// </summary>
        /// <param name="dt"></param>
        public void MoveItem(float dt)
        {
            //check above.  Then check below.
            BlockPos InputPosition  = pos.AddCopy(InputFace);
            BlockPos OutputPosition = pos.AddCopy(OutputFace);

            //if inventory below, attempt to move item in me to below
            if (api.World.BlockAccessor.GetBlockEntity(OutputPosition) is BlockEntityContainer && !inventory.IsEmpty)
            {
                BlockEntityContainer outputBox    = (BlockEntityContainer)api.World.BlockAccessor.GetBlockEntity(OutputPosition);
                ItemSlot             transferSlot = null;
                foreach (ItemSlot slot in inventory)
                {
                    if (!slot.Empty)
                    {
                        transferSlot = slot;
                        break;
                    }
                }

                if (transferSlot != null)
                {
                    WeightedSlot ws = outputBox.Inventory.GetBestSuitedSlot(transferSlot);
                    if (ws.slot != null)
                    {
                        ItemStackMoveOperation op = new ItemStackMoveOperation(api.World, EnumMouseButton.Left, 0, EnumMergePriority.DirectMerge, FlowAmount);
                        if (transferSlot.TryPutInto(ws.slot, ref op) > 0)
                        {
                            if (api.World.Rand.NextDouble() < 0.2)
                            {
                                api.World.PlaySoundAt(new AssetLocation("sounds/block/hoppertumble"), pos.X + 0.5, pos.Y + 0.5, pos.Z + 0.5, null, true, 8, 0.5f);
                            }
                        }
                    }
                } //transfer slot
            }     //Output move.

            //if inventory above, attempt to move item from above into me.  (LATER ON: CHECK FILTER)
            if (api.World.BlockAccessor.GetBlockEntity(InputPosition) is BlockEntityContainer)
            {
                BlockEntityContainer inputBox = (BlockEntityContainer)api.World.BlockAccessor.GetBlockEntity(InputPosition);
                if (inputBox.Inventory is InventoryGeneric)
                {
                    InventoryGeneric inputInventory = (InventoryGeneric)inputBox.Inventory;
                    if (!inputInventory.IsEmpty)
                    {
                        ItemSlot transferSlot = null;
                        foreach (ItemSlot slot in inputInventory)
                        {
                            if (!slot.Empty)
                            {
                                transferSlot = slot;
                            }
                        }

                        if (transferSlot != null)
                        {
                            WeightedSlot ws = inventory.GetBestSuitedSlot(transferSlot);
                            if (ws.slot != null)
                            {
                                ItemStackMoveOperation op = new ItemStackMoveOperation(api.World, EnumMouseButton.Left, 0, EnumMergePriority.DirectMerge, FlowAmount);

                                if (transferSlot.TryPutInto(ws.slot, ref op) > 0)
                                {
                                    if (api.World.Rand.NextDouble() < 0.2)
                                    {
                                        api.World.PlaySoundAt(new AssetLocation("sounds/block/hoppertumble"), pos.X + 0.5, pos.Y + 0.5, pos.Z + 0.5, null, true, 8, 0.5f);
                                    }
                                }
                            }
                        } //transfer slot
                    }     //Inventory empty check
                }         //Inventory Generic check.
            }             //Check for Block entity container.
        }
Exemplo n.º 9
0
        public override void OnInteract(EntityAgent byEntity, ItemSlot slot, Vec3d hitPosition, EnumInteractMode mode)
        {
            IPlayer plr = (byEntity as EntityPlayer)?.Player;

            if (plr != null && !byEntity.World.Claims.TryAccess(plr, Pos.AsBlockPos, EnumBlockAccessFlags.Use))
            {
                plr.InventoryManager.ActiveHotbarSlot.MarkDirty();
                WatchedAttributes.MarkAllDirty();
                return;
            }

            if (mode == EnumInteractMode.Interact && byEntity.RightHandItemSlot != null)
            {
                ItemSlot handslot = byEntity.RightHandItemSlot;
                if (handslot.Empty)
                {
                    // Start from armor slot because it can't wear clothes atm
                    for (int i = 0; i < GearInventory.Count; i++)
                    {
                        ItemSlot gslot = GearInventory[i];
                        if (gslot.Empty)
                        {
                            continue;
                        }

                        if (gslot.TryPutInto(byEntity.World, handslot) > 0)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    if (!ItemSlotCharacter.IsDressType(slot.Itemstack, EnumCharacterDressType.ArmorBody) && !ItemSlotCharacter.IsDressType(slot.Itemstack, EnumCharacterDressType.ArmorHead) && !ItemSlotCharacter.IsDressType(slot.Itemstack, EnumCharacterDressType.ArmorLegs))
                    {
                        (byEntity.World.Api as ICoreClientAPI)?.TriggerIngameError(this, "cantplace", "Cannot place dresses on armor stands");

                        return;
                    }
                }


                WeightedSlot sinkslot = GearInventory.GetBestSuitedSlot(handslot);
                if (sinkslot.weight > 0 && sinkslot.slot != null)
                {
                    handslot.TryPutInto(byEntity.World, sinkslot.slot);
                    return;
                }


                bool empty = true;
                for (int i = 0; i < GearInventory.Count; i++)
                {
                    ItemSlot gslot = GearInventory[i];
                    empty &= gslot.Empty;
                }

                if (empty && byEntity.Controls.Sneak)
                {
                    ItemStack stack = new ItemStack(byEntity.World.GetItem(new AssetLocation("armorstand")));
                    if (!byEntity.TryGiveItemStack(stack))
                    {
                        byEntity.World.SpawnItemEntity(stack, ServerPos.XYZ);
                    }
                    Die();
                    return;
                }
            }



            if (!Alive || World.Side == EnumAppSide.Client || mode == 0)
            {
                //base.OnInteract(byEntity, slot, hitPosition, mode);
                return;
            }


            base.OnInteract(byEntity, slot, hitPosition, mode);
        }