Пример #1
0
  public static Portal createPortal(SignChangeEvent event, Player player) {
    SignPost id = new SignPost(new Blox(event.getBlock()));
    Block idParent = id.getParent();
    if (idParent == null) {
      return null;
    }

    if (Gate.getGatesByControlBlock(idParent).length == 0) return null;

    if (Portal.getByBlock(idParent) != null) {
      Stargate.debug("createPortal", "idParent belongs to existing gate");
      return null;
    }

    Blox parent = new Blox(player.getWorld(), idParent.getX(), idParent.getY(), idParent.getZ());
    Blox topleft = null;
    String name = filterName(event.getLine(0));
    String destName = filterName(event.getLine(1));
    String network = filterName(event.getLine(2));
    String options = filterName(event.getLine(3));
    boolean hidden = (options.indexOf('h') != -1 || options.indexOf('H') != -1);
    boolean alwaysOn = (options.indexOf('a') != -1 || options.indexOf('A') != -1);
    boolean priv = (options.indexOf('p') != -1 || options.indexOf('P') != -1);
    boolean free = (options.indexOf('f') != -1 || options.indexOf('F') != -1);
    boolean backwards = (options.indexOf('b') != -1 || options.indexOf('B') != -1);
    boolean show = (options.indexOf('s') != -1 || options.indexOf('S') != -1);

    // Check permissions for options.
    if (hidden && !Stargate.canOption(player, "hidden")) hidden = false;
    if (alwaysOn && !Stargate.canOption(player, "alwayson")) alwaysOn = false;
    if (priv && !Stargate.canOption(player, "private")) priv = false;
    if (free && !Stargate.canOption(player, "free")) free = false;
    if (backwards && !Stargate.canOption(player, "backwards")) backwards = false;
    if (show && !Stargate.canOption(player, "show")) show = false;

    // Can not create a non-fixed always-on gate.
    if (alwaysOn && destName.length() == 0) {
      alwaysOn = false;
    }

    // Show isn't useful if A is false
    if (show && !alwaysOn) {
      show = false;
    }

    // Moved the layout check so as to avoid invalid messages when not making a gate
    int modX = 0;
    int modZ = 0;
    float rotX = 0f;
    int facing = 0;

    if (idParent.getX() > id.getBlock().getX()) {
      modZ -= 1;
      rotX = 90f;
      facing = 2;
    } else if (idParent.getX() < id.getBlock().getX()) {
      modZ += 1;
      rotX = 270f;
      facing = 1;
    } else if (idParent.getZ() > id.getBlock().getZ()) {
      modX += 1;
      rotX = 180f;
      facing = 4;
    } else if (idParent.getZ() < id.getBlock().getZ()) {
      modX -= 1;
      rotX = 0f;
      facing = 3;
    }

    Gate[] possibleGates = Gate.getGatesByControlBlock(idParent);
    Gate gate = null;
    RelativeBlockVector buttonVector = null;

    for (Gate possibility : possibleGates) {
      if ((gate == null) && (buttonVector == null)) {
        RelativeBlockVector[] vectors = possibility.getControls();
        RelativeBlockVector otherControl = null;

        for (RelativeBlockVector vector : vectors) {
          Blox tl =
              parent.modRelative(
                  -vector.getRight(), -vector.getDepth(), -vector.getDistance(), modX, 1, modZ);

          if (gate == null) {
            if (possibility.matches(tl, modX, modZ, true)) {
              gate = possibility;
              topleft = tl;

              if (otherControl != null) {
                buttonVector = otherControl;
              }
            }
          } else if (otherControl != null) {
            buttonVector = vector;
          }

          otherControl = vector;
        }
      }
    }

    if ((gate == null) || (buttonVector == null)) {
      Stargate.debug("createPortal", "Could not find matching gate layout");
      return null;
    }

    // Debug
    Stargate.debug(
        "createPortal",
        "h = " + hidden + " a = " + alwaysOn + " p = " + priv + " f = " + free + " b = " + backwards
            + " s = " + show);

    if ((network.length() < 1) || (network.length() > 11)) {
      network = Stargate.getDefaultNetwork();
    }

    // Check if the player can create gates on this network
    if (!Stargate.canCreate(player, network)) {
      Stargate.debug(
          "createPortal", "Player doesn't have create permissions on network. Trying personal");
      if (Stargate.canCreatePersonal(player)) {
        network = player.getName();
        if (network.length() > 11) network = network.substring(0, 11);
        Stargate.debug("createPortal", "Creating personal portal");
        Stargate.sendMessage(player, Stargate.getString("createPersonal"));
      } else {
        Stargate.debug("createPortal", "Player does not have access to network");
        Stargate.sendMessage(player, Stargate.getString("createNetDeny"));
        return null;
      }
    }

    // Check if the player can create this gate layout
    String gateName = gate.getFilename();
    gateName = gateName.substring(0, gateName.indexOf('.'));
    if (!Stargate.canCreateGate(player, gateName)) {
      Stargate.debug("createPortal", "Player does not have access to gate layout");
      Stargate.sendMessage(player, Stargate.getString("createGateDeny"));
      return null;
    }

    if (name.length() < 1 || name.length() > 11) {
      Stargate.debug("createPortal", "Name length error");
      Stargate.sendMessage(player, Stargate.getString("createNameLength"));
      return null;
    }

    if (getByName(name, network) != null) {
      Stargate.debug("createPortal", "Name Error");
      Stargate.sendMessage(player, Stargate.getString("createExists"));
      return null;
    }

    // Check if there are too many gates in this network
    ArrayList<String> netList = allPortalsNet.get(network.toLowerCase());
    if (Stargate.maxGates > 0 && netList != null && netList.size() >= Stargate.maxGates) {
      Stargate.sendMessage(player, Stargate.getString("createFull"));
      return null;
    }

    // Check if the user can create gates to this world.
    if (destName.length() > 0) {
      Portal p = Portal.getByName(destName, network);
      if (p != null) {
        String world = p.getWorld().getName();
        if (!Stargate.canAccessWorld(player, world)) {
          Stargate.debug("canCreate", "Player does not have access to destination world");
          Stargate.sendMessage(player, Stargate.getString("createWorldDeny"));
          return null;
        }
      }
    }

    // Bleh, gotta check to make sure none of this gate belongs to another gate. Boo slow.
    for (RelativeBlockVector v : gate.getBorder()) {
      Blox b = topleft.modRelative(v.getRight(), v.getDepth(), v.getDistance(), modX, 1, modZ);
      if (Portal.getByBlock(b.getBlock()) != null) {
        Stargate.debug("createPortal", "Gate conflicts with existing gate");
        Stargate.sendMessage(player, Stargate.getString("createConflict"));
        return null;
      }
    }

    int cost = Stargate.getCreateCost(player, gate);
    if (cost > 0) {
      if (!Stargate.chargePlayer(player, null, gate.getCreateCost())) {
        String inFundMsg = Stargate.getString("ecoInFunds");
        inFundMsg =
            Stargate.replaceVars(
                inFundMsg,
                new String[] {"%cost%", "%portal%"},
                new String[] {iConomyHandler.format(cost), name});
        Stargate.sendMessage(player, inFundMsg);
        Stargate.debug("createPortal", "Insufficient Funds");
        return null;
      }
      String deductMsg = Stargate.getString("ecoDeduct");
      deductMsg =
          Stargate.replaceVars(
              deductMsg,
              new String[] {"%cost%", "%portal%"},
              new String[] {iConomyHandler.format(cost), name});
      Stargate.sendMessage(player, deductMsg, false);
    }

    Portal portal = null;

    Blox button = null;
    // No button on an always-open gate.
    if (!alwaysOn) {
      button =
          topleft.modRelative(
              buttonVector.getRight(),
              buttonVector.getDepth(),
              buttonVector.getDistance() + 1,
              modX,
              1,
              modZ);
      button.setType(Material.STONE_BUTTON.getId());
      button.setData(facing);
    }
    portal =
        new Portal(
            topleft,
            modX,
            modZ,
            rotX,
            id,
            button,
            destName,
            name,
            true,
            network,
            gate,
            player.getName(),
            hidden,
            alwaysOn,
            priv,
            free,
            backwards,
            show);

    // Open always on gate
    if (portal.isAlwaysOn()) {
      Portal dest = Portal.getByName(destName, portal.getNetwork());
      if (dest != null) {
        portal.open(true);
        dest.drawSign();
      }
      // Set the inside of the gate to its closed material
    } else {
      for (Blox inside : portal.getEntrances()) {
        inside.setType(portal.getGate().getPortalBlockClosed());
      }
    }

    // Open any always on gate pointing at this gate
    for (String originName : allPortalsNet.get(portal.getNetwork().toLowerCase())) {
      Portal origin = Portal.getByName(originName, portal.getNetwork());
      if (origin == null) continue;
      if (!origin.getDestinationName().equalsIgnoreCase(portal.getName())) continue;
      if (!origin.isVerified()) continue;
      if (origin.isFixed()) origin.drawSign();
      if (origin.isAlwaysOn()) origin.open(true);
    }

    saveAllGates(portal.getWorld());

    return portal;
  }
