Exemplo n.º 1
0
        public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
        {
            BlockPos pos = blockSel.Position;
            ModSystemBlockReinforcement bR = api.ModLoader.GetModSystem <ModSystemBlockReinforcement>();

            if (disabled || bR.IsReinforced(pos) || bR.IsLocked(pos, byPlayer))
            {
                return(base.OnBlockInteractStart(world, byPlayer, blockSel, ref handling));
            }

            SwapSystem swapSystem = api.ModLoader.GetModSystem <SwapSystem>();

            handling = EnumHandling.PreventDefault;
            ItemSlot slot = byPlayer.InventoryManager.ActiveHotbarSlot;


            if (!(requireSneak && !byPlayer.Entity.Controls.Sneak) && slot.Itemstack != null)
            {
                string key = GetKey(slot.Itemstack.Collectible.Code.ToString());

                if (swapSystem.SwapPairs.TryGetValue(key, out SwapBlocks swap))
                {
                    if (swap.Takes != null && swap.Takes != block.Code.ToString())
                    {
                        return(true);
                    }
                    AssetLocation asset = slot.Itemstack.Collectible.Code;
                    if (asset.ToString() == swap.Tool)
                    {
                        AssetLocation toAsset = new AssetLocation(swap.Makes.WithDomain());
                        Block         toBlock = toAsset.GetBlock(world.Api);

                        int count = swap.Count;

                        if (count != 0)
                        {
                            if (count < 0)
                            {
                                ItemStack withCount = slot.Itemstack.Clone();
                                withCount.StackSize = Math.Abs(count);
                                if (!byPlayer.InventoryManager.TryGiveItemstack(withCount))
                                {
                                    world.SpawnItemEntity(withCount, pos.ToVec3d().Add(0.5, 0.5, 0.5));
                                }
                            }
                            else if (slot.Itemstack.StackSize >= count)
                            {
                                if (byPlayer.WorldData.CurrentGameMode.IsSurvival())
                                {
                                    slot.TakeOut(count);
                                }
                            }
                            else
                            {
                                return(true);
                            }
                        }

                        ((byPlayer.Entity as EntityPlayer)?.Player as IClientPlayer)?.TriggerFpAnimation(EnumHandInteract.HeldItemInteract);

                        if ((block.EntityClass != null && toBlock.EntityClass != null) && (toBlock.EntityClass == block.EntityClass))
                        {
                            world.BlockAccessor.ExchangeBlock(toBlock.BlockId, pos);
                        }
                        else
                        {
                            world.BlockAccessor.SetBlock(toBlock.BlockId, pos);
                        }
                        slot.MarkDirty();
                        PlaySoundDispenseParticles(world, pos, slot);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        public void PostOLInit()
        {
            SwapSystem swapSystem = api.ModLoader.GetModSystem <SwapSystem>();

            requireSneak   = properties["requireSneak"].AsBool(requireSneak);
            particleOrigin = properties["particleOrigin"].Exists ? properties["particleOrigin"].AsObject <Vec3d>() : particleOrigin;
            pRadius        = properties["particleRadius"].AsInt(pRadius);
            pQuantity      = properties["particleQuantity"].AsInt(pQuantity);
            playSound      = properties["playSound"].AsBool(true);

            if (properties["allowedVariants"].Exists)
            {
                string[] allowed = properties["allowedVariants"].AsArray <string>().WithDomain();

                disabled = true;
                if (allowed.Contains(block.Code.ToString()))
                {
                    disabled = false;
                }
                else
                {
                    return;
                }
            }
            if (properties["swapBlocks"].Exists)
            {
                if (api.World.Side.IsServer())
                {
                    try
                    {
                        SwapBlocks[] swapBlocks = properties["swapBlocks"].AsObject <SwapBlocks[]>();
                        foreach (SwapBlocks val in swapBlocks)
                        {
                            if (val.Tool.Contains("*"))
                            {
                                for (int i = 0; i < api.World.Blocks.Count; i++)
                                {
                                    Block iBlock = api.World.Blocks[i];
                                    if (iBlock != null && iBlock.WildCardMatch(new AssetLocation(val.Tool)))
                                    {
                                        SwapBlocks tmp = val.Copy();
                                        tmp.Tool = iBlock.Code.ToString();
                                        if (!swapSystem.SwapPairs.ContainsKey(GetKey(tmp.Tool)))
                                        {
                                            swapSystem.SwapPairs.Add(GetKey(tmp.Tool), tmp);
                                        }
                                    }
                                }
                                for (int i = 0; i < api.World.Items.Count; i++)
                                {
                                    Item iItem = api.World.Items[i];
                                    if (iItem != null && iItem.WildCardMatch(new AssetLocation(val.Tool)))
                                    {
                                        SwapBlocks tmp = val.Copy();
                                        tmp.Tool = iItem.Code.ToString();
                                        if (!swapSystem.SwapPairs.ContainsKey(GetKey(tmp.Tool)))
                                        {
                                            swapSystem.SwapPairs.Add(GetKey(tmp.Tool), tmp);
                                        }
                                    }
                                }
                            }
                            else if (!swapSystem.SwapPairs.ContainsKey(GetKey(val.Tool)))
                            {
                                swapSystem.SwapPairs.Add(GetKey(val.Tool), val);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        disabled = true;
                        api.World.Logger.Notification("Deprecated or unsupported use of swapblocks in " + block.Code.ToString());
                    }
                }
            }
            else
            {
                disabled = true;
                return;
            }
        }