Java Code Examples for net.minecraft.nbt.NBTTagCompound#hasKey()

The following examples show how to use net.minecraft.nbt.NBTTagCompound#hasKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TileCrucible.java    From ExNihiloAdscensio with MIT License 6 votes vote down vote up
@Override
public void readFromNBT(NBTTagCompound tag) {
	if (tag.hasKey("currentItem"))
	{
		currentItem = ItemInfo.readFromNBT(tag.getCompoundTag("currentItem"));
	}
	else
	{
	    currentItem = null;
	}
	
	solidAmount = tag.getInteger("solidAmount");
	
	if (tag.hasKey("itemHandler"))
	{
		itemHandler.deserializeNBT((NBTTagCompound) tag.getTag("itemHandler"));
	}
	
	if (tag.hasKey("tank"))
	{
		tank.readFromNBT((NBTTagCompound) tag.getTag("tank"));
	}
	
	super.readFromNBT(tag);
}
 
Example 2
Source File: StandardWizardryWorld.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void deserializeNBT(NBTTagCompound compound) {
	if (compound.hasKey("spell_object_manager")) {
		spellObjectManager = new SpellObjectManager();
		spellObjectManager.manager.fromNbt(compound.getCompoundTag("spell_object_manager"));
	}

	if (compound.hasKey("drives")) {
		NBTTagList driveNBT = compound.getTagList("drives", Constants.NBT.TAG_COMPOUND);
		for (NBTBase base : driveNBT) {
			if (base instanceof NBTTagCompound) {
				if (((NBTTagCompound) base).hasKey("pos") && ((NBTTagCompound) base).hasKey("drive")) {
					BlockPos pos = BlockPos.fromLong(((NBTTagCompound) base).getLong("pos"));
					NemezTracker tracker = new NemezTracker();
					tracker.deserializeNBT(((NBTTagCompound) base).getTagList("drive", Constants.NBT.TAG_COMPOUND));

					blockNemezDrives.put(pos, tracker);
				}
			}
		}
	}
}
 
