-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathAbstractMaterialPartBehavior.java
More file actions
101 lines (85 loc) · 4.06 KB
/
AbstractMaterialPartBehavior.java
File metadata and controls
101 lines (85 loc) · 4.06 KB
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
package gregtech.common.items.behaviors;
import gregtech.api.items.metaitem.stats.IItemBehaviour;
import gregtech.api.items.metaitem.stats.IItemColorProvider;
import gregtech.api.items.metaitem.stats.IItemDurabilityManager;
import gregtech.api.items.metaitem.stats.IItemNameProvider;
import gregtech.api.unification.material.Materials;
import gregtech.api.unification.material.type.IngotMaterial;
import gregtech.api.unification.material.type.Material;
import gregtech.api.util.LocalisationUtils;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.List;
public abstract class AbstractMaterialPartBehavior implements IItemBehaviour, IItemDurabilityManager, IItemColorProvider, IItemNameProvider {
protected NBTTagCompound getPartStatsTag(ItemStack itemStack) {
return itemStack.getSubCompound("GT.PartStats");
}
protected NBTTagCompound getOrCreatePartStatsTag(ItemStack itemStack) {
return itemStack.getOrCreateSubCompound("GT.PartStats");
}
public IngotMaterial getPartMaterial(ItemStack itemStack) {
NBTTagCompound compound = getPartStatsTag(itemStack);
IngotMaterial defaultMaterial = Materials.Darmstadtium;
if (compound == null || !compound.hasKey("Material", NBT.TAG_STRING)) {
return defaultMaterial;
}
String materialName = compound.getString("Material");
Material material = Material.MATERIAL_REGISTRY.getObject(materialName);
if (!(material instanceof IngotMaterial)) {
return defaultMaterial;
}
return (IngotMaterial) material;
}
public void setPartMaterial(ItemStack itemStack, IngotMaterial material) {
NBTTagCompound compound = getOrCreatePartStatsTag(itemStack);
compound.setString("Material", material.toString());
}
public abstract int getPartMaxDurability(ItemStack itemStack);
public int getPartDamage(ItemStack itemStack) {
NBTTagCompound compound = getPartStatsTag(itemStack);
if (compound == null || !compound.hasKey("Damage", NBT.TAG_ANY_NUMERIC)) {
return 0;
}
return compound.getInteger("Damage");
}
public void setPartDamage(ItemStack itemStack, int rotorDamage) {
NBTTagCompound compound = getOrCreatePartStatsTag(itemStack);
compound.setInteger("Damage", Math.min(getPartMaxDurability(itemStack), rotorDamage));
}
@Override
@SuppressWarnings("deprecation")
public String getItemStackDisplayName(ItemStack itemStack, String unlocalizedName) {
IngotMaterial material = getPartMaterial(itemStack);
return LocalisationUtils.format(unlocalizedName, material.getLocalizedName());
}
@Override
public void addInformation(ItemStack stack, List<String> lines) {
IngotMaterial material = getPartMaterial(stack);
int maxRotorDurability = getPartMaxDurability(stack);
int rotorDamage = getPartDamage(stack);
lines.add(I18n.format("metaitem.tool.tooltip.durability", maxRotorDurability - rotorDamage, maxRotorDurability));
lines.add(I18n.format("metaitem.tool.tooltip.primary_material", material.getLocalizedName(), material.harvestLevel));
}
@Override
public int getItemStackColor(ItemStack itemStack, int tintIndex) {
IngotMaterial material = getPartMaterial(itemStack);
return material.materialRGB;
}
@Override
public boolean showsDurabilityBar(ItemStack itemStack) {
return getPartDamage(itemStack) > 0;
}
@Override
public double getDurabilityForDisplay(ItemStack itemStack) {
return getPartDamage(itemStack) / (getPartMaxDurability(itemStack) * 1.0);
}
@Override
public int getRGBDurabilityForDisplay(ItemStack itemStack) {
return MathHelper.hsvToRGB((1.0f - (float) getDurabilityForDisplay(itemStack)) / 3.0f, 1.0f, 1.0f);
}
}