forked from wormzjl/ModularMachinery
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathBlockBus.java
More file actions
173 lines (150 loc) · 6.33 KB
/
Copy pathBlockBus.java
File metadata and controls
173 lines (150 loc) · 6.33 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
package hellfirepvp.modularmachinery.common.block;
import hellfirepvp.modularmachinery.ModularMachinery;
import hellfirepvp.modularmachinery.common.CommonProxy;
import hellfirepvp.modularmachinery.common.block.prop.ItemBusSize;
import hellfirepvp.modularmachinery.common.tiles.base.TileInventory;
import hellfirepvp.modularmachinery.common.tiles.base.TileItemBus;
import hellfirepvp.modularmachinery.common.util.IOInventory;
import hellfirepvp.modularmachinery.common.util.RedstoneHelper;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.world.IBlockAccess;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nullable;
import java.util.LinkedList;
import java.util.List;
public abstract class BlockBus extends BlockMachineComponent implements BlockCustomName, BlockVariants {
protected static final PropertyEnum<ItemBusSize> BUS_TYPE = PropertyEnum.create("size", ItemBusSize.class);
protected static final PropertyBool GLUED = PropertyBool.create("glued");
public BlockBus() {
super(Material.IRON);
setHardness(2F);
setResistance(10F);
setSoundType(SoundType.METAL);
setHarvestLevel("pickaxe", 1);
setCreativeTab(CommonProxy.creativeTabModularMachinery);
setDefaultState(this.blockState.getBaseState().withProperty(BUS_TYPE, ItemBusSize.TINY).withProperty(GLUED, false));
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
TileEntity te = worldIn.getTileEntity(pos);
if (te instanceof TileInventory) {
IOInventory inv = ((TileInventory) te).getInventory();
for (int i = 0; i < inv.getSlots(); i++) {
ItemStack stack = inv.getStackInSlot(i);
if (!stack.isEmpty()) {
spawnAsEntity(worldIn, pos, stack);
inv.setStackInSlot(i, ItemStack.EMPTY);
}
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void getSubBlocks(CreativeTabs itemIn, NonNullList<ItemStack> items) {
for (ItemBusSize size : ItemBusSize.values()) {
items.add(new ItemStack(this, 1, size.ordinal()));
}
}
@Override
public void addInformation(ItemStack stack, @Nullable World player, List<String> tooltip, ITooltipFlag advanced) {
ItemBusSize size = ItemBusSize.values()[MathHelper.clamp(stack.getMetadata(), 0, ItemBusSize.values().length - 1)];
tooltip.add(TextFormatting.GRAY + (size.getSlotCount() == 1 ?
I18n.format("tooltip.itembus.slot") : I18n.format("tooltip.itembus.slots", size.getSlotCount())));
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
if (!worldIn.isRemote) {
TileEntity te = worldIn.getTileEntity(pos);
if (te instanceof TileItemBus) {
playerIn.openGui(ModularMachinery.MODID, CommonProxy.GuiType.BUS_INVENTORY.ordinal(), worldIn, pos.getX(), pos.getY(), pos.getZ());
}
}
return true;
}
@Override
public EnumBlockRenderType getRenderType(IBlockState state) {
return EnumBlockRenderType.MODEL;
}
@Override
@SideOnly(Side.CLIENT)
public BlockRenderLayer getRenderLayer() {
return BlockRenderLayer.CUTOUT;
}
@Override
public int damageDropped(IBlockState state) {
return getMetaFromState(state);
}
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(BUS_TYPE, ItemBusSize.values()[meta]).withProperty(GLUED, false);
}
@Override
public int getMetaFromState(IBlockState state) {
return state.getValue(BUS_TYPE).ordinal();
}
@Override
public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
TileEntity tileEntity = worldIn.getTileEntity(pos);
if (!(tileEntity instanceof TileItemBus)) {
return state.withProperty(GLUED, false);
}
return state.withProperty(GLUED, ((TileItemBus) tileEntity).isExternalIODisabled());
}
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, BUS_TYPE, GLUED);
}
@Override
public Iterable<IBlockState> getValidStates() {
List<IBlockState> ret = new LinkedList<>();
for (ItemBusSize type : ItemBusSize.values()) {
ret.add(getDefaultState().withProperty(BUS_TYPE, type).withProperty(GLUED, false));
}
return ret;
}
@Override
public String getBlockStateName(IBlockState state) {
return state.getValue(BUS_TYPE).getName();
}
@Override
public boolean hasComparatorInputOverride(IBlockState state) {
return true;
}
@Override
public int getComparatorInputOverride(IBlockState blockState, World worldIn, BlockPos pos) {
return RedstoneHelper.getRedstoneLevel(worldIn.getTileEntity(pos));
}
@Nullable
@Override
public abstract TileEntity createTileEntity(World world, IBlockState state);
@Nullable
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return null;
}
@Override
public String getIdentifierForMeta(int meta) {
return getStateFromMeta(meta).getValue(BUS_TYPE).getName();
}
}