BlockJukebox .java

net.minecraft.block.BlockJukebox (class, 206 lines): members: TileEntityJukebox, insertRecord, HAS_RECORD, getComparatorInputOverride, createNewTileEntity.

net.minecraft.blockclass206 linescommonextends BlockContainer6.8 KB

API Surface (15)

boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
void insertRecord(World worldIn, BlockPos pos, IBlockState state, ItemStack recordStack)
void breakBlock(World worldIn, BlockPos pos, IBlockState state)
void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
TileEntity createNewTileEntity(World worldIn, int meta)
boolean hasComparatorInputOverride()
int getComparatorInputOverride(World worldIn, BlockPos pos)
int getRenderType()
IBlockState getStateFromMeta(int meta)
int getMetaFromState(IBlockState state)
BlockState createBlockState()
void readFromNBT(NBTTagCompound compound)
void writeToNBT(NBTTagCompound compound)
ItemStack getRecord()
void setRecord(ItemStack recordStack)

Fields & Constants (1)

HAS_RECORD

String literals (3)

"has_record"
"RecordItem"
"Record"

Full Source

BlockJukebox.java206 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.block;
2
3
import net.minecraft.block.material.MapColor;
4
import net.minecraft.block.material.Material;
5
import net.minecraft.block.properties.IProperty;
6
import net.minecraft.block.properties.PropertyBool;
7
import net.minecraft.block.state.BlockState;
8
import net.minecraft.block.state.IBlockState;
9
import net.minecraft.creativetab.CreativeTabs;
10
import net.minecraft.entity.item.EntityItem;
11
import net.minecraft.entity.player.EntityPlayer;
12
import net.minecraft.init.Items;
13
import net.minecraft.item.Item;
14
import net.minecraft.item.ItemStack;
15
import net.minecraft.nbt.NBTTagCompound;
16
import net.minecraft.tileentity.TileEntity;
17
import net.minecraft.util.BlockPos;
18
import net.minecraft.util.EnumFacing;
19
import net.minecraft.world.World;
20
21
public class BlockJukebox extends BlockContainer
22
{
23
    public static final PropertyBool HAS_RECORD = PropertyBool.create("has_record");
24
25
    protected BlockJukebox()
26
    {
27
        super(Material.wood, MapColor.dirtColor);
28
        this.setDefaultState(this.blockState.getBaseState().withProperty(HAS_RECORD, Boolean.valueOf(false)));
29
        this.setCreativeTab(CreativeTabs.tabDecorations);
30
    }
31
32
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
33
    {
34
        if (((Boolean)state.getValue(HAS_RECORD)).booleanValue())
35
        {
36
            this.dropRecord(worldIn, pos, state);
37
            state = state.withProperty(HAS_RECORD, Boolean.valueOf(false));
38
            worldIn.setBlockState(pos, state, 2);
39
            return true;
40
        }
41
        else
42
        {
43
            return false;
44
        }
45
    }
46
47
    public void insertRecord(World worldIn, BlockPos pos, IBlockState state, ItemStack recordStack)
48
    {
49
        if (!worldIn.isRemote)
50
        {
51
            TileEntity tileentity = worldIn.getTileEntity(pos);
52
53
            if (tileentity instanceof BlockJukebox.TileEntityJukebox)
54
            {
55
                ((BlockJukebox.TileEntityJukebox)tileentity).setRecord(new ItemStack(recordStack.getItem(), 1, recordStack.getMetadata()));
56
                worldIn.setBlockState(pos, state.withProperty(HAS_RECORD, Boolean.valueOf(true)), 2);
57
            }
58
        }
59
    }
60
61
    private void dropRecord(World worldIn, BlockPos pos, IBlockState state)
62
    {
63
        if (!worldIn.isRemote)
64
        {
65
            TileEntity tileentity = worldIn.getTileEntity(pos);
66
67
            if (tileentity instanceof BlockJukebox.TileEntityJukebox)
68
            {
69
                BlockJukebox.TileEntityJukebox blockjukebox$tileentityjukebox = (BlockJukebox.TileEntityJukebox)tileentity;
70
                ItemStack itemstack = blockjukebox$tileentityjukebox.getRecord();
71
72
                if (itemstack != null)
73
                {
74
                    worldIn.playAuxSFX(1005, pos, 0);
75
                    worldIn.playRecord(pos, (String)null);
76
                    blockjukebox$tileentityjukebox.setRecord((ItemStack)null);
77
                    float f = 0.7F;
78
                    double d0 = (double)(worldIn.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
79
                    double d1 = (double)(worldIn.rand.nextFloat() * f) + (double)(1.0F - f) * 0.2D + 0.6D;
80
                    double d2 = (double)(worldIn.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
81
                    ItemStack itemstack1 = itemstack.copy();
82
                    EntityItem entityitem = new EntityItem(worldIn, (double)pos.getX() + d0, (double)pos.getY() + d1, (double)pos.getZ() + d2, itemstack1);
83
                    entityitem.setDefaultPickupDelay();
84
                    worldIn.spawnEntityInWorld(entityitem);
85
                }
86
            }
87
        }
88
    }
89
90
    public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
91
    {
92
        this.dropRecord(worldIn, pos, state);
93
        super.breakBlock(worldIn, pos, state);
94
    }
95
96
    /**
97
     * Spawns this Block's drops into the World as EntityItems.
98
     */
99
    public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
100
    {
101
        if (!worldIn.isRemote)
102
        {
103
            super.dropBlockAsItemWithChance(worldIn, pos, state, chance, 0);
104
        }
105
    }
106
107
    /**
108
     * Returns a new instance of a block's tile entity class. Called on placing the block.
109
     */
110
    public TileEntity createNewTileEntity(World worldIn, int meta)
111
    {
112
        return new BlockJukebox.TileEntityJukebox();
113
    }
114
115
    public boolean hasComparatorInputOverride()
116
    {
117
        return true;
118
    }
119
120
    public int getComparatorInputOverride(World worldIn, BlockPos pos)
121
    {
122
        TileEntity tileentity = worldIn.getTileEntity(pos);
123
124
        if (tileentity instanceof BlockJukebox.TileEntityJukebox)
125
        {
126
            ItemStack itemstack = ((BlockJukebox.TileEntityJukebox)tileentity).getRecord();
127
128
            if (itemstack != null)
129
            {
130
                return Item.getIdFromItem(itemstack.getItem()) + 1 - Item.getIdFromItem(Items.record_13);
131
            }
132
        }
133
134
        return 0;
135
    }
136
137
    /**
138
     * The type of render function called. 3 for standard block models, 2 for TESR's, 1 for liquids, -1 is no render
139
     */
140
    public int getRenderType()
141
    {
142
        return 3;
143
    }
144
145
    /**
146
     * Convert the given metadata into a BlockState for this Block
147
     */
148
    public IBlockState getStateFromMeta(int meta)
149
    {
150
        return this.getDefaultState().withProperty(HAS_RECORD, Boolean.valueOf(meta > 0));
151
    }
152
153
    /**
154
     * Convert the BlockState into the correct metadata value
155
     */
156
    public int getMetaFromState(IBlockState state)
157
    {
158
        return ((Boolean)state.getValue(HAS_RECORD)).booleanValue() ? 1 : 0;
159
    }
160
161
    protected BlockState createBlockState()
162
    {
163
        return new BlockState(this, new IProperty[] {HAS_RECORD});
164
    }
165
166
    public static class TileEntityJukebox extends TileEntity
167
    {
168
        private ItemStack record;
169
170
        public void readFromNBT(NBTTagCompound compound)
171
        {
172
            super.readFromNBT(compound);
173
174
            if (compound.hasKey("RecordItem", 10))
175
            {
176
                this.setRecord(ItemStack.loadItemStackFromNBT(compound.getCompoundTag("RecordItem")));
177
            }
178
            else if (compound.getInteger("Record") > 0)
179
            {
180
                this.setRecord(new ItemStack(Item.getItemById(compound.getInteger("Record")), 1, 0));
181
            }
182
        }
183
184
        public void writeToNBT(NBTTagCompound compound)
185
        {
186
            super.writeToNBT(compound);
187
188
            if (this.getRecord() != null)
189
            {
190
                compound.setTag("RecordItem", this.getRecord().writeToNBT(new NBTTagCompound()));
191
            }
192
        }
193
194
        public ItemStack getRecord()
195
        {
196
            return this.record;
197
        }
198
199
        public void setRecord(ItemStack recordStack)
200
        {
201
            this.record = recordStack;
202
            this.markDirty();
203
        }
204
    }
205
}
206

Deobfuscated MCP 1.8.9 source of BlockJukebox from Marcelektro/MavenMCP-1.8.9 (net/minecraft/block/BlockJukebox.java), stored verbatim.