-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathBlockFrame.java
More file actions
310 lines (279 loc) · 12.2 KB
/
Copy pathBlockFrame.java
File metadata and controls
310 lines (279 loc) · 12.2 KB
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
305
306
307
308
309
310
package gregtech.common.blocks;
import gregtech.api.items.toolitem.ToolClasses;
import gregtech.api.items.toolitem.ToolHelper;
import gregtech.api.pipenet.block.BlockPipe;
import gregtech.api.pipenet.block.ItemBlockPipe;
import gregtech.api.pipenet.tile.IPipeTile;
import gregtech.api.pipenet.tile.TileEntityPipeBase;
import gregtech.api.recipes.ModHandler;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.info.MaterialIconType;
import gregtech.api.util.GTLog;
import gregtech.client.model.MaterialStateMapper;
import gregtech.client.model.modelfactories.MaterialBlockModelLoader;
import gregtech.common.ConfigHolder;
import gregtech.common.blocks.properties.PropertyMaterial;
import gregtech.common.creativetab.GTCreativeTabs;
import gregtech.integration.fluidlogged_api.IFluidloggableWrapper;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.EnumPushReaction;
import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving.SpawnPlacementType;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
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.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public abstract class BlockFrame extends BlockMaterialBase implements IFluidloggableWrapper {
public static final AxisAlignedBB COLLISION_BOX = new AxisAlignedBB(0.05, 0.0, 0.05, 0.95, 1.0, 0.95);
public static BlockFrame create(Material[] materials) {
PropertyMaterial property = PropertyMaterial.create("variant", materials);
return new BlockFrame() {
@NotNull
@Override
public PropertyMaterial getVariantProperty() {
return property;
}
};
}
private BlockFrame() {
super(net.minecraft.block.material.Material.IRON);
setTranslationKey("frame");
setHardness(3.0f);
setResistance(6.0f);
setCreativeTab(GTCreativeTabs.TAB_GREGTECH_MATERIALS);
}
@Override
public String getHarvestTool(@NotNull IBlockState state) {
Material material = getGtMaterial(state);
if (ModHandler.isMaterialWood(material)) {
return ToolClasses.AXE;
}
return ToolClasses.WRENCH;
}
@NotNull
@Override
public SoundType getSoundType(@NotNull IBlockState state, @NotNull World world, @NotNull BlockPos pos,
@Nullable Entity entity) {
Material material = getGtMaterial(state);
if (ModHandler.isMaterialWood(material)) {
return SoundType.WOOD;
}
return SoundType.METAL;
}
public SoundType getSoundType(ItemStack stack) {
Material material = getGtMaterial(stack);
if (ModHandler.isMaterialWood(material)) {
return SoundType.WOOD;
}
return SoundType.METAL;
}
@Override
public int getHarvestLevel(@NotNull IBlockState state) {
return 1;
}
@Override
@NotNull
@SuppressWarnings("deprecation")
public net.minecraft.block.material.Material getMaterial(@NotNull IBlockState state) {
Material material = getGtMaterial(state);
if (ModHandler.isMaterialWood(material)) {
return net.minecraft.block.material.Material.WOOD;
}
return super.getMaterial(state);
}
@Override
public boolean canCreatureSpawn(@NotNull IBlockState state, @NotNull IBlockAccess world, @NotNull BlockPos pos,
@NotNull SpawnPlacementType type) {
return false;
}
public boolean replaceWithFramedPipe(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
ItemStack stackInHand, EnumFacing facing) {
BlockPipe<?, ?, ?> blockPipe = (BlockPipe<?, ?, ?>) ((ItemBlockPipe<?, ?>) stackInHand.getItem()).getBlock();
if (blockPipe.getItemPipeType(stackInHand).getThickness() < 1) {
ItemBlock itemBlock = (ItemBlock) stackInHand.getItem();
IBlockState pipeState = blockPipe.getDefaultState();
// these 0 values are not actually used by forge
itemBlock.placeBlockAt(stackInHand, playerIn, worldIn, pos, facing, 0, 0, 0, pipeState);
IPipeTile<?, ?> pipeTile = blockPipe.getPipeTileEntity(worldIn, pos);
if (pipeTile instanceof TileEntityPipeBase) {
((TileEntityPipeBase<?, ?>) pipeTile).setFrameMaterial(getGtMaterial(state));
} else {
GTLog.logger.error("Pipe was not placed!");
return false;
}
SoundType type = blockPipe.getSoundType(state, worldIn, pos, playerIn);
worldIn.playSound(playerIn, pos, type.getPlaceSound(), SoundCategory.BLOCKS,
(type.getVolume() + 1.0F) / 2.0F, type.getPitch() * 0.8F);
if (!playerIn.capabilities.isCreativeMode) {
stackInHand.shrink(1);
}
return true;
}
return false;
}
public boolean removeFrame(World world, BlockPos pos, EntityPlayer player, ItemStack stack) {
TileEntity te = world.getTileEntity(pos);
if (te instanceof TileEntityPipeBase<?, ?>) {
TileEntityPipeBase<?, ?> pipeTile = (TileEntityPipeBase<?, ?>) te;
Material frameMaterial = pipeTile.getFrameMaterial();
if (frameMaterial != null) {
pipeTile.setFrameMaterial(null);
Block.spawnAsEntity(world, pos, this.getItem(frameMaterial));
ToolHelper.damageItem(stack, player);
ToolHelper.playToolSound(stack, player);
return true;
}
}
return false;
}
@Override
public boolean onBlockActivated(@NotNull World world, @NotNull BlockPos pos, @NotNull IBlockState state,
@NotNull EntityPlayer player, @NotNull EnumHand hand, @NotNull EnumFacing facing,
float hitX, float hitY, float hitZ) {
ItemStack stack = player.getHeldItem(hand);
if (stack.isEmpty()) {
return false;
}
// replace frame with pipe and set the frame material to this frame
if (stack.getItem() instanceof ItemBlockPipe) {
return replaceWithFramedPipe(world, pos, state, player, stack, facing);
}
if (stack.getItem().getToolClasses(stack).contains(ToolClasses.CROWBAR)) {
return removeFrame(world, pos, player, stack);
}
BlockFrame frameBlock = getFrameBlockFromItem(stack);
if (frameBlock == null) return false;
BlockPos.PooledMutableBlockPos blockPos = BlockPos.PooledMutableBlockPos.retain();
blockPos.setPos(pos);
for (int i = 0; i < 32; i++) {
if (world.getBlockState(blockPos).getBlock() instanceof BlockFrame) {
blockPos.move(EnumFacing.UP);
continue;
}
TileEntity te = world.getTileEntity(blockPos);
if (te instanceof IPipeTile && ((IPipeTile<?, ?>) te).getFrameMaterial() != null) {
blockPos.move(EnumFacing.UP);
continue;
}
if (canPlaceBlockAt(world, blockPos)) {
world.setBlockState(blockPos,
frameBlock.getStateFromMeta(stack.getItem().getMetadata(stack.getItemDamage())));
SoundType type = getSoundType(stack);
world.playSound(null, pos, type.getPlaceSound(), SoundCategory.BLOCKS, (type.getVolume() + 1.0F) / 2.0F,
type.getPitch() * 0.8F);
if (!player.capabilities.isCreativeMode) {
stack.shrink(1);
}
blockPos.release();
return true;
} else if (te instanceof TileEntityPipeBase && ((TileEntityPipeBase<?, ?>) te).getFrameMaterial() == null) {
((TileEntityPipeBase<?, ?>) te).setFrameMaterial(frameBlock.getGtMaterial(stack));
SoundType type = getSoundType(stack);
world.playSound(null, pos, type.getPlaceSound(), SoundCategory.BLOCKS, (type.getVolume() + 1.0F) / 2.0F,
type.getPitch() * 0.8F);
if (!player.capabilities.isCreativeMode) {
stack.shrink(1);
}
blockPos.release();
return true;
} else {
blockPos.release();
return false;
}
}
blockPos.release();
return false;
}
@Override
public void onEntityCollision(@NotNull World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state,
Entity entityIn) {
entityIn.motionX = MathHelper.clamp(entityIn.motionX, -0.15, 0.15);
entityIn.motionZ = MathHelper.clamp(entityIn.motionZ, -0.15, 0.15);
entityIn.fallDistance = 0.0F;
if (entityIn.motionY < -0.15D) {
entityIn.motionY = -0.15D;
}
if (entityIn.isSneaking() && entityIn.motionY < 0.0D) {
entityIn.motionY = 0.0D;
}
if (entityIn.collidedHorizontally) {
entityIn.motionY = 0.3;
}
}
@NotNull
@Override
@SuppressWarnings("deprecation")
public EnumPushReaction getPushReaction(@NotNull IBlockState state) {
return EnumPushReaction.DESTROY;
}
@Override
@SuppressWarnings("deprecation")
public AxisAlignedBB getCollisionBoundingBox(@NotNull IBlockState blockState, @NotNull IBlockAccess worldIn,
@NotNull BlockPos pos) {
return COLLISION_BOX;
}
@NotNull
@Override
public BlockRenderLayer getRenderLayer() {
return BlockRenderLayer.CUTOUT_MIPPED;
}
@Override
@SuppressWarnings("deprecation")
public boolean isOpaqueCube(@NotNull IBlockState state) {
return false;
}
@NotNull
@Override
@SuppressWarnings("deprecation")
public BlockFaceShape getBlockFaceShape(@NotNull IBlockAccess worldIn, @NotNull IBlockState state,
@NotNull BlockPos pos, @NotNull EnumFacing face) {
return BlockFaceShape.UNDEFINED;
}
@Override
public void addInformation(@NotNull ItemStack stack, @Nullable World world, @NotNull List<String> tooltip,
@NotNull ITooltipFlag flag) {
if (ConfigHolder.misc.debug) {
tooltip.add("MetaItem Id: frame" + getGtMaterial(stack).toCamelCaseString());
}
}
@SideOnly(Side.CLIENT)
public void onModelRegister() {
ModelLoader.setCustomStateMapper(this, new MaterialStateMapper(
MaterialIconType.frameGt, s -> getGtMaterial(s).getMaterialIconSet()));
for (IBlockState state : this.getBlockState().getValidStates()) {
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), this.getMetaFromState(state),
MaterialBlockModelLoader.registerItemModel(
MaterialIconType.frameGt,
getGtMaterial(state).getMaterialIconSet()));
}
}
@Nullable
public static BlockFrame getFrameBlockFromItem(ItemStack stack) {
Item item = stack.getItem();
if (item instanceof ItemBlock) {
Block block = ((ItemBlock) item).getBlock();
if (block instanceof BlockFrame) return (BlockFrame) block;
}
return null;
}
}