-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathFacadeItem.java
More file actions
85 lines (76 loc) · 3.39 KB
/
FacadeItem.java
File metadata and controls
85 lines (76 loc) · 3.39 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
package gregtech.common.items.behaviors;
import com.google.common.collect.ImmutableList;
import gregtech.api.items.metaitem.stats.IItemNameProvider;
import gregtech.api.items.metaitem.stats.ISubItemHandler;
import gregtech.api.util.LocalisationUtils;
import gregtech.common.ConfigHolder;
import gregtech.common.covers.facade.FacadeHelper;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.Constants.NBT;
import java.util.List;
import java.util.Objects;
public class FacadeItem implements IItemNameProvider, ISubItemHandler {
@Override
@SuppressWarnings("deprecation")
public String getItemStackDisplayName(ItemStack itemStack, String unlocalizedName) {
ItemStack facadeStack = getFacadeStack(itemStack);
String name = facadeStack.getItem().getItemStackDisplayName(facadeStack);
return LocalisationUtils.format(unlocalizedName, name);
}
@Override
public void getSubItems(ItemStack itemStack, CreativeTabs creativeTab, NonNullList<ItemStack> subItems) {
List<ItemStack> validFacades;
if (creativeTab == CreativeTabs.SEARCH && !ConfigHolder.hideFacadesInJEI) {
validFacades = FacadeHelper.getValidFacadeItems();
} else {
validFacades = ImmutableList.of(new ItemStack(Blocks.STONE));
}
for (ItemStack facadeStack : validFacades) {
ItemStack resultStack = itemStack.copy();
setFacadeStack(resultStack, facadeStack);
subItems.add(resultStack);
}
}
@Override
public String getItemSubType(ItemStack itemStack) {
ItemStack facadeStack = getFacadeStack(itemStack);
ResourceLocation registryName = Objects.requireNonNull(facadeStack.getItem().getRegistryName());
return String.format("%s:%s@%d", registryName.getNamespace(), registryName.getPath(), Items.FEATHER.getDamage(facadeStack));
}
public static void setFacadeStack(ItemStack itemStack, ItemStack facadeStack) {
facadeStack = facadeStack.copy();
facadeStack.setCount(1);
if (!FacadeHelper.isValidFacade(facadeStack)) {
facadeStack = new ItemStack(Blocks.STONE);
}
if (!itemStack.hasTagCompound()) {
itemStack.setTagCompound(new NBTTagCompound());
}
NBTTagCompound tagCompound = Objects.requireNonNull(itemStack.getTagCompound());
tagCompound.setTag("Facade", facadeStack.writeToNBT(new NBTTagCompound()));
}
public static ItemStack getFacadeStack(ItemStack itemStack) {
ItemStack unsafeStack = getFacadeStackUnsafe(itemStack);
if (unsafeStack == null) {
return new ItemStack(Blocks.STONE);
}
return unsafeStack;
}
private static ItemStack getFacadeStackUnsafe(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if (tagCompound == null || !tagCompound.hasKey("Facade", NBT.TAG_COMPOUND)) {
return null;
}
ItemStack facadeStack = new ItemStack(tagCompound.getCompoundTag("Facade"));
if (facadeStack.isEmpty() || !FacadeHelper.isValidFacade(facadeStack)) {
return null;
}
return facadeStack;
}
}