下面列出了怎么用net.minecraftforge.common.util.Constants.NBT的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
if (compound.hasKey("PipeBlock", NBT.TAG_STRING)) {
Block block = Block.REGISTRY.getObject(new ResourceLocation(compound.getString("PipeBlock")));
//noinspection unchecked
this.pipeBlock = block instanceof BlockPipe ? (BlockPipe<PipeType, NodeDataType, ?>) block : null;
}
this.pipeType = getPipeTypeClass().getEnumConstants()[compound.getInteger("PipeType")];
NBTTagCompound blockedConnectionsTag = compound.getCompoundTag("BlockedConnectionsMap");
this.blockedConnectionsMap.clear();
for(String attachmentTypeKey : blockedConnectionsTag.getKeySet()) {
int attachmentType = Integer.parseInt(attachmentTypeKey);
int blockedConnections = blockedConnectionsTag.getInteger(attachmentTypeKey);
this.blockedConnectionsMap.put(attachmentType, blockedConnections);
}
recomputeBlockedConnections();
this.insulationColor = compound.getInteger("InsulationColor");
this.coverableImplementation.readFromNBT(compound);
}
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
if (data.hasKey("ControllerPos")) {
this.controllerPos = NBTUtil.getPosFromTag(data.getCompoundTag("ControllerPos"));
} else {
NBTTagList connectedTanks = data.getTagList("ConnectedTanks", NBT.TAG_COMPOUND);
connectedTanks.forEach(pos -> this.connectedTanks.add(NBTUtil.getPosFromTag((NBTTagCompound) pos)));
recomputeTankSizeNow(true);
this.multiblockFluidTank.readFromNBT(data.getCompoundTag("FluidInventory"));
this.lastSentFluidStack = this.multiblockFluidTank.getFluid();
if (data.hasKey("MultiblockSize")) {
this.multiblockSize = NBTUtil.getPosFromTag(data.getCompoundTag("MultiblockSize"));
}
}
}
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
this.isActive = data.getBoolean("Active");
this.wasActiveAndNeedUpdate = data.getBoolean("WasActive");
this.fuelUnitsLeft = data.getFloat("FuelUnitsLeft");
this.maxProgressDuration = data.getInteger("MaxProgress");
if (maxProgressDuration > 0) {
this.currentProgress = data.getInteger("Progress");
NBTTagList itemOutputs = data.getTagList("Outputs", NBT.TAG_COMPOUND);
this.outputsList = NonNullList.create();
for (int i = 0; i < itemOutputs.tagCount(); i++) {
this.outputsList.add(new ItemStack(itemOutputs.getCompoundTagAt(i)));
}
}
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
destinationId = nbt.getInteger("destDimId");
landingPos.x = nbt.getFloat("landingx");
landingPos.y = nbt.getFloat("landingy");
landingPos.z = nbt.getFloat("landingz");
NBTTagList stationList = nbt.getTagList("stationMapping", NBT.TAG_COMPOUND);
for(int i = 0; i < stationList.tagCount(); i++) {
NBTTagCompound tag = stationList.getCompoundTagAt(i);
int pos[];
pos = tag.getIntArray("pos");
int id = tag.getInteger("id");
landingLoc.put(id, new HashedBlockPosition(pos[0], pos[1], pos[2]));
}
}
public ItemStack removeComponent(World world, ItemStack armor, int index) {
NBTTagCompound nbt;
NBTTagList componentList;
if(armor.hasTagCompound()) {
nbt = armor.getTagCompound();
componentList = nbt.getTagList(componentNBTName, NBT.TAG_COMPOUND);
}
else {
return ItemStack.EMPTY;
}
EmbeddedInventory inv = loadEmbeddedInventory(armor);
ItemStack stack = inv.getStackInSlot(index);
inv.setInventorySlotContents(index, ItemStack.EMPTY);
if(!stack.isEmpty()) {
IArmorComponent component = (IArmorComponent) stack.getItem();
component.onComponentRemoved(world, armor);
saveEmbeddedInventory(armor, inv);
}
return stack;
}
public void readFromNBT(NBTTagCompound nbt) {
id = nbt.getInteger("id");
temperature = nbt.getInteger("temperature");
name = nbt.getString("name");
posX = nbt.getShort("posX");
posZ = nbt.getShort("posZ");
if(nbt.hasKey("size"))
size = nbt.getFloat("size");
if(nbt.hasKey("seperation"))
starSeperation = nbt.getFloat("seperation");
subStars.clear();
if(nbt.hasKey("subStars")) {
NBTTagList list = nbt.getTagList("subStars", NBT.TAG_COMPOUND);
for(int i = 0; i < list.tagCount(); i++) {
StellarBody star = new StellarBody();
star.readFromNBT(list.getCompoundTagAt(i));
subStars.add(star);
}
}
}
@Override
public void readFromNbt(NBTTagCompound nbt) {
super.readFromNbt(nbt);
NBTTagList list = nbt.getTagList("composition", NBT.TAG_COMPOUND);
compositionMapping.clear();
for(int i = 0; i < list.tagCount(); i++) {
NBTTagCompound tag = list.getCompoundTagAt(i);
int blockId = tag.getInteger("id");
int rarity = tag.getInteger("amt");
compositionMapping.put(Block.getBlockById(blockId), rarity);
}
numberOfBlocks = nbt.getInteger("numBlocks");
uuid = nbt.getLong("uuid");
data.readFromNBT(nbt);
}
public void readFromNBT(NBTTagCompound data) {
NBTTagList coversList = data.getTagList("Covers", NBT.TAG_COMPOUND);
for (int index = 0; index < coversList.tagCount(); index++) {
NBTTagCompound tagCompound = coversList.getCompoundTagAt(index);
if (tagCompound.hasKey("CoverId", NBT.TAG_STRING)) {
EnumFacing coverSide = EnumFacing.VALUES[tagCompound.getByte("Side")];
ResourceLocation coverId = new ResourceLocation(tagCompound.getString("CoverId"));
CoverDefinition coverDefinition = CoverDefinition.getCoverById(coverId);
CoverBehavior coverBehavior = coverDefinition.createCoverBehavior(this, coverSide);
coverBehavior.readFromNBT(tagCompound);
this.coverBehaviors[coverSide.getIndex()] = coverBehavior;
}
}
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
this.pipeNets = new ArrayList<>();
NBTTagList allEnergyNets = nbt.getTagList("PipeNets", NBT.TAG_COMPOUND);
for (int i = 0; i < allEnergyNets.tagCount(); i++) {
NBTTagCompound pNetTag = allEnergyNets.getCompoundTagAt(i);
T pipeNet = createNetInstance();
pipeNet.deserializeNBT(pNetTag);
addPipeNetSilently(pipeNet);
}
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
NBTTagCompound updateTag = pkt.getNbtCompound();
NBTTagList tagList = updateTag.getTagList("d", NBT.TAG_COMPOUND);
for (int i = 0; i < tagList.tagCount(); i++) {
NBTTagCompound entryTag = tagList.getCompoundTagAt(i);
int discriminator = entryTag.getInteger("i");
byte[] updateData = entryTag.getByteArray("d");
ByteBuf backedBuffer = Unpooled.copiedBuffer(updateData);
receiveCustomData(discriminator, new PacketBuffer(backedBuffer));
}
}
@SideOnly(Side.CLIENT)
public int getPaintingColorForRendering() {
if (getWorld() == null && renderContextStack != null) {
NBTTagCompound tagCompound = renderContextStack.getTagCompound();
if (tagCompound != null && tagCompound.hasKey("PaintingColor", NBT.TAG_INT)) {
return tagCompound.getInteger("PaintingColor");
}
}
return paintingColor;
}
/**
* Called from ItemBlock to initialize this MTE with data contained in ItemStack
*
* @param itemStack itemstack of itemblock
*/
public void initFromItemStackData(NBTTagCompound itemStack) {
if (itemStack.hasKey("PaintingColor", NBT.TAG_INT)) {
setPaintingColor(itemStack.getInteger("PaintingColor"));
}
if (itemStack.hasKey("Fragile")) {
setFragile(itemStack.getBoolean("Fragile"));
}
}
public void readFromNBT(NBTTagCompound data) {
this.frontFacing = EnumFacing.VALUES[data.getInteger("FrontFacing")];
this.paintingColor = data.getInteger("PaintingColor");
this.cachedLightValue = data.getInteger("CachedLightValue");
if (shouldSerializeInventories()) {
GTUtility.readItems(importItems, "ImportInventory", data);
GTUtility.readItems(exportItems, "ExportInventory", data);
importFluids.deserializeNBT(data.getCompoundTag("ImportFluidInventory"));
exportFluids.deserializeNBT(data.getCompoundTag("ExportFluidInventory"));
}
for (MTETrait mteTrait : this.mteTraits) {
NBTTagCompound traitCompound = data.getCompoundTag(mteTrait.getName());
mteTrait.deserializeNBT(traitCompound);
}
NBTTagList coversList = data.getTagList("Covers", NBT.TAG_COMPOUND);
for (int index = 0; index < coversList.tagCount(); index++) {
NBTTagCompound tagCompound = coversList.getCompoundTagAt(index);
if (tagCompound.hasKey("CoverId", NBT.TAG_STRING)) {
EnumFacing coverSide = EnumFacing.VALUES[tagCompound.getByte("Side")];
ResourceLocation coverId = new ResourceLocation(tagCompound.getString("CoverId"));
CoverDefinition coverDefinition = CoverDefinition.getCoverById(coverId);
CoverBehavior coverBehavior = coverDefinition.createCoverBehavior(this, coverSide);
coverBehavior.readFromNBT(tagCompound);
this.coverBehaviors[coverSide.getIndex()] = coverBehavior;
}
}
this.isFragile = data.getBoolean("Fragile");
}
@Override
public void deserializeNBT(NBTTagCompound compound) {
if (!compound.hasKey("WorkEnabled", NBT.TAG_BYTE)) {
//change working mode only if there is a tag compound with it's value
this.workingEnabled = compound.getBoolean("WorkEnabled");
}
this.recipeDurationLeft = compound.getInteger("RecipeDurationLeft");
if (recipeDurationLeft > 0) {
this.recipeOutputVoltage = compound.getLong("RecipeOutputVoltage");
}
this.isActive = recipeDurationLeft > 0;
}
@Override
public long getMaxCharge() {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if (tagCompound == null)
return maxCharge;
if (tagCompound.hasKey("MaxCharge", NBT.TAG_LONG))
return tagCompound.getLong("MaxCharge");
return maxCharge;
}
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
if (data.hasKey("AllowEnergyOutput", NBT.TAG_ANY_NUMERIC)) {
this.allowEnergyOutput = data.getBoolean("AllowEnergyOutput");
}
}
@Override
public void initFromItemStackData(NBTTagCompound itemStack) {
super.initFromItemStackData(itemStack);
if (itemStack.hasKey("ComponentTier", NBT.TAG_ANY_NUMERIC)) {
this.unlockComponentTier = itemStack.getInteger("ComponentTier");
}
}
public void deserializeNBT(NBTTagCompound tagCompound) {
NBTTagList resultList = tagCompound.getTagList("Memory", NBT.TAG_COMPOUND);
for (int i = 0; i < resultList.tagCount(); i++) {
NBTTagCompound entryComponent = resultList.getCompoundTagAt(i);
int slotIndex = entryComponent.getInteger("Slot");
MemorizedRecipe recipe = MemorizedRecipe.deserializeNBT(entryComponent.getCompoundTag("Recipe"));
this.memorizedRecipes[slotIndex] = recipe;
}
}
@Override
public void initFromItemStackData(NBTTagCompound itemStack) {
super.initFromItemStackData(itemStack);
if (itemStack.hasKey(FluidHandlerItemStack.FLUID_NBT_KEY, NBT.TAG_COMPOUND)) {
FluidStack fluidStack = FluidStack.loadFluidStackFromNBT(itemStack.getCompoundTag(FluidHandlerItemStack.FLUID_NBT_KEY));
this.multiblockFluidTank.fill(fluidStack, true);
}
}
@SideOnly(Side.CLIENT)
private FluidStack getFluidForRendering() {
if (getWorld() == null && renderContextStack != null) {
NBTTagCompound tagCompound = renderContextStack.getTagCompound();
if (tagCompound != null && tagCompound.hasKey("Fluid", NBT.TAG_COMPOUND)) {
return FluidStack.loadFluidStackFromNBT(tagCompound.getCompoundTag("Fluid"));
}
return null;
}
return getActualTankFluid();
}
@Override
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, @Nullable World player, List<String> tooltip, boolean advanced) {
tooltip.add(I18n.format("gregtech.universal.tooltip.fluid_storage_capacity", tankSize));
tooltip.add(I18n.format("gregtech.machine.fluid_tank.max_multiblock", maxSizeHorizontal, maxSizeVertical, maxSizeHorizontal));
NBTTagCompound tagCompound = stack.getTagCompound();
if (tagCompound != null && tagCompound.hasKey("Fluid", NBT.TAG_COMPOUND)) {
FluidStack fluidStack = FluidStack.loadFluidStackFromNBT(tagCompound.getCompoundTag("Fluid"));
if (fluidStack != null) {
tooltip.add(I18n.format("gregtech.machine.fluid_tank.fluid", fluidStack.amount, fluidStack.getLocalizedName()));
}
}
}
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
if (data.hasKey("ItemStack", NBT.TAG_COMPOUND)) {
this.itemStack = new ItemStack(data.getCompoundTag("ItemStack"));
if (!itemStack.isEmpty()) {
this.itemsStoredInside = data.getLong("ItemAmount");
}
}
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
Material material = Material.MATERIAL_REGISTRY.getObject(compound.getString("Material"));
this.material = material == null ? Materials.Aluminium : material;
for (NBTBase undergroundMaterialNBTBase : compound.getTagList("UndergroundMaterials", NBT.TAG_STRING)) {
undergroundMaterials.add(Material.MATERIAL_REGISTRY.getObject(((NBTTagString) undergroundMaterialNBTBase).getString()));
}
}
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 int getPartDamage(ItemStack itemStack) {
NBTTagCompound compound = getPartStatsTag(itemStack);
if (compound == null || !compound.hasKey("Damage", NBT.TAG_ANY_NUMERIC)) {
return 0;
}
return compound.getInteger("Damage");
}
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;
}
public void readFromNBT(NBTTagCompound nbt) {
NBTTagList list = nbt.getTagList("list", NBT.TAG_COMPOUND);
pos.clear();
for(int i = 0; i < list.tagCount(); i++) {
NBTTagCompound nbttag = list.getCompoundTagAt(i);
int[] tag = nbttag.getIntArray("loc");
int dimid = nbttag.getInteger("dim");
pos.add(new DimensionBlockPosition(dimid, new HashedBlockPosition(tag[0], tag[1], tag[2])));
}
}
public void readFromNBT(NBTTagCompound nbt) {
NBTTagList list = nbt.getTagList("spaceContents", NBT.TAG_COMPOUND);
nextId = nbt.getInteger("nextInt");
nextStationTransitionTick = nbt.getLong("nextStationTransitionTick");
for(int i = 0; i < list.tagCount(); i++) {
NBTTagCompound tag = list.getCompoundTagAt(i);
try {
ISpaceObject object = (ISpaceObject)nameToClass.get(tag.getString("type")).newInstance();
object.readFromNbt(tag);
if(tag.hasKey("expireTime")) {
long expireTime = tag.getLong("expireTime");
int numPlayers = tag.getInteger("numPlayers");
if (DimensionManager.getWorld(Configuration.spaceDimId).getTotalWorldTime() >= expireTime && numPlayers == 0)
continue;
temporaryDimensions.put(object.getId(), expireTime);
temporaryDimensionPlayerNumber.put(object.getId(), numPlayers);
}
registerSpaceObject(object, object.getOrbitingPlanetId(), object.getId() );
} catch (Exception e) {
System.out.println(tag.getString("type"));
e.printStackTrace();
}
}
}
public EnumFacing getSide(ItemStack stack) {
NBTTagCompound nbt = stack.getTagCompound();
if (nbt == null || !nbt.hasKey("dir", NBT.TAG_INT)) {
return null;
}
int val = nbt.getInteger("dir");
if (val < 0 || val >= EnumFacing.VALUES.length) {
return null;
}
return EnumFacing.VALUES[val];
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
Arrays.fill(disabled, false);
NBTTagList list = nbt.getTagList("disabled", NBT.TAG_INT);
for (int i = 0; i < list.tagCount(); i++) {
disabled[((NBTTagInt)list.get(i)).getInt()] = true;
}
}