Example 3
Source File: CraftMetaBlockState.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
CraftMetaBlockState(NBTTagCompound tag, Material material) {
    super(tag);
    this.material = material;

    if (tag.hasKey(BLOCK_ENTITY_TAG.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) {
        blockEntityTag = tag.getCompoundTag(BLOCK_ENTITY_TAG.NBT);
    } else {
        blockEntityTag = null;
    }
}
 
Example 4
Source File: HexUpdateHandler.java    From TFC2 with GNU General Public License v3.0 5 votes vote down vote up
@SubscribeEvent
public void handle(HexUpdateEvent event)
{
	NBTTagCompound nbt = event.centerToUpdate.getCustomNBT();
	if(nbt.hasKey("TFC2_Data"))
	{
		NBTTagCompound data = nbt.getCompoundTag("TFC2_Data");
		if(data.hasKey("CropData"))
		{
			NBTTagCompound cropData = data.getCompoundTag("CropData");
			long lastRegenTick = cropData.getLong("lastRegenTick");
			if(lastRegenTick + Timekeeper.ticksInPeriod < Timekeeper.getInstance().getTotalTicks())
			{
				cropData.setLong("lastRegenTick", lastRegenTick + Timekeeper.ticksInPeriod);
				float nutrients = cropData.getFloat("nutrients");
				float maxNutrients = TileCrop.GetMaxNutrients(event.map);
				cropData.setFloat("nutrients", Math.min(maxNutrients, nutrients + maxNutrients/4));
			}
		}
		if(data.hasKey("hydration"))
		{
			byte[] hydrationArray = data.getByteArray("hydration");
			int waterLevel = 0;
			for(int i = 0; i < 64; i++)
			{
				hydrationArray[i] = (byte)Math.max(0, hydrationArray[i]-5);
				waterLevel += hydrationArray[i];
			}
			if(waterLevel > 0)
				data.setByteArray("hydration", hydrationArray);
			else
				data.removeTag("hydration");
		}
	}
}
 
Example 5
Source File: ItemPortalScaler.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void changeCoordinateScaleFactor(ItemStack stack, EntityPlayer player, int amount)
{
    ItemStack moduleStack = this.getSelectedModuleStack(stack, ModuleType.TYPE_MEMORY_CARD_MISC);

    if (moduleStack.isEmpty())
    {
        return;
    }

    NBTTagCompound tag = NBTUtils.getCompoundTag(moduleStack, "PortalScaler", true);

    int x = tag.hasKey("scaleX", Constants.NBT.TAG_BYTE) ? tag.getByte("scaleX") : 8;
    int y = tag.hasKey("scaleY", Constants.NBT.TAG_BYTE) ? tag.getByte("scaleY") : 1;
    int z = tag.hasKey("scaleZ", Constants.NBT.TAG_BYTE) ? tag.getByte("scaleZ") : 8;

    EnumFacing facing = EntityUtils.getLookingDirection(player);
    x += Math.abs(facing.getXOffset()) * amount;
    y += Math.abs(facing.getYOffset()) * amount;
    z += Math.abs(facing.getZOffset()) * amount;

    // Hop over zero and 1/1 (ie. -1)
    if (x == -1) { x += Math.abs(facing.getXOffset()) * amount * 2; }
    if (y == -1) { y += Math.abs(facing.getYOffset()) * amount * 2; }
    if (z == -1) { z += Math.abs(facing.getZOffset()) * amount * 2; }

    // Hop over zero
    if (x == 0) { x += Math.abs(facing.getXOffset()) * amount * 2; }
    if (y == 0) { y += Math.abs(facing.getYOffset()) * amount * 2; }
    if (z == 0) { z += Math.abs(facing.getZOffset()) * amount * 2; }

    x = MathHelper.clamp(x, -64, 64);
    y = MathHelper.clamp(y, -64, 64);
    z = MathHelper.clamp(z, -64, 64);

    tag.setByte("scaleX", (byte)x);
    tag.setByte("scaleY", (byte)y);
    tag.setByte("scaleZ", (byte)z);

    this.setSelectedModuleStack(stack, ModuleType.TYPE_MEMORY_CARD_MISC, moduleStack);
}
 
Example 6
Source File: TileEntityBlockMateralProxy.java    From GardenCollection with MIT License 5 votes vote down vote up
@Override
public void readFromNBT (NBTTagCompound tag) {
    super.readFromNBT(tag);

    if (tag.hasKey("P"))
        unpackUnifiedProtoData(tag.getInteger("P"));
}
 
Example 7
Source File: NbtUtils.java    From OpenModsLib with MIT License 5 votes vote down vote up
public static <T extends Enum<T>> T readEnum(NBTTagCompound tag, String name, Class<T> cls) {
	if (tag.hasKey(name, Constants.NBT.TAG_ANY_NUMERIC)) {
		int ordinal = tag.getInteger(name);
		return EnumUtils.fromOrdinal(cls, ordinal);
	}

	return null;
}
 
Example 8
Source File: TileEntityQuantumComputer.java    From qcraft-mod with Apache License 2.0 5 votes vote down vote up
public static AreaData decode( NBTTagCompound nbttagcompound )
{
    AreaData storedData = new AreaData();
    storedData.m_shape = new AreaShape();
    storedData.m_shape.m_xMin = nbttagcompound.getInteger( "xmin" );
    storedData.m_shape.m_xMax = nbttagcompound.getInteger( "xmax" );
    storedData.m_shape.m_yMin = nbttagcompound.getInteger( "ymin" );
    storedData.m_shape.m_yMax = nbttagcompound.getInteger( "ymax" );
    storedData.m_shape.m_zMin = nbttagcompound.getInteger( "zmin" );
    storedData.m_shape.m_zMax = nbttagcompound.getInteger( "zmax" );

    int size =
        ( storedData.m_shape.m_xMax - storedData.m_shape.m_xMin + 1 ) *
        ( storedData.m_shape.m_yMax - storedData.m_shape.m_yMin + 1 ) *
        ( storedData.m_shape.m_zMax - storedData.m_shape.m_zMin + 1 );
    storedData.m_blocks = new Block[ size ];
    if( nbttagcompound.hasKey( "blockData" ) )
    {
        int[] blockIDs = nbttagcompound.getIntArray( "blockData" );
        for( int i=0; i<size; ++i )
        {
            storedData.m_blocks[i] = Block.getBlockById( blockIDs[i] );
        }
    }
    else
    {
        NBTTagList blockNames = nbttagcompound.getTagList( "blockNames", Constants.NBT.TAG_STRING );
        for( int i=0; i<size; ++i )
        {
            String name = blockNames.getStringTagAt( i );
            if( name.length() > 0 && !name.equals( "null" ) )
            {
                storedData.m_blocks[i] = Block.getBlockFromName( name );
            }
        }
    }
    storedData.m_metaData = nbttagcompound.getIntArray( "metaData" );
    return storedData;
}
 
Example 9
Source File: ProfaneWandUpdate.java    From ForbiddenMagic with Do What The F*ck You Want To Public License 5 votes vote down vote up
public void onUpdate(ItemStack itemstack, EntityPlayer player) {

        if(player.ticksExisted % 20 == 0){

            NBTTagCompound tag = itemstack.getTagCompound();
            if(!tag.hasKey("contract")){
                Thaumcraft.proxy.getResearchManager().completeResearch(player, "ROD_profane");
                tag.setInteger("contract", 25000);
            }
            else if(tag.getInteger("contract") <= 0)
                return;

            for(int x = 0;x < primals.length && tag.getInteger("contract") > 0;x++){
                int deficit = ((ItemWandCasting)itemstack.getItem()).getMaxVis(itemstack) - ((ItemWandCasting)itemstack.getItem()).getVis(itemstack, primals[x]);
                if(deficit > 0) {
                    int restore = Math.min(tag.getInteger("contract"), deficit);
                    ((ItemWandCasting)itemstack.getItem()).addRealVis(itemstack, primals[x], restore, true);
                    tag.setInteger("contract", tag.getInteger("contract") - restore);
                    if(player.worldObj.rand.nextInt(2501) < restore)
                        Thaumcraft.addStickyWarpToPlayer(player, 1);
                }
            }

            if(tag.getInteger("contract") <= 0) {
                ((ItemWandCasting)itemstack.getItem()).setRod(itemstack, WandRod.rods.get("profaned"));
                Thaumcraft.addStickyWarpToPlayer(player, 1);
                player.worldObj.spawnParticle("largeexplode", player.posX, player.posY + (double)(player.height / 2.0F), player.posZ, 0.0D, 0.0D, 0.0D);
            }
            itemstack.setTagCompound(tag);
        }

    }
 
Example 10
Source File: GTTileBaseMachine.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void applyRecipeEffect(MachineOutput output) {
	if (output == null || output.getMetadata() == null) {
		if (recipeEnergy != energyConsume) {
			recipeEnergy = energyConsume;
			if (recipeEnergy < 1) {
				recipeEnergy = 1;
			}
			getNetwork().updateTileGuiField(this, "recipeEnergy");
		}
		if (recipeOperation != operationLength) {
			recipeOperation = operationLength;
			if (recipeOperation < 1) {
				recipeOperation = 1;
			}
			getNetwork().updateTileGuiField(this, "recipeOperation");
		}
		return;
	}
	NBTTagCompound nbt = output.getMetadata();
	double energyMod = nbt.hasKey("RecipeEnergyModifier") ? nbt.getDouble("RecipeEnergyModifier") : 1F;
	int newEnergy = TileEntityBasicElectricMachine.applyModifier(energyConsume, nbt.getInteger("RecipeEnergy"), energyMod);
	if (newEnergy != recipeEnergy) {
		recipeEnergy = newEnergy;
		if (recipeEnergy < 1) {
			recipeEnergy = 1;
		}
		getNetwork().updateTileGuiField(this, "recipeEnergy");
	}
	double progMod = nbt.hasKey("RecipeTimeModifier") ? nbt.getDouble("RecipeTimeModifier") : 1F;
	int newProgress = TileEntityBasicElectricMachine.applyModifier(operationLength, nbt.getInteger("RecipeTime"), progMod);
	if (newProgress != recipeOperation) {
		recipeOperation = newProgress;
		if (recipeOperation < 1) {
			recipeOperation = 1;
		}
		getNetwork().updateTileGuiField(this, "recipeOperation");
	}
}
 
Example 11
Source File: MultiblockTileEntityBase.java    From BeefCore with MIT License 5 votes vote down vote up
@Override
public void readFromNBT(NBTTagCompound data) {
	super.readFromNBT(data);
	
	// We can't directly initialize a multiblock controller yet, so we cache the data here until
	// we receive a validate() call, which creates the controller and hands off the cached data.
	if(data.hasKey("multiblockData")) {
		this.cachedMultiblockData = data.getCompoundTag("multiblockData");
	}
}
 
Example 12
Source File: ItemBlueprint.java    From Cyberware with MIT License 5 votes vote down vote up
@Override
public ItemStack getResult(ItemStack stack, ItemStack[] craftingItems)
{
	if (stack.hasTagCompound())
	{
		NBTTagCompound comp = stack.getTagCompound();
		if (comp.hasKey("blueprintItem"))
		{
			ItemStack blueprintItem = ItemStack.loadItemStackFromNBT(comp.getCompoundTag("blueprintItem"));
			if (blueprintItem != null && CyberwareAPI.canDeconstruct(blueprintItem))
			{
				ItemStack[] requiredItems = CyberwareAPI.getComponents(blueprintItem).clone();
				for (int i = 0; i < requiredItems.length; i++)
				{
					ItemStack required = requiredItems[i].copy();
					boolean satisfied = false;
					for (ItemStack crafting : craftingItems)
					{
						if (crafting != null && required != null)
						{
							if (crafting.getItem() == required.getItem() && crafting.getItemDamage() == required.getItemDamage() && (!required.hasTagCompound() || (ItemStack.areItemStackTagsEqual(required, crafting))))
							{
								required.stackSize -= crafting.stackSize;
							}
							if (required.stackSize <= 0)
							{
								satisfied = true;
								break;
							}
						}
					}
					if (!satisfied) return null;
				}
				
				return blueprintItem;
			}
		}
	}
	return null;
}
 
Example 13
Source File: GTBlockStorage.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer,
		ItemStack stack) {
	super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
	TileEntity tile = worldIn.getTileEntity(pos);
	NBTTagCompound nbt = StackUtil.getNbtData(stack);
	if (tile instanceof IGTRecolorableStorageTile) {
		IGTRecolorableStorageTile colorTile = (IGTRecolorableStorageTile) tile;
		if (nbt.hasKey("color")) {
			colorTile.setTileColor(nbt.getInteger("color"));
		} else {
			colorTile.setTileColor(16383998);
		}
	}
}
 
