@Override public String apply(ItemStack itemStack) { NBTTagCompound nbtTagCompound = itemStack.getTagCompound(); if (nbtTagCompound == null || nbtTagCompound.isEmpty()) { return ISubtypeInterpreter.NONE; } return nbtTagCompound.toString(); } }
@Nullable public static String getNBTHash(@Nullable NBTTagCompound tag) { if (tag == null || tag.isEmpty()) return null; try { MessageDigest digest = MessageDigest.getInstance("MD5"); OutputStream dump = new NullOutputStream(); DigestOutputStream hasher = new DigestOutputStream(dump, digest); DataOutput output = new DataOutputStream(hasher); CompressedStreamTools.write(tag, output); byte[] hash = digest.digest(); return new String(Hex.encodeHex(hash)); } catch (NoSuchAlgorithmException | IOException e) { DebugLogger.error("Cannot hash NBT", e); return null; } } }
@Nullable public static BlockPos getPosition(ItemStack itemStack) { NBTTagCompound nbtTagCompound = itemStack.getTagCompound(); if (nbtTagCompound == null || !nbtTagCompound.hasKey(POSITION_KEY)) { return null; } NBTTagCompound compound = nbtTagCompound.getCompoundTag(POSITION_KEY); if (compound.isEmpty()) { return null; } return NBTUtil.getPosFromTag(compound); }
@Nullable public static <T extends ISimpleParsable> T getInstanceFromNBT(NBTTagCompound nbt, String key, Class<T> classToGet) { NBTTagCompound rawData = nbt.getCompoundTag(key); if (!rawData.isEmpty() && rawData.hasKey("Class", Constants.NBT.TAG_STRING) && rawData.hasKey("Data", Constants.NBT.TAG_COMPOUND)) { try { Class c = Class.forName(rawData.getString("Class")); if (classToGet.isAssignableFrom(c)) { Object o = c.newInstance(); if (o instanceof ISimpleParsable) { ((ISimpleParsable) o).onLoad(rawData.getCompoundTag("Data")); return (T) o; } } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); } } return null; }
@Override public void write(JsonWriter out, ItemStack stack) throws IOException { if (stack.isEmpty()) { out.nullValue(); return; } String id = stack.getItem().getRegistryName().toString(); out.beginObject(); out.name(ID).value(id); if (stack.getItemDamage() != 0) { out.name(DAMAGE).value(stack.getItemDamage()); } if (stack.getCount() != 1) { out.name(STACK_SIZE).value(stack.getCount()); } if (stack.hasTagCompound() && !stack.getTagCompound().isEmpty()) { NBT_TAG_COMPOUND.write(out.name(NBT), stack.getTagCompound()); } out.endObject(); }
@Override public boolean isEqualNbt(@Nullable ItemStack left, @Nullable ItemStack right) { EnumActionResult validity = getResult(left, right); if (validity == EnumActionResult.FAIL || validity == EnumActionResult.SUCCESS) { return validity == EnumActionResult.SUCCESS; } if (!ItemStack.areItemStackTagsEqual(left, right)) { if (left.hasTagCompound() && !right.hasTagCompound() && left.getTagCompound().isEmpty()) { return true; } else if (!left.hasTagCompound() && right.hasTagCompound() && right.getTagCompound().isEmpty()) { return true; } else if (!left.hasTagCompound() && !right.hasTagCompound()) { return true; } return false; } return true; }
if (file1 == null || !file1.exists() || data == null || data.isEmpty()) { ModCyclic.logger.error("Data not found for " + player.getDisplayNameString());//+ ". Trying to load backup data."
/** * Quickly gets the species without loading the whole genome. */ @Nullable public static IAllele getSpeciesDirectly(ItemStack itemStack, IChromosomeType chromosomeType, boolean active) { NBTTagCompound nbtTagCompound = itemStack.getTagCompound(); if (nbtTagCompound == null) { return null; } NBTTagCompound genomeNBT = nbtTagCompound.getCompoundTag("Genome"); if (genomeNBT.isEmpty()) { return null; } NBTTagList chromosomesNBT = genomeNBT.getTagList("Chromosomes", 10); if (chromosomesNBT.isEmpty()) { return null; } NBTTagCompound chromosomeNBT = chromosomesNBT.getCompoundTagAt(0); Chromosome chromosome = Chromosome.create(null, null, chromosomeType, chromosomeNBT); return active ? chromosome.getActiveAllele() : chromosome.getInactiveAllele(); }
/** * Quickly gets the species without loading the whole genome. * We need this because the client uses the species for rendering. */ @Nullable public static IAlleleSpecies getSpeciesDirectly(ISpeciesRoot speciesRoot, ItemStack itemStack) { NBTTagCompound nbtTagCompound = itemStack.getTagCompound(); if (nbtTagCompound == null) { return null; } NBTTagCompound genomeNBT = nbtTagCompound.getCompoundTag("Genome"); if (genomeNBT.isEmpty()) { return null; } NBTTagList chromosomesNBT = genomeNBT.getTagList("Chromosomes", 10); if (chromosomesNBT.isEmpty()) { return null; } NBTTagCompound chromosomeNBT = chromosomesNBT.getCompoundTagAt(0); Chromosome chromosome = Chromosome.create(null, null, speciesRoot.getSpeciesChromosomeType(), chromosomeNBT); IAllele activeAllele = chromosome.getActiveAllele(); if (!(activeAllele instanceof IAlleleSpecies)) { return null; } return (IAlleleSpecies) activeAllele; }
public static boolean isCraftingEquivalent(ItemStack base, ItemStack comparison, boolean craftingTools) { if (base.isEmpty() || comparison.isEmpty()) { return false; } if (craftingTools && isCraftingToolEquivalent(base, comparison)) { return true; } if (isCraftingEquivalent(base, comparison)) { return true; } if (base.getTagCompound() != null && !base.getTagCompound().isEmpty()) { if (!ItemStack.areItemStacksEqual(base, comparison)) { return false; } } return false; }
@Override public void readNBT(Capability<AnimalData> capability, AnimalData instance, EnumFacing side, NBTBase nbtBase) { NBTTagCompound nbt = (NBTTagCompound) nbtBase; instance.setMaximumTotalTrades(nbt.getInteger("MaximumTrades")); instance.setTotalTrades(nbt.getInteger("TotalTrades")); instance.setMerchantName(nbt.getString("MerchantName")); if (nbt.hasKey("Stock")) { NBTTagCompound stock = nbt.getCompoundTag("Stock"); MerchantRecipeList recipes = stock.isEmpty() ? new MerchantRecipeList() : new MerchantRecipeList(stock); instance.setStock(recipes); } } }
public static boolean isCraftingToolEquivalent(ItemStack base, ItemStack comparison) { if (base.isEmpty() || comparison.isEmpty()) { return false; } Item baseItem = base.getItem(); if (baseItem != comparison.getItem()) { return false; } if (base.getTagCompound() == null || base.getTagCompound().isEmpty()) { // tool uses meta for damage return true; } else { // tool uses NBT for damage if (base.getItemDamage() == OreDictionary.WILDCARD_VALUE) { return true; } return base.getItemDamage() == comparison.getItemDamage(); } }
if(compound != null && !compound.isEmpty()){ event.getToolTip().add(ADVANCED_INFO_HEADER_PRE+StringUtil.localize("tooltip."+ActuallyAdditions.MODID+".nbt.desc")+":"); if(GuiScreen.isShiftKeyDown()){
/** * Compare two item stacks for crafting equivalency without oreDictionary or craftingTools */ public static boolean isCraftingEquivalent(ItemStack base, ItemStack comparison) { if (base.isEmpty() || comparison.isEmpty()) { return false; } if (base.getItem() != comparison.getItem()) { return false; } if (base.getItemDamage() != OreDictionary.WILDCARD_VALUE) { if (base.getItemDamage() != comparison.getItemDamage()) { return false; } } // When the base stackTagCompound is null or empty, treat it as a wildcard for crafting if (base.getTagCompound() == null || base.getTagCompound().isEmpty()) { return true; } else { return ItemStack.areItemStackTagsEqual(base, comparison); } }
if(!data.isEmpty()){ stack.setTagCompound(new NBTTagCompound()); stack.getTagCompound().setTag("Data", data);
private static IChromosome getChromosome(ItemStack itemStack, IChromosomeType chromosomeType, ISpeciesRoot speciesRoot) { NBTTagCompound nbtTagCompound = itemStack.getTagCompound(); if (nbtTagCompound == null) { nbtTagCompound = new NBTTagCompound(); itemStack.setTagCompound(nbtTagCompound); } NBTTagCompound genomeNbt = nbtTagCompound.getCompoundTag("Genome"); if (genomeNbt.isEmpty()) { Log.error("Got a genetic item with no genome, setting it to a default value."); genomeNbt = new NBTTagCompound(); IAllele[] defaultTemplate = speciesRoot.getDefaultTemplate(); IGenome genome = speciesRoot.templateAsGenome(defaultTemplate); genome.writeToNBT(genomeNbt); nbtTagCompound.setTag("Genome", genomeNbt); } IChromosome[] chromosomes = getChromosomes(genomeNbt, speciesRoot); return chromosomes[chromosomeType.ordinal()]; }
if (tag != null && tag.isEmpty()) stack.setTagCompound(null);