Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
NotLe0n committed Apr 17, 2021
1 parent 71d5dd3 commit 11e4fdf
Show file tree
Hide file tree
Showing 10 changed files with 322 additions and 0 deletions.
51 changes: 51 additions & 0 deletions BetterChests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Terraria;
using Terraria.ModLoader;
using Terraria.UI;

namespace BetterChests
{
public class BetterChests : Mod
{
internal UserInterface UserInterface;

public override void Load()
{
if (!Main.dedServ)
{
UserInterface = new UserInterface();
UserInterface.SetState(new BetterChestsUI());
}

ILEdits.Load();
}

private GameTime _lastUpdateUiGameTime;
public override void UpdateUI(GameTime gameTime)
{
_lastUpdateUiGameTime = gameTime;
if (Main.LocalPlayer.chest != -1 && BetterChestsUI.visible)
{
UserInterface.Update(gameTime);
}
}
public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
{
int mouseTextIndex = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Mouse Text"));
if (mouseTextIndex != -1)
{
layers.Insert(mouseTextIndex, new LegacyGameInterfaceLayer(
"BetterChests: UI",
delegate
{
if (Main.LocalPlayer.chest != -1 && BetterChestsUI.visible)
{
UserInterface.Draw(Main.spriteBatch, _lastUpdateUiGameTime);
}
return true;
}, InterfaceScaleType.UI));
}
}
}
}
16 changes: 16 additions & 0 deletions BetterChests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\references\tModLoader.targets" />
<PropertyGroup>
<AssemblyName>BetterChests</AssemblyName>
<TargetFramework>net45</TargetFramework>
<PlatformTarget>x86</PlatformTarget>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<Target Name="BuildMod" AfterTargets="Build">
<Exec Command="&quot;$(tMLBuildServerPath)&quot; -build $(ProjectDir) -eac $(TargetPath) -define &quot;$(DefineConstants)&quot; -unsafe $(AllowUnsafeBlocks)" />
</Target>
<ItemGroup>
<PackageReference Include="tModLoader.CodeAssist" Version="0.1.*" />
</ItemGroup>
</Project>
25 changes: 25 additions & 0 deletions BetterChests.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.31112.23
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BetterChests", "BetterChests.csproj", "{4AE73323-57DD-4F6E-BA82-91458D43C821}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4AE73323-57DD-4F6E-BA82-91458D43C821}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4AE73323-57DD-4F6E-BA82-91458D43C821}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4AE73323-57DD-4F6E-BA82-91458D43C821}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4AE73323-57DD-4F6E-BA82-91458D43C821}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4EC07AD4-6ABE-46BC-927C-8BA28E80A370}
EndGlobalSection
EndGlobal
101 changes: 101 additions & 0 deletions BetterChestsUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using Terraria.GameContent.UI.Elements;
using Terraria.UI;
using Terraria;
using System.Linq;
using Terraria.ID;
using System;
using Microsoft.Xna.Framework;
using Terraria.ModLoader;
using Terraria.UI.Chat;

