-
Notifications
You must be signed in to change notification settings - Fork 497
/
ItemPipe.java
163 lines (137 loc) · 4.32 KB
/
ItemPipe.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
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.transport;
import java.util.List;
import org.apache.logging.log4j.Level;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import buildcraft.BuildCraftTransport;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.IIconProvider;
import buildcraft.core.CreativeTabBuildCraft;
import buildcraft.core.IItemPipe;
import buildcraft.core.ItemBuildCraft;
import buildcraft.core.utils.ColorUtils;
import buildcraft.core.utils.StringUtils;
public class ItemPipe extends ItemBuildCraft implements IItemPipe {
@SideOnly(Side.CLIENT)
private IIconProvider iconProvider;
private int pipeIconIndex;
protected ItemPipe(CreativeTabBuildCraft creativeTab) {
super(creativeTab);
this.setMaxDamage(0);
this.setHasSubtypes(true);
}
@Override
public boolean onItemUse(ItemStack itemstack, EntityPlayer entityplayer, World world, int x, int y, int z,
int sideI, float par8, float par9, float par10) {
int side = sideI;
Block block = BuildCraftTransport.genericPipeBlock;
int i = x;
int j = y;
int k = z;
Block worldBlock = world.getBlock(i, j, k);
if (worldBlock == Blocks.snow) {
side = 1;
} else if (worldBlock != Blocks.vine && worldBlock != Blocks.tallgrass && worldBlock != Blocks.deadbush
&& (worldBlock == null || !worldBlock.isReplaceable(world, i, j, k))) {
if (side == 0) {
j--;
}
if (side == 1) {
j++;
}
if (side == 2) {
k--;
}
if (side == 3) {
k++;
}
if (side == 4) {
i--;
}
if (side == 5) {
i++;
}
}
if (itemstack.stackSize == 0) {
return false;
}
if (world.canPlaceEntityOnSide(block, i, j, k, false, side, entityplayer, itemstack)) {
Pipe<?> pipe = BlockGenericPipe.createPipe(this);
if (pipe == null) {
BCLog.logger.log(Level.WARN, "Pipe failed to create during placement at {0},{1},{2}", i, j, k);
return true;
}
if (BlockGenericPipe.placePipe(pipe, world, i, j, k, block, 0, entityplayer)) {
block.onBlockPlacedBy(world, i, j, k, entityplayer, itemstack);
if (!world.isRemote) {
TileEntity tile = world.getTileEntity(i, j, k);
((TileGenericPipe) tile).initializeFromItemMetadata(itemstack.getItemDamage());
}
// TODO: Fix sound
//world.playSoundEffect(i + 0.5F, j + 0.5F, k + 0.5F,
// block.stepSound.getPlaceSound(),
// (block.stepSound.getVolume() + 1.0F) / 2.0F,
// block.stepSound.getPitch() * 0.8F);
itemstack.stackSize--;
}
return true;
} else {
return false;
}
}
@SideOnly(Side.CLIENT)
public void setPipesIcons(IIconProvider iconProvider) {
this.iconProvider = iconProvider;
}
public void setPipeIconIndex(int index) {
this.pipeIconIndex = index;
}
@Override
@SideOnly(Side.CLIENT)
public IIcon getIconFromDamage(int par1) {
if (iconProvider != null) { // invalid pipes won't have this set
return iconProvider.getIcon(pipeIconIndex);
} else {
return null;
}
}
@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister par1IconRegister) {
// NOOP
}
@Override
@SideOnly(Side.CLIENT)
public int getSpriteNumber() {
return 0;
}
@Override
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean advanced) {
super.addInformation(stack, player, list, advanced);
if (stack.getItemDamage() >= 1) {
int color = (stack.getItemDamage() - 1) & 15;
list.add(ColorUtils.getFormattingTooltip(color) + EnumChatFormatting.ITALIC + StringUtils.localize("color." + ColorUtils.getName(color)));
}
Class<? extends Pipe> pipe = BlockGenericPipe.pipes.get(this);
List<String> toolTip = PipeToolTipManager.getToolTip(pipe, advanced);
list.addAll(toolTip);
}
}