Example 14
Source File: ItemQBlock.java    From qcraft-mod with Apache License 2.0 5 votes vote down vote up
public static void setEntanglementFrequency( ItemStack stack, int entanglementFrequency )
{
    // Ensure the nbt
    if( !stack.hasTagCompound() )
    {
        stack.setTagCompound( new NBTTagCompound() );
    }

    // Set the tags
    NBTTagCompound nbt = stack.getTagCompound();
    if( entanglementFrequency < 0 )
    {
        // No frequency
        if( nbt.hasKey( "e" ) )
        {
            nbt.removeTag( "e" );
        }
        if( nbt.hasKey( "R" ) )
        {
            nbt.removeTag( "R" );
        }
    }
    else if( entanglementFrequency == 0 )
    {
        // Unknown frequency
        nbt.setInteger( "e", entanglementFrequency );
        nbt.setInteger( "R", TileEntityQBlock.s_random.nextInt( 0xffffff ) );
    }
    else
    {
        // Known frequency
        nbt.setInteger( "e", entanglementFrequency );
        if( nbt.hasKey( "R" ) )
        {
            nbt.removeTag( "R" );
        }
    }
}
 
Example 15
Source File: TargetData.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
public NBTTagCompound readTargetTagFromNBT(NBTTagCompound nbt)
{
    if (nbtHasTargetTag(nbt) == false)
    {
        return null;
    }

    NBTTagCompound tag = nbt.getCompoundTag("Target");
    this.pos = new BlockPos(tag.getInteger("posX"), tag.getInteger("posY"), tag.getInteger("posZ"));
    this.dimension = tag.getInteger("Dim");
    this.dimensionName = tag.getString("DimName");
    this.blockName = tag.getString("BlockName");
    this.blockMeta = tag.getByte("BlockMeta");
    this.itemMeta = tag.getByte("ItemMeta");
    this.blockFace = tag.getByte("BlockFace");
    this.facing = EnumFacing.byIndex(this.blockFace);

    this.dPosX = tag.hasKey("dPosX", Constants.NBT.TAG_DOUBLE) ? tag.getDouble("dPosX") : this.pos.getX() + 0.5d;
    this.dPosY = tag.hasKey("dPosY", Constants.NBT.TAG_DOUBLE) ? tag.getDouble("dPosY") : this.pos.getY();
    this.dPosZ = tag.hasKey("dPosZ", Constants.NBT.TAG_DOUBLE) ? tag.getDouble("dPosZ") : this.pos.getZ() + 0.5d;

    if (tag.hasKey("Yaw", Constants.NBT.TAG_FLOAT) && tag.hasKey("Pitch", Constants.NBT.TAG_FLOAT))
    {
        this.hasRotation = true;
        this.yaw = tag.getFloat("Yaw");
        this.pitch = tag.getFloat("Pitch");
    }

    return tag;
}
 
