/
BlockCeramicBrick.java
264 lines (234 loc) · 9.32 KB
/
BlockCeramicBrick.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package binnie.botany.ceramic.brick;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.common.property.ExtendedBlockState;
import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.common.property.IUnlistedProperty;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.core.IItemModelRegister;
import forestry.api.core.IModelManager;
import forestry.api.core.ISpriteRegister;
import forestry.api.core.IStateMapperRegister;
import forestry.api.core.ITextureManager;
import forestry.core.blocks.IColoredBlock;
import forestry.core.blocks.properties.UnlistedBlockAccess;
import forestry.core.blocks.properties.UnlistedBlockPos;
import forestry.core.models.BlockModelEntry;
import binnie.Constants;
import binnie.botany.CreativeTabBotany;
import binnie.botany.ceramic.TileCeramic;
import binnie.botany.genetics.EnumFlowerColor;
import binnie.core.BinnieCore;
import binnie.core.block.IMultipassBlock;
import binnie.core.models.DefaultStateMapper;
import binnie.core.models.ModelManager;
import binnie.core.models.ModelMutlipass;
import binnie.core.util.TileUtil;
public class BlockCeramicBrick extends Block implements IMultipassBlock<CeramicBrickPair>, IColoredBlock, ISpriteRegister, IStateMapperRegister, IItemModelRegister {
private static final PropertyEnum<CeramicBrickType> TYPE = PropertyEnum.create("type", CeramicBrickType.class);
public BlockCeramicBrick() {
super(Material.ROCK);
setHardness(1.0f);
setResistance(5.0f);
setRegistryName("ceramicBrick");
setCreativeTab(CreativeTabBotany.instance);
}
@Override
public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
return Collections.emptyList();
}
@Override
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, @Nullable EntityPlayer player, boolean willHarvest) {
List<ItemStack> drops = new ArrayList<>();
TileCeramic ceramic = TileUtil.getTile(world, pos, TileCeramic.class);
if (ceramic != null) {
drops = Collections.singletonList(ceramic.getStack());
}
boolean hasBeenBroken = world.setBlockToAir(pos);
if (hasBeenBroken && BinnieCore.getBinnieProxy().isSimulating(world) && drops.size() > 0 && (player == null || !player.capabilities.isCreativeMode)) {
for (ItemStack drop : drops) {
spawnAsEntity(world, pos, drop);
}
}
return hasBeenBroken;
}
@Override
public TileEntity createTileEntity(World world, IBlockState state) {
return new TileCeramicBrick();
}
@Override
public boolean hasTileEntity(IBlockState state) {
return true;
}
@SideOnly(Side.CLIENT)
@Override
public void getSubBlocks(Item item, CreativeTabs tab, NonNullList<ItemStack> itemList) {
for (EnumFlowerColor color : EnumFlowerColor.values()) {
itemList.add(new CeramicBrickPair(color, color, CeramicBrickType.TILE).getStack(1));
}
for (CeramicBrickType type : CeramicBrickType.VALUES) {
if (type.canDouble()) {
itemList.add(new CeramicBrickPair(EnumFlowerColor.Brown, EnumFlowerColor.Gold, type).getStack(1));
}
}
itemList.add(new CeramicBrickPair(EnumFlowerColor.Gold, EnumFlowerColor.Gold, CeramicBrickType.SPLIT).getStack(1));
itemList.add(new CeramicBrickPair(EnumFlowerColor.Brown, EnumFlowerColor.Brown, CeramicBrickType.CHEQUERED).getStack(1));
itemList.add(new CeramicBrickPair(EnumFlowerColor.Gold, EnumFlowerColor.Brown, CeramicBrickType.LARGE_BRICK).getStack(1));
}
@SideOnly(Side.CLIENT)
@Override
public void registerStateMapper() {
ResourceLocation resourceLocation = new ResourceLocation(Constants.BOTANY_MOD_ID, "ceramicBrick");
ModelLoader.setCustomStateMapper(this, new DefaultStateMapper(resourceLocation));
ModelManager.registerCustomBlockModel(new BlockModelEntry(
new ModelResourceLocation(resourceLocation, "normal"),
new ModelResourceLocation(resourceLocation, "inventory"),
new ModelMutlipass<>(BlockCeramicBrick.class),
this
));
}
@SideOnly(Side.CLIENT)
@Override
public void registerModel(Item item, IModelManager manager) {
manager.registerItemModel(item, new CeramicBrickMeshDefinition());
}
@Override
protected BlockStateContainer createBlockState() {
return new ExtendedBlockState(
this,
new IProperty[]{TYPE},
new IUnlistedProperty[]{
UnlistedBlockPos.POS,
UnlistedBlockAccess.BLOCKACCESS
}
);
}
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(TYPE, CeramicBrickType.VALUES[meta]);
}
@Override
public int getMetaFromState(IBlockState state) {
return state.getValue(TYPE).ordinal();
}
@Override
public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos) {
return ((IExtendedBlockState) super.getExtendedState(state, world, pos)).withProperty(UnlistedBlockPos.POS, pos).withProperty(UnlistedBlockAccess.BLOCKACCESS, world);
}
@Override
public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer, EnumHand hand) {
ItemStack stack = placer.getHeldItem(hand);
CeramicBrickPair pair = new CeramicBrickPair(stack);
return getDefaultState().withProperty(TYPE, pair.type);
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
TileCeramicBrick ceramic = TileUtil.getTile(worldIn, pos, TileCeramicBrick.class);
if (ceramic != null) {
CeramicBrickPair pair = new CeramicBrickPair(stack);
ceramic.setColors(pair.colorFirst, pair.colorSecond);
}
}
@Override
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) {
TileCeramicBrick ceramic = TileUtil.getTile(world, pos, TileCeramicBrick.class);
if (ceramic != null) {
return ceramic.pair().getStack(0);
}
return super.getPickBlock(state, target, world, pos, player);
}
@SideOnly(Side.CLIENT)
@Override
public int colorMultiplier(IBlockState state, @Nullable IBlockAccess world, @Nullable BlockPos pos, int tintIndex) {
if (world != null && pos != null) {
TileCeramicBrick ceramic = TileUtil.getTile(world, pos, TileCeramicBrick.class);
if (ceramic != null) {
if (tintIndex == 1) {
return ceramic.getColorFirst().getFlowerColorAllele().getColor(false);
} else if (tintIndex == 2) {
return ceramic.getColorSecond().getFlowerColorAllele().getColor(false);
}
}
}
return 0xffffff;
}
@SideOnly(Side.CLIENT)
@Override
public BlockRenderLayer getBlockLayer() {
return BlockRenderLayer.CUTOUT;
}
@Override
@SideOnly(Side.CLIENT)
public int getRenderPasses() {
return 3;
}
@Override
@SideOnly(Side.CLIENT)
public CeramicBrickPair getInventoryKey(ItemStack stack) {
return new CeramicBrickPair(stack);
}
@Override
@SideOnly(Side.CLIENT)
public CeramicBrickPair getWorldKey(IBlockState state) {
IExtendedBlockState extendedState = (IExtendedBlockState) state;
IBlockAccess world = extendedState.getValue(UnlistedBlockAccess.BLOCKACCESS);
BlockPos pos = extendedState.getValue(UnlistedBlockPos.POS);
TileCeramicBrick ceramic = TileUtil.getTile(world, pos, TileCeramicBrick.class);
if (ceramic != null) {
return ceramic.pair();
}
return null;
}
@Override
@SideOnly(Side.CLIENT)
public TextureAtlasSprite getSprite(CeramicBrickPair type, @Nullable EnumFacing facing, int pass) {
return type.getSprite(pass);
}
@Override
@SideOnly(Side.CLIENT)
public void registerSprites(ITextureManager manager) {
TextureMap textureMap = Minecraft.getMinecraft().getTextureMapBlocks();
for (CeramicBrickType type : CeramicBrickType.VALUES) {
type.sprites = new TextureAtlasSprite[3];
for (int i = 0; i < 3; ++i) {
type.sprites[i] = textureMap.registerSprite(new ResourceLocation(Constants.BOTANY_MOD_ID + ":blocks/ceramic." + type.id + "." + i));
}
}
}
private class CeramicBrickMeshDefinition implements ItemMeshDefinition {
@Override
public ModelResourceLocation getModelLocation(ItemStack stack) {
return new ModelResourceLocation(Constants.BOTANY_MOD_ID + ":ceramicBrick", "inventory");
}
}
}