Пример #2
0
  public static void loadAllGates(World world) {
    String location = Stargate.getSaveLocation();

    File db = new File(location, world.getName() + ".db");

    if (db.exists()) {
      int l = 0;
      int portalCount = 0;
      try {
        Scanner scanner = new Scanner(db);
        while (scanner.hasNextLine()) {
          l++;
          String line = scanner.nextLine().trim();
          if (line.startsWith("#") || line.isEmpty()) {
            continue;
          }
          String[] split = line.split(":");
          if (split.length < 8) {
            Stargate.log.info("[Stargate] Invalid line - " + l);
            continue;
          }
          String name = split[0];
          Blox s = new Blox(world, split[1]);
          if (!(s.getBlock().getState() instanceof Sign)) {
            Stargate.log.info(
                "[Stargate] Sign on line "
                    + l
                    + " doesn't exist. BlockType = "
                    + s.getBlock().getType());
            continue;
          }
          SignPost sign = new SignPost(s);
          Blox button = (split[2].length() > 0) ? new Blox(world, split[2]) : null;
          int modX = Integer.parseInt(split[3]);
          int modZ = Integer.parseInt(split[4]);
          float rotX = Float.parseFloat(split[5]);
          Blox topLeft = new Blox(world, split[6]);
          Gate gate =
              (split[7].contains(";"))
                  ? Gate.getGateByName("nethergate.gate")
                  : Gate.getGateByName(split[7]);
          if (gate == null) {
            Stargate.log.info(
                "[Stargate] Gate layout on line " + l + " does not exist [" + split[7] + "]");
            continue;
          }

          String dest = (split.length > 8) ? split[8] : "";
          String network = (split.length > 9) ? split[9] : Stargate.getDefaultNetwork();
          if (network.isEmpty()) network = Stargate.getDefaultNetwork();
          String owner = (split.length > 10) ? split[10] : "";
          boolean hidden = (split.length > 11) ? split[11].equalsIgnoreCase("true") : false;
          boolean alwaysOn = (split.length > 12) ? split[12].equalsIgnoreCase("true") : false;
          boolean priv = (split.length > 13) ? split[13].equalsIgnoreCase("true") : false;
          boolean free = (split.length > 15) ? split[15].equalsIgnoreCase("true") : false;
          boolean backwards = (split.length > 16) ? split[16].equalsIgnoreCase("true") : false;
          boolean show = (split.length > 17) ? split[17].equalsIgnoreCase("true") : false;

          Portal portal =
              new Portal(
                  topLeft, modX, modZ, rotX, sign, button, dest, name, false, network, gate, owner,
                  hidden, alwaysOn, priv, free, backwards, show);
          portal.close(true);
        }
        scanner.close();

        // Open any always-on gates. Do this here as it should be more efficient than in the loop.
        int OpenCount = 0;
        for (Iterator<Portal> iter = allPortals.iterator(); iter.hasNext(); ) {
          Portal portal = iter.next();
          if (portal == null) continue;

          // Verify portal integrity/register portal
          if (!portal.wasVerified()) {
            if (!portal.isVerified() || !portal.checkIntegrity()) {
              // DEBUG
              for (RelativeBlockVector control : portal.getGate().getControls()) {
                if (portal.getBlockAt(control).getBlock().getTypeId()
                    != portal.getGate().getControlBlock()) {
                  Stargate.debug(
                      "loadAllGates",
                      "Control Block Type == " + portal.getBlockAt(control).getBlock().getTypeId());
                }
              }
              portal.unregister(false);
              iter.remove();
              Stargate.log.info("[Stargate] Destroying stargate at " + portal.toString());
              continue;
            } else {
              portal.drawSign();
              portalCount++;
            }
          }

          if (!portal.isFixed()) continue;
          Portal dest = portal.getDestination();
          if (dest != null) {
            if (portal.isAlwaysOn()) {
              portal.open(true);
              OpenCount++;
            }
            portal.drawSign();
            dest.drawSign();
          }
        }
        Stargate.log.info(
            "[Stargate] {"
                + world.getName()
                + "} Loaded "
                + portalCount
                + " stargates with "
                + OpenCount
                + " set as always-on");
      } catch (Exception e) {
        Stargate.log.log(
            Level.SEVERE, "Exception while reading stargates from " + db.getName() + ": " + l);
        e.printStackTrace();
      }
    } else {
      Stargate.log.info("[Stargate] {" + world.getName() + "} No stargates for world ");
    }
  }