-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathBlockCable.java
More file actions
209 lines (183 loc) · 8.14 KB
/
Copy pathBlockCable.java
File metadata and controls
209 lines (183 loc) · 8.14 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
package gregtech.common.pipelike.cable;
import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.damagesources.DamageSources;
import gregtech.api.items.toolitem.ToolClasses;
import gregtech.api.items.toolitem.ToolHelper;
import gregtech.api.pipenet.block.material.BlockMaterialPipe;
import gregtech.api.pipenet.tile.IPipeTile;
import gregtech.api.pipenet.tile.TileEntityPipeBase;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.properties.WireProperties;
import gregtech.api.unification.material.registry.MaterialRegistry;
import gregtech.api.util.GTUtility;
import gregtech.client.renderer.pipe.CableRenderer;
import gregtech.client.renderer.pipe.PipeRenderer;
import gregtech.common.creativetab.GTCreativeTabs;
import gregtech.common.pipelike.cable.net.WorldENet;
import gregtech.common.pipelike.cable.tile.TileEntityCable;
import gregtech.common.pipelike.cable.tile.TileEntityCableTickable;
import gregtech.core.advancement.AdvancementTriggers;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class BlockCable extends BlockMaterialPipe<Insulation, WireProperties, WorldENet>
implements ITileEntityProvider {
private final Map<Material, WireProperties> enabledMaterials = new TreeMap<>();
public BlockCable(Insulation cableType, MaterialRegistry registry) {
super(cableType, registry);
setCreativeTab(GTCreativeTabs.TAB_GREGTECH_CABLES);
setHarvestLevel(ToolClasses.WIRE_CUTTER, 1);
}
public void addCableMaterial(Material material, WireProperties wireProperties) {
Preconditions.checkNotNull(material, "material was null");
Preconditions.checkNotNull(wireProperties, "material %s wireProperties was null", material);
Preconditions.checkArgument(material.getRegistry().getNameForObject(material) != null,
"material %s is not registered", material);
if (!pipeType.orePrefix.isIgnored(material)) {
this.enabledMaterials.put(material, wireProperties);
}
}
public Collection<Material> getEnabledMaterials() {
return Collections.unmodifiableSet(enabledMaterials.keySet());
}
@Override
public Class<Insulation> getPipeTypeClass() {
return Insulation.class;
}
@Override
protected WireProperties createProperties(Insulation insulation, Material material) {
return insulation.modifyProperties(enabledMaterials.getOrDefault(material, getFallbackType()));
}
@Override
public boolean isFluidloggable(IBlockState state, World world, BlockPos pos) {
return pipeType.thickness < 1.0f;
}
@SideOnly(Side.CLIENT)
@NotNull
@Override
public PipeRenderer getPipeRenderer() {
return CableRenderer.INSTANCE;
}
@Override
protected WireProperties getFallbackType() {
return enabledMaterials.values().iterator().next();
}
@Override
public WorldENet getWorldPipeNet(World world) {
return WorldENet.getWorldENet(world);
}
@Override
public void getSubBlocks(@NotNull CreativeTabs itemIn, @NotNull NonNullList<ItemStack> items) {
for (Material material : enabledMaterials.keySet()) {
items.add(getItem(material));
}
}
@Override
protected boolean isPipeTool(@NotNull ItemStack stack) {
return ToolHelper.isTool(stack, ToolClasses.WIRE_CUTTER);
}
@Override
public int getLightValue(@NotNull IBlockState state, IBlockAccess world, @NotNull BlockPos pos) {
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileEntityCable) {
TileEntityCable cable = (TileEntityCable) tile;
int temp = cable.getTemperature();
// max light at 5000 K
// min light at 500 K
if (temp >= 5000) {
return 15;
}
if (temp > 500) {
return (temp - 500) * 15 / (4500);
}
}
return 0;
}
@Override
public void breakBlock(@NotNull World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state) {
if (worldIn.isRemote) {
TileEntityCable cable = (TileEntityCable) getPipeTileEntity(worldIn, pos);
cable.killParticle();
}
super.breakBlock(worldIn, pos, state);
}
@Override
public boolean canPipesConnect(IPipeTile<Insulation, WireProperties> selfTile, EnumFacing side,
IPipeTile<Insulation, WireProperties> sideTile) {
return selfTile instanceof TileEntityCable && sideTile instanceof TileEntityCable;
}
@Override
public boolean canPipeConnectToBlock(IPipeTile<Insulation, WireProperties> selfTile, EnumFacing side,
TileEntity tile) {
return tile != null &&
tile.getCapability(GregtechCapabilities.CAPABILITY_ENERGY_CONTAINER, side.getOpposite()) != null;
}
@Override
public boolean isHoldingPipe(EntityPlayer player) {
if (player == null) {
return false;
}
ItemStack stack = player.getHeldItemMainhand();
return stack != ItemStack.EMPTY && stack.getItem() instanceof ItemBlockCable;
}
@Override
public void onEntityCollision(World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state,
@NotNull Entity entityIn) {
super.onEntityCollision(worldIn, pos, state, entityIn);
if (worldIn.isRemote) return;
Insulation insulation = getPipeTileEntity(worldIn, pos).getPipeType();
if (insulation.insulationLevel == -1 && entityIn instanceof EntityLivingBase) {
EntityLivingBase entityLiving = (EntityLivingBase) entityIn;
TileEntityCable cable = (TileEntityCable) getPipeTileEntity(worldIn, pos);
if (cable != null && cable.getFrameMaterial() == null && cable.getNodeData().getLossPerBlock() > 0) {
long voltage = cable.getCurrentMaxVoltage();
double amperage = cable.getAverageAmperage();
if (voltage > 0L && amperage > 0L) {
float damageAmount = (float) ((GTUtility.getTierByVoltage(voltage) + 1) * amperage * 4);
entityLiving.attackEntityFrom(DamageSources.getElectricDamage(), damageAmount);
if (entityLiving instanceof EntityPlayerMP) {
AdvancementTriggers.ELECTROCUTION_DEATH.trigger((EntityPlayerMP) entityLiving);
}
}
}
}
}
@NotNull
@Override
@SideOnly(Side.CLIENT)
@SuppressWarnings("deprecation")
public EnumBlockRenderType getRenderType(@NotNull IBlockState state) {
return CableRenderer.INSTANCE.getBlockRenderType();
}
@Override
public TileEntityPipeBase<Insulation, WireProperties> createNewTileEntity(boolean supportsTicking) {
return supportsTicking ? new TileEntityCableTickable() : new TileEntityCable();
}
@Override
@SideOnly(Side.CLIENT)
protected Pair<TextureAtlasSprite, Integer> getParticleTexture(World world, BlockPos blockPos) {
return CableRenderer.INSTANCE.getParticleTexture((TileEntityCable) world.getTileEntity(blockPos));
}
}