Skip to content

Commit

Permalink
HERO's Mod & Config Optimization (#14)
Browse files Browse the repository at this point in the history
* Added HERO's Mod server config permissions.

* Moved Sell Item config into server scope.
  • Loading branch information
TheBambino authored Sep 1, 2021
1 parent e2bab49 commit b9bf446
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ obj
App.config
/.vs/AutoTrash/v16/.suo
/AutoTrash.sln
*.v2
39 changes: 39 additions & 0 deletions AutoTrash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ namespace AutoTrash
internal class AutoTrash : Mod
{
// TODO: Pre-designed trash items sets. Sorting arrays?
public Mod herosmod;
public const string heropermission = "AutoTrash";
public const string heropermissiondisplayname = "Auto Trash";
public bool hasPermission;

//public static AutoTrash instance;
internal static AutoTrash instance;
internal AutoTrashGlobalItem autoTrashGlobalItem;
internal static UserInterface autoTrashUserInterface;
Expand All @@ -31,6 +37,7 @@ public override void Load()
//{
// throw new Exception("\nThis mod uses functionality only present in the latest tModLoader. Please update tModLoader to use this mod\n\n");
//}
herosmod = ModLoader.GetMod("HEROsMod");
Mod cheatSheet = ModLoader.GetMod("CheatSheet");
if (cheatSheet != null && cheatSheet.Version <= new Version(0, 2, 5, 10))
{
Expand All @@ -50,15 +57,47 @@ public override void Load()
}
}

public AutoTrash GetInstance()
{
return instance;
}

public void SetupHerosMod()
{
if (herosmod != null)
{
herosmod.Call(
// Special string
"AddPermission",
// Permission Name
heropermission,
// Permission Display Name
heropermissiondisplayname);
}
}

public bool getPermission()
{
return hasPermission;
}

public override void Unload()
{
instance = null;
autoTrashUserInterface = null;
herosmod = null;
hasPermission = false;

UICheckbox.checkboxTexture = null;
UICheckbox.checkmarkTexture = null;
}

public override void PostAddRecipes()
{
SetupHerosMod();

}

