private void crafting(ShapedRecipe recipe, InventoryHolder invMinecart) {

    HashSet<ItemStack> removedItems = new HashSet<ItemStack>();
    for (Iterator<Entry<Character, ItemStack>> irt2 =
            recipe.getIngredientMap().entrySet().iterator();
        irt2.hasNext(); ) {
      Entry<Character, ItemStack> entry = irt2.next();
      ItemStack is = entry.getValue();

      if (is == null) {
        continue;
      }
      ItemStack remove = invMinecart.getInventory().removeItem(is).get(0);
      if (remove == null) {
        removedItems.add(is);
      } else {
        invMinecart.getInventory().addItem(remove);
        for (ItemStack backItem : removedItems) {
          invMinecart.getInventory().addItem(backItem);
        }
        return;
      }
    }
    invMinecart.getInventory().addItem(recipe.getResult());
  }
  @EventHandler
  public void openInventory(InventoryOpenEvent ioe) {
    if (!(ioe.getPlayer() instanceof Player)) {
      return;
    }

    Player player = (Player) ioe.getPlayer();
    Game game = Main.getInstance().getGameManager().getGameOfPlayer(player);

    if (game == null) {
      return;
    }

    if (game.getState() != GameState.RUNNING) {
      return;
    }

    if (ioe.getInventory().getType() == InventoryType.ENCHANTING
        || ioe.getInventory().getType() == InventoryType.BREWING
        || (ioe.getInventory().getType() == InventoryType.CRAFTING
            && !Main.getInstance().getBooleanConfig("allow-crafting", false))) {
      ioe.setCancelled(true);
      return;
    } else if (ioe.getInventory().getType() == InventoryType.CRAFTING
        && Main.getInstance().getBooleanConfig("allow-crafting", false)) {
      return;
    }

    if (game.isSpectator(player)) {
      if (ioe.getInventory().getName().equals(Main._l("ingame.spectator"))) {
        return;
      }

      ioe.setCancelled(true);
    }

    if (ioe.getInventory().getHolder() == null) {
      return;
    }

    if (game.getRegion().getInventories().contains(ioe.getInventory())) {
      return;
    }

    InventoryHolder holder = ioe.getInventory().getHolder();
    for (Class<?> interfaze : holder.getClass().getInterfaces()) {

      if (interfaze.equals(BlockState.class)) {
        game.getRegion().addInventory(ioe.getInventory());
        return;
      }

      for (Class<?> interfaze2 : interfaze.getInterfaces()) {
        if (interfaze2.equals(BlockState.class)) {
          game.getRegion().addInventory(ioe.getInventory());
          return;
        }
      }
    }
  }
 @EventHandler
 public void onInventoryClose(InventoryCloseEvent e) {
   if (!Config.separateInv) return;
   if (!(e.getPlayer() instanceof Player)) return;
   Player p = (Player) e.getPlayer();
   if (e.getInventory().getType() != InventoryType.PLAYER) return;
   InventoryHolder thisIh = e.getInventory().getHolder();
   InventoryHolder playIh = p.getInventory().getHolder();
   if (thisIh == null || playIh == null) return;
   if (!thisIh.equals(playIh)) return; // only save their inv when they close /their/ inv
   String group = getWorldGroup(p.getWorld());
   if (group == null) return; // only manage worlds that are in the config
   saveInventory(p, p.getInventory());
 }
