Skip to content

Commit

Permalink
AutoTeam Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Metacinnabar committed Aug 10, 2021
1 parent c6b83cb commit 1f26fe0
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 0 deletions.
57 changes: 57 additions & 0 deletions AutoTeam.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{ECAA2D38-4288-425D-99CB-553D79AAC61A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AutoTeam</RootNamespace>
<AssemblyName>AutoTeam</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="OTAPI">
<HintPath>lib\OTAPI.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="TerrariaServer">
<HintPath>lib\TerrariaServer.exe</HintPath>
</Reference>
<Reference Include="TShockAPI">
<HintPath>lib\TShockAPI.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="*.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
25 changes: 25 additions & 0 deletions AutoTeam.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31515.178
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoTeam", "AutoTeam.csproj", "{ECAA2D38-4288-425D-99CB-553D79AAC61A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{ECAA2D38-4288-425D-99CB-553D79AAC61A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ECAA2D38-4288-425D-99CB-553D79AAC61A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ECAA2D38-4288-425D-99CB-553D79AAC61A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ECAA2D38-4288-425D-99CB-553D79AAC61A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DA2E2E05-2AAC-4C64-A6D6-FE90A487892D}
EndGlobalSection
EndGlobal
17 changes: 17 additions & 0 deletions AutoTeamConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.IO;
using TShockAPI;
using TShockAPI.Configuration;

namespace AutoTeam
{
public class AutoTeamConfig
{
public static string DirectoryPath = Path.Combine(TShock.SavePath, "AutoTeam");

public static string FilePath = Path.Combine(DirectoryPath, "AutoTeam.json");

public byte Team { get; set; } = (byte)AutoTeamPlugin.TeamId.Red;
}

public class AutoTeamConfigFile : ConfigFile<AutoTeamConfig> { }
}
97 changes: 97 additions & 0 deletions AutoTeamPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.IO;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;

namespace AutoTeam
{
[ApiVersion(2, 1)]
public class AutoTeamPlugin : TerrariaPlugin
{
public override string Author => "GoodPro712";

public override string Description => "Automatically joins players to a configurable team when they join the server.";

public override string Name => "Auto Team";

public override Version Version => new Version(1, 0, 0);

private AutoTeamConfigFile config;

public AutoTeamPlugin(Main game) : base(game) { }

public override void Initialize()
{
// create a new config
config = new AutoTeamConfigFile();
// "load" the config with the determined values at the file path (if it exists)
config.Read(AutoTeamConfig.FilePath, out bool notFound);

// check if the files exists or not
if (notFound)
{
// create directory structure for the config
Directory.CreateDirectory(AutoTeamConfig.DirectoryPath);
// create the default file at the file path
config.Write(AutoTeamConfig.FilePath);
// tell the console (and server logs) that the file has been generated
TShock.Log.ConsoleError($"[AutoTeam] Config file not present. Generated a config file at {AutoTeamConfig.FilePath}");
}

ServerApi.Hooks.NetGetData.Register(this, OnGetData);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
// deregister the getdata hook
ServerApi.Hooks.NetGetData.Deregister(this, OnGetData);
// unload the config
config = null;
}

base.Dispose(disposing);
}

public enum TeamId
{
None = 0,
Red = 1,
Green = 2,
Blue = 3,
Yellow = 4,
Pink = 5
}

private void OnGetData(GetDataEventArgs args)
{
// check if the packet sent is the player update packet
if (args.MsgID == PacketTypes.PlayerUpdate)
{
// create a memory stream used to read information from the packet
using (MemoryStream data = new MemoryStream(args.Msg.readBuffer, args.Index, args.Length))
{
// player variable read from the first byte of the packet
TSPlayer player = TShock.Players[data.ReadByte()];
// quick local bool to check if the config is within range of the teams (so there isnt a super long code line)
bool correctConfig = config.Settings.Team >= (byte)TeamId.Red && config.Settings.Team <= (byte)TeamId.Pink;
// local team variable defaulting to red team if the config is incorrect
byte team = correctConfig ? config.Settings.Team : (byte)TeamId.Red;

// check if the player has been teamed or not
if (player.GetData<bool>("autoTeamed") == false)
{
// change the player's team
player.SetTeam(team);
// set the player to be teamed with TSPlayer's custom data setter
player.SetData("autoTeamed", true);
// log this information to the console, notifing the server host that a player's team has been changed
TShock.Log.ConsoleInfo($"[AutoTeam] Set {player.Name}'s team to {(TeamId)team}.");
}
}
}
}
}
}
36 changes: 36 additions & 0 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("AutoTeam")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AutoTeam")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("ecaa2d38-4288-425d-99cb-553d79aac61a")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Binary file added lib/OTAPI.dll
Binary file not shown.
Binary file added lib/TShockAPI.dll
Binary file not shown.
Binary file added lib/TerrariaServer.exe
Binary file not shown.

0 comments on commit 1f26fe0

Please sign in to comment.