Example 16
Source File: NewRenderPlayer.java    From Et-Futurum with The Unlicense 5 votes vote down vote up
private void setModel(EntityPlayer player) {
	boolean isAlex;

	NBTTagCompound nbt = player.getEntityData();
	if (nbt.hasKey(SetPlayerModelCommand.MODEL_KEY, Constants.NBT.TAG_BYTE))
		isAlex = nbt.getBoolean(SetPlayerModelCommand.MODEL_KEY);
	else
		isAlex = PlayerModelManager.isPlayerModelAlex(getEntityTexture(player));

	mainModel = isAlex ? ALEX : STEVE;
	modelBipedMain = (ModelBiped) mainModel;
}
 
Example 17
Source File: ItemArtifactArmor.java    From Artifacts with MIT License 4 votes vote down vote up
@Override
public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack)
{
	NBTTagCompound data = itemStack.getTagCompound();
	int effectID = 0;
	if(data != null) {
		if(!world.isRemote) {
			IArtifactComponent c;
			effectID = data.getInteger("onArmorTickUpdate");
			if(effectID != 0) {
				c = ArtifactsAPI.artifacts.getComponent(effectID);
				if(c != null)
					c.onArmorTickUpdate(world, player, itemStack, true);
			}
			effectID = data.getInteger("onArmorTickUpdate2");
			if(effectID != 0) {
				c = ArtifactsAPI.artifacts.getComponent(effectID);
				if(c != null)
					c.onArmorTickUpdate(world, player, itemStack, true);
			}
			effectID = data.getInteger("onTakeDamage");
			if(effectID != 0) {
				c = ArtifactsAPI.artifacts.getComponent(effectID);
				if(c != null && !(c instanceof ComponentBreathing))
					c.onArmorTickUpdate(world, player, itemStack, true);
			}
			effectID = data.getInteger("onDeath");
			if(effectID != 0) {
				c = ArtifactsAPI.artifacts.getComponent(effectID);
				if(c != null)
					c.onArmorTickUpdate(world, player, itemStack, true);
			}
		}
		ArrayList<String> keys = ArtifactsAPI.artifacts.getNBTKeys();
		String kk = "";
		int n = 0;
		for(int k = keys.size() - 1; k >= 0; k--) {
			kk = keys.get(k)+"_armor";
			if(data.hasKey(kk)) {
				n = data.getInteger(kk);
				if(n > 0)
					n--;
				data.setInteger(kk,n);
			}
		}
	}
	else if(!world.isRemote) {
		ItemStack newStack = ArtifactsAPI.artifacts.applyRandomEffects(new ItemStack(this));
		
		for(int i = 0; i < player.inventory.armorInventory.length; i++) {
			if(player.inventory.armorInventory[i] == itemStack) {
				player.inventory.armorInventory[i] = newStack;
			}
		}
	}
}
 
Example 18
Source File: AbstractUsableBehaviour.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
public final int getUsesLeft(ItemStack stack) {
    NBTTagCompound tagCompound = stack.getTagCompound();
    if (tagCompound == null || !tagCompound.hasKey("GT.UsesLeft", NBT.TAG_INT))
        return totalUses;
    return tagCompound.getInteger("GT.UsesLeft");
}
 
Example 19
Source File: NbtUtils.java    From OpenModsLib with MIT License 4 votes vote down vote up
public static boolean hasCoordinates(NBTTagCompound tag) {
	return tag.hasKey(TAG_X, Constants.NBT.TAG_ANY_NUMERIC) &&
			tag.hasKey(TAG_Y, Constants.NBT.TAG_ANY_NUMERIC) &&
			tag.hasKey(TAG_Z, Constants.NBT.TAG_ANY_NUMERIC);
}
 
Example 20
Source File: HandItem.java    From pycode-minecraft with MIT License 4 votes vote down vote up
@Override
public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) {
    NBTTagCompound compound = stack.getTagCompound();
    if (compound == null) return;
    if (compound.hasKey(PythonCode.CODE_NBT_TAG)) tooltip.add("[has code]");
}