-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyItem.cs
146 lines (106 loc) · 3.2 KB
/
MyItem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using HamstarHelpers.Classes.DataStructures;
using HamstarHelpers.Helpers.Debug;
using HamstarHelpers.Helpers.DotNET;
using HamstarHelpers.Services.EntityGroups;
using System;
using System.IO;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
namespace Starvation {
partial class StarvationItem : GlobalItem {
public static bool CanSpoil( Item item ) {
var mymod = StarvationMod.Instance;
if( !mymod.Config.FoodSpoilageEnabled ) {
return false;
}
if( item.buffType == BuffID.WellFed && item.buffTime > 0 ) {
return true;
}
if( mymod.Config.FoodIngredientsAlsoSpoil ) {
IReadOnlySet<int> ingredients;
if( EntityGroups.TryGetItemGroup("Any Food Ingredient", out ingredients) && ingredients.Contains( item.type ) ) {
switch( item.type ) {
case ItemID.Pumpkin:
case ItemID.BlinkrootSeeds:
case ItemID.Hay:
case ItemID.Mushroom:
case ItemID.Bowl:
break;
default:
return true;
}
}
}
return false;
}
////////////////
public long TimestampInSeconds;
////////////////
public override bool InstancePerEntity => true;
////////////////
public override GlobalItem Clone( Item item, Item itemClone ) {
var clone = (StarvationItem)base.Clone( item, itemClone );
if( this.NeedsSaving( item ) ) {
clone.TimestampInSeconds = this.TimestampInSeconds;
}
return clone;
}
public override bool NeedsSaving( Item item ) {
return StarvationItem.CanSpoil( item );
}
public override void Load( Item item, TagCompound tags ) {
if( this.NeedsSaving( item ) ) {
this.TimestampInSeconds = SystemHelpers.TimeStampInSeconds();
if( tags.ContainsKey( "duration" ) ) {
this.TimestampInSeconds -= tags.GetInt( "duration" );
}
}
}
public override TagCompound Save( Item item ) {
if( this.NeedsSaving( item ) ) {
return new TagCompound {
{ "duration", (int)(SystemHelpers.TimeStampInSeconds() - this.TimestampInSeconds) }
};
}
return new TagCompound();
}
////
public override void NetReceive( Item item, BinaryReader reader ) {
if( this.NeedsSaving( item ) ) {
this.TimestampInSeconds = SystemHelpers.TimeStampInSeconds() - reader.ReadInt32();
}
}
public override void NetSend( Item item, BinaryWriter writer ) {
if( this.NeedsSaving( item ) ) {
writer.Write( (Int32)SystemHelpers.TimeStampInSeconds() - this.TimestampInSeconds );
}
}
////////////////
public override void SetDefaults( Item item ) {
if( this.NeedsSaving( item ) ) {
item.maxStack = 1;
if( item.buffType == BuffID.WellFed ) {
this.ApplyWellFedModifiers( item );
}
}
}
////////////////
public override void Update( Item item, ref float gravity, ref float maxFallSpeed ) {
this.ResetTimestampAndMaxStackSize( item );
}
public override void UpdateInventory( Item item, Player player ) {
this.ResetTimestampAndMaxStackSize( item );
}
////////////////
public void ResetTimestampAndMaxStackSize( Item item ) {
if( this.NeedsSaving( item ) ) {
item.maxStack = 1;
if( this.TimestampInSeconds == 0 ) {
this.TimestampInSeconds = SystemHelpers.TimeStampInSeconds();
}
}
}
}
}