예제 #1
0
        // Return value:
        // - null = continue with default behaviour of hammer hit
        // - anything else: prevent default behaviour.
        private object HandleDowngradeRequest(BasePlayer player, Crafter crafter)
        {
            float lastRequest = lastHammerHit[player];

            if (Time.time - lastRequest > Constants.HammerConfirmTime)             // Confirm the downgrade
            {
                string message = Lang.Translate(player, "hp-message", crafter.Recycler.Health(), crafter.Recycler.MaxHealth());
                message += "\n\n" + Lang.Translate(player, "hammer-confirm-downgrade");

                lastHammerHit[player] = Time.time;
                player.ShowScreenMessage(message, Constants.HammerConfirmTime);
                return(true);
            }

            lastHammerHit[player] = 0;             // Reset time

            CrafterManager.DestroyCrafter(crafter, true, false);
            FxManager.PlayFx(crafter.Position, Constants.UpgradeMetalFxPrefab);
            player.HideScreenMessage();

            foreach (var itemAmount in UpgradeCost)
            {
                player.GiveItem(ItemManager.CreateByItemID(itemAmount.itemid, (int)itemAmount.amount));
            }

            // Refund codelock if one is attached
            if (crafter.CodeLock != null)
            {
                var item = ItemManager.Create(ItemManager.FindItemDefinition("lock.code"));
                player.GiveItem(item);
            }

            return(true);
        }
예제 #2
0
        private object OnServerCommand(ConsoleSystem.Arg arg)
        {
            if (arg.Connection == null)
            {
                return(null);
            }

            var player = (BasePlayer)arg.Connection.player;

            if (arg.cmd?.FullName == "craft.canceltask")
            {
                int taskid = arg.GetInt(0, -1);

                if (taskid == -1)
                {
                    return(null);
                }

                var crafters = CrafterManager.FindAllByPlayer(player);

                foreach (var crafter in crafters)
                {
                    if (crafter.CancelByTaskId(player, taskid))
                    {
                        return(true);
                    }
                }

                return(null);
            }

            return(null);
        }
예제 #3
0
        void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
        {
            float newHealth = entity.Health() - info.damageTypes.Total();

            // Empty recycler inventory if it's about to be killed to avoid dropping hidden items.
            if (newHealth <= 0)
            {
                var recycler = entity as Recycler;

                if (!(entity is Recycler))
                {
                    return;
                }

                var crafter = CrafterManager.GetCrafter(recycler);

                if (crafter == null)
                {
                    return;
                }

                recycler.inventory.Clear();
                recycler.inventory.itemList.Clear();
            }
        }
예제 #4
0
        void OnEntityKill(BaseNetworkable entity)
        {
            if (!serverInitialized)             // Check if server is initialized. This hook tends to call on startup before OnServerInitialized has been called.
            {
                return;
            }

            var researchTable = entity as ResearchTable;

            if (researchTable != null)
            {
                upgradeableEntities.Remove(researchTable);
            }

            var recycler = entity as Recycler;

            if (recycler == null)
            {
                return;
            }

            var crafter = CrafterManager.GetCrafter(recycler);

            if (crafter == null)
            {
                return;
            }

            CrafterManager.DestroyCrafter(crafter, false, false);
        }
예제 #5
0
        private void OnServerSave()
        {
            CrafterManager.Save();

            if (Utility.Config.ShowPlayerInstructionsOnFirstJoin)
            {
                Core.Interface.Oxide.DataFileSystem.WriteObject("AutoCrafter/IntroducedPlayers", introducedPlayers);
            }
        }
예제 #6
0
        private object OnRecycleItem(Recycler recycler, Item item)
        {
            if (CrafterManager.ContainsRecycler(recycler))
            {
                // Prevent recycling
                return(true);
            }

            return(null);
        }
예제 #7
0
        private void Unload()
        {
            if (!serverInitialized)             // Check if server is initialized. This hook tends to call on startup before OnServerInitialized has been called.
            {
                return;
            }

            FxManager.Destroy();
            CrafterManager.Destroy();
            UiManager.Destroy();
        }
예제 #8
0
        private object OnItemCraft(ItemCraftTask task)
        {
            var player  = task.owner;
            var crafter = CrafterManager.FindByPlayer(player);

            if (crafter != null && crafter.PlayerCanAccess(player))
            {
                crafter.AddCraftTask(task);
                return(true);
            }

            return(null);
        }
예제 #9
0
        object OnRecyclerToggle(Recycler recycler, BasePlayer player)
        {
            var crafter = CrafterManager.GetCrafter(recycler);

            if (crafter == null)
            {
                return(null);
            }

            if (!crafter.PlayerCanAccess(player))
            {
                crafter.PlayLockedSound();
                return(true);
            }

            return(null);
        }
