/
DrawbridgeLogic.java
210 lines (170 loc) · 5.81 KB
/
DrawbridgeLogic.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
package slimeknights.tmechworks.blocks.logic;
import net.minecraft.block.Block;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.Container;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.util.FakePlayer;
import slimeknights.tmechworks.client.gui.GuiDrawbridge;
import slimeknights.tmechworks.inventory.ContainerDrawbridge;
import slimeknights.tmechworks.library.Util;
public class DrawbridgeLogic extends DrawbridgeLogicBase
{
public DrawbridgeLogic ()
{
super(Util.prefix("inventory.drawbridge"), 2);
}
@Override public void setupStatistics (DrawbridgeStats ds)
{
}
@Override public ItemStack getStackInSlot (int slot)
{
return super.getStackInSlot(slot == -1 ? 0 : slot);
}
@Override public void setInventorySlotContents (int slot, ItemStack item)
{
super.setInventorySlotContents(slot == -1 ? 0 : slot, item);
if (slot == 0)
{
if (item == null)
{
setInventorySlotContents(1, null);
}
else
{
setInventorySlotContents(1, new ItemStack(item.getItem(), 1, item.getItemDamage()));
}
}
markDirty();
}
public ItemStack getNextBlock ()
{
return getStackInSlot(0);
}
public ItemStack getLastBlock ()
{
return getStackInSlot(1);
}
public void subtractNextBlock ()
{
// Handled by onPlaceItemIntoWorld
/*decrStackSize(-1, 1);
ItemStack stack = getStackInSlot(0);
if (stack != null && stack.stackSize <= 0)
{
super.setInventorySlotContents(0, null);
}*/
}
public void addLastBlock ()
{
if (getStackInSlot(0) != null)
{
decrStackSize(-1, -1);
}
else
{
super.setInventorySlotContents(0, getStackInSlot(1));
}
}
@Override public boolean extendNext ()
{
EnumFacing face = getFacingDirection();
int extend = getExtendState() + 1;
BlockPos nextPos = new BlockPos(pos.getX() + face.getFrontOffsetX() * extend, pos.getY() + face.getFrontOffsetY() * extend, pos.getZ() + face.getFrontOffsetZ() * extend);
if (placeBlock(nextPos))
{
subtractNextBlock();
return true;
}
return false;
}
@Override public boolean retractNext ()
{
EnumFacing face = getFacingDirection();
int extend = getExtendState();
BlockPos nextPos = new BlockPos(pos.getX() + face.getFrontOffsetX() * extend, pos.getY() + face.getFrontOffsetY() * extend, pos.getZ() + face.getFrontOffsetZ() * extend);
if (breakBlock(nextPos))
{
world.setBlockState(nextPos, Blocks.AIR.getDefaultState());
addLastBlock();
return true;
}
setInventorySlotContents(0, getStackInSlot(0)); // Looks weird, but this'll update the ghost item on retract failure
return false;
}
@Override
public String getVariantName() {
return "normal";
}
public boolean placeBlock (BlockPos position)
{
FakePlayer fakePlayer = getFakePlayer(position.getX(), position.getY(), position.getZ());
if (fakePlayer == null)
return false;
ItemStack stack = getNextBlock();
if (stack == null)
{
return false;
}
Item item = stack.getItem();
if (item instanceof ItemBlock)
{
if (world.getBlockState(position).getBlock().isReplaceable(world, position))
{
fakePlayer.inventory.setInventorySlotContents(0, stack);
return ForgeHooks.onPlaceItemIntoWorld(stack, fakePlayer, world, position, getPlaceDirection(), 0, 0, 0, EnumHand.MAIN_HAND) == EnumActionResult.SUCCESS;
}
else
{
return false;
}
}
return false;
}
public boolean breakBlock (BlockPos position)
{
ItemStack stack = getLastBlock();
if (stack == null)
{
return false;
}
Item item = stack.getItem();
if (item instanceof ItemBlock)
{
ItemBlock ib = (ItemBlock) item;
if (ib.getBlock() == world.getBlockState(position).getBlock() && (!ib.getHasSubtypes() || ib.getMetadata(stack) == world.getBlockState(position).getBlock()
.getMetaFromState(world.getBlockState(position))))
{
return world.setBlockToAir(position);
}
return false;
}
Block block = Block.getBlockFromItem(item);
if (block != null)
{
if (block == world.getBlockState(position).getBlock() && (!item.getHasSubtypes() || stack.getMetadata() == world.getBlockState(position).getBlock()
.getMetaFromState(world.getBlockState(position))))
{
return world.setBlockToAir(position);
}
}
return false;
}
@Override public Container createContainer (InventoryPlayer inventoryplayer, World world, BlockPos pos)
{
return new ContainerDrawbridge(this, inventoryplayer);
}
@Override public GuiContainer createGui (InventoryPlayer inventoryplayer, World world, BlockPos pos)
{
return new GuiDrawbridge(new ContainerDrawbridge(this, inventoryplayer));
}
}