Ejemplo n.º 1
0
 public boolean isBlockTracked(Class<? extends Block> block, int meta) {
   for (SegmentBlock segment : segmentsBlocks) {
     if (segment.getCheckClass().isAssignableFrom(block)
         && (segment.getMeta() == -1 || segment.getMeta() == meta)) return true;
   }
   return false;
 }
Ejemplo n.º 2
0
    private SegmentBlock deserializeBlock(JsonObject json, JsonDeserializationContext context) {
      if (!json.has("actions")) {
        throw new ProtectionParseException("Missing actions identifier");
      }
      SegmentBlock segment = new SegmentBlock();
      segment.types.addAll(
          deserializeAsArray(
              json.get("actions"),
              context,
              new TypeToken<BlockType>() {},
              new TypeToken<List<BlockType>>() {}.getType()));
      json.remove("actions");

      if (json.has("meta")) {
        segment.meta = json.get("meta").getAsInt();
        json.remove("meta");
      }

      if (json.has("clientUpdate")) {
        segment.clientUpdate =
            new ClientBlockUpdate(
                (Volume)
                    context.deserialize(
                        json.get("clientUpdate").getAsJsonObject().get("coords"), Volume.class));
        json.remove("clientUpdate");
      }

      return segment;
    }
Ejemplo n.º 3
0
  /** Checking right click actions on blocks. */
  public boolean checkBlockInteraction(
      Resident res, BlockPos bp, PlayerInteractEvent.Action action) {
    Block blockType =
        DimensionManager.getWorld(bp.getDim()).getBlock(bp.getX(), bp.getY(), bp.getZ());
    for (SegmentBlock segment : segmentsBlocks) {
      if (segment.getCheckClass().isAssignableFrom(blockType.getClass())
          && (segment.getMeta() == -1
              || segment.getMeta()
                  == DimensionManager.getWorld(bp.getDim())
                      .getBlockMetadata(bp.getX(), bp.getY(), bp.getZ()))
          && (segment.getType() == BlockType.ANY_CLICK
              || segment.getType() == BlockType.RIGHT_CLICK
                  && action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK
              || segment.getType() == BlockType.LEFT_CLICK
                  && action == PlayerInteractEvent.Action.LEFT_CLICK_BLOCK)) {
        int dim = bp.getDim();
        int x = bp.getX();
        int y = bp.getY();
        int z = bp.getZ();

        if (!hasPermission(res, segment, dim, x, y, z)) {
          if (segment.hasClientUpdate())
            sendClientUpdate(
                segment.getClientUpdateCoords(), bp, (EntityPlayerMP) res.getPlayer(), null);
          return true;
        }
      }
    }

    return false;
  }
Ejemplo n.º 4
0
  public static void checkBlockInteraction(
      Resident res, BlockPos bp, PlayerInteractEvent.Action action, Event ev) {
    if (!ev.isCancelable()) {
      return;
    }
    World world = MinecraftServer.getServer().worldServerForDimension(bp.getDim());
    Block block = world.getBlock(bp.getX(), bp.getY(), bp.getZ());

    for (SegmentBlock segment : segmentsBlock.get(block.getClass())) {
      if (!segment.shouldInteract(res, bp, action)) {
        ev.setCanceled(true);
      }
    }
  }
Ejemplo n.º 5
0
 private void serializeBlock(
     SegmentBlock segment, JsonObject json, JsonSerializationContext context) {
   json.add("actions", serializeAsElementOrArray(segment.types, context));
   json.addProperty("meta", segment.getMeta());
   if (segment.clientUpdate != null) {
     JsonObject jsonUpdate = new JsonObject();
     jsonUpdate.add("coords", context.serialize(segment.clientUpdate.relativeCoords));
     json.add("clientUpdate", jsonUpdate);
   }
 }
  protected void handleBlockReply(BlockReplyMessage blockReplyMessage) {

    if (logger.isDebugEnabled()) logger.debug("processing message:" + blockReplyMessage.toString());

    // stats
    this.videoSignaling.getStats().updateOutgoingRequest(blockReplyMessage);

    final SegmentBlock segmentBlock = blockReplyMessage.getSegmentBlock();

    PeerId sender = blockReplyMessage.getSender();

    // Make sure we never save anything empty
    if (blockReplyMessage.getReplyCode() == BlockReplyCode.GRANTED && segmentBlock != null) {

      // increases reputation, resets timeout
      this.tuner.getNeighborList().registerSuccess(sender, segmentBlock);

      // increments application-level hop count
      if (segmentBlock != null) {
        segmentBlock.setDownloadTime();
        segmentBlock.incrementHopCount();
      }

      // writes segment (important!)
      final boolean successPut = this.tuner.putSegmentBlock(sender, segmentBlock);

      if (successPut) {
        Runnable runner =
            new Runnable() {
              @Override
              public void run() {

                try {
                  // signals player that it has arrived (maybe it's waiting for it)
                  if (tuner.getVideoPlayer() != null) tuner.getVideoPlayer().tryAndPlay();

                  // sends HAVE messages
                  videoSignaling.sendHave(
                      segmentBlock.getSegmentIdentifier(), segmentBlock.getBlockNumber());

                } catch (Exception e) {
                  // just so it doesn't die silently if an unhandled exception happened
                  logger.error("error calling player or sending Haves: " + e.getMessage());
                  e.printStackTrace();
                }
              }
            };
        ExecutorPool.getGeneralExecutorService().execute(runner);
      }
    } else if (blockReplyMessage.getReplyCode() == BlockReplyCode.REJECTED
        || blockReplyMessage.getReplyCode() == BlockReplyCode.DONT_HAVE
        || blockReplyMessage.getReplyCode() == BlockReplyCode.NO_SLOT) {

      // no slot granted, so it probably missed the message ungranting the slot (or message could be
      // on its way when it lost slot)
      if (blockReplyMessage.getReplyCode() == BlockReplyCode.NO_SLOT)
        this.tuner
            .getNeighborList()
            .removeUsGranter(
                blockReplyMessage.getSender(),
                blockReplyMessage.getSegmentBlock().getSegmentIdentifier(),
                10000);

      // DONT_HAVE is the same as HAVE (!doHave)
      // except that a peer keeps track if many requests are failing in order to send unsubscribe
      // (hopefully eventually solving the sync issue)
      // ===> DISABLED BECAUSE IT COULD MAKE A PEER STALL FOR A LONG TIME
      // else if (blockReplyMessage.getReplyCode()==BlockReplyCode.DONT_HAVE)
      //	this.tuner.updateNeighborBlockMap(blockReplyMessage.getSender(),
      // blockReplyMessage.getSegmentBlock().getSegmentIdentifier(),
      // blockReplyMessage.getSegmentBlock().getBlockNumber(), false, -1);

      // anyway, a block request has failed, so let's react quickly and reschedule the block
      if (segmentBlock != null && segmentBlock.getBlockNumber() != -1)
        this.tuner
            .getNeighborList()
            .registerFailure(
                segmentBlock.getSegmentIdentifier(),
                segmentBlock.getBlockNumber(),
                blockReplyMessage.getSender());
    } else {
      logger.warn("unexpected format of message: " + blockReplyMessage);
    }
    //		TODO: see what to do when getReplyCode() is different or segmentBlock==null
  }