Search in sources :

Example 41 with CompoundNBT

use of net.minecraft.nbt.CompoundNBT in project BluePower by Qmunity.

the class ItemStackDatabase method saveItemStack.

public void saveItemStack(ItemStack stack) {
    new File(saveLocation).mkdirs();
    File targetRegistryName = new File(saveLocation + stack.getDisplayName() + FILE_EXTENSION);
    CompoundNBT tag = new CompoundNBT();
    stack.save(tag);
    ResourceLocation ui = stack.getItem().getRegistryName();
    tag.putString("owner", ui.getNamespace());
    tag.putString("name", ui.getPath());
    try {
        FileOutputStream fos = new FileOutputStream(targetRegistryName);
        DataOutputStream dos = new DataOutputStream(fos);
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        CompressedStreamTools.writeCompressed(tag, byteStream);
        byte[] abyte = byteStream.toByteArray();
        dos.writeShort((short) abyte.length);
        dos.write(abyte);
        dos.close();
    } catch (IOException e) {
        BluePower.log.error("IOException when trying to save an ItemStack in the database: " + e);
    }
    cache = null;
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) ResourceLocation(net.minecraft.util.ResourceLocation)

Example 42 with CompoundNBT

use of net.minecraft.nbt.CompoundNBT in project BluePower by Qmunity.

the class TubeStack method copy.

public TubeStack copy() {
    CompoundNBT tag = new CompoundNBT();
    writeToNBT(tag);
    return loadFromNBT(tag);
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT)

Example 43 with CompoundNBT

use of net.minecraft.nbt.CompoundNBT in project BluePower by Qmunity.

the class ItemBPPart method place.

@Override
public ActionResultType place(BlockItemUseContext context) {
    BlockState state = context.getLevel().getBlockState(context.getClickedPos());
    BlockState thisState = getBlock().getStateForPlacement(context);
    if (state.getBlock() instanceof IBPPartBlock && thisState != null && !AABBUtils.testOcclusion(((IBPPartBlock) thisState.getBlock()).getOcclusionShape(thisState), state.getShape(context.getLevel(), context.getClickedPos()))) {
        // Save the Tile Entity Data
        CompoundNBT nbt = new CompoundNBT();
        TileEntity tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
        if (tileEntity != null) {
            nbt = tileEntity.save(nbt);
        }
        // Replace with Multipart
        context.getLevel().setBlockAndUpdate(context.getClickedPos(), BPBlocks.multipart.defaultBlockState());
        tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
        if (tileEntity instanceof TileBPMultipart) {
            // Add the original State to the Multipart
            ((TileBPMultipart) tileEntity).addState(state);
            // Restore the Tile Entity Data
            TileEntity tile = ((TileBPMultipart) tileEntity).getTileForState(state);
            if (tile != null)
                tile.load(state, nbt);
            // Add the new State
            ((TileBPMultipart) tileEntity).addState(thisState);
            thisState.getBlock().setPlacedBy(context.getLevel(), context.getClickedPos(), thisState, context.getPlayer(), context.getItemInHand());
        }
        // Update Self
        state.neighborChanged(context.getLevel(), context.getClickedPos(), state.getBlock(), context.getClickedPos(), false);
        context.getItemInHand().shrink(1);
        // Place Sound
        context.getLevel().playSound(null, context.getClickedPos(), SoundEvents.STONE_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
        return ActionResultType.SUCCESS;
    } else if (state.getBlock() instanceof BlockBPMultipart && thisState != null && !AABBUtils.testOcclusion(((IBPPartBlock) thisState.getBlock()).getOcclusionShape(thisState), state.getShape(context.getLevel(), context.getClickedPos()))) {
        // Add to the Existing Multipart
        TileEntity tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
        if (tileEntity instanceof TileBPMultipart) {
            ((TileBPMultipart) tileEntity).addState(thisState);
            thisState.getBlock().setPlacedBy(context.getLevel(), context.getClickedPos(), thisState, context.getPlayer(), context.getItemInHand());
            // Update Neighbors
            for (Direction dir : Direction.values()) {
                context.getLevel().getBlockState(context.getClickedPos().relative(dir)).neighborChanged(context.getLevel(), context.getClickedPos().relative(dir), state.getBlock(), context.getClickedPos(), false);
            }
            // Update Self
            state.neighborChanged(context.getLevel(), context.getClickedPos(), state.getBlock(), context.getClickedPos(), false);
            context.getItemInHand().shrink(1);
            // Place Sound
            context.getLevel().playSound(null, context.getClickedPos(), SoundEvents.STONE_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
            return ActionResultType.SUCCESS;
        }
    }
    return super.place(context);
}
Also used : IBPPartBlock(com.bluepowermod.api.multipart.IBPPartBlock) TileEntity(net.minecraft.tileentity.TileEntity) BlockState(net.minecraft.block.BlockState) CompoundNBT(net.minecraft.nbt.CompoundNBT) BlockBPMultipart(com.bluepowermod.block.BlockBPMultipart) TileBPMultipart(com.bluepowermod.tile.TileBPMultipart) Direction(net.minecraft.util.Direction)

Example 44 with CompoundNBT

use of net.minecraft.nbt.CompoundNBT in project BluePower by Qmunity.

the class ItemDamageableColorableOverlay method setUsesUsed.

public static void setUsesUsed(ItemStack stack, int newUses) {
    CompoundNBT tag = stack.getTag();
    if (tag == null) {
        tag = new CompoundNBT();
        stack.setTag(tag);
    }
    tag.putInt("usesUsed", newUses);
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT)

Example 45 with CompoundNBT

use of net.minecraft.nbt.CompoundNBT in project BluePower by Qmunity.

the class TileKinectGenerator method save.

/**
 * This function gets called whenever the world/chunk is saved
 */
@Override
public CompoundNBT save(CompoundNBT tCompound) {
    super.save(tCompound);
    for (int i = 0; i < 1; i++) {
        if (!allInventories.get(i).isEmpty()) {
            CompoundNBT tc = new CompoundNBT();
            allInventories.get(i).save(tc);
            tCompound.put("inventory" + i, tc);
        }
    }
    return tCompound;
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT)

Aggregations

CompoundNBT (net.minecraft.nbt.CompoundNBT)127 ItemStack (net.minecraft.item.ItemStack)33 ListNBT (net.minecraft.nbt.ListNBT)19 Nonnull (javax.annotation.Nonnull)12 PlayerEntity (net.minecraft.entity.player.PlayerEntity)10 BlockPos (net.minecraft.util.math.BlockPos)10 ResourceLocation (net.minecraft.util.ResourceLocation)9 BlockState (net.minecraft.block.BlockState)8 TileEntity (net.minecraft.tileentity.TileEntity)7 StringTextComponent (net.minecraft.util.text.StringTextComponent)7 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)6 DimensionType (net.minecraft.world.dimension.DimensionType)6 TileBPMultipart (com.bluepowermod.tile.TileBPMultipart)5 Block (net.minecraft.block.Block)5 ArrayList (java.util.ArrayList)4 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)4 SUpdateTileEntityPacket (net.minecraft.network.play.server.SUpdateTileEntityPacket)4 LongItemStack (com.cjm721.overloaded.storage.stacks.intint.LongItemStack)3 Nullable (javax.annotation.Nullable)3 IItemProvider (net.minecraft.util.IItemProvider)3