/
ItemHeatHandler.java
134 lines (116 loc) · 4.07 KB
/
ItemHeatHandler.java
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
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/
package net.dries007.tfc.api.capability.heat;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.dries007.tfc.world.classic.CalenderTFC;
/**
* This is an implementation of ItemHeat that automatically cools down over time
* Prefer extending or using this than implementing IItemHeat directly
* Exceptions if you want to extend another capability object (see SmallVessel) but you should still implement this functionality somewhere
*/
public class ItemHeatHandler implements ICapabilitySerializable<NBTTagCompound>, IItemHeat
{
// These are "constants". Some implementations will want to change these based on other factors. (See ItemMold)
protected float heatCapacity;
protected float meltTemp;
// These are the values from last point of update. They are updated when read from NBT, or when the temperature is set manually.
// Note that if temperature is == 0, lastUpdateTick should set itself to -1 to keep their capabilities compatible - i.e. stackable
protected float temperature;
protected long lastUpdateTick;
/**
* Default ItemHeatHandler implementation
*
* @param nbt The NBT of the itemstack. (Provided in Item#initCapabilities())
* @param heatCapacity The heat capacity
* @param meltTemp The melting point
*/
public ItemHeatHandler(@Nullable NBTTagCompound nbt, float heatCapacity, float meltTemp)
{
this.heatCapacity = heatCapacity;
this.meltTemp = meltTemp;
deserializeNBT(nbt);
}
public ItemHeatHandler() {} // This is here so you can do a custom implementation
/**
* This gets the outwards facing temperature. It will differ from the internal temperature value or the value saved to NBT
* Note: if checking the temperature internally, DO NOT use temperature, use this instead, as temperature does not represent the current temperature
*
* @return The current temperature
*/
@Override
public float getTemperature()
{
return CapabilityItemHeat.adjustTemp(temperature, heatCapacity, CalenderTFC.getTotalTime() - lastUpdateTick);
}
/**
* Update the temperature, and save the timestamp of when it was updated
*
* @param temperature the temperature to set. Between 0 - 1600
*/
@Override
public void setTemperature(float temperature)
{
this.temperature = temperature;
this.lastUpdateTick = CalenderTFC.getTotalTime();
}
@Override
public float getHeatCapacity()
{
return heatCapacity;
}
@Override
public float getMeltTemp()
{
return meltTemp;
}
@Override
public boolean isMolten()
{
return getTemperature() >= meltTemp;
}
@Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing)
{
return capability == CapabilityItemHeat.ITEM_HEAT_CAPABILITY;
}
@Nullable
@Override
@SuppressWarnings("unchecked")
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing)
{
return hasCapability(capability, facing) ? (T) this : null;
}
@Override
@Nonnull
public NBTTagCompound serializeNBT()
{
NBTTagCompound nbt = new NBTTagCompound();
float temp = getTemperature();
nbt.setFloat("heat", temp);
if (temp <= 0)
{
nbt.setLong("ticks", -1);
}
else
{
nbt.setLong("ticks", CalenderTFC.getTotalTime());
}
return nbt;
}
@Override
public void deserializeNBT(@Nullable NBTTagCompound nbt)
{
if (nbt != null)
{
temperature = nbt.getFloat("heat");
lastUpdateTick = nbt.getLong("ticks");
}
}
}