Figured out the logic of how to have custom pickaxes! Now to make the other tools/upgrades/recipes...

This commit is contained in:
JoshuaNavarro 2019-09-06 14:15:54 -07:00
parent 4bfdef4383
commit a4d8a4c1a8
12 changed files with 530 additions and 31 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 908 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 369 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@ -0,0 +1,126 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewValley;
using StardustCore.UIUtilities;
namespace Revitalize.Framework.Hacks
{
/// <summary>
/// Taken from SDV to be able to swap tool colors
/// </summary>
public static class ColorChanger
{
private static Texture2D mostRecentPicxaxeSwap;
public static void SwapPickaxeTextures(Texture2D TargetTexture)
{
/*
if (mostRecentPicxaxeSwap != null)
{
if (TargetTexture == mostRecentPicxaxeSwap) return;
}
*/
int height = Game1.toolSpriteSheet.Height;
int width = Game1.toolSpriteSheet.Width;
Color[] beforeData = new Color[Game1.toolSpriteSheet.Width * Game1.toolSpriteSheet.Height];
Game1.toolSpriteSheet.GetData<Color>(beforeData);
Color[] afterData = new Color[TargetTexture.Width * TargetTexture.Height]; //Get a color data to replace
TargetTexture.GetData<Color>(afterData); //Get data from swap texture.
///Convert tool data to grid
Color[,] beforeGrid = new Color[height, width];
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
// Assumes row major ordering of the array.
beforeGrid[row, column] = beforeData[row * width + column];
}
}
//Convert target data to grid
int targetHeight = TargetTexture.Height;
int targetWidth = TargetTexture.Width;
Color[,] afterGrid = new Color[targetHeight, targetWidth];
for (int row = 0; row < targetHeight; row++)
{
for (int column = 0; column < targetWidth; column++)
{
// Assumes row major ordering of the array.
afterGrid[row, column] = afterData[row * targetWidth + column];
}
}
//Copy over data from the target texture into the before grid.
Rectangle stoneRegion = new Rectangle(0,80,80,32);
Rectangle copperRegion = new Rectangle();
Rectangle ironRegion = new Rectangle(224, 80, 80, 32); //Create the region we want to replace.
Rectangle goldRegion = new Rectangle();
Rectangle irridumRegon = new Rectangle();
List<Rectangle> rects = new List<Rectangle>();
//rects.Add(ironRegion);
rects.Add(stoneRegion);
foreach (Rectangle region in rects)
{
for (int x = region.X; x < region.X + region.Width; x++)
{
for (int y = region.Y; y < region.Y + region.Height; y++)
{
//ModCore.log("value is: " + new Vector2(x, y));
beforeGrid[y, x] = afterGrid[(int)(y - region.Y), x - region.X]; //Row, column order aka y,x order.
}
}
//Convert the tool grid back into a 1d color array
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
try
{
beforeData[row * width + column] = beforeGrid[row, column];
}
catch (Exception err)
{
ModCore.log("Setting pixel color at: " + new Vector2(column, row));
ModCore.log("That's position: " + (row * width + column).ToString());
}
//ModCore.log("Setting pixel color at: " + new Vector2(column, row));
//ModCore.log("That's position: " + (row * width + column).ToString());
//beforeData[row * targetWidth + column] = beforeGrid[row,column];
}
}
}
//beforeGrid.CopyTo(beforeData, 0);
//Reapply the texture.
Game1.toolSpriteSheet.SetData<Color>(beforeData);
Stream stream = File.Create(Path.Combine(ModCore.ModHelper.DirectoryPath,"ToolTest.png"));
Game1.toolSpriteSheet.SaveAsPng(stream, width, height);
stream.Dispose();
}
public static void ResetPickaxeTexture()
{
SwapPickaxeTextures(TextureManager.GetTexture(ModCore.Manifest, "Tools", "DefaultPickaxeWorking"));
}
public static void ResetToolColorSwaps()
{
ResetPickaxeTexture();
}
}
}

View File