public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
{
int inventoryLayerIndex = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Inventory"));
Expand Down
10 changes: 2 additions & 8 deletions AutoTrashClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ class AutoTrashClientConfig : ModConfig
[Label("AutoTrash Slot Icon Position X")]
[Tooltip("Customize the position of the AutoTrash slot measured left to right from the 1st hotbar")]
[Range(0, 10)]
public int SlotPositionX;
public int SlotPositionX { get; set; }

[DefaultValue(5)]
[Label("AutoTrash Slot Icon Position Y")]
[Tooltip("Customize the position of the AutoTrash slot measured top to bottom from the top hotbar")]
[Range(0, 10)]
public int SlotPositionY;

[DefaultValue(false)]
[Label("Sell items instead")]
[Tooltip("Will sell items on pickup instead of trashing them")]
public bool SellInstead { get; set; }

public int SlotPositionY { get; set; }
}
}
18 changes: 9 additions & 9 deletions AutoTrashGlobalItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ internal void DrawUpdateAutoTrash()
{
int originalID = singleSlotArray[0].type;

if (clientconfig.SellInstead) {
if (serverconfig.SellInstead) {
float sellPercent = (serverconfig.SellValue >= 1 ? serverconfig.SellValue : 1) / 100f;
var value = (int)Math.Floor(singleSlotArray[0].value * singleSlotArray[0].stack * sellPercent);
if (!Main.mouseItem.IsAir || Main.LocalPlayer.BuyItem(value))
Expand Down Expand Up @@ -197,21 +197,21 @@ internal void DrawUpdateAutoTrash()
//else
//{
Main.hoverItemName = singleSlotArray[0].type != ItemID.None
? (clientconfig.SellInstead ? "Click to remove from Auto-Sell list" : "Click to remove from Auto-Trash list")
: (clientconfig.SellInstead ? "Place item to add to Auto-Sell list" : "Place item to add to Auto-Trash list");
? (serverconfig.SellInstead ? "Click to remove from Auto-Sell list" : "Click to remove from Auto-Trash list")
: (serverconfig.SellInstead ? "Place item to add to Auto-Sell list" : "Place item to add to Auto-Trash list");
//}
}
else
{
Main.hoverItemName = (clientconfig.SellInstead ? "Enable Auto-Sell to automatically sell items on pickup" : "Enable Auto-Trash to automatically trash items on pickup");
Main.hoverItemName = (serverconfig.SellInstead ? "Enable Auto-Sell to automatically sell items on pickup" : "Enable Auto-Trash to automatically trash items on pickup");
}
}
singleSlotArray[0].newAndShiny = false;
if (!autoTrashPlayer.AutoTrashEnabled)
{
Terraria.UI.ItemSlot.Draw(Main.spriteBatch, singleSlotArray, Terraria.UI.ItemSlot.Context.ChestItem, 0, new Vector2((float)xPosition, (float)yPosition), default(Color));
}
else if (clientconfig.SellInstead)
else if (serverconfig.SellInstead)
{
Main.spriteBatch.Draw(ModContent.GetTexture("AutoTrash/AutoSellInvSlot"), new Vector2(xPosition + 9, yPosition + 9), Color.White * 0.7f);
Terraria.UI.ItemSlot.Draw(Main.spriteBatch, singleSlotArray, Terraria.UI.ItemSlot.Context.ShopItem, 0, new Vector2(xPosition, yPosition));
Expand All @@ -232,18 +232,18 @@ internal void DrawUpdateAutoTrash()
{
Main.HoverItem = new Item();
Main.hoverItemName = autoTrashPlayer.AutoTrashEnabled
? (clientconfig.SellInstead ? "Auto-Sell Enabled: " : "Auto-Trash Enabled: ") + autoTrashPlayer.AutoTrashItems.Count + " items"
: (clientconfig.SellInstead ? "Auto-Sell Disabled: " : "Auto-Trash Disabled: ");
? (serverconfig.SellInstead ? "Auto-Sell Enabled: " : "Auto-Trash Enabled: ") + autoTrashPlayer.AutoTrashItems.Count + " items"
: (serverconfig.SellInstead ? "Auto-Sell Disabled: " : "Auto-Trash Disabled: ");
}
if (clearButtonHover)
{
Main.HoverItem = new Item();
Main.hoverItemName = (clientconfig.SellInstead ? "Hold Alt and Click to Clear Auto-Sell list" : "Hold Alt and Click to Clear Auto-Trash list");
Main.hoverItemName = (serverconfig.SellInstead ? "Hold Alt and Click to Clear Auto-Sell list" : "Hold Alt and Click to Clear Auto-Trash list");
}
if (listButtonHover)
{
Main.HoverItem = new Item();
Main.hoverItemName = (clientconfig.SellInstead ? "Click to View Auto-Sell list" : "Click to View Auto-Trash list");
Main.hoverItemName = (serverconfig.SellInstead ? "Click to View Auto-Sell list" : "Click to View Auto-Trash list");
}

Main.inventoryScale = 0.85f;
Expand Down
4 changes: 2 additions & 2 deletions AutoTrashPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public override bool ShiftClickSlot(Item[] inventory, int context, int slot)
{
if (Main.keyState.IsKeyDown(Keys.LeftControl) || Main.keyState.IsKeyDown(Keys.RightControl))
{
if (AutoTrashEnabled && (!AutoTrashItems.Any(x => x.type == inventory[slot].type) || ModContent.GetInstance<AutoTrashClientConfig>().SellInstead))
if (AutoTrashEnabled && (!AutoTrashItems.Any(x => x.type == inventory[slot].type) || ModContent.GetInstance<AutoTrashServerConfig>().SellInstead))
{
Main.PlaySound(SoundID.Grab, -1, -1, 1, 1f, 0f);

Expand Down Expand Up @@ -117,7 +117,7 @@ public void OnItemAutotrashed() {
var clientconfig = ModContent.GetInstance<AutoTrashClientConfig>();
var serverconfig = ModContent.GetInstance<AutoTrashServerConfig>();

if (clientconfig.SellInstead && LastAutoTrashItem.value > 0 && !(LastAutoTrashItem.type >= ItemID.CopperCoin && LastAutoTrashItem.type <= ItemID.PlatinumCoin)) {
if (serverconfig.SellInstead && LastAutoTrashItem.value > 0 && !(LastAutoTrashItem.type >= ItemID.CopperCoin && LastAutoTrashItem.type <= ItemID.PlatinumCoin)) {
float sellPercent = (serverconfig.SellValue >= 1 ? serverconfig.SellValue : 1) / 100f;
var value = Math.Floor((double)(LastAutoTrashItem.value * LastAutoTrashItem.stack * sellPercent));

Expand Down
36 changes: 33 additions & 3 deletions AutoTrashServerConfig.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
using Microsoft.Xna.Framework;
using System.ComponentModel;
using Terraria;
using Terraria.ModLoader.Config;

namespace AutoTrash
{
class AutoTrashServerConfig : ModConfig
{
public override ConfigScope Mode => ConfigScope.ServerSide;

[DefaultValue(15)]
[Label("AutoSell % sell value")]
[Tooltip("Customize the sell value of a item, default merchant sell value is 20%, mod's default sell value is ~15%")]
[Range(1, 100)]
public int SellValue;
}
public int SellValue { get; set; }

[DefaultValue(false)]
[Label("Sell items instead")]
[Tooltip("Will sell items on pickup instead of trashing them")]
public bool SellInstead { get; set; }

public override bool AcceptClientChanges(ModConfig pendingConfig, int whoAmI, ref string message)
{
string deny = "You do not have proper permission.";
string accept = "Your changes have been accepted.";

if (AutoTrash.instance.herosmod != null)
{
if (AutoTrash.instance.herosmod.Call("HasPermission", whoAmI, AutoTrash.heropermission) is bool result && result)
{
message = accept;
return true;
}
}
else
{
message = accept;
return true;
}
message = deny;
return false;
}

}

}

0 comments on commit b9bf446

Please sign in to comment.