namespace BetterChests
{
class BetterChestsUI : UIState
{
public static bool visible = false;
private bool _reversed = false;
private UIList list;

public override void OnInitialize()
{
var header = new UIText("Sorting Options");
header.Top.Set(0, 0.255f);
header.Left.Set(0, 0.32f);
Append(header);

list = new UIList();
list.Top.Set(0, 0.28f);
list.Left.Set(0, 0.32f);
list.Width.Set(300, 0);
list.Height.Set(400, 0);
list.ListPadding = 14;
Append(list);

AddSortOption("Default Sort", (evt, elm) => ItemSorting.SortChest());
AddSortOption("Sort by ID", (evt, elm) => Sort(x => x.netID, _reversed));
AddSortOption("Sort by name", (evt, elm) => Sort(x => x.Name, _reversed));
AddSortOption("Sort by rarity", (evt, elm) => Sort(x => x.rare, !_reversed));
AddSortOption("Sort by stack size", (evt, elm) => Sort(x => x.stack, !_reversed));
AddSortOption("Sort by value", (evt, elm) => Sort(x => x.value, !_reversed));
AddSortOption("Sort by Damage", (evt, elm) => Sort(x => x.damage, !_reversed));
AddSortOption("Sort by Defense", (evt, elm) => Sort(x => x.defense, !_reversed));

}

private void AddSortOption(string title, MouseEvent onclick)
{
var option = new UITextOption(title);
option.OnClick += onclick;
list.Add(option);
}

private void Sort<T>(Func<Item, T> func, bool reversed)
{
// all items in the chest
ref var items = ref Main.chest[Main.LocalPlayer.chest].item;

// order the items according to the function.
var sortedItems = items.OrderBy(func).ToArray();

if (reversed)
{
// reverse the order
sortedItems = sortedItems.Reverse().ToArray();
}

// Air always goes last
sortedItems = sortedItems.OrderBy(x => x.IsAir).ToArray();

// Change color of changed slots
for (int i = 0; i < items.Length; i++)
{
if (!items[i].IsAir && items[i] != sortedItems[i])
{
ItemSlot.SetGlow(i, Main.rand.NextFloat(), true);
}
}

// Apply changes
items = sortedItems;

// sync chest contents with all clients
if (Main.netMode == NetmodeID.MultiplayerClient)
{
for (int i = 0; i < items.Length; i++)
{
NetMessage.SendData(MessageID.SyncChestItem, number: Main.LocalPlayer.chest, number2: i);
}
}
}

public override void Update(GameTime gameTime)
{
base.Update(gameTime);

if (ContainsPoint(Main.MouseScreen))
{
Main.LocalPlayer.mouseInterface = true;
}
}
}
}
40 changes: 40 additions & 0 deletions ILEdits.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Terraria;
using System.Reflection;
using MonoMod.Cil;
using Mono.Cecil.Cil;
using Terraria.UI;
using System.Diagnostics;

namespace BetterChests
{
public class ILEdits
{
public static void Load()
{
IL.Terraria.UI.ChestUI.DrawButton += EditButton;
}

private static void EditButton(ILContext il)
{
var c = new ILCursor(il);

// The goal of this IL edit is to replace SortChest() with code to open my UI
// IL_0385: br.s IL_038C
// IL_0387: call void Terraria.UI.ItemSorting::SortChest()
// <=== here

if (!c.TryGotoNext(MoveType.After, i => i.MatchCall<ItemSorting>("SortChest")))
return;

c.Prev.Operand = typeof(ILEdits).GetMethod("OpenUI", BindingFlags.NonPublic | BindingFlags.Static);
}

private static void OpenUI()
{
BetterChestsUI.visible = !BetterChestsUI.visible;
}
}
}
14 changes: 14 additions & 0 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"profiles": {
"Terraria": {
"commandName": "Executable",
"executablePath": "$(tMLPath)",
"workingDirectory": "$(TerrariaSteamPath)"
},
"TerrariaServer": {
"commandName": "Executable",
"executablePath": "$(tMLServerPath)",
"workingDirectory": "$(TerrariaSteamPath)"
}
}
}
55 changes: 55 additions & 0 deletions UITextOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.ID;
using Terraria.UI;

namespace BetterChests
{
public class UITextOption : UIText
{
public float TextScale { get; set; }
public bool isLarge;
private float _firstTextScale;

public UITextOption(string text, float textScale = 0.85f, bool large = false) : base(text, textScale, large)
{
_firstTextScale = textScale;
TextScale = textScale;
isLarge = large;
}

public override void MouseOver(UIMouseEvent evt)
{
base.MouseOver(evt);
Main.PlaySound(SoundID.MenuTick);
}

public override void Draw(SpriteBatch spriteBatch)
{
base.Draw(spriteBatch);

if (IsMouseHovering)
{
if (TextScale <= 1)
{
TextScale += 0.05f;
}
}
else
{
if (TextScale >= _firstTextScale)
{
TextScale -= 0.05f;
}
}

SetText(Text, TextScale, isLarge);
}
}
}
8 changes: 8 additions & 0 deletions build.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
displayName = Better Chests
author = NotLe0n
version = 0.1
hideCode = false
hideResources = false
includeSource = false
buildIgnore = vs/*, *.csproj, *.sln, *.pidb, *.userprefs, *.user, obj/*, bin/*
homepage = https://forums.terraria.org/index.php?threads/better-chests.104841/
12 changes: 12 additions & 0 deletions description.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[c/FFE900:This mod adds more options to sort your chests]

For a Changlog and more information visit my homepage!

Contact [c/66EEFF:NotLe0n#7696] on discord if you have any problems.
_________________________

[c/FFE900: How to use]
_________________________

[c/C4C4C4: Simply open a chest and click on "Sort Items".]
[c/C4C4C4: This will open another menu to the right.]
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 11e4fdf

Please sign in to comment.