// Block events
  public static BlockEvent createBlockEvent(Event event) {
    if (!(event instanceof ChangeBlockEvent)) {
      throw new IllegalArgumentException("Event is not a valid ChangeBlockEvent.");
    }

    ChangeBlockEvent spongeEvent = (ChangeBlockEvent) event;
    Location<World> location =
        spongeEvent.getTransactions().get(0).getOriginal().getLocation().get();
    net.minecraft.world.World world = (net.minecraft.world.World) location.getExtent();
    BlockPos pos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
    BlockEvent forgeEvent = new BlockEvent(world, pos, world.getBlockState(pos));
    return forgeEvent;
  }
Example #2
0
  @Listener
  public void onModifyBlock(ChangeBlockEvent event, @First Player player) {
    logger.info("Call: " + event.getClass());
    if (event
        .getCause()
        .get(NamedCause.NOTIFIER, Player.class)
        .map(p -> p.equals(player))
        .orElse(false)) {
      logger.info("Player cause is notifier.");
      return;
    }

    WorldFallbackContext worldContext = worldContexts.get(event.getTargetWorld().getUniqueId());

    boolean canModifyWild =
        Optional.ofNullable(worldContext)
            .flatMap(w -> w.getWilderness().getPublicPermission(MODIFY))
            .orElse(MODIFY.isAllowedByDefaultOnTheWild());

    Optional<Map<Vector3i, ClaimedChunk>> chunkMap = getChunkMap(event.getTargetWorld());

    claimedCheck:
    if (chunkMap.isPresent()) {
      Map<Vector3i, ClaimedChunk> subMap = chunkMap.get();
      Set<Vector3i> checkedChunks = new HashSet<>(2);

      for (Transaction<BlockSnapshot> transaction : event.getTransactions()) {
        Vector3i chunkPosition = blockToChunk(transaction.getOriginal().getPosition());
        if (checkedChunks.contains(chunkPosition)) continue;

        ClaimedChunk claimedChunk = subMap.get(chunkPosition);
        if (claimedChunk != null && !claimedChunk.check(MODIFY, player)) {
          logger.info("Chunk modification cancelled: " + chunkPosition + " " + event.getCause());
          event.setCancelled(true);
          return;
        } else if (!canModifyWild) break claimedCheck;

        checkedChunks.add(chunkPosition);
        logger.info("Chunk modification allowed: " + chunkPosition + " " + event);
      }

      return;
    }

    if (!canModifyWild) {
      if (worldContext != null) worldContext.getWilderness().notifyFailure(MODIFY, player);
      else MODIFY.notifyFailure(player, PlayerName.WILDERNESS);

      logger.info("Chunk modification cancelled because MODIFY is not allowed on unclaimed chunks");
      event.setCancelled(true);
    }
  }