/
BlockGeneric.java
253 lines (219 loc) · 9.31 KB
/
BlockGeneric.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
/*------------------------------------------------------------------------------
Copyright (c) CovertJaguar, 2011-2020
http://railcraft.info
This code is the property of CovertJaguar
and may only be used with explicit written
permission unless otherwise specified on the
license page at http://railcraft.info/wiki/info:license.
-----------------------------------------------------------------------------*/
package mods.railcraft.common.blocks.aesthetics.generic;
import mods.railcraft.api.core.IVariantEnum;
import mods.railcraft.common.blocks.BlockMeta;
import mods.railcraft.common.blocks.BlockRailcraftSubtyped;
import mods.railcraft.common.blocks.RailcraftBlocks;
import mods.railcraft.common.carts.EntityTunnelBore;
import mods.railcraft.common.plugins.forge.CreativePlugin;
import mods.railcraft.common.plugins.forge.HarvestPlugin;
import mods.railcraft.common.plugins.forge.WorldPlugin;
import mods.railcraft.common.plugins.misc.MicroBlockPlugin;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.Explosion;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Random;
import static mods.railcraft.common.blocks.aesthetics.metals.EnumMetal.*;
@BlockMeta.Variant(EnumGeneric.class)
public class BlockGeneric extends BlockRailcraftSubtyped<EnumGeneric> {
public BlockGeneric() {
super(Material.ROCK);
setDefaultState(blockState.getBaseState().withProperty(getVariantEnumProperty(), EnumGeneric.BLOCK_COKE));
setResistance(20);
setHardness(5);
setSoundType(SoundType.STONE);
setTickRandomly(true);
setCreativeTab(CreativePlugin.STRUCTURE_TAB);
}
@Override
public void initializeDefinition() {
HarvestPlugin.setStateHarvestLevel("shovel", 3, EnumGeneric.CRUSHED_OBSIDIAN);
EntityTunnelBore.addMineableBlock(this);
for (EnumGeneric block : EnumGeneric.VALUES) {
MicroBlockPlugin.addMicroBlockCandidate(this, block.ordinal());
}
OreDictionary.registerOre("blockFuelCoke", EnumGeneric.BLOCK_COKE.getStack());
}
@Override
public IBlockState getState(@Nullable IVariantEnum variant) {
IBlockState state = getDefaultState();
if (variant != null) {
checkVariant(variant);
state = state.withProperty(getVariantEnumProperty(), (EnumGeneric) variant);
}
return state;
}
public static BlockGeneric getBlock() {
return (BlockGeneric) RailcraftBlocks.GENERIC.block();
}
/**
* Convert the given metadata into a BlockState for this Block
*/
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(getVariantEnumProperty(), EnumGeneric.fromOrdinal(meta));
}
/**
* Convert the BlockState into the correct metadata value
*/
@Override
public int getMetaFromState(IBlockState state) {
return state.getValue(getVariantEnumProperty()).ordinal();
}
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, getVariantEnumProperty());
}
private EnumGeneric getVariant(IBlockAccess world, BlockPos pos) {
return getVariant(WorldPlugin.getBlockState(world, pos));
}
@SuppressWarnings("deprecation")
@Override
public float getBlockHardness(IBlockState blockState, World worldIn, BlockPos pos) {
return getVariant(blockState).getHardness();
}
@Override
public int damageDropped(IBlockState state) {
return state.getValue(getVariantEnumProperty()).ordinal();
}
@SuppressWarnings("deprecation")
@Override
public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block neighborBlock, BlockPos neighborPos) {
getVariant(state).getBlockDef().onNeighborBlockChange(worldIn, pos, state, neighborBlock);
}
@Override
public void updateTick(World world, BlockPos pos, IBlockState state, Random rand) {
getVariant(state).getBlockDef().updateTick(world, pos, rand);
}
@Override
public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand) {
getVariant(stateIn).getBlockDef().randomDisplayTick(worldIn, pos, rand);
}
@Override
public void onBlockAdded(World world, BlockPos pos, IBlockState state) {
getVariant(state).getBlockDef().onBlockAdded(world, pos);
}
@Override
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest) {
return getVariant(state).getBlockDef().removedByPlayer(world, player, pos);
}
@Override
public boolean canCreatureSpawn(IBlockState state, IBlockAccess world, BlockPos pos, EntityLiving.SpawnPlacementType type) {
return getVariant(state).getBlockDef().canCreatureSpawn(type, world, pos);
}
@Override
public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> list) {
for (EnumGeneric type : EnumGeneric.getCreativeList()) {
if (type.isEnabled())
CreativePlugin.addToList(list, type.getStack());
}
}
@Override
public float getExplosionResistance(World world, BlockPos pos, @Nullable Entity exploder, Explosion explosion) {
return getVariant(world, pos).getResistance() * 3f / 5f;
}
@Override
public int getFireSpreadSpeed(IBlockAccess world, BlockPos pos, EnumFacing face) {
return getVariant(world, pos).getBlockDef().getFireSpreadSpeed(world, pos, face);
}
@Override
public int getFlammability(IBlockAccess world, BlockPos pos, EnumFacing face) {
return getVariant(world, pos).getBlockDef().getFlammability(world, pos, face);
}
@Override
public boolean isFlammable(IBlockAccess world, BlockPos pos, EnumFacing face) {
return getVariant(world, pos).getBlockDef().isFlammable(world, pos, face);
}
@Override
public SoundType getSoundType(IBlockState state, World world, BlockPos pos, @Nullable Entity entity) {
switch (getVariant(state)) {
case CRUSHED_OBSIDIAN:
return SoundType.GROUND;
}
return super.getSoundType(state, world, pos, entity);
}
@Override
@SuppressWarnings("deprecation")
public void randomTick(World worldIn, BlockPos pos, IBlockState state, Random random) {
EnumGeneric generic = getVariant(state);
IBlockState newState = null;
switch (generic) {
case STONE_ABYSSAL:
newState = RailcraftBlocks.ABYSSAL_STONE.getDefaultState();
break;
case STONE_QUARRIED:
newState = RailcraftBlocks.QUARRIED_STONE.getDefaultState();
break;
case BLOCK_CONCRETE:
newState = RailcraftBlocks.REINFORCED_CONCRETE.getDefaultState();
break;
case BLOCK_COKE:
newState = RailcraftBlocks.COKE_BLOCK.getDefaultState();
break;
case BLOCK_CREOSOTE:
newState = RailcraftBlocks.CREOSOTE_BLOCK.getDefaultState();
break;
case BLOCK_BRASS:
newState = RailcraftBlocks.METAL.getState(BLOCK_BRASS);
break;
case BLOCK_BRONZE:
newState = RailcraftBlocks.METAL.getState(BLOCK_BRONZE);
break;
case BLOCK_COPPER:
newState = RailcraftBlocks.METAL.getState(BLOCK_COPPER);
break;
case BLOCK_INVAR:
newState = RailcraftBlocks.METAL.getState(BLOCK_INVAR);
break;
case BLOCK_LEAD:
newState = RailcraftBlocks.METAL.getState(BLOCK_LEAD);
break;
case BLOCK_NICKEL:
newState = RailcraftBlocks.METAL.getState(BLOCK_NICKEL);
break;
case BLOCK_SILVER:
newState = RailcraftBlocks.METAL.getState(BLOCK_SILVER);
break;
case BLOCK_STEEL:
newState = RailcraftBlocks.METAL.getState(BLOCK_STEEL);
break;
case BLOCK_TIN:
newState = RailcraftBlocks.METAL.getState(BLOCK_TIN);
break;
case BLOCK_ZINC:
newState = RailcraftBlocks.METAL.getState(BLOCK_ZINC);
break;
}
if (newState != null)
WorldPlugin.setBlockState(worldIn, pos, newState);
Arrays.stream(EnumFacing.VALUES).forEach(side -> {
try {
IBlockState neighbor = WorldPlugin.getBlockState(worldIn, pos.offset(side));
neighbor.getBlock().randomTick(worldIn, pos.offset(side), neighbor, random);
} catch (Exception ignored) {}
});
}
}