@ -8,7 +8,9 @@ using Revitalize.Framework.Crafting;
using Revitalize.Framework.Objects;
using Revitalize.Framework.Objects.Furniture;
using Revitalize.Framework.Utilities;
using StardewModdingAPI;
using StardewValley;
using StardewValley.Tools;
using SObject = StardewValley.Object;
namespace Revitalize.Framework.Hacks
{
@ -152,5 +154,25 @@ namespace Revitalize.Framework.Hacks
|| potentialDescendant == potentialBase;
}
/// <summary>
/// Resets tool colors when left click is used for normal tools.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void ResetNormalToolsColorOnLeftClick(object sender, StardewModdingAPI.Events.ButtonPressedEventArgs e)
{
if (e.Button == SButton.MouseLeft)
{
if (Game1.player.CurrentTool != null)
{
if (ObjectUtilities.IsSameType(Game1.player.CurrentTool.GetType(), typeof(Pickaxe)) || ObjectUtilities.IsSameType(Game1.player.CurrentTool.GetType(), typeof(Axe)) || ObjectUtilities.IsSameType(Game1.player.CurrentTool.GetType(), typeof(Hoe)) || ObjectUtilities.IsSameType(Game1.player.CurrentTool.GetType(), typeof(WateringCan)))
{
ColorChanger.ResetToolColorSwaps();
}
}
}
}
}
}

View File

@ -701,34 +701,6 @@ namespace Revitalize.Framework.Objects
Revitalize.ModCore.Serializer.SerializeGUID(this.guid.ToString(), this);
return serializedInfo;
}
/// <summary>
/// Gets all of the data necessary for syncing.
/// </summary>
/// <returns></returns>
public override Dictionary<string, string> getSyncData()
{
Dictionary<string, string> syncData = new Dictionary<string, string>();
//syncData.Add("ID", this.ItemInfo);
//syncData.Add("BasicItemInfo", Revitalize.ModCore.Serializer.ToJSONString(this.info));
syncData.Add("Greeting:", "Hello from: " + Game1.player.Name);
ModCore.log("Send off SYNC DATA!");
return syncData;
}
/// <summary>
/// Syncs all of the info to all players.
/// </summary>
/// <param name="syncData"></param>
public override void sync(Dictionary<string, string> syncData)
{
//Revitalize.ModCore.log("SYNC OBJECT DATA!");
//this.info = Revitalize.ModCore.Serializer.DeserializeFromJSONString<BasicItemInformation>(syncData["BasicItemInfo"]);
//this.ItemInfo = syncData["ID"];
string greeting = syncData["Greeting"];
ModCore.log(greeting);
}
#endregion
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Revitalize.Framework.Objects.Interfaces
{
public interface IItemInfo
{
BasicItemInformation Info
{
get;
set;
}
}
}

View File

