コード例 #1
0
  public CategoryMenu read(File file) throws DocumentException, SettlerCraftException {
    Preconditions.checkArgument(
        file.exists(), "File '" + file.getAbsolutePath() + "' does not exist!");

    Document d = new SAXReader().read(file);

    CategoryMenu menu = new DefaultCategoryMenu("Buy & Build");

    List<Node> rows = d.selectNodes("Menu/SlotRow");
    if (rows.size() > 2) {
      throw new SettlerCraftException("Max rows is 2 for menu.xml");
    }

    boolean hasRow2 = false;

    for (int row = 0; row < rows.size(); row++) {
      Node rowNode = rows.get(row);
      List<Node> slotNodes = rowNode.selectNodes("Slot");
      // Slot #1 is reserved for all category
      if (row == 0 && slotNodes.size() > 8) {
        throw new SettlerCraftException(" 'SlotRow#1' has max 8 slots to customize");
      } else if (slotNodes.size() > 9) {
        throw new SettlerCraftException(
            " 'SlotRow#" + (row + 2) + "' has max 9 slots to customize");
      }
      int count = 0;

      for (Node categorySlotNode : slotNodes) {

        if (!categorySlotNode.hasContent()) {
          count++;
          continue;
        }

        Node mId = categorySlotNode.selectSingleNode("MaterialID");
        Node cat = categorySlotNode.selectSingleNode("Category");
        //                Node ali = categorySlotNode.selectSingleNode("Aliases");

        if (mId == null) {
          throw new SettlerCraftException(
              "Missing 'MaterialID' element in 'SlotRow#"
                  + (row + 1)
                  + "' 'Slot#"
                  + (count + 1)
                  + "'");
        }
        if (cat == null) {
          throw new SettlerCraftException(
              "Missing 'Category' element in 'SlotRow#"
                  + (row + 1)
                  + "' 'Slot#"
                  + (count + 1)
                  + "'");
        }

        int id;
        try {
          id = Integer.parseInt(mId.getText());
        } catch (NumberFormatException nfe) {
          throw new SettlerCraftException(
              "Invalid number for 'MaterialID' element in 'SlotRow#"
                  + (row + 1)
                  + "' 'Slot#"
                  + (count + 1)
                  + "'");
        }

        Node catNameNode = cat.selectSingleNode("Name");
        if (catNameNode == null) {
          throw new SettlerCraftException(
              "Missing 'Name' element in 'SlotRow#" + (row + 1) + "' 'Slot#" + (count + 1) + "'");
        }

        String category = catNameNode.getText();
        if (category.isEmpty()) {
          Element catEl = (Element) cat;
          category = catEl.attributeValue("value");
        }
        if (category.trim().isEmpty()) {
          throw new SettlerCraftException(
              "Empty 'Category' element in 'SlotRow#"
                  + (row + 1)
                  + "' and 'Slot#"
                  + (count + 1)
                  + "'");
        }
        category = category.replaceAll(" AND ", "&");

        Node synonymsNode = cat.selectSingleNode("Synonyms");

        // Set aliases
        String[] synonyms;
        if (synonymsNode == null) {
          synonyms = new String[0];
        } else {
          List<Node> synonymNodes = synonymsNode.selectNodes("Synonym");
          synonyms = new String[synonymNodes.size()];
          for (int j = 0; j < synonymNodes.size(); j++) {
            String synonym = synonymNodes.get(j).getText();

            if (synonym.isEmpty()) {
              Element synoEl = (Element) cat;
              synonym = synoEl.attributeValue("value");
            }
            if (synonym.trim().isEmpty()) {
              throw new SettlerCraftException(
                  "Empty 'Synonym' element in  'SlotRow#"
                      + (row + 1)
                      + "' and 'Slot#"
                      + (count + 1)
                      + "' and 'Synonym#"
                      + (j + 1)
                      + "'");
            }

            synonyms[j] = synonymNodes.get(j).getText();
          }
        }
        int slot = count;
        if (row == 0) {
          slot += 1; // slot 0 is reserved...
        } else {

          hasRow2 = true;
        }

        CategorySlot categorySlot = SlotFactory.getInstance().createCategorySlot(category, id);
        categorySlot.addSynonyms(synonyms);
        menu.setCategorySlot((row * 9) + slot, categorySlot);

        count++;
      }
      // fill remaining
      if (count < 8 && row == 0) {
        for (int i = count; i < 8; i++) {
          menu.setLocked(i);
        }

      } else if (row > 0 && count < 9) {
        for (int i = count; i < 9; i++) {
          menu.setLocked((row * 9) + i);
        }
      }
    }

    if (hasRow2) {
      menu.setLocked(19, 20, 21, 22, 23, 24, 25);
      menu.setActionSlot(18, "Previous", 173); // block of coal
      menu.setActionSlot(26, "Next", 173);
    } else {
      menu.setLocked(10, 11, 12, 13, 14, 15, 16);
      menu.setActionSlot(9, "Previous", 173);
      menu.setActionSlot(17, "Next", 173);
    }
    return menu;
  }