예제 #10
0
        void OnPlayerInput(BasePlayer player, InputState input)
        {
            if (input.WasJustPressed(BUTTON.FIRE_PRIMARY))
            {
                var activeItem = player.GetActiveItem();

                if (activeItem?.info.itemid != -975723312)                 // Codelock
                {
                    return;
                }

                var        ray = player.eyes.HeadRay();
                RaycastHit hit;

                if (!Physics.Raycast(ray, out hit, 2.2f, 1 << (int)Layer.Deployed))
                {
                    return;
                }

                var recycler = hit.transform.GetComponentInParent <Recycler>();

                if (recycler == null)
                {
                    return;
                }

                if (player.IsBuildingBlocked(recycler.ServerPosition, recycler.ServerRotation, recycler.bounds))
                {
                    return;
                }

                var crafter = CrafterManager.GetCrafter(recycler);

                if (crafter == null)
                {
                    return;
                }

                if (crafter.AddCodeLock())
                {
                    activeItem.UseItem();
                    FxManager.PlayFx(crafter.CodeLock.ServerPosition, Constants.CodelockPlaceSoundPrefab);
                }
            }
        }
예제 #11
0
        // Return value:
        // - null = continue with default behaviour of hammer hit
        // - anything else: prevent default behaviour.
        private object HandleUpgradeRequest(BasePlayer player, ResearchTable researchTable)
        {
            if (UpgradeCost.Count > 0)
            {
                if (!player.CanCraft(UpgradeCost))
                {
                    StringBuilder builder = new StringBuilder();

                    foreach (var ingredient in UpgradeCost)
                    {
                        builder.AppendLine("- x" + ingredient.amount.ToString("0") + " " + ingredient.itemDef.displayName.english);
                    }

                    string ingredientsStr = builder.ToString();

                    player.ShowScreenMessage(Lang.Translate(player, "ingredients-missing-youneed") + "\n" + ingredientsStr, 10, TextAnchor.MiddleLeft);
                    return(true);
                }
            }

            float lastHit = lastHammerHit[player];

            if (Time.time - lastHit > Constants.HammerConfirmTime)             // Confirm the upgrade
            {
                lastHammerHit[player] = Time.time;
                player.ShowScreenMessage(Lang.Translate(player, "hammer-confirm-upgrade"), Constants.HammerConfirmTime);
                return(true);
            }

            lastHammerHit[player] = 0;             // Reset time

            foreach (var ingredient in UpgradeCost)
            {
                List <Item> takenItems = new List <Item>();
                player.inventory.Take(takenItems, ingredient.itemid, (int)ingredient.amount);
            }

            CrafterManager.CreateCrafter(researchTable);
            FxManager.PlayFx(researchTable.ServerPosition, Constants.UpgradeTopTierFxPrefab);
            player.HideScreenMessage();
            return(true);
        }
예제 #12
0
        void OnEntityGroundMissing(BaseEntity entity)
        {
            var recycler = entity as Recycler;

            if (recycler == null)
            {
                return;
            }

            var crafter = CrafterManager.GetCrafter(recycler);

            if (crafter == null)
            {
                return;
            }

            // Empty recycler, otherwise the hidden items inside it will drop into the world.
            recycler.inventory.Clear();
            recycler.inventory.itemList.Clear();
        }
예제 #13
0
        private void OnServerInitialized()
        {
            Utility.Timer = timer;

            Config.Settings.AddConverters();
            permission.RegisterPermission(Constants.UsePermission, this);
            lang.RegisterMessages(Lang.DefaultMessages, this, "en");

            UiManager.Initialize();
            Lang.Initialize(this, lang);
            FxManager.Initialize();

            foreach (var itemAmount in Utility.Config.UpgradeCost)
            {
                var itemDef = ItemManager.FindItemDefinition(itemAmount.Shortname);

                if (itemDef == null)
                {
                    PrintError(Lang.Translate(null, "item-notfound-skipping-ingredient", itemAmount.Shortname));
                    continue;
                }

                UpgradeCost.Add(new ItemAmount(itemDef, itemAmount.Amount));
            }

            CrafterManager.Initialize();
            CrafterManager.Load();

            if (Utility.Config.ShowPlayerInstructionsOnFirstJoin)
            {
                // Load previously introduced players
                introducedPlayers = Core.Interface.Oxide.DataFileSystem.ReadObject <List <ulong> >("AutoCrafter/IntroducedPlayers");

                foreach (var player in BasePlayer.activePlayerList)
                {
                    ShowJoinMessage(player);
                }
            }

            serverInitialized = true;
        }