@ -0,0 +1,307 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json;
using PyTK.CustomElementHandler;
using Revitalize.Framework.Hacks;
using Revitalize.Framework.Objects.Interfaces;
using Revitalize.Framework.Utilities;
using StardewValley;
using StardewValley.Tools;
using StardustCore.UIUtilities;
namespace Revitalize.Framework.Objects.Items.Tools
{
public class PickaxeExtended:StardewValley.Tools.Pickaxe, ISaveElement,IItemInfo
{
public BasicItemInformation info;
public Texture2DExtended workingTexture;
/// <summary>
/// Used only for accessibility for casting.
/// </summary>
[JsonIgnore]
public BasicItemInformation Info
{
get
{
return this.info;
}
set
{
this.info = value;
}
}
public Guid guid;
public override string Name
{
get
{
if (this.info != null)
{
return this.netName.Value.Split('>')[0];
//return this.info.name;
}
if (this.netName == null)
{
return this.Name;
}
else
{
return this.netName.Value.Split('>')[0]; //Return the value before the name because that is the true value.
}
}
set
{
if (this.netName == null)
{
return;
}
if (this.netName.Value == null)
{
return;
}
if (this.netName.Value.Split('>') is string[] split && split.Length > 1)
{
this.netName.Value = value + ">" + split[1]; //When setting the name if appended data is added on set the new value and add that appended data back.
}
else
{
this.netName.Value = value; //Otherwise just set the net name.
}
}
}
public override string DisplayName
{
get
{
if (this.info != null)
{
return this.info.name;
}
return this.netName.Value.Split('>')[0];
}
set
{
if (this.netName == null) return;
if (this.netName.Value == null) return;
if (this.netName.Value.Split('>') is string[] split && split.Length > 1)
this.netName.Value = value + ">" + split[1];
else
this.netName.Value = value;
}
}
public virtual string text
{
get
{
if (this.netName.Value.Split('>') is string[] split && split.Length > 1)
return split[1]; //This is custom data. If the net name has a much larger name split the value and return the result.
else
return ""; //Otherwise return nothing.
}
set
{
if (this.netName == null) return;
if (this.netName.Value == null) return;
{
this.netName.Value = this.netName.Value.Split('>')[0] + ">" + value; //When setting the custom dataappend it to the end of the name.
}
}
}
[JsonIgnore]
public virtual string ItemInfo
{
get
{
return Revitalize.ModCore.Serializer.ToJSONString(this.info) + "<" + this.guid+"<"+ Revitalize.ModCore.Serializer.ToJSONString(this.workingTexture);
}
set
{
if (string.IsNullOrEmpty(value)) return;
string[] data = value.Split('<');
string infoString = data[0];
string guidString = data[1];
string WorkingTexture = data[2];
this.info = (BasicItemInformation)Revitalize.ModCore.Serializer.DeserializeFromJSONString(infoString, typeof(BasicItemInformation));
Guid oldGuid = this.guid;
this.guid = Guid.Parse(guidString);
this.workingTexture = Revitalize.ModCore.Serializer.DeserializeFromJSONString<Texture2DExtended>(WorkingTexture);
if (ModCore.CustomObjects.ContainsKey(this.guid))
{
//ModCore.log("Update item with guid: " + this.guid);
ModCore.CustomItems[this.guid] = this;
}
else
{
//ModCore.log("Add in new guid: " + this.guid);
ModCore.CustomItems.Add(this.guid, this);
}
}
}
public PickaxeExtended()
{
}
public PickaxeExtended(BasicItemInformation ItemInfo,int UpgradeLevel, Texture2DExtended WorkingTexture)
{
this.info = ItemInfo;
this.upgradeLevel.Value = UpgradeLevel;
this.guid = Guid.NewGuid();
this.workingTexture = WorkingTexture;
this.updateInfo();
}
public override void draw(SpriteBatch b)
{
if (this.lastUser == null || this.lastUser.toolPower <= 0 || !this.lastUser.canReleaseTool)
return;
this.updateInfo();
foreach (Vector2 vector2 in this.tilesAffected(this.lastUser.GetToolLocation(false) / 64f, this.lastUser.toolPower, this.lastUser))
this.info.animationManager.draw(b, Game1.GlobalToLocal(new Vector2((float)((int)vector2.X * 64), (float)((int)vector2.Y * 64))), Color.White, 4f, SpriteEffects.None, 0.01f);
}
public override void drawAttachments(SpriteBatch b, int x, int y)
{
this.updateInfo();
//base.drawAttachments(b, x, y);
//this.info.animationManager.draw(b,)
}
public override void drawInMenu(SpriteBatch spriteBatch, Vector2 location, float scaleSize, float transparency, float layerDepth, bool drawStackNumber, Color color, bool drawShadow)
{
this.updateInfo();
this.info.animationManager.draw(spriteBatch, location, color*transparency, 4f * scaleSize, SpriteEffects.None, layerDepth);
//base.drawInMenu(spriteBatch, location, scaleSize, transparency, layerDepth, drawStackNumber, color, drawShadow);
}
public Dictionary<string, string> getAdditionalSaveData()
{
Dictionary<string, string> serializedInfo = new Dictionary<string, string>();
serializedInfo.Add("id", this.ItemInfo);
serializedInfo.Add("ItemInfo", Revitalize.ModCore.Serializer.ToJSONString(this.info));
Revitalize.ModCore.Serializer.SerializeGUID(this.guid.ToString(), this);
return serializedInfo;
}
public override bool beginUsing(GameLocation location, int x, int y, Farmer who)
{
this.updateInfo();
Revitalize.Framework.Hacks.ColorChanger.SwapPickaxeTextures(this.workingTexture.texture);
return base.beginUsing(location, x, y, who);
}
public override void endUsing(GameLocation location, Farmer who)
{
//Revitalize.Framework.Hacks.ColorChanger.ResetPickaxeTexture();
base.endUsing(location, who);
}
public override bool onRelease(GameLocation location, int x, int y, Farmer who)
{
//Revitalize.Framework.Hacks.ColorChanger.ResetPickaxeTexture();
return base.onRelease(location, x, y, who);
}
public override void actionWhenStopBeingHeld(Farmer who)
{
Revitalize.Framework.Hacks.ColorChanger.ResetPickaxeTexture();
base.actionWhenStopBeingHeld(who);
}
public override Color getCategoryColor()
{
return this.info.categoryColor;
}
public override string getCategoryName()
{
return this.info.categoryName;
}
public override string getDescription()
{
return this.info.name;
}
public override Item getOne()
{
return new PickaxeExtended(this.info.Copy(), this.UpgradeLevel,this.workingTexture.Copy());
}
public object getReplacement()
{
return new StardewValley.Tools.Pickaxe { UpgradeLevel = this.UpgradeLevel };
}
public void rebuild(Dictionary<string, string> additionalSaveData, object replacement)
{
this.info = ModCore.Serializer.DeserializeFromJSONString<BasicItemInformation>(additionalSaveData["ItemInfo"]);
this.upgradeLevel.Value = (replacement as Pickaxe).UpgradeLevel;
}
/// <summary>
/// Updates the info on the item.
/// </summary>
public virtual void updateInfo()
{
if (this.info == null || this.workingTexture==null)
{
this.ItemInfo = this.text;
ModCore.log("Updated item info!");
return;
}
if (this.requiresUpdate())
{
this.text = this.ItemInfo;
this.info.cleanAfterUpdate();
MultiplayerUtilities.RequestUpdateSync(this.guid);
}
}
/// <summary>
/// Gets an update for this item.
/// </summary>
public virtual void getUpdate()
{
this.ItemInfo = this.text;
}
/// <summary>
/// Checks to see if this item requires a sync update.
/// </summary>
/// <returns></returns>
public virtual bool requiresUpdate()
{
if (this.info.requiresSyncUpdate())
{
return true;
}
else
{
return false;
}
}
}
}

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Revitalize.Framework.Objects.Furniture;
using Revitalize.Framework.Objects.Interfaces;
using StardewModdingAPI;
using StardewValley;
@ -53,6 +54,8 @@ namespace Revitalize.Framework.Objects
public Dictionary<string, CustomObject> ItemsByName;
public Dictionary<string, StardewValley.Tool> Tools;
/// <summary>
/// Constructor.
/// </summary>
@ -88,7 +91,7 @@ namespace Revitalize.Framework.Objects
this.resources = new ResourceManager();
this.ItemsByName = new Dictionary<string, CustomObject>();
ChairMultiTiledObject s = new ChairMultiTiledObject();
this.Tools = new Dictionary<string, Tool>();
}
/// <summary>
@ -252,6 +255,17 @@ namespace Revitalize.Framework.Objects
}
}
/// <summary>
/// Gets a tool from the list of managed tools.
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
public Item GetTool(string Name)
{
if (this.Tools.ContainsKey("Name")) return this.Tools[Name].getOne();
else return null;
}
/// <summary>
/// Adds a new object manager to the master pool of managers.
/// </summary>
@ -365,6 +379,14 @@ namespace Revitalize.Framework.Objects
return I;
}
}
foreach(var v in this.Tools)
{
if (v.Value.GetType() == T && (v.Value as IItemInfo).Info.id == ID)
{
Item I = v.Value.getOne();
return I;
}
}
return null;
}

