Commits
Tim Blattner authored and md_5 committed b2de47d581b
10 10 | import net.minecraft.server.BlockEnchantmentTable; |
11 11 | import net.minecraft.server.BlockPosition; |
12 12 | import net.minecraft.server.BlockWorkbench; |
13 13 | import net.minecraft.server.Blocks; |
14 14 | import net.minecraft.server.ChatComponentText; |
15 15 | import net.minecraft.server.Container; |
16 16 | import net.minecraft.server.Containers; |
17 17 | import net.minecraft.server.CraftingManager; |
18 18 | import net.minecraft.server.Entity; |
19 19 | import net.minecraft.server.EntityHuman; |
20 - | import net.minecraft.server.EntityMinecartHopper; |
21 20 | import net.minecraft.server.EntityPlayer; |
22 21 | import net.minecraft.server.EntityTypes; |
23 22 | import net.minecraft.server.EnumMainHand; |
24 23 | import net.minecraft.server.IBlockData; |
25 24 | import net.minecraft.server.IChatBaseComponent; |
26 - | import net.minecraft.server.IInventory; |
27 25 | import net.minecraft.server.IMerchant; |
28 26 | import net.minecraft.server.IRecipe; |
29 27 | import net.minecraft.server.ITileInventory; |
30 28 | import net.minecraft.server.ItemCooldown; |
31 29 | import net.minecraft.server.NBTTagCompound; |
32 30 | import net.minecraft.server.PacketPlayInCloseWindow; |
33 31 | import net.minecraft.server.PacketPlayOutOpenWindow; |
34 32 | import net.minecraft.server.TileEntity; |
35 - | import net.minecraft.server.TileEntityBarrel; |
36 - | import net.minecraft.server.TileEntityBeacon; |
37 - | import net.minecraft.server.TileEntityBlastFurnace; |
38 - | import net.minecraft.server.TileEntityBrewingStand; |
39 - | import net.minecraft.server.TileEntityDispenser; |
40 - | import net.minecraft.server.TileEntityDropper; |
41 - | import net.minecraft.server.TileEntityFurnaceFurnace; |
42 - | import net.minecraft.server.TileEntityHopper; |
43 - | import net.minecraft.server.TileEntityLectern; |
44 - | import net.minecraft.server.TileEntityShulkerBox; |
45 - | import net.minecraft.server.TileEntitySmoker; |
33 + | import net.minecraft.server.TileEntityContainer; |
46 34 | import net.minecraft.server.Vec3D; |
47 35 | import org.bukkit.GameMode; |
48 36 | import org.bukkit.Location; |
49 37 | import org.bukkit.Material; |
50 38 | import org.bukkit.NamespacedKey; |
51 39 | import org.bukkit.World; |
52 40 | import org.bukkit.block.Block; |
53 41 | import org.bukkit.craftbukkit.CraftServer; |
54 42 | import org.bukkit.craftbukkit.CraftWorld; |
55 43 | import org.bukkit.craftbukkit.event.CraftEventFactory; |
311 299 | |
312 300 | |
313 301 | public InventoryView getOpenInventory() { |
314 302 | return getHandle().activeContainer.getBukkitView(); |
315 303 | } |
316 304 | |
317 305 | |
318 306 | public InventoryView openInventory(Inventory inventory) { |
319 307 | if(!(getHandle() instanceof EntityPlayer)) return null; |
320 308 | EntityPlayer player = (EntityPlayer) getHandle(); |
321 - | InventoryType type = inventory.getType(); |
322 309 | Container formerContainer = getHandle().activeContainer; |
323 310 | |
324 311 | ITileInventory iinventory = null; |
325 312 | if (inventory instanceof CraftInventoryDoubleChest) { |
326 313 | iinventory = ((CraftInventoryDoubleChest) inventory).tile; |
327 314 | } else if (inventory instanceof CraftInventory) { |
328 315 | CraftInventory craft = (CraftInventory) inventory; |
329 316 | if (craft.getInventory() instanceof ITileInventory) { |
330 317 | iinventory = (ITileInventory) craft.getInventory(); |
331 318 | } |
332 319 | } |
333 320 | |
334 321 | if (iinventory instanceof ITileInventory) { |
335 322 | if (iinventory instanceof TileEntity) { |
336 323 | TileEntity te = (TileEntity) iinventory; |
337 324 | if (!te.hasWorld()) { |
338 325 | te.setLocation(getHandle().world, getHandle().getChunkCoordinates()); |
339 326 | } |
340 327 | } |
341 328 | } |
342 329 | |
343 - | switch (type) { |
344 - | case PLAYER: |
345 - | case CHEST: |
346 - | case ENDER_CHEST: |
347 - | if (iinventory instanceof ITileInventory) { |
348 - | getHandle().openContainer((ITileInventory) iinventory); |
349 - | } else { |
350 - | Containers customSize; |
351 - | switch (inventory.getSize()) { |
352 - | case 9: |
353 - | customSize = Containers.GENERIC_9X1; |
354 - | break; |
355 - | case 18: |
356 - | customSize = Containers.GENERIC_9X2; |
357 - | break; |
358 - | case 27: |
359 - | customSize = Containers.GENERIC_9X3; |
360 - | break; |
361 - | case 36: |
362 - | case 41: // PLAYER |
363 - | customSize = Containers.GENERIC_9X4; |
364 - | break; |
365 - | case 45: |
366 - | customSize = Containers.GENERIC_9X5; |
367 - | break; |
368 - | case 54: |
369 - | customSize = Containers.GENERIC_9X6; |
370 - | break; |
371 - | default: |
372 - | throw new IllegalArgumentException("Unsupported custom inventory size " + inventory.getSize()); |
373 - | } |
374 - | openCustomInventory(inventory, player, customSize); |
375 - | } |
376 - | break; |
377 - | case DISPENSER: |
378 - | if (iinventory instanceof TileEntityDispenser) { |
379 - | getHandle().openContainer((TileEntityDispenser) iinventory); |
380 - | } else { |
381 - | openCustomInventory(inventory, player, Containers.GENERIC_3X3); |
382 - | } |
383 - | break; |
384 - | case DROPPER: |
385 - | if (iinventory instanceof TileEntityDropper) { |
386 - | getHandle().openContainer((TileEntityDropper) iinventory); |
387 - | } else { |
388 - | openCustomInventory(inventory, player, Containers.GENERIC_3X3); |
389 - | } |
390 - | break; |
391 - | case FURNACE: |
392 - | if (iinventory instanceof TileEntityFurnaceFurnace) { |
393 - | getHandle().openContainer((TileEntityFurnaceFurnace) iinventory); |
394 - | } else { |
395 - | openCustomInventory(inventory, player, Containers.FURNACE); |
396 - | } |
397 - | break; |
398 - | case WORKBENCH: |
399 - | openCustomInventory(inventory, player, Containers.CRAFTING); |
400 - | break; |
401 - | case BREWING: |
402 - | if (iinventory instanceof TileEntityBrewingStand) { |
403 - | getHandle().openContainer((TileEntityBrewingStand) iinventory); |
404 - | } else { |
405 - | openCustomInventory(inventory, player, Containers.BREWING_STAND); |
406 - | } |
407 - | break; |
408 - | case ENCHANTING: |
409 - | openCustomInventory(inventory, player, Containers.ENCHANTMENT); |
410 - | break; |
411 - | case HOPPER: |
412 - | if (iinventory instanceof TileEntityHopper) { |
413 - | getHandle().openContainer((TileEntityHopper) iinventory); |
414 - | } else if (iinventory instanceof EntityMinecartHopper) { |
415 - | getHandle().openContainer((EntityMinecartHopper) iinventory); |
416 - | } else { |
417 - | openCustomInventory(inventory, player, Containers.HOPPER); |
418 - | } |
419 - | break; |
420 - | case BEACON: |
421 - | if (iinventory instanceof TileEntityBeacon) { |
422 - | getHandle().openContainer((TileEntityBeacon) iinventory); |
423 - | } else { |
424 - | openCustomInventory(inventory, player, Containers.BEACON); |
425 - | } |
426 - | break; |
427 - | case ANVIL: |
428 - | if (iinventory instanceof ITileInventory) { |
429 - | getHandle().openContainer((ITileInventory) iinventory); |
430 - | } else { |
431 - | openCustomInventory(inventory, player, Containers.ANVIL); |
432 - | } |
433 - | break; |
434 - | case SHULKER_BOX: |
435 - | if (iinventory instanceof TileEntityShulkerBox) { |
436 - | getHandle().openContainer((TileEntityShulkerBox) iinventory); |
437 - | } else { |
438 - | openCustomInventory(inventory, player, Containers.SHULKER_BOX); |
439 - | } |
440 - | break; |
441 - | case BARREL: |
442 - | if (iinventory instanceof TileEntityBarrel) { |
443 - | getHandle().openContainer((TileEntityBarrel) iinventory); |
444 - | } else { |
445 - | openCustomInventory(inventory, player, Containers.GENERIC_9X3); |
446 - | } |
447 - | break; |
448 - | case BLAST_FURNACE: |
449 - | if (iinventory instanceof TileEntityBlastFurnace) { |
450 - | getHandle().openContainer((TileEntityBlastFurnace) iinventory); |
451 - | } else { |
452 - | openCustomInventory(inventory, player, Containers.BLAST_FURNACE); |
453 - | } |
454 - | break; |
455 - | case LECTERN: |
456 - | if (iinventory instanceof TileEntityLectern) { |
457 - | getHandle().openContainer((TileEntityLectern) iinventory); |
458 - | } else { |
459 - | openCustomInventory(inventory, player, Containers.LECTERN); |
460 - | } |
461 - | break; |
462 - | case SMOKER: |
463 - | if (iinventory instanceof TileEntitySmoker) { |
464 - | getHandle().openContainer((TileEntitySmoker) iinventory); |
465 - | } else { |
466 - | openCustomInventory(inventory, player, Containers.SMOKER); |
467 - | } |
468 - | break; |
469 - | case STONECUTTER: |
470 - | openCustomInventory(inventory, player, Containers.STONECUTTER); |
471 - | break; |
472 - | case LOOM: |
473 - | openCustomInventory(inventory, player, Containers.LOOM); |
474 - | break; |
475 - | case CARTOGRAPHY: |
476 - | openCustomInventory(inventory, player, Containers.CARTOGRAPHY_TABLE); |
477 - | break; |
478 - | case GRINDSTONE: |
479 - | openCustomInventory(inventory, player, Containers.GRINDSTONE); |
480 - | break; |
481 - | case CREATIVE: |
482 - | case CRAFTING: |
483 - | case MERCHANT: |
484 - | default: |
485 - | throw new IllegalArgumentException("Can't open a " + type + " inventory!"); |
330 + | Containers<?> container = CraftContainer.getNotchInventoryType(inventory); |
331 + | if (iinventory instanceof TileEntityContainer) { |
332 + | getHandle().openContainer(iinventory); |
333 + | } else { |
334 + | openCustomInventory(inventory, player, container); |
486 335 | } |
336 + | |
487 337 | if (getHandle().activeContainer == formerContainer) { |
488 338 | return null; |
489 339 | } |
490 340 | getHandle().activeContainer.checkReachable = false; |
491 341 | return getHandle().activeContainer.getBukkitView(); |
492 342 | } |
493 343 | |
494 344 | private void openCustomInventory(Inventory inventory, EntityPlayer player, Containers<?> windowType) { |
495 345 | if (player.playerConnection == null) return; |
496 346 | Preconditions.checkArgument(windowType != null, "Unknown windowType"); |
562 412 | container = new CraftContainer(inventory, this.getHandle(), player.nextContainerCounter()); |
563 413 | } |
564 414 | |
565 415 | // Trigger an INVENTORY_OPEN event |
566 416 | container = CraftEventFactory.callInventoryOpenEvent(player, container); |
567 417 | if (container == null) { |
568 418 | return; |
569 419 | } |
570 420 | |
571 421 | // Now open the window |
572 - | InventoryType type = inventory.getType(); |
573 - | Containers<?> windowType = CraftContainer.getNotchInventoryType(type); |
422 + | Containers<?> windowType = CraftContainer.getNotchInventoryType(inventory.getTopInventory()); |
574 423 | String title = inventory.getTitle(); |
575 424 | player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(container.windowId, windowType, new ChatComponentText(title))); |
576 425 | player.activeContainer = container; |
577 426 | player.activeContainer.addSlotListener(player); |
578 427 | } |
579 428 | |
580 429 | |
581 430 | public InventoryView openMerchant(Villager villager, boolean force) { |
582 431 | Preconditions.checkNotNull(villager, "villager cannot be null"); |
583 432 | |