Exemplo n.º 1
0
        public override void StartServerSide(ICoreServerAPI api)
        {
            string returnthings_help = "/returnthings [from player] [to player] [index] or /returnthings [from player] list";

            api.RegisterCommand("returnthings",
                                ConstantsCore.ModPrefix + "Returns things losing on last death", returnthings_help,
                                (IServerPlayer byPlayer, int groupId, CmdArgs args) =>
            {
                if (args.Length < 2)
                {
                    byPlayer.SendMessage(returnthings_help);
                    return;
                }


                IPlayer fromPlayer = api.World.AllPlayers.FirstOrDefault(p => p.PlayerName.ToLower() == args[0].ToLower());
                if (fromPlayer == null)
                {
                    byPlayer.SendMessage(Lang.Get("Player {0} not found", args[0]));
                    return;
                }

                string datapath = api.GetOrCreateDataPath($"ModData/{api.GetWorldId()}/{ConstantsCore.ModId}/{fromPlayer.PlayerUID}");
                string[] files  = Directory.GetFiles(datapath).OrderByDescending(f => new FileInfo(f).Name).ToArray();

                if (args[1] == "list")
                {
                    if (files.Length == 0)
                    {
                        byPlayer.SendMessage(Lang.Get("No data saved"));
                        return;
                    }

                    StringBuilder str = new StringBuilder();
                    for (int i = 0; i < files.Length; i++)
                    {
                        str.AppendLine($"{i}. {Path.GetFileName(files[i])}");
                    }
                    byPlayer.SendMessage(str.ToString());
                    return;
                }

                IPlayer toPlayer = api.World.AllPlayers.FirstOrDefault(p => p.PlayerName.ToLower() == args[1].ToLower());
                if (toPlayer == null)
                {
                    byPlayer.SendMessage(Lang.Get("Player {0} not found", args[1]));
                    return;
                }

                if (!api.World.AllOnlinePlayers.Contains(toPlayer) || toPlayer.Entity == null)
                {
                    byPlayer.SendMessage(Lang.Get("Player {0} is offline or not fully loaded.", args[1]));
                    return;
                }

                int offset = args.Length > 2 ? args[2].ToInt(-1) : 0;
                if (offset == -1 || files.Length <= offset)
                {
                    byPlayer.SendMessage(Lang.Get("Index {0} not found", args.Length > 2 ? args[2] : offset.ToString()));
                    return;
                }

                InventoryGeneric inventory = LoadLastDeathContent(fromPlayer, offset);
                foreach (var slot in inventory)
                {
                    if (slot.Empty)
                    {
                        continue;
                    }

                    if (!toPlayer.InventoryManager.TryGiveItemstack(slot.Itemstack))
                    {
                        api.World.SpawnItemEntity(slot.Itemstack, toPlayer.Entity.ServerPos.XYZ.AddCopy(0, 1, 0));
                    }
                    slot.Itemstack = null;
                    slot.MarkDirty();
                }

                byPlayer.SendMessage(Lang.Get("Returned things from {0} to {1} with index {2}", fromPlayer.PlayerName, toPlayer.PlayerName, offset));
            },
                                Config.Current.NeedPrivilegeForReturnThings.Val
                                );
        }
