Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package net.minecraft.server;
2
3import org.bukkit.craftbukkit.inventory.CraftItemStack;
4import org.bukkit.event.block.BlockDispenseEvent;
5import org.bukkit.util.Vector;
6
7import java.util.Random;
8
9// CraftBukkit start
10// CraftBukkit end
11
12public class BlockDispenser extends BlockContainer {
13
14 private Random a = new Random();
15
16 protected BlockDispenser(int i) {
17 super(i, Material.STONE);
18 this.textureId = 45;
19 }
20
21 public int c() {
22 return 4;
23 }
24
25 public int a(int i, Random random) {
26 return Block.DISPENSER.id;
27 }
28
29 public void c(World world, int i, int j, int k) {
30 super.c(world, i, j, k);
31 this.g(world, i, j, k);
32 }
33
34 private void g(World world, int i, int j, int k) {
35 if (!world.isStatic) {
36 int l = world.getTypeId(i, j, k - 1);
37 int i1 = world.getTypeId(i, j, k + 1);
38 int j1 = world.getTypeId(i - 1, j, k);
39 int k1 = world.getTypeId(i + 1, j, k);
40 byte b0 = 3;
41
42 if (Block.o[l] && !Block.o[i1]) {
43 b0 = 3;
44 }
45
46 if (Block.o[i1] && !Block.o[l]) {
47 b0 = 2;
48 }
49
50 if (Block.o[j1] && !Block.o[k1]) {
51 b0 = 5;
52 }
53
54 if (Block.o[k1] && !Block.o[j1]) {
55 b0 = 4;
56 }
57
58 world.setData(i, j, k, b0);
59 }
60 }
61
62 public int a(int i) {
63 return i == 1 ? this.textureId + 17 : (i == 0 ? this.textureId + 17 : (i == 3 ? this.textureId + 1 : this.textureId));
64 }
65
66 public boolean interact(World world, int i, int j, int k, EntityHuman entityhuman) {
67 if (world.isStatic) {
68 return true;
69 } else {
70 TileEntityDispenser tileentitydispenser = (TileEntityDispenser) world.getTileEntity(i, j, k);
71
72 entityhuman.a(tileentitydispenser);
73 return true;
74 }
75 }
76
77 // CraftBukkit - private -> public
78 public void dispense(World world, int i, int j, int k, Random random) {
79 int l = world.getData(i, j, k);
80 byte b0 = 0;
81 byte b1 = 0;
82
83 if (l == 3) {
84 b1 = 1;
85 } else if (l == 2) {
86 b1 = -1;
87 } else if (l == 5) {
88 b0 = 1;
89 } else {
90 b0 = -1;
91 }
92
93 TileEntityDispenser tileentitydispenser = (TileEntityDispenser) world.getTileEntity(i, j, k);
94 // CraftBukkit start
95 int dispenseSlot = tileentitydispenser.findDispenseSlot();
96 ItemStack itemstack = null;
97 if (dispenseSlot > -1) {
98 itemstack = tileentitydispenser.getContents()[dispenseSlot];
99
100 // Copy item stack, because we want it to have 1 item
101 itemstack = new ItemStack(itemstack.id, 1, itemstack.damage);
102 }
103 // CraftBukkit end
104
105 double d0 = (double) i + (double) b0 * 0.6D + 0.5D;
106 double d1 = (double) j + 0.5D;
107 double d2 = (double) k + (double) b1 * 0.6D + 0.5D;
108
109 if (itemstack == null) {
110 world.e(1001, i, j, k, 0);
111 } else {
112 // CraftBukkit start
113 double d3 = random.nextDouble() * 0.1D + 0.2D;
114 double motX = (double) b0 * d3;
115 double motY = 0.20000000298023224D;
116 double motZ = (double) b1 * d3;
117 motX += random.nextGaussian() * 0.007499999832361937D * 6.0D;
118 motY += random.nextGaussian() * 0.007499999832361937D * 6.0D;
119 motZ += random.nextGaussian() * 0.007499999832361937D * 6.0D;
120
121 org.bukkit.block.Block block = world.getWorld().getBlockAt(i, j, k);
122 org.bukkit.inventory.ItemStack bukkitItem = new CraftItemStack(itemstack).clone();
123
124 BlockDispenseEvent event = new BlockDispenseEvent(block, bukkitItem, new Vector(motX, motY, motZ));
125 world.getServer().getPluginManager().callEvent(event);
126
127 if (event.isCancelled()) {
128 return;
129 }
130
131 // Actually remove the item
132 tileentitydispenser.splitStack(dispenseSlot, 1);
133
134 motX = event.getVelocity().getX();
135 motY = event.getVelocity().getY();
136 motZ = event.getVelocity().getZ();
137
138 itemstack = new ItemStack(event.getItem().getTypeId(), event.getItem().getAmount(), event.getItem().getDurability());
139 // CraftBukkit end
140
141 if (itemstack.id == Item.ARROW.id) {
142 EntityArrow entityarrow = new EntityArrow(world, d0, d1, d2);
143
144 entityarrow.a((double) b0, 0.10000000149011612D, (double) b1, 1.1F, 6.0F);
145 entityarrow.fromPlayer = true;
146 world.addEntity(entityarrow);
147 world.e(1002, i, j, k, 0);
148 } else if (itemstack.id == Item.EGG.id) {
149 EntityEgg entityegg = new EntityEgg(world, d0, d1, d2);
150
151 entityegg.a((double) b0, 0.10000000149011612D, (double) b1, 1.1F, 6.0F);
152 world.addEntity(entityegg);
153 world.e(1002, i, j, k, 0);
154 } else if (itemstack.id == Item.SNOW_BALL.id) {
155 EntitySnowball entitysnowball = new EntitySnowball(world, d0, d1, d2);
156
157 entitysnowball.a((double) b0, 0.10000000149011612D, (double) b1, 1.1F, 6.0F);
158 world.addEntity(entitysnowball);
159 world.e(1002, i, j, k, 0);
160 } else {
161 EntityItem entityitem = new EntityItem(world, d0, d1 - 0.3D, d2, itemstack);
162 // CraftBukkit start
163 // double d3 = random.nextDouble() * 0.1D + 0.2D; // Moved up
164 entityitem.motX = motX;
165 entityitem.motY = motY;
166 entityitem.motZ = motZ;
167 // CraftBukkit end
168 world.addEntity(entityitem);
169 world.e(1000, i, j, k, 0);
170 }
171
172 world.e(2000, i, j, k, b0 + 1 + (b1 + 1) * 3);
173 }
174 }
175
176 public void doPhysics(World world, int i, int j, int k, int l) {
177 if (l > 0 && Block.byId[l].isPowerSource()) {
178 boolean flag = world.isBlockIndirectlyPowered(i, j, k) || world.isBlockIndirectlyPowered(i, j + 1, k);
179
180 if (flag) {
181 world.c(i, j, k, this.id, this.c());
182 }
183 }
184 }
185
186 public void a(World world, int i, int j, int k, Random random) {
187 if (world.isBlockIndirectlyPowered(i, j, k) || world.isBlockIndirectlyPowered(i, j + 1, k)) {
188 this.dispense(world, i, j, k, random);
189 }
190 }
191
192 protected TileEntity a_() {
193 return new TileEntityDispenser();
194 }
195
196 public void postPlace(World world, int i, int j, int k, EntityLiving entityliving) {
197 int l = MathHelper.floor((double) (entityliving.yaw * 4.0F / 360.0F) + 0.5D) & 3;
198
199 if (l == 0) {
200 world.setData(i, j, k, 2);
201 }
202
203 if (l == 1) {
204 world.setData(i, j, k, 5);
205 }
206
207 if (l == 2) {
208 world.setData(i, j, k, 3);
209 }
210
211 if (l == 3) {
212 world.setData(i, j, k, 4);
213 }
214 }
215
216 public void remove(World world, int i, int j, int k) {
217 TileEntityDispenser tileentitydispenser = (TileEntityDispenser) world.getTileEntity(i, j, k);
218
219 for (int l = 0; l < tileentitydispenser.getSize(); ++l) {
220 ItemStack itemstack = tileentitydispenser.getItem(l);
221
222 if (itemstack != null) {
223 float f = this.a.nextFloat() * 0.8F + 0.1F;
224 float f1 = this.a.nextFloat() * 0.8F + 0.1F;
225 float f2 = this.a.nextFloat() * 0.8F + 0.1F;
226
227 while (itemstack.count > 0) {
228 int i1 = this.a.nextInt(21) + 10;
229
230 if (i1 > itemstack.count) {
231 i1 = itemstack.count;
232 }
233
234 itemstack.count -= i1;
235 EntityItem entityitem = new EntityItem(world, (double) ((float) i + f), (double) ((float) j + f1), (double) ((float) k + f2), new ItemStack(itemstack.id, i1, itemstack.getData()));
236 float f3 = 0.05F;
237
238 entityitem.motX = (double) ((float) this.a.nextGaussian() * f3);
239 entityitem.motY = (double) ((float) this.a.nextGaussian() * f3 + 0.2F);
240 entityitem.motZ = (double) ((float) this.a.nextGaussian() * f3);
241 world.addEntity(entityitem);
242 }
243 tileentitydispenser.setItem(l, null);
244 }
245 }
246
247 super.remove(world, i, j, k);
248 }
249}