-
Notifications
You must be signed in to change notification settings - Fork 497
/
ItemBlueprint.java
148 lines (123 loc) · 4.88 KB
/
ItemBlueprint.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/** Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team http://www.mod-buildcraft.com
* <p/>
* BuildCraft is distributed under the terms of the Minecraft Mod Public License 1.0, or MMPL. Please check the contents
* of the license located in http://www.mod-buildcraft.com/MMPL-1.0.txt */
package buildcraft.builders;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.blueprints.BuildingPermission;
import buildcraft.api.enums.EnumBlueprintType;
import buildcraft.api.items.IBlueprintItem;
import buildcraft.core.BCCreativeTab;
import buildcraft.core.blueprints.Blueprint;
import buildcraft.core.blueprints.BlueprintBase;
import buildcraft.core.blueprints.LibraryId;
import buildcraft.core.blueprints.Template;
import buildcraft.core.lib.items.ItemBuildCraft;
import buildcraft.core.lib.utils.BCStringUtils;
import buildcraft.core.lib.utils.ModelHelper;
import buildcraft.core.lib.utils.NBTUtils;
public abstract class ItemBlueprint extends ItemBuildCraft implements IBlueprintItem {
public ItemBlueprint() {
super(BCCreativeTab.get("main"));
}
@Override
public String getName(ItemStack stack) {
return NBTUtils.getItemData(stack).getString("name");
}
@Override
public boolean setName(ItemStack stack, String name) {
NBTUtils.getItemData(stack).setString("name", name);
return true;
}
// @Override
// @SideOnly(Side.CLIENT)
// public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
// if (world.isRemote) {
// // BlueprintBase bpt = loadBlueprint(stack);
// // if (bpt != null) {
// // openGui(bpt);
// // }
// }
// return stack;
// }
@SideOnly(Side.CLIENT)
protected abstract void openGui(BlueprintBase bpt);
@Override
public void addInformation(ItemStack stack, EntityPlayer player, List<String> list, boolean advanced) {
if (NBTUtils.getItemData(stack).hasKey("name")) {
String name = NBTUtils.getItemData(stack).getString("name");
if ("".equals(name)) {
list.add(BCStringUtils.localize("item.blueprint.unnamed"));
} else {
list.add(name);
}
list.add(BCStringUtils.localize("item.blueprint.author") + " " + NBTUtils.getItemData(stack).getString("author"));
} else {
list.add(BCStringUtils.localize("item.blueprint.blank"));
}
if (NBTUtils.getItemData(stack).hasKey("permission")) {
BuildingPermission p = BuildingPermission.values()[NBTUtils.getItemData(stack).getByte("permission")];
if (p == BuildingPermission.CREATIVE_ONLY) {
list.add(BCStringUtils.localize("item.blueprint.creative_only"));
} else if (p == BuildingPermission.NONE) {
list.add(BCStringUtils.localize("item.blueprint.no_build"));
}
}
if (NBTUtils.getItemData(stack).hasKey("isComplete")) {
boolean isComplete = NBTUtils.getItemData(stack).getBoolean("isComplete");
if (!isComplete) {
list.add(BCStringUtils.localize("item.blueprint.incomplete"));
}
}
}
@Override
public int getItemStackLimit(ItemStack stack) {
return NBTUtils.getItemData(stack).hasKey("name") ? 1 : 16;
}
public static boolean isContentReadable(ItemStack stack) {
return getId(stack) != null;
}
public static LibraryId getId(ItemStack stack) {
NBTTagCompound nbt = NBTUtils.getItemData(stack);
if (nbt == null) {
return null;
}
LibraryId id = new LibraryId();
id.read(nbt);
if (BuildCraftBuilders.serverDB.exists(id)) {
return id;
} else {
return null;
}
}
public static BlueprintBase loadBlueprint(ItemStack stack) {
if (stack == null || stack.getItem() == null || !(stack.getItem() instanceof IBlueprintItem)) {
return null;
}
LibraryId id = getId(stack);
if (id == null) {
return null;
}
NBTTagCompound nbt = BuildCraftBuilders.serverDB.load(id);
BlueprintBase base;
if (((IBlueprintItem) stack.getItem()).getType(stack) == EnumBlueprintType.TEMPLATE) {
base = new Template();
} else {
base = new Blueprint();
}
base.readFromNBT(nbt);
base.id = id;
return base;
}
@Override
public void registerModels() {
ModelHelper.registerItemModel(this, 0, "buildcraftbuilders:", textureName + "_clean");
ModelHelper.registerItemModel(this, 1, "buildcraftbuilders:", textureName + "_used");
}
}