Exemple #4
0
  private ItemStack[] getRecords(List<Block> containables) {
    List<ItemStack> tempItems = new ArrayList<ItemStack>();
    ItemStack[] isTotalItems;

    for (Block bHolder : containables) {
      if (bHolder.getState() instanceof InventoryHolder) {
        InventoryHolder Holder = (InventoryHolder) bHolder.getState();
        for (ItemStack item : Holder.getInventory().getContents()) {
          if (item != null && item.getAmount() > 0 && item.getType().isRecord()) {
            tempItems.add(item);
          }
        }
      }
    }
    isTotalItems = tempItems.toArray(new ItemStack[tempItems.size()]);
    return isTotalItems;
  }
  @EventHandler(priority = EventPriority.LOWEST)
  public void onInventoryOpenEvent(InventoryOpenEvent event) {
    if (event.isCancelled()
        || event.getInventory() == null
        || event.getInventory().getHolder() == null) return;

    final Player player = (Player) event.getPlayer();
    final InventoryHolder h = event.getInventory().getHolder();

    if (h instanceof Chest
        || h instanceof DoubleChest
        || h instanceof Furnace
        || h instanceof Dispenser) {
      if (player.getWorld() != plugin.getIslandWorld()) return;
      if (player.isOp()) return;
      if (player.hasPermission("islandworld.bypass.island")) return;

      Location loc = player.getLocation();
      if (event.getInventory().getHolder() instanceof Chest) {
        final Chest chest = (Chest) event.getInventory().getHolder();
        loc = chest.getLocation();
      } else if (event.getInventory().getHolder() instanceof Furnace) {
        final Furnace fur = (Furnace) event.getInventory().getHolder();
        loc = fur.getLocation();
      } else if (event.getInventory().getHolder() instanceof DoubleChest) {
        final DoubleChest chest = (DoubleChest) event.getInventory().getHolder();
        loc = chest.getLocation();
      } else if (event.getInventory().getHolder() instanceof Dispenser) {
        final Dispenser disp = (Dispenser) event.getInventory().getHolder();
        loc = disp.getLocation();
      }

      if (!(plugin.canBuildOnLocation(player, loc))) event.setCancelled(true);
    }

    plugin.debug(
        "Event :"
            + event.getEventName()
            + ", class: "
            + h.getClass()
            + ", cancelled:"
            + event.isCancelled());
  }
  public static void handleHolderRemoval(Consumer consumer, String remover, BlockState state) {
    InventoryHolder holder = (InventoryHolder) state;

    if (InventoryUtil.isHolderValid(holder)) {
      List<ItemStack> invContents =
          compressInventory(
              (holder instanceof Chest
                      ? ((Chest) state).getBlockInventory()
                      : holder.getInventory())
                  .getContents());

      if (!invContents.isEmpty()) {
        for (String str : serializeInventory(ContainerEntry.getSerializer(), invContents))
          consumer.addEntry(
              new ContainerExtract(
                  remover, DataType.CONTAINER_EXTRACT, InventoryUtil.getHolderLoc(holder), str));
      }
    }
  }
 /**
  * Get the type of chest that the inventory holder is.
  *
  * @param inventoryHolder holder to get the chest type for.
  * @return {@link com.caved_in.commons.block.ChestType} of the given inventory holder. Unknown if
  *     it's not a chest.
  */
 public static ChestType getChestType(InventoryHolder inventoryHolder) {
   if (isChest(inventoryHolder)) {
     return ChestType.SINGLE_CHEST;
   } else if (isDoubleChest(inventoryHolder)) {
     return ChestType.DOUBLE_CHEST;
   } else if (inventoryHolder.getInventory().getType() == InventoryType.ENDER_CHEST) {
     return ChestType.ENDER_CHEST;
   } else {
     return ChestType.UNKNOWN;
   }
 }
 @Override
 public boolean resetChanges(HungerGame game) {
   EquatableWeakReference<HungerGame> eMap = new EquatableWeakReference<HungerGame>(game);
   if (!changedBlocks.containsKey(new EquatableWeakReference<HungerGame>(game))) return true;
   for (Location l : changedBlocks.get(eMap).keySet()) {
     BlockState state = changedBlocks.get(eMap).get(l);
     l.getBlock().setTypeId(state.getTypeId());
     l.getBlock().setData(state.getRawData());
   }
   int chests = 0;
   for (Location l : changedInvs.get(eMap).keySet()) {
     BlockState state = l.getBlock().getState();
     if (!(state instanceof InventoryHolder))
       throw new IllegalStateException(
           "Error when resetting a game: inventory saved for non-InventoryHolder");
     ((InventoryHolder) state).getInventory().setContents(changedInvs.get(eMap).get(l));
     chests++;
   }
   Logging.debug("Reset " + chests + " chests");
   changedBlocks.get(eMap).clear();
   return true;
 }
 PerformResult perform() throws WorldEditorException {
   if (dontRollback.contains(replaced)) return PerformResult.BLACKLISTED;
   final Block block = loc.getBlock();
   if (replaced == 0 && block.getTypeId() == 0) return PerformResult.NO_ACTION;
   final BlockState state = block.getState();
   if (!world.isChunkLoaded(block.getChunk())) world.loadChunk(block.getChunk());
   if (type == replaced) {
     if (type == 0) {
       if (!block.setTypeId(0))
         throw new WorldEditorException(block.getTypeId(), 0, block.getLocation());
     } else if (ca != null && (type == 23 || type == 54 || type == 61 || type == 62)) {
       int leftover = 0;
       try {
         leftover =
             modifyContainer(
                 state, new ItemStack(ca.itemType, -ca.itemAmount, (short) 0, ca.itemData));
         if (leftover > 0)
           for (final BlockFace face :
               new BlockFace[] {
                 BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST
               })
             if (block.getRelative(face).getTypeId() == 54)
               leftover =
                   modifyContainer(
                       block.getRelative(face).getState(),
                       new ItemStack(
                           ca.itemType,
                           ca.itemAmount < 0 ? leftover : -leftover,
                           (short) 0,
                           ca.itemData));
       } catch (final Exception ex) {
         throw new WorldEditorException(ex.getMessage(), block.getLocation());
       }
       if (!state.update())
         throw new WorldEditorException(
             "Failed to update inventory of " + materialName(block.getTypeId()),
             block.getLocation());
       if (leftover > 0 && ca.itemAmount < 0)
         throw new WorldEditorException(
             "Not enough space left in " + materialName(block.getTypeId()), block.getLocation());
     } else return PerformResult.NO_ACTION;
     return PerformResult.SUCCESS;
   }
   if (!(equalTypes(block.getTypeId(), type) || replaceAnyway.contains(block.getTypeId())))
     return PerformResult.NO_ACTION;
   if (state instanceof InventoryHolder) {
     ((InventoryHolder) state).getInventory().clear();
     state.update();
   }
   if (block.getTypeId() == replaced) {
     if (block.getData() != (type == 0 ? data : (byte) 0))
       block.setData(type == 0 ? data : (byte) 0, true);
     else return PerformResult.NO_ACTION;
   } else if (!block.setTypeIdAndData(replaced, type == 0 ? data : (byte) 0, true))
     throw new WorldEditorException(block.getTypeId(), replaced, block.getLocation());
   final int curtype = block.getTypeId();
   if (signtext != null && (curtype == 63 || curtype == 68)) {
     final Sign sign = (Sign) block.getState();
     final String[] lines = signtext.split("\0", 4);
     if (lines.length < 4) return PerformResult.NO_ACTION;
     for (int i = 0; i < 4; i++) sign.setLine(i, lines[i]);
     if (!sign.update())
       throw new WorldEditorException(
           "Failed to update signtext of " + materialName(block.getTypeId()),
           block.getLocation());
   } else if (curtype == 26) {
     final Bed bed = (Bed) block.getState().getData();
     final Block secBlock =
         bed.isHeadOfBed()
             ? block.getRelative(bed.getFacing().getOppositeFace())
             : block.getRelative(bed.getFacing());
     if (secBlock.getTypeId() == 0
         && !secBlock.setTypeIdAndData(26, (byte) (bed.getData() | 8), true))
       throw new WorldEditorException(secBlock.getTypeId(), 26, secBlock.getLocation());
   } else if (curtype == 64 || curtype == 71) {
     final byte blockData = block.getData();
     final Block secBlock =
         (blockData & 8) == 8
             ? block.getRelative(BlockFace.DOWN)
             : block.getRelative(BlockFace.UP);
     if (secBlock.getTypeId() == 0
         && !secBlock.setTypeIdAndData(curtype, (byte) (blockData | 8), true))
       throw new WorldEditorException(secBlock.getTypeId(), curtype, secBlock.getLocation());
   } else if ((curtype == 29 || curtype == 33) && (block.getData() & 8) > 0) {
     final PistonBaseMaterial piston = (PistonBaseMaterial) block.getState().getData();
     final Block secBlock = block.getRelative(piston.getFacing());
     if (secBlock.getTypeId() == 0
         && !secBlock.setTypeIdAndData(
             34,
             curtype == 29 ? (byte) (block.getData() | 8) : (byte) (block.getData() & ~8),
             true))
       throw new WorldEditorException(secBlock.getTypeId(), 34, secBlock.getLocation());
   } else if (curtype == 34) {
     final PistonExtensionMaterial piston = (PistonExtensionMaterial) block.getState().getData();
     final Block secBlock = block.getRelative(piston.getFacing().getOppositeFace());
     if (secBlock.getTypeId() == 0
         && !secBlock.setTypeIdAndData(
             piston.isSticky() ? 29 : 33, (byte) (block.getData() | 8), true))
       throw new WorldEditorException(
           secBlock.getTypeId(), piston.isSticky() ? 29 : 33, secBlock.getLocation());
   } else if (curtype == 18 && (block.getData() & 8) > 0)
     block.setData((byte) (block.getData() & 0xF7));
   return PerformResult.SUCCESS;
 }
  public static void clear(final Location bottom, final Location top) {
    final AthionMaps pmi = getMap(bottom);

    final int bottomX = bottom.getBlockX();
    final int topX = top.getBlockX();
    final int bottomZ = bottom.getBlockZ();
    final int topZ = top.getBlockZ();

    final int minChunkX = (int) Math.floor((double) bottomX / 16);
    final int maxChunkX = (int) Math.floor((double) topX / 16);
    final int minChunkZ = (int) Math.floor((double) bottomZ / 16);
    final int maxChunkZ = (int) Math.floor((double) topZ / 16);

    final World w = bottom.getWorld();

    for (int cx = minChunkX; cx <= maxChunkX; cx++) {
      for (int cz = minChunkZ; cz <= maxChunkZ; cz++) {
        final Chunk chunk = w.getChunkAt(cx, cz);

        for (final Entity e : chunk.getEntities()) {
          final Location eloc = e.getLocation();

          if (!(e instanceof Player)
              && (eloc.getBlockX() >= bottom.getBlockX())
              && (eloc.getBlockX() <= top.getBlockX())
              && (eloc.getBlockZ() >= bottom.getBlockZ())
              && (eloc.getBlockZ() <= top.getBlockZ())) {
            e.remove();
          }
        }
      }
    }

    for (int x = bottomX; x <= topX; x++) {
      for (int z = bottomZ; z <= topZ; z++) {
        Block block = new Location(w, x, 0, z).getBlock();

        block.setBiome(Biome.PLAINS);

        for (int y = w.getMaxHeight(); y >= 0; y--) {
          block = new Location(w, x, y, z).getBlock();

          final BlockState state = block.getState();

          if (state instanceof InventoryHolder) {
            final InventoryHolder holder = (InventoryHolder) state;
            holder.getInventory().clear();
          }

          if (state instanceof Jukebox) {
            final Jukebox jukebox = (Jukebox) state;
            // Remove once they fix the NullPointerException
            try {
              jukebox.setPlaying(Material.AIR);
            } catch (final Exception e) {
            }
          }

          if (y == 0) {
            block.setTypeId(pmi.BottomBlockId);
          } else if (y == pmi.RoadHeight) {
            block.setTypeId(pmi.PlotFloorBlockId);
          } else if (y < pmi.RoadHeight) {
            block.setTypeId(pmi.PlotFillingBlockId);
          } else {
            if ((y == (pmi.RoadHeight))
                && ((x == (bottomX - 1))
                    || (x == (topX + 1))
                    || (z == (bottomZ - 1))
                    || (z == (topZ + 1)))) {
              // block.setTypeId(pmi.WallBlockId);
            } else {
              block.setTypeIdAndData(0, (byte) 0, false); // .setType(Material.AIR);
            }
          }
        }
      }
    }

    adjustWall(bottom);
  }