示例#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
 private Blox getBlockAt(RelativeBlockVector vector) {
   return topLeft.modRelative(
       vector.getRight(), vector.getDepth(), vector.getDistance(), modX, 1, modZ);
 }