Example #1
0
  public Location getExit(Location traveller) {
    Location loc = null;
    // Check if the gate has an exit block
    if (gate.getExit() != null) {
      Blox exit = getBlockAt(gate.getExit());
      int back = (isBackwards()) ? -1 : 1;
      loc =
          exit.modRelativeLoc(
              0D, 0D, 1D, traveller.getYaw(), traveller.getPitch(), modX * back, 1, modZ * back);
    } else {
      Stargate.log.log(
          Level.WARNING,
          "[Stargate] Missing destination point in .gate file " + gate.getFilename());
    }
    if (loc != null) {

      if (getWorld().getBlockTypeIdAt(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())
          == Material.STEP.getId()) {
        loc.setY(loc.getY() + 0.5);
      }

      loc.setPitch(traveller.getPitch());
      return loc;
    }
    return traveller;
  }
Example #2
0
  public void close(boolean force) {
    if (!isOpen) return;
    // Call the StargateCloseEvent
    StargateCloseEvent event = new StargateCloseEvent(this, force);
    Stargate.server.getPluginManager().callEvent(event);
    if (event.isCancelled()) return;
    force = event.getForce();

    if (isAlwaysOn() && !force) return; // Only close always-open if forced

    // Close this gate, then the dest gate.
    for (Blox inside : getEntrances()) {
      inside.setType(gate.getPortalBlockClosed());
    }

    player = null;
    isOpen = false;
    Stargate.openList.remove(this);
    Stargate.activeList.remove(this);

    if (!isAlwaysOn()) {
      Portal end = getDestination();

      if (end != null && end.isOpen()) {
        end.deactivate(); // Clear it's destination first.
        end.close(false);
      }
    }

    deactivate();
  }
Example #3
0
  public static void saveAllGates(World world) {
    String loc = Stargate.getSaveLocation() + "/" + world.getName() + ".db";

    try {
      BufferedWriter bw = new BufferedWriter(new FileWriter(loc, false));

      for (Portal portal : allPortals) {
        String wName = portal.world.getName();
        if (!wName.equalsIgnoreCase(world.getName())) continue;
        StringBuilder builder = new StringBuilder();
        Blox sign = new Blox(portal.id.getBlock());
        Blox button = portal.button;

        builder.append(portal.name);
        builder.append(':');
        builder.append(sign.toString());
        builder.append(':');
        builder.append((button != null) ? button.toString() : "");
        builder.append(':');
        builder.append(portal.modX);
        builder.append(':');
        builder.append(portal.modZ);
        builder.append(':');
        builder.append(portal.rotX);
        builder.append(':');
        builder.append(portal.topLeft.toString());
        builder.append(':');
        builder.append(portal.gate.getFilename());
        builder.append(':');
        builder.append(portal.isFixed() ? portal.getDestinationName() : "");
        builder.append(':');
        builder.append(portal.getNetwork());
        builder.append(':');
        builder.append(portal.getOwner());
        builder.append(':');
        builder.append(portal.isHidden());
        builder.append(':');
        builder.append(portal.isAlwaysOn());
        builder.append(':');
        builder.append(portal.isPrivate());
        builder.append(':');
        builder.append(portal.world.getName());
        builder.append(':');
        builder.append(portal.isFree());
        builder.append(':');
        builder.append(portal.isBackwards());
        builder.append(':');
        builder.append(portal.isShown());

        bw.append(builder.toString());
        bw.newLine();
      }

      bw.close();
    } catch (Exception e) {
      Stargate.log.log(Level.SEVERE, "Exception while writing stargates to " + loc + ": " + e);
    }
  }
Example #4
0
  private Portal(
      Blox topLeft,
      int modX,
      int modZ,
      float rotX,
      SignPost id,
      Blox button,
      String dest,
      String name,
      boolean verified,
      String network,
      Gate gate,
      String owner,
      boolean hidden,
      boolean alwaysOn,
      boolean priv,
      boolean free,
      boolean backwards,
      boolean show) {
    this.topLeft = topLeft;
    this.modX = modX;
    this.modZ = modZ;
    this.rotX = rotX;
    this.id = id;
    this.destination = dest;
    this.button = button;
    this.verified = verified;
    this.network = network;
    this.name = name;
    this.gate = gate;
    this.owner = owner;
    this.hidden = hidden;
    this.alwaysOn = alwaysOn;
    this.priv = priv;
    this.free = free;
    this.backwards = backwards;
    this.show = show;
    this.world = topLeft.getWorld();
    this.fixed = dest.length() > 0;

    if (this.isAlwaysOn() && !this.isFixed()) {
      this.alwaysOn = false;
      Stargate.debug(
          "Portal", "Can not create a non-fixed always-on gate. Setting AlwaysOn = false");
    }

    this.register();
    if (verified) {
      this.drawSign();
    }
  }
Example #5
0
  public boolean open(Player openFor, boolean force) {
    // Call the StargateOpenEvent
    StargateOpenEvent event = new StargateOpenEvent(openFor, this, force);
    Stargate.server.getPluginManager().callEvent(event);
    if (event.isCancelled()) return false;
    force = event.getForce();

    if (isOpen() && !force) return false;

    getWorld().loadChunk(getWorld().getChunkAt(topLeft.getBlock()));

    for (Blox inside : getEntrances()) {
      inside.setType(gate.getPortalBlockOpen());
    }

    isOpen = true;
    openTime = System.currentTimeMillis() / 1000;
    Stargate.openList.add(this);
    Stargate.activeList.remove(this);

    // Open remote gate
    if (!isAlwaysOn()) {
      player = openFor;

      Portal end = getDestination();
      // Only open dest if it's not-fixed or points at this gate
      if (end != null
          && (!end.isFixed() || end.getDestinationName().equalsIgnoreCase(getName()))
          && !end.isOpen()) {
        end.open(openFor, false);
        end.setDestination(this);
        if (end.isVerified()) end.drawSign();
      }
    }

    return true;
  }
Example #6
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;
  }
Example #7
0
 private Blox getBlockAt(RelativeBlockVector vector) {
   return topLeft.modRelative(
       vector.getRight(), vector.getDepth(), vector.getDistance(), modX, 1, modZ);
 }
Example #8
0
 public void loadChunk() {
   getWorld().loadChunk(topLeft.getBlock().getChunk());
 }
Example #9
0
 public boolean isChunkLoaded() {
   return getWorld().isChunkLoaded(topLeft.getBlock().getChunk());
 }
Example #10
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 ");
    }
  }