/
ReduxBlock.java
304 lines (264 loc) · 13 KB
/
ReduxBlock.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package mods.quiddity.redux;
import mods.quiddity.redux.json.model.Block;
import mods.quiddity.redux.json.model.Flags;
import mods.quiddity.redux.json.model.Pack;
import mods.quiddity.redux.json.model.Trigger;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.statemap.StateMap;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.relauncher.Side;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Stub class that represents all Redux pack blocks.
*
* @author winsock on 2/3/15.
*/
public class ReduxBlock extends net.minecraft.block.Block implements ITileEntityProvider {
private final Pack pack;
private final Block reduxBlock;
public static final PropertyInteger SUCCESS_COUNT_META = PropertyInteger.create("lastSuccessCount", 0, 15);
public static final ThreadLocal<Block> blockThreadLocal = new ThreadLocal<Block>();
private final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
private Map<String, PropertyInteger> customBlockProperties = new HashMap<String, PropertyInteger>();
private int lastRedstoneStrongStrength[] = {0, 0, 0, 0, 0, 0}, lastRedstoneWeakStrength = 0;
public ReduxBlock(Pack parentPack, Block reduxBlock) {
super(reduxBlock.getMaterial());
this.pack = parentPack;
this.reduxBlock = reduxBlock;
setUnlocalizedName(reduxBlock.getName());
setCreativeTab(reduxBlock.getCreativeTab());
IBlockState defaultBlockState = this.blockState.getBaseState().withProperty(SUCCESS_COUNT_META, 0);
if (reduxBlock.shouldAddFacingProperty())
defaultBlockState = defaultBlockState.withProperty(FACING, null);
if (reduxBlock.getCustomProperties() != null) {
for (Flags<String, Integer> customProperty : reduxBlock.getCustomProperties()) {
PropertyInteger customIntegerProperty = PropertyInteger.create(customProperty.getKey(), Integer.MIN_VALUE, Integer.MAX_VALUE);
customBlockProperties.put(customProperty.getKey(), customIntegerProperty);
defaultBlockState = defaultBlockState.withProperty(customIntegerProperty, customProperty.getValue());
}
}
this.setDefaultState(defaultBlockState);
if (FMLCommonHandler.instance().getSide() == Side.CLIENT) {
StateMap.Builder stateMapBuilder = (new StateMap.Builder()).addPropertiesToIgnore(SUCCESS_COUNT_META);
if (reduxBlock.getIgnoredProperties() != null) {
for (String s : reduxBlock.getIgnoredProperties()) {
PropertyInteger propertyInteger = customBlockProperties.get(s);
if (propertyInteger != null) {
stateMapBuilder.addPropertiesToIgnore(propertyInteger);
}
}
}
FMLClientHandler.instance().getClient().getBlockRendererDispatcher().getBlockModelShapes().registerBlockWithStateMapper(this, stateMapBuilder.build());
}
}
@Override
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing blockFaceClickedOn, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
super.onBlockPlaced(worldIn, pos, blockFaceClickedOn, hitX, hitY, hitZ, meta, placer);
if (worldIn.getTileEntity(pos) instanceof ReduxCommandBlockTileEntity && reduxBlock.shouldAddFacingProperty()) {
// Find the quadrant the player is facing, add 0.5 for half a quadrant to get proper round to east/west/north/south, then
int playerFacingDirection = (placer == null) ? 0 : MathHelper.floor_double((placer.rotationYaw / 90.0F) + 0.5D) & 3;
return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(playerFacingDirection));
}
return this.getDefaultState();
}
@Override
public void addCollisionBoxesToList(World worldIn, BlockPos pos, IBlockState state, AxisAlignedBB mask, List list, Entity collidingEntity) {
if (!reduxBlock.hasMultipleCollisionBoxes()) {
super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
} else {
for (Block.CollisionBox collisionBox : reduxBlock.getCollisionBoxes()) {
this.setBlockBounds(collisionBox.getMinX(), collisionBox.getMinY(), collisionBox.getMinZ(),
collisionBox.getMaxX(), collisionBox.getMaxY(), collisionBox.getMaxZ());
super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
}
this.setBlockBoundsForItemRender();
}
}
@Override
public boolean isCollidable() {
return reduxBlock.getCollisionBoxes() == null || !reduxBlock.getCollisionBoxes().isEmpty();
}
@Override
public boolean hasTileEntity(IBlockState state) {
return reduxBlock.tickable() || !reduxBlock.getScript().isEmpty();
}
@Override
public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, Entity entityIn) {
if (worldIn.getTileEntity(pos) instanceof ReduxCommandBlockTileEntity) {
ReduxCommandBlockTileEntity commandBlockTileEntity = (ReduxCommandBlockTileEntity) worldIn.getTileEntity(pos);
commandBlockTileEntity.triggerSpecialEvent(Trigger.TriggerEvent.OnEntityCollide, entityIn);
}
}
@Override
public TileEntity createTileEntity(World world, IBlockState state) {
return createNewTileEntity(world, 0);
}
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
ReduxCommandBlockTileEntity te = null;
if (reduxBlock.tickable()) {
te = new ReduxCommandBlockTickableTileEntity();
} else if (!reduxBlock.getScript().isEmpty()) {
te = new ReduxCommandBlockTileEntity();
}
// Do initial initialization of the tile entity
if (te != null)
te.init(pack.getId(), reduxBlock);
return te;
}
@Override
public boolean hasComparatorInputOverride() {
return hasTileEntity(null);
}
@Override
public int getComparatorInputOverride(World worldIn, BlockPos pos) {
TileEntity tileentity = worldIn.getTileEntity(pos);
return tileentity instanceof ReduxCommandBlockTileEntity ? ((ReduxCommandBlockTileEntity)tileentity).getLastSuccessCount() > 15
? 15 : ((ReduxCommandBlockTileEntity)tileentity).getLastSuccessCount(): 0;
}
public PropertyInteger getPropertyFromName(String name) {
return customBlockProperties.get(name);
}
@Override
public int getMetaFromState(IBlockState state) {
if (hasTileEntity(state) && !state.getPropertyNames().isEmpty()) {
return (Integer)state.getValue(SUCCESS_COUNT_META);
}
return 0;
}
public Class<? extends ReduxCommandBlockTileEntity> getTileEntityClass() {
if (reduxBlock.tickable())
return ReduxCommandBlockTickableTileEntity.class;
if (!reduxBlock.getScript().isEmpty())
return ReduxCommandBlockTileEntity.class;
return null;
}
@Override
protected BlockState createBlockState() {
Block reduxBlock;
if (this.reduxBlock == null)
reduxBlock = blockThreadLocal.get();
else
reduxBlock = this.reduxBlock;
if (reduxBlock == null) throw new AssertionError();
if (reduxBlock.shouldAddFacingProperty())
return new BlockState(this, SUCCESS_COUNT_META, FACING);
else
return new BlockState(this, SUCCESS_COUNT_META);
}
@Override
public boolean isOpaqueCube() {
Block reduxBlock;
if (this.reduxBlock == null)
reduxBlock = blockThreadLocal.get();
else
reduxBlock = this.reduxBlock;
if (reduxBlock == null) throw new AssertionError();
return reduxBlock.isFullCube();
}
@Override
public boolean isFullCube() {
Block reduxBlock;
if (this.reduxBlock == null)
reduxBlock = blockThreadLocal.get();
else
reduxBlock = this.reduxBlock;
if (reduxBlock == null) throw new AssertionError();
return reduxBlock.isFullCube();
}
@Override
public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, net.minecraft.block.Block neighborBlock) {
if (!worldIn.isRemote && !this.canPlaceBlockAt(worldIn, pos) && reduxBlock.isWeak()) {
this.dropBlockAsItem(worldIn, pos, state, 0);
worldIn.setBlockToAir(pos);
}
boolean redstoneChanged = false;
for (EnumFacing dir : EnumFacing.values()) {
if (lastRedstoneStrongStrength[dir.getIndex()] != worldIn.getRedstonePower(pos, dir)) {
lastRedstoneStrongStrength[dir.getIndex()] = worldIn.getRedstonePower(pos, dir);
redstoneChanged = true;
}
}
if (lastRedstoneWeakStrength != worldIn.isBlockIndirectlyGettingPowered(pos)) {
lastRedstoneWeakStrength = worldIn.isBlockIndirectlyGettingPowered(pos);
redstoneChanged = true;
}
if (redstoneChanged && worldIn.getTileEntity(pos) instanceof ReduxCommandBlockTileEntity) {
ReduxCommandBlockTileEntity commandBlockTileEntity = (ReduxCommandBlockTileEntity) worldIn.getTileEntity(pos);
commandBlockTileEntity.triggerSpecialEvent(Trigger.TriggerEvent.OnRestoneStrengthChange, lastRedstoneStrongStrength, lastRedstoneWeakStrength);
}
}
@Override
public boolean canPlaceBlockAt(World worldIn, BlockPos pos) {
return reduxBlock.isWeak() ? (World.doesBlockHaveSolidTopSurface(worldIn, pos.down()) || worldIn.getBlockState(pos.down()).getBlock() == Blocks.glowstone)
: super.canPlaceBlockAt(worldIn, pos);
}
/**
* Left click?
* @param worldIn The world
* @param pos The position of the block
* @param playerIn The player whom clicked
*/
@Override
public void onBlockClicked(World worldIn, BlockPos pos, EntityPlayer playerIn) {
if (worldIn.getTileEntity(pos) instanceof ReduxCommandBlockTileEntity) {
ReduxCommandBlockTileEntity commandBlockTileEntity = (ReduxCommandBlockTileEntity) worldIn.getTileEntity(pos);
commandBlockTileEntity.triggerSpecialEvent(Trigger.TriggerEvent.OnEntityCollide, false, playerIn);
}
}
// Right click?
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) {
if (worldIn.getTileEntity(pos) instanceof ReduxCommandBlockTileEntity) {
ReduxCommandBlockTileEntity commandBlockTileEntity = (ReduxCommandBlockTileEntity) worldIn.getTileEntity(pos);
return commandBlockTileEntity.triggerSpecialEvent(Trigger.TriggerEvent.OnEntityCollide, true, playerIn, side, hitX, hitY, hitZ);
}
return false;
}
@Override
public boolean canProvidePower() {
return reduxBlock.getRedstoneOutputProperty() != null && !reduxBlock.getRedstoneOutputProperty().isEmpty();
}
@Override
public int isProvidingWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side) {
if (customBlockProperties.get(reduxBlock.getRedstoneOutputProperty()) != null) {
PropertyInteger redstoneProperty = customBlockProperties.get(reduxBlock.getRedstoneOutputProperty());
return ((Integer)state.getValue(redstoneProperty)) > 15 ? 15 : ((Integer)state.getValue(redstoneProperty));
}
return 0;
}
@Override
public int isProvidingStrongPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side) {
if (customBlockProperties.get(reduxBlock.getRedstoneOutputProperty()) != null) {
PropertyInteger redstoneProperty = customBlockProperties.get(reduxBlock.getRedstoneOutputProperty());
return ((Integer)state.getValue(redstoneProperty)) > 15 ? 15 : ((Integer)state.getValue(redstoneProperty));
}
return 0;
}
/**
* Always render the item with full bounds.
* Andrew Querol: I cannot think of a need for anything else.
*/
@Override
public void setBlockBoundsForItemRender() {
this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
}
}