View File

@ -229,13 +229,13 @@ namespace Revitalize.Framework.Objects.Resources.OreVeins
if (this.location != null)
{
this.location.playSound("hammer");
ModCore.log("Ore has this much health left and location is not null: "+this.healthValue);
//ModCore.log("Ore has this much health left and location is not null: "+this.healthValue);
this.info.shakeTimer = 200;
}
else
{
Game1.player.currentLocation.playSound("hammer");
ModCore.log("Ore has this much health left and location is null!: "+this.healthValue);
//ModCore.log("Ore has this much health left and location is null!: "+this.healthValue);
this.info.shakeTimer = 200;
}
return false;

View File

@ -29,6 +29,8 @@ using System.Linq;
using StardustCore.UIUtilities.MenuComponents.ComponentsV2.Buttons;
using Revitalize.Framework.Menus;
using Revitalize.Framework.Objects.CraftingTables;
using Revitalize.Framework.Objects.Items.Tools;
using StardewValley.Tools;
namespace Revitalize
{
@ -228,6 +230,7 @@ namespace Revitalize
public static VanillaRecipeBook VanillaRecipeBook;
public static Dictionary<Guid, CustomObject> CustomObjects;
public static Dictionary<Guid, Item> CustomItems;
public static ConfigManager Configs;
public override void Entry(IModHelper helper)
@ -242,6 +245,7 @@ namespace Revitalize
Serializer = new Serializer();
playerInfo = new PlayerInfo();
CustomObjects = new Dictionary<Guid, CustomObject>();
CustomItems = new Dictionary<Guid, Item>();
//Loads in textures to be used by the mod.
this.loadInTextures();
@ -271,6 +275,7 @@ namespace Revitalize
//ModHelper.Events.Display.Rendered += MenuHacks.EndOfDay_OnMenuChanged;
//ModHelper.Events.GameLoop.Saved += MenuHacks.EndOfDay_CleanupForNewDay;
ModHelper.Events.Multiplayer.ModMessageReceived += MultiplayerUtilities.GetModMessage;
ModHelper.Events.Input.ButtonPressed += ObjectInteractionHacks.ResetNormalToolsColorOnLeftClick;
//Adds in recipes to the mod.
VanillaRecipeBook = new VanillaRecipeBook();
@ -280,6 +285,8 @@ namespace Revitalize
}
/// <summary>
/// Loads in textures to be used by the mod.
/// </summary>
@ -293,6 +300,9 @@ namespace Revitalize
TextureManager.GetTextureManager(Manifest, "Resources.Ore").searchForTextures(ModHelper, this.ModManifest, Path.Combine("Content", "Graphics", "Objects", "Resources", "Ore"));
TextureManager.AddTextureManager(Manifest, "Items.Resources.Ore");
TextureManager.GetTextureManager(Manifest, "Items.Resources.Ore").searchForTextures(ModHelper, this.ModManifest, Path.Combine("Content", "Graphics", "Items", "Resources", "Ore"));
TextureManager.AddTextureManager(Manifest, "Tools");
TextureManager.GetTextureManager(Manifest, "Tools").searchForTextures(ModHelper, this.ModManifest, Path.Combine("Content", "Graphics", "Items", "Tools"));
TextureManager.AddTextureManager(Manifest, "Menus");
TextureManager.GetTextureManager(Manifest, "Menus").searchForTextures(ModHelper, this.ModManifest, Path.Combine("Content", "Graphics", "Menus", "Misc"));
TextureManager.AddTextureManager(Manifest, "CraftingMenu");
@ -300,6 +310,8 @@ namespace Revitalize
TextureManager.AddTextureManager(Manifest, "Objects.Crafting");
TextureManager.GetTextureManager(Manifest, "Objects.Crafting").searchForTextures(ModHelper, this.ModManifest, Path.Combine("Content", "Graphics", "Objects", "Crafting"));
}
private void Input_ButtonPressed(object sender, StardewModdingAPI.Events.ButtonPressedEventArgs e)
@ -548,6 +560,13 @@ namespace Revitalize
*/
Game1.player.addItemToInventory(new StardewValley.Object((int)Enums.SDVObject.Coal, 1));
Game1.player.addItemByMenuIfNecessary(ModCore.ObjectManager.GetItem("SteelIngot", 20));
PickaxeExtended pick = new PickaxeExtended(new BasicItemInformation("My First Pickaxe", "Omegasis.Revitalize.Items.Tools.MyFirstPickaxe", "A testing pickaxe. Does it work?", "Tool", Color.SlateGray, 0, 0, false, 500, false, false, TextureManager.GetTexture(Manifest, "Tools", "Pickaxe"), new AnimationManager(TextureManager.GetExtendedTexture(Manifest, "Tools", "Pickaxe"), new Animation(0, 0, 16, 16)), Color.White, true, null, null),2,TextureManager.GetExtendedTexture(Manifest,"Tools","TestingPickaxeWorking"));
Game1.player.addItemsByMenuIfNecessary(new List<Item>()
{
pick,
new StardewValley.Object((int)Enums.SDVObject.Wood,100)
});
}
/*

View File

@ -20,6 +20,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -74,6 +75,7 @@
<Compile Include="Framework\Factories\Objects\FactoryInfo.cs" />
<Compile Include="Framework\Factories\Objects\Furniture\TableFactoryInfo.cs" />
<Compile Include="Framework\Factories\Objects\Resources\OreFactoryInfo.cs" />
<Compile Include="Framework\Hacks\ColorChanger.cs" />
<Compile Include="Framework\Hacks\MenuHacks.cs" />
<Compile Include="Framework\Hacks\ObjectInteractionHacks.cs" />
<Compile Include="Framework\Hacks\ShopHacks.cs" />
@ -141,7 +143,9 @@
<Compile Include="Framework\Objects\InformationFiles\Furniture\ChairInformation.cs" />
<Compile Include="Framework\Objects\InformationFiles\Furniture\TableInformation.cs" />
<Compile Include="Framework\Objects\InformationFiles\ResourceInformaton.cs" />
<Compile Include="Framework\Objects\Interfaces\IItemInfo.cs" />
<Compile Include="Framework\Objects\Items\Resources\Ore.cs" />
<Compile Include="Framework\Objects\Items\Tools\PickaxeExtended.cs" />
<Compile Include="Framework\Objects\Machines\Machine.cs" />
<Compile Include="Framework\Objects\MultiTiledComponent.cs" />
<Compile Include="Framework\Objects\MultiTiledObject.cs" />
@ -235,6 +239,15 @@
<Content Include="Content\Graphics\Items\Resources\Ore\TitaniumOre.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Content\Graphics\Items\Tools\DefaultPickaxeWorking.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Content\Graphics\Items\Tools\Pickaxe.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Content\Graphics\Items\Tools\TestingPickaxeWorking.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Content\Graphics\Menus\CraftingMenu\CraftButton.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>