Exemplo n.º 2
0
        public override void StartServerSide(ICoreServerAPI api)
        {
            base.StartServerSide(api);

            this.sapi = api;

            api.RegisterCommand("tpimp", ConstantsCore.ModPrefix + "Import teleport schematic", "[list|paste]",
                                (IServerPlayer player, int groupId, CmdArgs args) =>
            {
                switch (args?.PopWord())
                {
                case "help":

                    player.SendMessage(groupId, "/tpimp [list|paste]", EnumChatType.CommandError);
                    break;


                case "list":

                    List <IAsset> schematics = api.Assets.GetMany(Constants.TELEPORT_SCHEMATIC_PATH);

                    if (schematics == null || schematics.Count == 0)
                    {
                        player.SendMessage(groupId, Lang.Get("Not found"), EnumChatType.CommandError);
                        break;
                    }

                    StringBuilder list = new StringBuilder();
                    foreach (var sch in schematics)
                    {
                        list.AppendLine(sch.Name.Remove(sch.Name.Length - 5));
                    }
                    player.SendMessage(groupId, list.ToString(), EnumChatType.CommandSuccess);

                    break;


                case "paste":

                    string name = args.PopWord();
                    if (name == null || name.Length == 0)
                    {
                        player.SendMessage(groupId, "/tpimp paste [name]", EnumChatType.CommandError);
                        break;
                    }

                    IAsset schema = api.Assets.TryGet($"{Constants.TELEPORT_SCHEMATIC_PATH}/{name}.json");
                    if (schema == null)
                    {
                        player.SendMessage(groupId, Lang.Get("Not found"), EnumChatType.CommandError);
                        break;
                    }

                    string error = null;

                    BlockSchematic schematic = BlockSchematic.LoadFromFile(
                        schema.Origin.OriginPath + "/" + "game" + "/" + schema.Location.Path, ref error);

                    if (error != null)
                    {
                        player.SendMessage(groupId, error, EnumChatType.CommandError);
                        break;
                    }

                    PasteSchematic(schematic, player.Entity.Pos.AsBlockPos.Add(0, -1, 0));

                    break;


                default:
                    player.SendMessage(groupId, "/tpimp [list|paste]", EnumChatType.CommandError);
                    break;
                }
            },
                                Privilege.controlserver
                                );

            api.RegisterCommand("rndtp", ConstantsCore.ModPrefix + "Teleport player to random location", "[range]",
                                (IServerPlayer player, int groupId, CmdArgs args) =>
            {
                RandomTeleport(player, (int)args.PopInt(-1));
            },
                                Privilege.tp
                                );

            api.RegisterCommand("tpnetconfig", ConstantsCore.ModPrefix + "Config for TPNet", "[shared|unbreakable]",
                                (IServerPlayer player, int groupId, CmdArgs args) =>
            {
                switch (args?.PopWord())
                {
                case "help":

                    player.SendMessage(groupId, "/tpnetconfig [shared|unbreakable]", EnumChatType.CommandError);
                    break;


                case "shared":

                    Config.Current.SharedTeleports.Val = !Config.Current.SharedTeleports.Val;
                    api.StoreModConfig <Config>(Config.Current, api.GetWorldId() + "/" + ConstantsCore.ModId);
                    player.SendMessage(groupId, Lang.Get("Shared teleports now is " + (Config.Current.SharedTeleports.Val ? "on" : "off")), EnumChatType.CommandSuccess);
                    break;


                case "unbreakable":

                    Config.Current.Unbreakable.Val = !Config.Current.Unbreakable.Val;
                    api.StoreModConfig <Config>(Config.Current, api.GetWorldId() + "/" + ConstantsCore.ModId);
                    player.SendMessage(groupId, Lang.Get("Unbreakable teleports now is " + (Config.Current.Unbreakable.Val ? "on" : "off")), EnumChatType.CommandSuccess);
                    break;


                default:
                    player.SendMessage(groupId, "/tpnetconfig [shared|unbreakable]", EnumChatType.CommandError);
                    break;
                }
            },
                                Privilege.tp
                                );
        }
Exemplo n.º 3
0
        private void ReturnThingsCommand(IServerPlayer player, int groupId, CmdArgs args)
        {
            if (args.Length < 2)
            {
                player.SendMessage(ReturnThingsHelp);
                return;
            }


            IPlayer fromPlayer = api.World.AllPlayers.FirstOrDefault(p => p.PlayerName.ToLower() == args[0].ToLower());

            if (fromPlayer == null)
            {
                player.SendMessage(Lang.Get("Player {0} not found", args[0]));
                return;
            }

            string localPath = "ModData/" + api.GetWorldId() + "/" + Mod.Info.ModID + "/" + fromPlayer.PlayerUID;
            string path      = api.GetOrCreateDataPath(localPath);

            string[] files = Directory.GetFiles(path).OrderByDescending(f => new FileInfo(f).Name).ToArray();

            if (args[1] == "list")
            {
                if (files.Length == 0)
                {
                    player.SendMessage(Lang.Get("No data saved"));
                    return;
                }

                StringBuilder str = new StringBuilder();
                for (int i = 0; i < files.Length; i++)
                {
                    str.AppendLine(i + ". " + Path.GetFileName(files[i]));
                }
                player.SendMessage(str.ToString());
                return;
            }

            IPlayer toPlayer = api.World.AllPlayers.FirstOrDefault(p => p.PlayerName.ToLower() == args[1].ToLower());

            if (toPlayer == null)
            {
                player.SendMessage(Lang.Get("Player {0} not found", args[1]));
                return;
            }

            if (!api.World.AllOnlinePlayers.Contains(toPlayer) || toPlayer.Entity == null)
            {
                player.SendMessage(Lang.Get("Player {0} is offline or not fully loaded.", args[1]));
                return;
            }

            int offset = args.Length > 2 ? args[2].ToInt(-1) : 0;

            if (offset == -1 || files.Length <= offset)
            {
                player.SendMessage(Lang.Get("Index {0} not found", args.Length > 2 ? args[2] : offset.ToString()));
                return;
            }

            var dcm = api.ModLoader.GetModSystem <DeathContentManager>();
            InventoryGeneric inventory = dcm.LoadLastDeathContent(fromPlayer, offset);

            foreach (var slot in inventory)
            {
                if (slot.Empty)
                {
                    continue;
                }

                if (!toPlayer.InventoryManager.TryGiveItemstack(slot.Itemstack))
                {
                    api.World.SpawnItemEntity(slot.Itemstack, toPlayer.Entity.ServerPos.XYZ.AddCopy(0, 1, 0));
                }
                slot.Itemstack = null;
                slot.MarkDirty();
            }

            player.SendMessage(Lang.Get("Returned things from {0} to {1} with index {2}", fromPlayer.PlayerName, toPlayer.PlayerName, offset));
        }