예제 #14
0
        private void OnLootEntity(BasePlayer player, BaseEntity entity)
        {
            var recycler = entity as Recycler;

            if (recycler == null)
            {
                return;
            }

            var crafter = CrafterManager.GetCrafter(recycler);

            if (crafter == null)
            {
                return;
            }

            // Open the output container instead of the recycler ui.
            NextFrame(() =>
            {
                if (!crafter.PlayerCanAccess(player))
                {
                    crafter.PlayLockedSound();
                    player.CloseInventory();
                    return;
                }

                player.inventory.loot.Clear();
                player.inventory.loot.StartLootingEntity(crafter.Output);
                player.inventory.loot.AddContainer(crafter.OutputInventory);
                player.inventory.loot.SendImmediate();
                player.ClientRPCPlayer(null, player, "RPC_OpenLootPanel", crafter.Output.lootPanelName);

                if (crafter.IsLocked())
                {
                    crafter.PlayAccessSound();
                }
            });
        }
예제 #15
0
        private object OnHammerHit(BasePlayer player, HitInfo info)
        {
            var entity        = info.HitEntity as BaseCombatEntity;
            var recycler      = entity as Recycler;
            var researchTable = entity as ResearchTable;

            if (entity == null || (recycler == null && researchTable == null))
            {
                return(null);
            }

            Func <string> hpMessage = () =>
            {
                return(Lang.Translate(player, "hp-message", entity.Health(), entity.MaxHealth()));
            };

            // Don't allow upgrading/downgrading/repairing if there's less than 8 seconds since the entity was attacked.
            if (entity.SecondsSinceAttacked < 8)
            {
                if (recycler != null && CrafterManager.ContainsRecycler(recycler))
                {
                    // Show hp info if repairing is blocked.
                    player.ShowScreenMessage(hpMessage(), 2);
                }
                return(null);
            }

            if (!lastHammerHit.ContainsKey(player))
            {
                lastHammerHit[player] = 0;
            }

            ((DecayEntity)entity).DecayTouch();              // Reset decay

            // Make sure entity is full health, otherwise repair.
            if (entity.Health() < entity.MaxHealth())
            {
                if (recycler == null)
                {
                    return(null);
                }

                if (!CrafterManager.ContainsRecycler(recycler))
                {
                    return(null);
                }

                if (Time.time - lastHammerHit[player] > Constants.HammerConfirmTime)
                {
                    player.ShowScreenMessage(hpMessage() + "\n\n" + Lang.Translate(player, "hit-again-to-repair"), Constants.HammerConfirmTime);
                    lastHammerHit[player] = Time.time;
                    return(true);
                }

                lastHammerHit[player] = Time.time;
                player.HideScreenMessage();
                entity.DoRepair(player);
                player.ShowScreenMessage(hpMessage(), 2);

                // Reset last hammer hit so that the player won't accidentally downgrade/upgrade with the next hammer hit.
                if (entity.Health() >= entity.MaxHealth())
                {
                    lastHammerHit[player] = 0;
                }

                return(true);
            }

            // Only allow upgrading/downgrading if we have building permission.
            if (player.IsBuildingBlocked(entity.ServerPosition, entity.ServerRotation, entity.bounds))
            {
                if (recycler != null && CrafterManager.ContainsRecycler(recycler))                 // Only show hp info if this is a crafter
                {
                    // Show hp info if building blocked.
                    player.ShowScreenMessage(hpMessage(), 2);
                }

                return(null);
            }

            // Check permission and if the entity owner is the current player.
            if (!permission.UserHasPermission(player.UserIDString, Constants.UsePermission) || entity.OwnerID != player.userID)
            {
                if (recycler != null && CrafterManager.ContainsRecycler(recycler))
                {
                    player.ShowScreenMessage(hpMessage(), 2);
                }

                return(null);
            }

            if (researchTable != null)             // Upgrade to crafter (if less than 10 minutes since placement)
            {
                if (!upgradeableEntities.Contains(researchTable))
                {
                    return(null);
                }

                return(HandleUpgradeRequest(player, researchTable));
            }

            var crafter = CrafterManager.GetCrafter(recycler);

            if (crafter == null)
            {
                return(null);
            }

            if (DateTime.UtcNow - crafter.CreationTime > TimeSpan.FromSeconds(Constants.TimeToUpgrade))
            {
                player.ShowScreenMessage(hpMessage(), 2);
                return(null);
            }

            // Check if player has authed on potential codelock.
            if (!crafter.PlayerCanAccess(player))
            {
                crafter.PlayLockedSound();
                return(true);
            }

            return(HandleDowngradeRequest(player, crafter));
        }